@bigcommerce/api-nodejs

中文文档
0.1.2-alpha发布于 2023/07/25MITTypeScriptCommonJS近 30 天下载 2305

A node module for authentication and communication with the BigCommerce API

原始使用文档
文档目录8 节
  1. Installation (In Development)
  2. Type Generation
  3. Usage
  4. The BigCommerce Object
  5. OAuth
  6. REST
  7. Rate Limiting Management
  8. Available Resources and Methods

Bigcommerce for Node.js

A node module for authentication and communication with the BigCommerce API.

Installation (In Development)

  1. Clone this repository: git clone [email protected]:bigcommerce/bigcommerce-api-node.git
  2. Navigate into the cloned repository: cd bigcommerce-api-node
  3. Run npm link: npm link

Now, you can navigate into the directory of some other package (e.g., sample-app-nodejs) and run npm link bigcommerce-api-node which will then allow you to use and test this package as if it was downloaded from NPM.

Type Generation

The bigcommerce-api-node package automatically generates Typescript types from the Open API spec .yml files that power our public API documentation. While making contributions to the bigcommerce-api-node package, you may find it necessary to re-generate types from the spec.

In order to do so, simply run:

npm run generate

This command will:

  1. Run npm run init to compile the TypeGenerator.ts class
  2. Run dist/generate/TypeGenerator.js to generate types
  3. Run npm build to build the types into the distributed Node.js package

Usage

First, import the package into your project:

import BigCommerce from '@bigcommerce/api-nodejs';

Then, create a BigCommerce object with configuration options relevant to your use case.

The BigCommerce Object

The main BigCommerce import is an object that contains properties for different use cases of the BigCommerce Node Client. The properties available are described below:

  • Auth: This class can be instantiated and used to handle the OAuth flow that begins when a merchant clicks Install on a single-click app.
  • Rest: This class can be instantiated and used to make API requests to the BigCommerce Public REST API.

OAuth

The bigcommerce-api-node package can be used to handle the OAuth flow that begins when a merchant clicks Install on a single-click app.

First, create a BigCommerce object with clientId, clientSecret, and authCallback as required configuration options:

const bigcommerceAuth = new BigCommerce.Auth({
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  authCallback: 'https://yourapplication.com/auth',
});

The bigcommerceAuth object created above exposes two public methods: authorize and verifyJWT.

The authorize method takes one parameter — an object containing string values for code, scope, and context, which are provided by the GET request to your store when a merchant installs your app.

const payload = await bigcommerceAuth.authorize({code, scope, context});

The object stored in the payload variable above will contain the following key/value pairs:

{
  access_token: '123abc',
  scope: 'store_v2_orders etc.',
  user: {
    id: 12345,
    username: '[email protected]',
    email: '[email protected]',
  },
  context: 'stores/{STORE_HASH}',
  account_uuid: 'uuid-string'
}

The verifyJWT method can be used to verify and return the payload returned by the load, uninstall, and remove User callbacks. Each event triggers a GET request from BigCommerce to your app's callback endpoints containing a signed_payload_jwt as a query parameter. Once you parse the signed_payload_jwt from the request parameters, you can pass it to the verifyJWT method as follows:

const payload = bigcommerceAuth.verifyJWT(signed_payload_jwt);

The object stored in the payload variable above will contain the following key/value pairs:

{
  aud: 'YOUR_CLIENT_ID',
  iss: 'bc',
  iat: 1646844813,
  nbf: 1646844808,
  exp: 1646931213,
  jti: 'uuid-value',
  sub: 'stores/{STORE_HASH}',
  user: { id: 1470672, email: '[email protected]' },
  owner: { id: 1470672, email: '[email protected]' },
  url: '/'
}

REST

The bigcommerce-api-node package can be used to communicate with the BigCommerce Public REST API.

const bigcommerceRest = new BigCommerce.Rest({
  storeHash: 'yourStoreHash',
  accessToken: 'yourStoreAccessToken',
})

// bigcommerceRest.<resource_name>.<method_name>

Each method returns a Promise that resolves to a response containing the resource data.

bigcommerceRest.v2.orders
  .list({ limit: 5 })
  .then(orders => console.log(orders))
  .catch(err => console.error(err));

Some resources contain a listAll() method which returns an Iterator allowing you to loop through every single resource available, with pagination handled for you.

for await (const order of bigcommerceRest.v2.orders.listAll()) {
  console.log(order);
}

Rate Limiting Management

The RestClient class provides information on its status in relation to the BigCommerce API rate limiter. The available information includes:

  • msToReset: Time (in milliseconds) until rate limiting window resets
  • nextWindowTime: Date object for the start of the next rate limiting window
  • windowSize: Total size of the current rate limiting window
  • requestsRemaining: Number of requests remaining in the current window before rate limiting is enforced
  • requestsQuota: Total requests allowed per window

This information is updated on every request. It can be accessed via the rateLimitManager property and used to avoid receiving a 429 error from the server.

bigcommerceRest.rateLimitManager.status  // <-- { msToReset, windowSize, requestsRemaining, requestsQuota }

RestClient can be optionally configured to delay requests until the next rate limiting window when a minimum request threshold is met.

  const bigcommerceRest = new BigCommerce.Rest({
    storeHash: STORE_HASH,
    accessToken: ACCESS_TOKEN,
    rateLimitConfig: {
      enableWait: true,
      minRequestsRemaining: 10,
    },

Additionally, a custom callback can be provided with optional params object to be run when the request threshold is met.

  const limitCallback = params => console.log(params.message);

  const bigcommerceRest = new BigCommerce.Rest({
    storeHash: STORE_HASH,
    accessToken: ACCESS_TOKEN,
    rateLimitConfig: {
      enableWait: false,
      minRequestsRemaining: 10,
      callbackParams: { message: 'request threshold reached' },
      callback: limitCallback  // <-- function called with callbackParams when minRequestsRemaining threashold is met
    },

Available Resources and Methods

  • v2
    • v2.orders
      • get(orderId): Get an Order
      • update(orderId, data): Update an Order
      • archive(orderId): Archive an Order
      • count(): Get a Count of Orders
      • list([params]): Get All Orders
      • listAll([params]): Get All Orders (Paginated)
      • create(data): Create an Order
      • archiveAll(): Archive All Orders
    • v2.orders.orderCoupons
      • list(orderId[, params]): List Order Coupons
      • listAll(orderId[, params]): List Order Coupons (Paginated)
    • v2.orders.orderProducts
      • list(orderId[, params]): List Order Products
      • listAll(orderId[, params]): List Order Products (Paginated)
      • get(orderId, productId): Get an Order Product
    • v2.orders.orderTaxes
      • list(orderId[, params]): Get All Order Taxes
      • listAll(orderId[, params]): Get All Order Taxes (Paginated)
    • v2.orders.orderStatus
      • list(): Get All Order Statuses
      • get(statusId): Get a Single Order Status
    • v2.orders.orderShipments
      • list(orderId[, params]): Get Order Shipments
      • listAll(orderId[, params]): Get Order Shipments (Paginated)
      • create(orderId, data): Create Order Shipment
      • deleteAll(orderId): Delete All Order Shipments
      • count(orderId)Get a Count of Order Shipments
      • get(orderId, shipmentId): Get a Shipment
      • update(orderId, shipmentId, data): Update a Shipment
      • delete(orderId, shipmentId): Delete an Order Shipment
    • v2.orders.orderShippingAddresses
      • list(orderId[, params]): Get Order Shipping Addresses
      • listAll(orderId[, params]): Get Order Shipping Addresses (Paginated)
      • get(orderId, addressId): Get a Shipping Address
      • update(orderId, addressId, data): Update a Shipping Address
    • v2.orders.orderMessages
      • list(orderId[, params]): Get Order Messages
      • listAll(orderId[, params]): Get Order Messages (Paginated)
    • v2.orders.orderShippingQuotes
      • list(orderId, addressId): Get Order Shipping Quotes

版本与发布记录

@bigcommerce/api-nodejs 共发布 3 个版本,首次发布于 2022/08/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2-alphalatest2023/07/253 年前
0.1.1-alpha2022/08/174 年前
0.1.0-alpha2022/08/174 年前

关于 @bigcommerce/api-nodejs 的常见问题

@bigcommerce/api-nodejs 是什么?

A node module for authentication and communication with the BigCommerce API

如何安装 @bigcommerce/api-nodejs?

在项目目录运行 npm install @bigcommerce/api-nodejs,或使用 pnpm add / yarn add / bun add。

@bigcommerce/api-nodejs 支持 TypeScript 吗?

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

@bigcommerce/api-nodejs 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bigcommerce/api-nodejs 需要什么版本的 Node.js?

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

@bigcommerce/api-nodejs 可以商用吗?

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

@bigcommerce/api-nodejs 的下载量有多少?

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

@bigcommerce/api-nodejs 还在维护吗?

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