进阶 trpc.io 2026-10-12 05:42:24 · 31 阅读

第7章 AWS Lambda + API Gateway 适配器

版本:11.x AWS Lambda + API Gateway 适配器 AWS Lambda 适配器 AWS Lambda 适配器支持 API Gateway REST API (v1)、HTTP API (v2) 以及 Lambda Function URL 使用场景。 `httpBatchLink` 要求路由器工作在单个 API Gateway Resource 上(如示例所示)。 如果你希望每个 Procedure 对应一个 Resource,可以改用 `httpLink`(更多信息)。 示例应用 | 描述 | 链接 | | :--- | :--- | | 使用 NodeJS 客户端的 API Gateway | 源代码 | | 支持响应流式传输的 API Gateway REST API | 源代码 | 如何添加 tRPC 1. 安装依赖 ```bash yarn add @trpc/server ``` AI 代理 如果你使用 AI 编码代理,请安装 tRPC 技能以获得更好的代码生成效果: ```bash npx @tanstack/intent@latest install ``` 2. 创建 tRPC 路由器 实现你的 tRPC 路由器。以下是一个示例路由器: server.ts ```typescript import { initTRPC } from '@trpc/server'; import { z } from 'zod'; export const t = initTRPC.create(); const appRouter = t.router({ getUser: t.procedure.input(z.string()).query((opts) => { opts.input; // string return { id: opts.input, name: 'Bilbo' }; }), }); // 导出 API 的类型定义 export type AppRouter = typeof appRouter; ``` 3. 使用 Amazon API Gateway 适配器 tRPC 自带 API Gateway 适配器。该适配器允许你通过 API Gateway handler 运行路由。 server.ts ```typescript import type { APIGatewayProxyEventV2 } from 'aws-lambda'; import type { CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda'; import { awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda'; import { appRouter } from './router'; // 为每个请求创建 const createContext = ({ event, context, }: CreateAWSLambdaContextOptions) => ({}); // 无上下文 type Context = Awaited>; export const handler = awsLambdaRequestHandler({ router: appRouter, createContext, }); ``` 构建并部署你的代码,现在可以使用你的 API Gateway URL 来调用该函数。 端点 | HTTP | URI | | :--- | :--- | | getUser | GET https:///getUser?input=INPUT |

这里 INPUT 是一个经过 URI 编码的 JSON 字符串。 关于 Payload 格式版本 API Gateway 在调用 Lambda 时采用两种不同的事件数据格式。对于 REST API,格式固定为 "1.0" 版本(即 APIGatewayProxyEvent);而对于 HTTP API,你可以选择 "1.0" 或 "2.0" 版本。 1.0 版本:APIGatewayProxyEvent 2.0 版本:APIGatewayProxyEventV2 如果你不确定当前使用的是哪个版本,可以按以下方式使用上下文进行推断: tsfunction createContext({ event, context,}: CreateAWSLambdaContextOptions) { // ...} // CreateAWSLambdaContextOptions 或 CreateAWSLambdaContextOptions tsfunction createContext({ event, context,}: CreateAWSLambdaContextOptions) { // ...} // CreateAWSLambdaContextOptions 或 CreateAWSLambdaContextOptions 在此处阅读更多关于 Payload 格式版本的说明 AWS Lambda 响应流式处理适配器 AWS Lambda 支持通过 Lambda Function URL 和 API Gateway REST API 向客户端流式传输响应。 Lambda Function URL 和 API Gateway REST API 均支持响应流式传输。若使用 API Gateway REST API,需将集成配置中的 responseTransferMode 设置为 STREAM。关于 Lambda 响应流式和 API Gateway 响应流式的更多细节,请参阅相关文档。 响应流式传输 流式处理器的签名与默认处理器不同。除了默认 Node 处理器的参数 event 和 context 外,流式处理器还额外接收一个可写流参数 responseStream。为了让 Lambda 对响应执行流式传输,必须使用 awslambda.streamifyResponse() 装饰器包裹函数处理器。 注意,awslambda 命名空间由 Lambda 执行环境自动提供。你可以通过从 @types/aws-lambda 导入类型来增强全局命名空间,从而引入 awslambda 命名空间。 server.tsts/// import type { APIGatewayProxyEventV2 } from 'aws-lambda';import type { CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda';import { awsLambdaStreamingRequestHandler } from '@trpc/server/adapters/aws-lambda';import { appRouter } from './router'; // 每个请求都会创建const createContext = ({ event, context,}: CreateAWSLambdaContextOptions) => ({ // 你的 context});type Context = Awaited>; export const handler = awslambda.streamifyResponse( awsLambdaStreamingRequestHandler({ router: appRouter, createContext, }),); server.tsts/// import type { APIGatewayProxyEventV2 } from 'aws-lambda';import type { CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda';import { awsLambdaStreamingRequestHandler } from '@trpc/server/adapters/aws-lambda';import { appRouter } from './router'; // 每个请求都会创建const createContext = ({ event, context,}: CreateAWSLambdaContextOptions) => ({ // 你的 context});type Context = Awaited>; export const handler = awslambda.streamifyResponse( awsLambdaStreamingRequestHandler({ router: appRouter, createContext, }),);

评论 (0)