npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@commercelayer/algolia-importer

v1.0.10

Published

> A Javascript library that lets you easily import your ecommerce catalog into Algolia.

Downloads

24

Readme

Commerce Layer Algolia Importer

A Javascript library that lets you easily import your ecommerce catalog into Algolia.

What is Commerce Layer?

Commerce Layer is a headless platform that lets you easily build enterprise-grade ecommerce into any website, by using the language, CMS, and tools you already master and love.

Table of contents

How it works

Commerce Layer Algolia Importer is designed to make it quick and simple to import your products data, index them into Algolia, and eventually get their prices from Commerce Layer and add them to the search.

This importer is CMS-agnostic — you can import your content data from any CMS of your choice, according to any content model you defined.

Getting started

To get started and import your data into Algolia you need to install the importer and retrieve your credentials from your Algolia account.

Installation

Commerce Layer Algolia Importer is available as an npm package:

// npm
npm install @commercelayer/algolia-importer

// yarn
yarn add @commercelayer/algolia-importer

Import

You can use the ES6 default import as follows:

import importer from '@commercelayer/algolia-importer'

Usage

The importer function

Commerce Layer Algolia importer function returns as a promise an array of Algolia index objects — one for each of your selling countries (associated with the specified translation). You need to pass to it as a parameter an object containing at least:

  • algoliaConfig — your Algolia app ID and admin API key
  • items — the list of products you want to index
importer({
  algoliaConfig: {
    appId: 'your-algolia-app-id',
    adminApiKey: 'your-algolia-admin-api-key'
  },
  items: yourItems,
}).then((results) => {
    console.log(results)
})

The items array

items is an array of objects. It has to be properly built by specifying the language code, the country code, and a list of key/value parameters related to the single variants:

// Typescript types

{
  [languageCode: string]: {
    [countryCode: string]: {
        customRanking: number
        reference: string
        objectID: string
        category: string[] | string
        description: string
        name: string
        code: string
        image: string
    }[],
    marketId?: string
  }[]
}[]

// Example

[
  {
    'en-US': [
      {
        us: [
          {
            customRanking: 0,
            reference: 'your-reference',
            objectID: 'your-objectID',
            category: 'your-category',
            description: 'your-description',
            name: 'your-variant-name',
            code: 'your-sku-code',
            image: 'your-image-url'
          }
        ]
      }
    ]
  }
]

For each item you can define a custom ranking by setting the customRanking parameter — the lower its value the higher the item will be ranked within the search results.

Default settings

Commerce Layer Algolia importer comes with some default values for its main parameters:

showPrice: false,
clConfig: {
  clientId: '',
  endpoint: '',
},
algoliaIndexPrefix: 'store'
algoliaSettings: {
  searchableAttributes: [
    'category',
    'code',
    'description',
    'name',
    'reference',
  ],
  customRanking: ['asc(customRanking)'],
  attributesForFaceting: ['category'],
  distinct: 1,
  attributeForDistinct: 'reference',
  replicas: [],
}

The algoliaSettings object can be overridden at your leisure — please refer to Algolia documentation for more info on how to set their API parameters.

Adding prices to the search

Our Algolia importer lets you import from Commerce Layer the prices of each product you want to index and sort (ascending or descending) your search result by price. To do that, just set the showPrice parameter to true and add the clConfig object with your Commerce Layer organization credentials, remembering to specify the market in scope at a country code level in the items array:

const items = [
  {
    'en-US': [
      {
        us: [
          {
            customRanking: 0,
            reference: 'your-reference',
            objectID: 'your-objectID',
            category: 'your-category',
            description: 'your-description',
            name: 'your-variant-name',
            code: 'your-sku-code',
            image: 'your-image-url'
           }
        ],
        marketId: 'your-market-id'
      }
    ]
  }
]

importer({
  algoliaConfig: {
    appId: 'your-algolia-app-id',
    adminApiKey: 'your-algolia-admin-api-key'
  },
  items: items,
  showPrice: true,
  clConfig: {
    clientId: 'your-client-id',
    endpoint: 'https://yourdomain.commercelayer.io'
  }
})

This way, all the prices are added to the related index and two Algolia replicas (price_asc and price_desc) are automatically created.


License

This repository is published under the MIT license.