@jsondrop/commerce

中文文档
0.1.0发布于 2026/11/01MITTypeScriptES Module零依赖

Drupal Commerce types and a commerce_api client (cart, checkout, payment) for @jsondrop/core.

原始使用文档
文档目录5 节
  1. Catalog
  2. Cart and checkout
  3. Product bundles
  4. Other Commerce modules
  5. Extending

@jsondrop/commerce

Drupal Commerce types and a client for the commerce_api module: cart, coupons, checkout, shipping and payment.

npm install @jsondrop/core @jsondrop/drupal @jsondrop/commerce

Needs commerce_api (8.x-1.x) on the Drupal side.

Catalog

commerce_api renames Commerce's resource types: the commerce_ prefix goes and underscores become hyphens, and the paths are pluralized.

DrupalResource typePath
commerce_product (default)product--default/jsonapi/products/default
commerce_product_variation (default)product-variation--default/jsonapi/product-variations/default
commerce_order (default)order--default/jsonapi/orders/default
commerce_order_item (default)order-item--default/jsonapi/order-items/default
commerce_store (online)store--online/jsonapi/stores/online
commerce_promotion_couponpromotion-coupon/jsonapi/promotion-coupons

JSON:API Extras overrides the renaming. On a site with Extras, types keep Drupal's names (commerce_product_variation--default) unless its resource configs say otherwise, and sites mix both. The types accept both namings; for paths, use client.withDiscoveredPaths(); for the type names CommerceApi sends, set naming: 'drupal' (and pass orders rather than IDs, so their own type is sent).

withCommerceApi(client) gives a client that resolves those paths, and the types are registered by entity type, so every product bundle is a Product, every variation a ProductVariation, and so on:

import { JsonApiClient } from '@jsondrop/core';
import { withCommerceApi } from '@jsondrop/commerce';

const drupal = withCommerceApi(new JsonApiClient({ baseUrl: 'https://shop.example.com' }));
const products = await drupal.getCollection('product--default', { query: { include: ['variations'] } });
products[0].variations; // ProductVariation[], with resolved_price

Cart and checkout

import { CommerceApi, checkoutStatus, createCartToken } from '@jsondrop/commerce';

const commerce = new CommerceApi(drupal).withCartToken(token ?? createCartToken());

await commerce.addToCart({ type: 'product-variation--default', id: variationId, quantity: 2 });
const [cart] = await commerce.getCarts();
await commerce.applyCoupons(cart.id, 'SAVE10');

await commerce.updateCheckout(cart.id, {
  email: '[email protected]',
  billing_information: { address: { country_code: 'HR', locality: 'Zagreb', postal_code: '10000', address_line1: 'Ilica 1' } },
});
const { constraints, shippingRates, paymentOptions, ready } = checkoutStatus(await commerce.getCheckout(cart.id));
MethodEndpoint
getCurrentStore()GET /current-store
getCarts(), getCart(id)GET /carts, GET /carts/{order}
addToCart(items)POST /cart/add
updateCartItem(order, item, { quantity })PATCH /carts/{order}/items/{item}
removeCartItems(order, items)DELETE /carts/{order}/items
clearCart(order)DELETE /carts/{order}
applyCoupons(order, codes), removeCoupons(order)PATCH / DELETE /carts/{order}/coupons
getCheckout(order), updateCheckout(order, changes)GET / PATCH /checkout/{order}
getShippingMethods(order)GET /checkout/{order}/shipping-methods
createPayment(order, { gatewayId })POST /checkout/{order}/payment
approvePayment(order, params)GET /checkout/{order}/payment/approve (offsite return)

Orders can be passed as an ID or as the order itself (getCarts() returns them); with the order, requests that send its type use the real one (order--regular, not the configured default).

Your own models. Methods are generic and typed with the package's types by default. If you map responses to models of your own, take them untyped: commerce.getCarts<Resource>().

Cart token. commerce_api never issues one: the client makes one up (createCartToken()) and sends it as Commerce-Cart-Token; carts created under it belong to whoever holds it. Keep it in an httpOnly cookie and call Drupal from the server. See example 6.

Payments. createPayment pays the order's balance with its payment instrument (onsite and manual gateways) and places the order. It returns the whole response, so a gateway module that answers with an extra step can be handled by the caller. Offsite gateways redirect the customer, and approvePayment completes the payment on their return.

Product bundles

For commerce_variation_bundle: a variation type with the "variation bundle" trait holds bundle items (bundle-item--{type}), each a quantity of another variation.

import { bundleItems, type VariationBundle } from '@jsondrop/commerce';

declare module '@jsondrop/core' {
  interface ResourceTypes {
    'product-variation--bundle': VariationBundle<'bundle'>;
  }
}

const set = await drupal.getResource('product-variation--bundle', id, {
  query: { include: ['bundle_items.variation'] },
});
set.bundle_discount; // percentage off the items
for (const { variation, quantity, unitPrice } of bundleItems(set)) {
  // enabled items, with their variation and its resolved unit price
}

isVariationBundle(variation) tells bundles from plain variations. Add a bundle to the cart like any variation; the module prices it from its items.

Other Commerce modules

commerce_api renames every commerce_* entity type, contrib ones too. COMMERCE_ENTITY_TYPES lists the ones path resolution knows (Commerce, commerce_shipping, commerce_variation_bundle); add others in commerce_api's naming:

const drupal = withCommerceApi(client, ['license', 'wishlist-item']);
// or
const commerce = new CommerceApi(client, { entityTypes: ['license'] });

Extending

  • Your bundles and fields: example 4.
  • Your modules' endpoints, headers and payment flows: subclass CommerceApi, example 5.
  • Other names: orderType, couponType, paymentType and cartInclude options.

版本与发布记录

@jsondrop/commerce 共发布 2 个版本,首次发布于 2026/11/01;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.1.0latest2026/11/011 天前
0.0.0-stage2026/11/011 天前

关于 @jsondrop/commerce 的常见问题

@jsondrop/commerce 是什么?

Drupal Commerce类型和一个用于@jsondrop/core的commerce_api客户端(购物车、结帐、支付)。

如何安装 @jsondrop/commerce?

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

@jsondrop/commerce 支持 TypeScript 吗?

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

@jsondrop/commerce 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@jsondrop/commerce 需要什么版本的 Node.js?

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

@jsondrop/commerce 可以商用吗?

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

@jsondrop/commerce 还在维护吗?

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