JSPM

  • Created
  • Published
  • Downloads 481
  • Score
    100M100P100Q115399F
  • License MIT

Commerce Layer Javascript SDK Utils library

Package Exports

  • @commercelayer/sdk-utils
  • @commercelayer/sdk-utils/lib/cjs/index.js
  • @commercelayer/sdk-utils/lib/esm/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@commercelayer/sdk-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Commerce Layer SDK Utils

Version Downloads/week License semantic-release: angular Release CodeQL TypeScript

A JavaScript Library that makes even more easier to interact with Commerce Layer API using the official JavaScript SDK.

Installation and usage

import CommerceLayer from "@commercelayer/sdk"
import CommerceLayerUtils, { executeBatch } from '@commercelayer/sdk'

const cl = CommerceLayer({ organization, accessToken })
CommerceLayerUtils(cl)

await executeBatch(batch)

Table of helpers

Common functions

Cleanups

Exports

Webhooks

  • denormalizePayload - parse a webhook payload and transform it in the appropriate SDK object

Common functions

executeBatch

This function allows to prepare and then execute a number of API calls without having to worry about current rate limits.

for (const emailAddress of emailList) {
    const task: Task = {
      resourceType: "customers",
      operation: "create",
      resource: { email: emailAddress } as CustomerCreate,
      onSuccess: { callback: sendEmail },
      onFailure: { errorHandler: handleError }
    }
    tasks.push(task)
  }

  const batch: Batch = {
    tasks,
    options: { refreshToken: refreshAccessToken }
  }


await executeBatch(batch)
retrieveAll

This function allows to fetch all existing resources of a specific type executing all necessary API requests respecting current API rate limits.

const skus = await retrieveAll<Sku>('skus')
updateAll

This function allows to modify a set of resources of a specific type, using a filter to identify them.

const skuData = { reference_origin: 'legacy-system-0' }

const filters = { created_at_lt: '2023-01-01' }

const updateResult = await updateAll('skus', skuData, { filters })

Cleanups

prepareCleanups

Split cleanup in multiple cleanups respecting the maximum limit of resources included

const clp: CleanupCreate = {
 resource_type: 'customers',
 filters: { created_at_gt: '2020-01-01'}
}

const prepClp = await prepareCleanups(clp)
cleanupsToBatchTasks

Convert a list of cleanups generated by prepareCleanups to a list of tasks that can be executed by the function executeBatch

const prepClp = await prepareCleanups(clp)

const tasks = cleanupsToBatchTasks(prepClp)

Exports

prepareExports

Split an export in multiple exports respecting the maximum limit of resources included

const exp: ExportCreate = {
 resource_type: 'customers',
 filters: { created_at_gt: '2020-01-01'}
}

const prepExp = await prepareExports(exp)
exportsToBatchTasks

Convert a list of exports generated by prepareExports to a list of tasks that can be executed by the function executeBatch

const prepExp = await prepareExports(exp)

const tasks = exportsToBatchTasks(prepExp)

Webhooks

denormalizePayload

This function takes in input the payload of a webhook in JSON format and transform it in the appropriate typed resource to be used with the official Typescript SDK.

const shipment = denormalizePayload<Shipment>(webhookPayload)