@gradientedge/commercetools-utils

中文文档
6.32.0发布于 2026/09/07MITTypeScriptESM + CJS近 30 天下载 4505

Provide utility classes and functions for interacting with the commercetools API

原始使用文档
文档目录7 节
  1. How does this compare to the official commercetools SDK?
  2. Installation
  3. Code examples
  4. Creating a client grant
  5. Logging in a customer
  6. Get an anonymous customer grant
  7. Refresh an existing customer grant

semantic-release Commitizen friendly Coverage

Gradient Edge commercetools utils

An easy-to-use toolkit for working with the commercetools HTTP API: https://docs.commercetools.com/api/

Please note: this API is being built out bit by bit. If you're looking to use a method that doesn't appear to exist, please either raise an issue or feel free to make the change yourself and raise a PR 👍

See full documentation here: API documentation.

How does this compare to the official commercetools SDK?

We think it's easier to use and less verbose. On the flip side, the official commercetools SDK is more flexible and certainly more complete and up to date.

Installation

Simply install the @gradientedge/commercetools-utils package using your package manager, e.g.:

npm install --save @gradientedge/commercetools-utils

Code examples

For clarity, we use the term grant to describe the object that holds the access token, refresh token, scope and expiry details.

Creating a client grant

import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'

async function example() {
  const auth = new CommercetoolsAuth({
    projectKey: 'your-project-key',
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    region: Region.EUROPE_GCP,
    clientScopes: ['create_anonymous_token']
  })

  let grant: CommercetoolsGrant

  try {
    const grant = await auth.getClientGrant()
    console.log('Grant:', grant)
  } catch (error) {
    // 'error' will likely be an instance of CommercetoolsError.
    // See the API for full details.
    console.error(error)
    throw error
  }
}

example()

Logging in a customer

import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'

async function example() {
  const auth = new CommercetoolsAuth({
    projectKey: 'your-project-key',
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    region: Region.EUROPE_GCP,
    clientScopes: ['create_anonymous_token']
  })

  let grant: CommercetoolsGrant

  try {
    const grant = await auth.login({
      username: 'myUsername',
      password: 'myPassword'
    })
    console.log('Grant:', grant)
  } catch (error) {
    console.error(error)
    throw error
  }
}

example()

Note that the getClientGrant method here could be swapped out for say, login, in order to login a customer. There is no need to call getClientGrant before calling login. Ensuring that there's an active client grant will be taken care of by the CommercetoolsAuth class.

Get an anonymous customer grant

import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'

async function example() {
  const auth = new CommercetoolsAuth({
    projectKey: 'your-project-key',
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    region: Region.EUROPE_GCP,
    clientScopes: ['create_anonymous_token']
  })

  let grant: CommercetoolsGrant

  try {
    const grant = await auth.getAnonymousGrant()
    console.log('Grant:', grant)
  } catch (error) {
    console.error(error)
    throw error
  }
}

example()

Refresh an existing customer grant

The code below demonstrates how we can call the refreshCustomerGrant method with the refreshToken of an existing grant. Realistically, that grant would probably have come from something like a JWT that was passed to your back-end server by your UI.

import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'

async function example() {
  const auth = new CommercetoolsAuth({
    projectKey: 'your-project-key',
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    region: Region.EUROPE_GCP,
    clientScopes: ['create_anonymous_token']
  })

  let grant: CommercetoolsGrant

  try {
    const grant = await auth.login({
      username: 'myUsername',
      password: 'myPassword'
    })
    console.log('Grant:', grant)
  } catch (error) {
    console.error(error)
    throw error
  }

  // The 'grant' would have been
  const refreshToken = grant.refreshToken
  try {
    const grant = await auth.refreshCustomerGrant(refreshToken)
    console.log('Refreshed grant:', grant)
  } catch (error) {
    console.error(error)
    throw error
  }
}

example()

版本与发布记录

@gradientedge/commercetools-utils 共发布 216 个版本,首次发布于 2021/02/21;过去 12 个月发布了 24 个版本。

版本发布日期距今
6.32.0latest2026/09/072 个月前
6.31.02026/08/282 个月前
6.30.02026/08/252 个月前
6.29.02026/08/212 个月前
6.28.02026/06/175 个月前
6.27.12026/06/145 个月前

关于 @gradientedge/commercetools-utils 的常见问题

@gradientedge/commercetools-utils 是什么?

Provide utility classes and functions for interacting with the commercetools API

如何安装 @gradientedge/commercetools-utils?

在项目目录运行 npm install @gradientedge/commercetools-utils,或使用 pnpm add / yarn add / bun add。

@gradientedge/commercetools-utils 支持 TypeScript 吗?

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

@gradientedge/commercetools-utils 支持 ESM 还是 CommonJS?

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

@gradientedge/commercetools-utils 可以商用吗?

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

@gradientedge/commercetools-utils 的下载量有多少?

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

@gradientedge/commercetools-utils 还在维护吗?

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