commerce-app-webhooks
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConfigure Commerce App Webhooks
配置Commerce应用Webhooks
Adds or modifies webhook interceptors in an existing .
Webhooks intercept Commerce operations — you can validate input, append data, or modify behavior before or after an operation executes.
Other extensibility domains (events, business config) are added separately via their own skills.
app.commerce.config.ts在现有的中添加或修改webhook拦截器。
Webhooks可拦截Commerce操作——你可以在操作执行前后验证输入、追加数据或修改行为。
其他扩展领域(事件、业务配置)需通过各自的技能单独添加。
app.commerce.config.tsPrerequisites
前提条件
- Verify the app is scaffolded and initialized, not merely that the config exists. Require both:
- present in the project root, and
app.commerce.config.ts - the project initialized — signalled by the generated directory and installed
src/commerce-extensibility-1/(thenode_modulesdependency).@adobe/aio-commerce-lib-app
- If is missing, stop and invoke
app.commerce.config.tsfirst (it writes the config, then runs init).commerce-app-init - If the config is present but the project is not initialized (no or
src/commerce-extensibility-1/), runnode_modulesbefore continuing. Init is idempotent — it finds the existing config, skips the interactive prompts, installs dependencies, and generates the project files.npx @adobe/aio-commerce-lib-app init - Actions can be authored in TypeScript only once the project has the TypeScript build setup (+ root
webpack-config.cjs) thattsconfig.jsonscaffolds for a TypeScript Commerce config — seeinit. Otherwise, author actions in JavaScript.commerce-app-init
- 验证应用已搭建并初始化,而不仅仅是存在配置文件。需同时满足:
- 项目根目录下存在,并且
app.commerce.config.ts - 项目已完成初始化——标志为生成了目录和已安装的
src/commerce-extensibility-1/(包含node_modules依赖)。@adobe/aio-commerce-lib-app
- 项目根目录下存在
- 如果缺失,请先停止并调用
app.commerce.config.ts(它会生成配置文件,然后执行初始化)。commerce-app-init - 如果配置文件存在但项目未初始化(没有或
src/commerce-extensibility-1/),请先运行node_modules再继续。初始化是幂等操作——它会找到现有配置,跳过交互式提示,安装依赖并生成项目文件。npx @adobe/aio-commerce-lib-app init - 只有当项目具备TypeScript构建环境(+ 根目录
webpack-config.cjs,由tsconfig.json为TypeScript Commerce配置搭建)时,才能用TypeScript编写动作,详情见init。否则,请用JavaScript编写动作。commerce-app-init
Step 1 — Understand intent
步骤1 — 明确需求
Ask the user what they want to intercept and how:
- What operation: the (the Commerce operation, e.g.,
webhook_method) andplugin.magento.catalog_product.save(webhook_typeorbefore)after - How the handler is reached: either a runtime action in this app () or an explicit external URL (
runtimeAction: "<package>/<action>") — these are mutually exclusivewebhook.url - Category (optional): (block if invalid),
validation(add data), orappend(alter data) — used for conflict detectionmodification - Batch and hook identifiers: groups related hooks;
batch_nameuniquely identifies this hook within the batchhook_name
询问用户想要拦截的内容及方式:
- 操作类型:(Commerce操作,例如
webhook_method)和plugin.magento.catalog_product.save(webhook_type或before)after - 处理器访问方式:要么是本应用中的Runtime动作(),要么是明确的外部URL(
runtimeAction: "<package>/<action>")——二者互斥webhook.url - 类别(可选):(验证不通过则阻止)、
validation(添加数据)或append(修改数据)——用于冲突检测modification - 批次与钩子标识符:用于分组相关钩子;
batch_name用于在批次内唯一标识该钩子hook_name
Step 2 — Derive config values
步骤2 — 推导配置值
Apply the following validation rules before writing. Surface any issues to the user before proceeding.
| Field | Constraint |
|---|---|
| |
| |
| Optional; must be |
| |
| Must be a valid absolute URL ( |
| Required, non-empty |
| Required, non-empty |
| Required HTTP method (e.g., |
| Optional; positive integer (milliseconds) |
| Optional; positive integer |
在写入前应用以下验证规则。在继续操作前向用户说明任何问题。
| 字段 | 约束条件 |
|---|---|
| 仅允许 |
| 仅允许 |
| 可选;必须为 |
| |
| 必须是有效的绝对URL( |
| 必填,不能为空 |
| 必填,不能为空 |
| 必填HTTP方法(例如 |
| 可选;正整数(毫秒) |
| 可选;正整数 |
Step 3 — Update app.commerce.config.ts
app.commerce.config.ts步骤3 — 更新app.commerce.config.ts
app.commerce.config.tsAdd entries to the top-level array (or create it), preserving all other domains. If the config already has a key, append to it rather than replacing it.
webhookswebhooksMinimal examples:
ts
// Runtime action handler (handler lives in this app)
webhooks: [
{
label: "Validate Product Save",
description: "Validates product data before saving.",
category: "validation", // optional
runtimeAction: "my-package/validate-product", // <package>/<action>
webhook: {
webhook_method: "plugin.magento.catalog_product.save",
webhook_type: "before",
batch_name: "my_app", // [a-zA-Z0-9_]+ only
hook_name: "validate_product", // [a-zA-Z0-9_]+ only
method: "POST",
},
},
];
// URL handler (external endpoint)
webhooks: [
{
label: "Fraud Check",
description: "Calls external fraud service before order placement.",
webhook: {
webhook_method: "plugin.magento.sales_order.place",
webhook_type: "before",
batch_name: "my_app",
hook_name: "fraud_check",
method: "POST",
url: "https://fraud.example.com/check", // inside webhook object, not top level
},
},
];Each entry also accepts an optional array ( / ) to scope it to specific Commerce environments. When omitted, the webhook applies to all environments; when set, it is only subscribed at install time on the listed environments.
env"paas""saas"See assets/webhooks-config.ts for the full annotated reference.
在顶层数组中添加条目(若不存在则创建),保留所有其他配置域。如果配置中已有键,请追加而非替换现有内容。
webhookswebhooks最简示例:
ts
// Runtime动作处理器(处理器位于本应用内)
webhooks: [
{
label: "Validate Product Save",
description: "Validates product data before saving.",
category: "validation", // optional
runtimeAction: "my-package/validate-product", // <package>/<action>
webhook: {
webhook_method: "plugin.magento.catalog_product.save",
webhook_type: "before",
batch_name: "my_app", // [a-zA-Z0-9_]+ only
hook_name: "validate_product", // [a-zA-Z0-9_]+ only
method: "POST",
},
},
];
// URL处理器(外部端点)
webhooks: [
{
label: "Fraud Check",
description: "Calls external fraud service before order placement.",
webhook: {
webhook_method: "plugin.magento.sales_order.place",
webhook_type: "before",
batch_name: "my_app",
hook_name: "fraud_check",
method: "POST",
url: "https://fraud.example.com/check", // inside webhook object, not top level
},
},
];每个条目还接受可选的数组( / ),用于限定到特定Commerce环境。省略时,webhook适用于所有环境;设置后,仅在安装时订阅到列出的环境。
env"paas""saas"完整带注释的参考配置请见assets/webhooks-config.ts。
Creating the handler action
创建处理器动作
For webhook entries that use , create the action file under and register it in .
runtimeActionsrc/actions/app.config.yaml对于使用的webhook条目,请在下创建动作文件,并在中注册。
runtimeActionsrc/actions/app.config.yamlRegister the action
注册动作
Add a user-defined package to alongside the existing package. Use any name except (reserved by the framework):
src/commerce-extensibility-1/ext.config.yamlapp-managementapp-managementyaml
undefined在中添加用户自定义包,与现有包并列。使用除之外的任意名称(该名称由框架保留):
src/commerce-extensibility-1/ext.config.yamlapp-managementapp-managementyaml
undefinedsrc/commerce-extensibility-1/ext.config.yaml
src/commerce-extensibility-1/ext.config.yaml
(add below the auto-generated app-management package)
(添加到自动生成的app-management包下方)
runtimeManifest:
packages:
app-management:
# ... auto-generated — do not edit
my-app: # your package name — any name except "app-management"
actions:
validate-product:
function: actions/validate-product/index.js # relative to src/commerce-extensibility-1/
web: "yes"
runtime: nodejs:24
annotations:
require-adobe-auth: true
The `<package>/<action>` format in `runtimeAction` maps directly: `my-app/validate-product` → package `my-app`, action `validate-product`.runtimeManifest:
packages:
app-management:
# ... 自动生成内容——请勿编辑
my-app: # 你的包名称——除"app-management"外的任意名称
actions:
validate-product:
function: actions/validate-product/index.js # 相对于src/commerce-extensibility-1/的路径
web: "yes"
runtime: nodejs:24
annotations:
require-adobe-auth: true
`runtimeAction`中的`<package>/<action>`格式直接对应:`my-app/validate-product` → 包`my-app`,动作`validate-product`。Handler skeleton
处理器骨架
typescript
// src/commerce-extensibility-1/actions/validate-product/index.ts
import {
ok,
successOperation,
exceptionOperation,
addOperation,
replaceOperation,
removeOperation,
} from "@adobe/aio-commerce-lib-webhooks/responses";
export async function main(params: Record<string, unknown>) {
// params contains the Commerce operation payload
// Allow the operation to proceed
return ok(successOperation());
// Block the operation (validation failure)
// return ok(exceptionOperation("Product SKU is required"));
// Append data to the operation result
// return ok(addOperation("result/custom_field", { value: "appended" }));
// Modify a field in the result
// return ok(replaceOperation("result/price", 99.99));
// Remove a field from the result
// return ok(removeOperation("result/unwanted_field"));
}Operation types:
| Response | Effect |
|---|---|
| Allow — operation proceeds unchanged |
| Block — operation is rejected with this message |
| Append data at |
| Replace the value at |
| Remove the field at |
typescript
// src/commerce-extensibility-1/actions/validate-product/index.ts
import {
ok,
successOperation,
exceptionOperation,
addOperation,
replaceOperation,
removeOperation,
} from "@adobe/aio-commerce-lib-webhooks/responses";
export async function main(params: Record<string, unknown>) {
// params包含Commerce操作的负载
// 允许操作继续执行
return ok(successOperation());
// 阻止操作(验证失败)
// return ok(exceptionOperation("Product SKU is required"));
// 向操作结果追加数据
// return ok(addOperation("result/custom_field", { value: "appended" }));
// 修改结果中的字段
// return ok(replaceOperation("result/price", 99.99));
// 移除结果中的字段
// return ok(removeOperation("result/unwanted_field"));
}操作类型:
| 响应 | 效果 |
|---|---|
| 允许——操作按原样继续执行 |
| 阻止——操作被拒绝并返回该消息 |
| 在结果的 |
| 替换结果 |
| 从结果中移除 |
Step 4 — Validate
步骤4 — 验证
Build the project to confirm the updated config is valid:
sh
aio app buildA build failure with a validation error points directly to the offending config field.
构建项目以确认更新后的配置有效:
sh
aio app build包含验证错误的构建失败会直接指向有问题的配置字段。
Common Issues
常见问题
- or
batch_namerejected: Use underscores as separators (hook_name,my_app) — hyphens, dots, and spaces are not accepted.validate_product_save - Both and
runtimeActionset: These are mutually exclusive — usewebhook.urlwhen the handler lives in this app;runtimeActionfor an external endpoint.webhook.url - at wrong level: For URL-based entries,
urlmust be inside the nestedurlobject, not at the top level alongsidewebhook.label - package name conflict: The framework generates this package in
app-managementon every build. Use any other name for your own actions.ext.config.yaml - Function path is relative to : Do not use
src/commerce-extensibility-1/or project-root-relative paths.src/...resolves correctly;actions/validate-product/index.jsdoes not.src/commerce-extensibility-1/actions/validate-product/index.js - not found: Ensure
defineConfigis installed and@adobe/aio-commerce-lib-appis imported fromdefineConfig.@adobe/aio-commerce-lib-app/config
- 或
batch_name被拒绝:使用下划线作为分隔符(hook_name、my_app)——不接受连字符、点号和空格。validate_product_save - 同时设置了和
runtimeAction:二者互斥——当处理器位于本应用内时使用webhook.url;外部端点使用runtimeAction。webhook.url - 层级错误:基于URL的条目中,
url必须嵌套在url对象内,而非与webhook同级的顶层。label - 包名称冲突:框架会在每次构建时在
app-management中生成该包。请为自己的动作使用其他名称。ext.config.yaml - 函数路径相对于:请勿使用
src/commerce-extensibility-1/或项目根目录相对路径。src/...路径有效;actions/validate-product/index.js无效。src/commerce-extensibility-1/actions/validate-product/index.js - 找不到:确保已安装
defineConfig,并从@adobe/aio-commerce-lib-app导入@adobe/aio-commerce-lib-app/config。defineConfig
Quality Bar
质量标准
- completes without errors
aio app build
- 无错误完成
aio app build
Chaining
后续操作
After passes:
aio app build- Add merchant settings — invoke to expose configurable settings in Commerce Admin
commerce-app-business-config - Add event subscriptions — invoke to subscribe to Commerce or external events
commerce-app-eventing - Extend the Admin UI — invoke to add custom columns, mass actions, order view buttons, or menu entries in Commerce Admin
commerce-app-admin-ui - Add persistent storage — invoke to back webhook handlers with queryable DB storage
commerce-app-storage
aio app build- 添加商家设置——调用在Commerce Admin中公开可配置的设置
commerce-app-business-config - 添加事件订阅——调用订阅Commerce或外部事件
commerce-app-eventing - 扩展Admin UI——调用在Commerce Admin中添加自定义列、批量操作、订单查看按钮或菜单项
commerce-app-admin-ui - 添加持久化存储——调用为webhook处理器提供可查询的数据库存储支持
commerce-app-storage
References
参考资料
- assets/webhooks-config.ts — Reference config showing both runtime action and URL-based webhook entry shapes
- assets/webhooks-config.ts——展示Runtime动作和基于URL的webhook条目格式的参考配置