Created
December 15, 2021 14:39
-
-
Save gje4/e88e4d5b2fef03a260ad6dc1c0e56a9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-ignore | |
const dotenv = require('dotenv') | |
const algoliasearch = require('algoliasearch/lite') | |
const BigCommerce = require('node-bigcommerce') | |
dotenv.config() | |
const bigCommerceV3 = new BigCommerce({ | |
logLevel: 'info', | |
clientId: process.env.BIGCOMMERCE_STORE_API_CLIENT_ID, | |
accessToken: process.env.BIGCOMMERCE_STORE_API_TOKEN, | |
storeHash: process.env.BIGCOMMERCE_STORE_API_STORE_HASH, | |
responseType: 'json', | |
headers: { 'Accept-Encoding': '*' }, // Override headers (Overriding the default encoding of GZipped is useful in development) | |
apiVersion: 'v3', // Default is v2 | |
}) | |
interface bcProduct { | |
objectID: any | |
name: string | |
description?: number | |
price: number | |
retail_price: number | |
sale_price: number | |
categories?: [] | |
brand_id?: bigint | |
inventory_level?: bigint | |
reviews_rating_sum?: bigint | |
reviews_count?: bigint | |
date_created: string | |
} | |
async function getCatalog( | |
type: string, | |
page = 1, | |
limit = 250, | |
bcProducts: bcProduct[] = [] | |
): Promise<bcProduct[]> { | |
const includeFields = | |
'name,description,price,retail_price,sale_price,categories,brand_id,inventory_level,reviews_rating_sum,reviews_count,date_created' | |
const isVisibleParam = type === 'products' ? '&is_visible=true' : '' | |
const includeParam = type === 'products' ? '&include=custom_fields,images,options,variants' : '' | |
try { | |
console.log(`Getting page ${page} of ${type}`) | |
const { data, meta } = await bigCommerceV3.get( | |
`/catalog/${type}?limit=${limit}&page=${page}&include_fields=${includeFields}${isVisibleParam}${includeParam}` | |
) | |
const newProduct: bcProduct[] = data.map((record: any) => { | |
//any extra transormtion for algolia | |
record.objectID = record.id | |
return record | |
}) | |
bcProducts = bcProducts.concat(newProduct) | |
if (meta.pagination.total_pages > meta.pagination.current_page) { | |
page++ | |
return getCatalog(type, page, limit, bcProducts) | |
} else { | |
return bcProducts | |
} | |
} catch (err) { | |
const d = new Date() | |
console.error(`${d.toUTCString()} Error retrieving ${type} page ${page}`, err) | |
throw err | |
} | |
} | |
const job = async () => { | |
try { | |
const bigcommerceProducts = await getCatalog('products') | |
// initialize the client with your environment variables | |
const client = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_KEY) | |
// initialize the index with your index name | |
const index = client.initIndex('sandbox_product') | |
const algoliaResponse = await index.saveObjects(bigcommerceProducts) | |
console.log(`Sucessfully added ${algoliaResponse.objectIDs.length}`) | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
job() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment