@commercelayer/sdk-utils
TypeScript icon, indicating that this package has built-in type declarations

1.6.4 • Public • Published

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

  • splitExport - split a large export in multiple small exports
  • executeExport - split a large export in multiple small exports and execute them
  • exportsToBatchTasks - translate a list of exports in executable batch tasks

Imports

  • splitImport - split a large import in multiple small imports
  • executeImport - split a large import in multiple small imports and execute them
  • importsToBatchTasks - translate a list of imports in executable batch tasks

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)

In the example above the onSuccess and onFailure callbacks have been used to handle the task result in case of success or failure of the execution.

It's also possible to fill the resource attributes taking values from the result of the previous batch step. In the following example we are updating a resource taking its id from a previous retrieve or create task:

const task: Task = {
    resourceType: "customers",
    operation: "update",
    prepareResource: (res: TaskResourceParam, last: TaskResourceResult): TaskResourceParam => {
        return {
          ...res,
          id: res.id,
          reference: 'new-reference'
        }
      }
  }
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

splitCleanup

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 splitClp: CleanupCreate[] = await splitCleanup(clp)
executeCleanup

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

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

const execClp: CleanupResult[] = await executeCleanup(clp)
cleanupsToBatchTasks

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

const clps = await splitCleanup(clp)

const tasks = cleanupsToBatchTasks(clps)

Exports

splitExport

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 splitExp: ExportCreate[] = await splitExport(exp)
executeExport

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

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

const execExp: ExportResult[] = await executeExport(exp)
exportsToBatchTasks

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

const exps = await splitExport(exp)

const tasks = exportsToBatchTasks(exps)

Imports

splitImport

Split an import in multiple imports respecting the maximum limit of inputs included

const exp: ImportCreate = {
 resource_type: 'customers',
 inputs: [
  { ... },
  { ... },
  ...
  { ... }
 ]
}

const splitImp: ImportCreate[] = await splitImport(imp)
executeImport

Split an import in multiple imports respecting the maximum limit of inputs included and execute them

const exp: ImportCreate = {
 resource_type: 'customers',
 inputs: [
  { ... },
  { ... },
  ...
  { ... }
 ]
}

const execImp: ImportResult[] = await executeImport(imp)
importsToBatchTasks

Convert a list of imports generated by splitImport to a list of tasks that can be executed by the function executeBatch

const imps = await splitImport(imp)

const tasks = importsToBatchTasks(imps)

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)

Package Sidebar

Install

npm i @commercelayer/sdk-utils

Weekly Downloads

387

Version

1.6.4

License

MIT

Unpacked Size

54.4 kB

Total Files

7

Last publish

Collaborators

  • commercelayer.io
  • drpierlu