@commercelayer/sdk-utils

中文文档
5.0.0发布于 2026/10/07MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 8923

Commerce Layer Javascript SDK Utils library

原始使用文档
文档目录8 节
  1. Installation and usage
  2. Table of functions and utilities
  3. Common functions
  4. Cleanups
  5. Exports
  6. Imports
  7. Webhooks
  8. SDK query helpers

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

SDK v8.x (requires ^8.0.0)

The Core API is versioned, so SDK v8 requires apiVersion when creating the client. The default entry of @commercelayer/sdk is the bundled client, which carries every resource: pass it to CommerceLayerUtils and all of them are available to the utilities.

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

const cl = CommerceLayer({ organization, accessToken, apiVersion: '2026-05' })
CommerceLayerUtils(cl)

await executeBatch(batch)

To take advantage of tree shaking, use the single client from @commercelayer/sdk/single-client and pass only the resources you really need.

import { CommerceLayer, orders, customers } from "@commercelayer/sdk/single-client"
import CommerceLayerUtils, { retrieveAll } from '@commercelayer/sdk-utils'

const cl = CommerceLayer({ organization, accessToken, apiVersion: '2026-05' })
CommerceLayerUtils(cl, [ orders, customers ])

const customerList = await retrieveAll<Customer>('customers')

Resources can also be added later to the CommerceLayerUtils configuration.

import { CommerceLayer, orders, customers, skus } from "@commercelayer/sdk/single-client"
import CommerceLayerUtils from '@commercelayer/sdk-utils'

const cl = CommerceLayer({ organization, accessToken, apiVersion: '2026-05' })
const utilsConfig = CommerceLayerUtils(cl)

utilsConfig.addApiResources(orders, customers)
utilsConfig.addApiResource(skus)

[!NOTE] With the single client, a utility can only use the resources that were passed to CommerceLayerUtils: calling retrieveAll('skus') without adding skus throws Resource [skus] not available.

SDK v7.x

Use @commercelayer/sdk-utils 4.x (requires ^7.11.0).


Table of functions and utilities

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

SDK query helpers

  • IncludeHelper - build an includeobject to use in query filters
  • FilterHelper - build a filter object to use in query filters

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: last.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. The limit option can be used to retrieve only a limited number of resources.

const skus = await retrieveAll<Sku>('skus', { limit: 100 })
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 })
deleteAll

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


const filters = { created_at_lt: '2023-01-01', reference_origin_eq: 'legacy-system-0' }

const deleteResult = await deleteAll('skus', { filters })
retrievePage

This function allows to fetch a specific subset of resources defining page attributes that are not permitted by the API and using all the standard common filters. An error will be thrown if the page parameters are not compatible with the number of existing resources.

 const skus = await retrievePage<Sku>('skus', { pageNumber: 16, pageSize: 139, sort: ['code'] })

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)

SDK query helpers

IncludeHelper

This helper can be used to easily build the array of included resources to use in SDK query parameters.

Note: The versions resource has been removed from the Commerce Layer API and is no longer available in the include helper.

Standard include
const include: QueryInclude = [ 'market', 'customer', 'customer.customer_group' ]
Include with IncludeHelper
import { Include } from '@commercelayer/sdk-utils'

const include: QueryInclude = [ Include.orders.market.build(), Include.orders.customer.build(), Include.orders.customer.customer_group.build() ]

// The same as above but using a single instance of the orders helper
const io = Include.orders
const includeSingle: QueryInclude = [ io.market.build(), io.customer.build(), io.customer.customer_group.build() ]
Include using buildInclude function
import { buildInclude } from '@commercelayer/sdk-utils'

const include: QueryInclude = buildInclude(Include.orders.market, Include.orders.customer, Include.orders.customer.customer_group)
Add resources to include to an existing query filter
import { Include } from '@commercelayer/sdk-utils'

const queryFilter: QueryParams = {}

const io = Include.orders

io.market.addTo(queryFilter)
io.customer.addTo(queryFilter)
io.customer.customer_group.addTo(queryFilter)
FilterHelper

This helper can be used to easily build the object containing the filter predicates to use in SDK query parameters.

Note: The versions resource has been removed from the Commerce Layer API and is no longer available in the filter helper.

Standard filter
const filter: QueryFilter = {
  number_eq: '123',
  market_reference_eq: 'MKT',
  reference_or_customer_customer_group_reference_eq: 'REF',
  status_in: 'placed,approved,cancelled',
  reference_origin_not_null: 'true'
}
Filter with FilterHelper
import { Filter } from '@commercelayer/sdk-utils'

const filter: QueryFilter = {
  ...Filter.orders.number.eq('123'),
  ...Filter.orders.market.reference.eq('MKT'),
  ...Filter.orders.reference.or.customer.customer_group.reference.eq('REF'),
  ...Filter.orders.status.in('placed', 'approved', 'cancelled'),
  ...Filter.orders.reference_origin.not_null()
}

// The same as above but using a single instance of the orders helper
const of = Filter.orders

const filterSingle: QueryFilter = {
  ...of.number.eq('123'),
  ...of.market.reference.eq('MKT'),
  ...of.reference.or.customer.customer_group.reference.eq('REF'),
  ...of.status.in('placed', 'approved', 'cancelled'),
  ...of.reference_origin.not_null()
}
Filter using buildFilter function
import { buildFilter } from '@commercelayer/sdk-utils'

const filter: QueryFilter = buildFilter(
  of.number.eq('123'),
  of.market.reference.eq('MKT'),
  of.reference.or.customer.customer_group.reference.eq('REF'),
  of.status.in('placed', 'approved', 'cancelled'),
  of.reference_origin.not_null()
)

版本与发布记录

@commercelayer/sdk-utils 共发布 54 个版本,首次发布于 2023/10/11;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.0latest2026/10/071 个月前
4.0.02026/06/124 个月前
3.5.12026/04/087 个月前
3.5.02026/03/257 个月前
3.4.22026/03/217 个月前
3.4.12026/03/217 个月前

其他发布通道:sdk6 → 2.6.0。

关于 @commercelayer/sdk-utils 的常见问题

@commercelayer/sdk-utils 是什么?

Commerce Layer Javascript SDK Utils library

如何安装 @commercelayer/sdk-utils?

在项目目录运行 npm install @commercelayer/sdk-utils,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@commercelayer/sdk(^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@commercelayer/sdk-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@commercelayer/sdk-utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@commercelayer/sdk-utils 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@commercelayer/sdk-utils 可以商用吗?

@commercelayer/sdk-utils 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@commercelayer/sdk-utils 的下载量有多少?

根据 npm 官方下载统计,@commercelayer/sdk-utils 在 2026/10/11 – 2026/11/09 这 30 天内被下载 8,923 次,日均约 297 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@commercelayer/sdk-utils 还在维护吗?

最新版本 5.0.0 发布于 2026/10/07(1 个月前);至今共发布 54 个版本,其中过去 12 个月发布了 12 个;首次发布于 2023/10/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。