@gradientedge/commercetools-utils

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

提供与商务工具 API 互动的实用类和功能

中文使用文档
文档目录7 节
  1. 与官方商务工具SDK相比?
  2. 安装
  3. 代码示例
  4. 创建客户赠款
  5. 在客户中登录
  6. 拿个匿名的客户补助金
  7. 刷新现有的客户赠款

semantic-release Commitizen friendly Coverage

渐渐边缘商业工具

一个与商务工具HTTP API合作的易用工具包: https://docs.com/api/ 商业工具

请注意: 这个 API 正在逐个建立中 。 如果你想要用一个方法 似乎不存在,请 要么提出一个问题 要么自由做出改变 {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}你自己 举个公关 {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}你

见本文全文: API 文档。 。 。 .

与官方商务工具SDK相比?

我们认为比较容易使用而较少动词. 在翻转的一面 官方商务工具 SDK 实值 更灵活,当然更完整和最新.

安装

简单安装 @gradientedge/commercetools-utils 使用软件包管理器的软件包,例如:

npm install --save @gradientedge/commercetools-utils

代码示例

为了清楚起见,我们用这个词 赠款 描述持有访问符的物体, 刷新标志、范围和过期细节.

创建客户赠款

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()

在客户中登录

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()

注意 getClientGrant 这里的方法可以被交换出来说, login,以登录客户. 没必要打电话 getClientGrant 打电话前 login. 确保有积极的客户赠款 将会由 CommercetoolsAuth 课.

拿个匿名的客户补助金

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()

刷新现有的客户赠款

下面的代码显示了我们如何称呼 refreshCustomerGrant 使用该方法 refreshToken 一、导 言 现有赠款。 现实地说,这笔补助金可能来自于 某种像JWT的东西 通过 您的 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 是什么?

提供与商务工具 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 和提交记录判断。