webiny-http-route
它能做什么
这项技能在你的 AI 将一个普通网址暴露在 Webiny API 上时提供指导。适合需要简单 HTTP 端点(用于 webhook、集成或自定义响应)的开发者。
适用场景
- ✓当你需要 API 上的普通 HTTP 端点,而不是 GraphQL 查询的时候。
- ✓当另一个系统必须调用自定义 URL,向你的项目发送数据的时候。
- ✓当自定义路由需要访问平台服务,例如日志记录的时候。
技能链接
https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/http-route/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在我们的 Webiny API 上创建一个 POST 路由,接收支付服务商的 webhook 数据,并将其记录到日志中。”
每周 5 个新技能,1 分钟读完。
工作原理
技能说明如何编写一个带有 handle 方法的处理器类,并在配置文件中用路由的方法和路径进行注册。处理器通过容器接收其服务。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Custom HTTP Routes
TL;DR
Write a handler that implements HttpRouteHandler.Interface, then point <Api.Route> at it in
webiny.config.tsx. The method and path props configure both the API Gateway route and the
router, so the handler file never restates them. Handlers get full DI.
YOU MUST include the full file path with the .ts extension in the src prop. Use
src={"/extensions/MyRoute.ts"}, not src={"/extensions/MyRoute"}. Omitting it fails the build.
YOU MUST use export default for the createImplementation() call. Named exports fail here.
The route pattern
// extensions/MyRoute.ts
import { HttpRouteHandler, Logger } from "webiny/api";
class MyRouteImpl implements HttpRouteHandler.Interface {
constructor(private logger: Logger.Interface) {}
async handle(request: HttpRouteHandler.Request, response: HttpRouteHandler.Response) {
this.logger.info({ path: request.path }, "Handling request");
return response.status(200).json({ status: "ok" });
}
}
export default HttpRouteHandler.createImplementation({
implementation: MyRouteImpl,
dependencies: [Logger]
});
Register it:
<Api.Route method={"POST"} path={"/my-route"} src={"/extensions/MyRoute.ts"} />
Props reference
| Prop | Type | Required | Description |
|---|---|---|---|
path |
string |
Yes | Route path — must start with / |
method |
string |
Yes | HTTP method (see below) |
src |
string |
Yes | Path to the handler file (must include .ts) |
routeName |
string |
No | Route name (kebab-case). Derived from path + method if omitted. Doubles as the Pulumi resource name and the id a decorator matches on |
Methods: DELETE, GET, HEAD, PATCH, POST, PUT, OPTIONS, ANY. Use ANY to match every
method on a path.
Path parameters
Write them either way. {orderId} is API Gateway syntax, :orderId is the router's; the extension
converts to whichever each consumer needs, so both work and mean the same thing.
<Api.Route method={"GET"} path={"/orders/{orderId}"} src={"/extensions/GetOrderRoute.ts"} />
<Api.Route method={"GET"} path={"/orders/:orderId"} src={"/extensions/GetOrderRoute.ts"} />
Read them off request.pathParameters:
class GetOrderRouteImpl implements HttpRouteHandler.Interface {
constructor(private getOrder: GetOrderUseCase.Interface) {}
async handle(request: HttpRouteHandler.Request, response: HttpRouteHandler.Response) {
const order = await this.getOrder.execute(request.pathParameters.orderId);
return response.status(200).json(order);
}
}
export default HttpRouteHandler.createImplementation({
implementation: GetOrderRouteImpl,
dependencies: [GetOrderUseCase]
});
Wildcards are the exception: /files/* matches for the router, {proxy+} for API Gateway, and they
capture differently. Write a wildcard route for the target you mean.
Request
HttpRouteHandler.Request is transport-agnostic — no API Gateway or Node types leak into your code.
interface Request {
method: string;
path: string;
headers: Record<string, string>;
query: Record<string, string>;
pathParameters: Record<string, string>;
body: any;
/** Which route matched — `{ name, method, path }`. */
route: MatchedRouteDefinition;
}
route is what makes a decorator able to act on one route (see below), and lets a handler read its
own identity. method/path on it are the route's PATTERN (/orders/:orderId), where the
top-level method/path are the request's actual values (/orders/abc123).
Response
HttpRouteHandler.Response is a mutable builder, the res of an Express-style handler. Every method
returns this, so calls chain:
| Method | Purpose |
|---|
常见问题
方法和路径在哪里设置?
在配置文件的路由注册中设置,因此处理器文件无需重复它们。
处理器可以使用日志记录器吗?
可以。技能的示例把日志记录器作为依赖注入。
路径中需要特殊的文件扩展名吗?
需要。源路径必须包含 .ts 扩展名。
详情
作者的原始描述
Adding custom HTTP routes to the API with <Api.Route> and HttpRouteHandler. Use this skill when the developer wants to expose a custom HTTP endpoint (GET, POST, PUT, etc.) on the API alongside the GraphQL handler, implement a route handler with full DI support, or register a custom HTTP route in webiny.config.tsx.
repo: webiny/webiny-js
热度
- GitHub 星标
- 8.1k
- 排名
- #12588
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
prd-to-plan
8.1k帮你的智能体把产品需求文档转化为分阶段的计划,由许多小的端到端切片组成,并保存为 Markdown 文件。
webiny-admin-architect
8.1k教你的智能体如何为 Webiny 平台构建管理界面,使用其业务逻辑层和呈现器模式。
webiny-admin-permissions
8.1k教你的智能体为 Webiny 管理界面添加权限控制,包括自动生成的权限表单以及显示或隐藏检查。
webiny-form-model
8.1k教你的智能体使用声明式字段构建器、布局、校验和条件规则,在 Webiny 中构建表单。