Hoppa till huvudinnehållet
Version: 9.x

Användning med Amazon Lambda via API Gateway

Inofficiell Beta-översättning

Denna sida har översatts av PageTurner AI (beta). Inte officiellt godkänd av projektet. Hittade du ett fel? Rapportera problem →

Amazon Lambda-adapter

AWS Lambda-adaptern stöds för användningsfall med API Gateway Rest API(v1) och HTTP API(v2).

Exempelapp

DescriptionURLLinks
API Gateway with NodeJS client.n/a

Så här lägger du till tRPC

1. Installera beroenden

bash
yarn add @trpc/server
bash
yarn add @trpc/server

2. Skapa en tRPC-router

Implementera din tRPC-router. En exempelrouter visas nedan:

server.ts
ts
import * as trpc from '@trpc/server';
import { z } from 'zod';
const appRouter = trpc.router().query('getUser', {
input: z.string(),
async resolve(req) {
req.input; // string
return { id: req.input, name: 'Bilbo' };
},
});
// export type definition of API
export type AppRouter = typeof appRouter;
server.ts
ts
import * as trpc from '@trpc/server';
import { z } from 'zod';
const appRouter = trpc.router().query('getUser', {
input: z.string(),
async resolve(req) {
req.input; // string
return { id: req.input, name: 'Bilbo' };
},
});
// export type definition of API
export type AppRouter = typeof appRouter;

3. Använd Amazon API Gateway-adaptern

tRPC innehåller en adapter för API Gateway direkt ur lådan. Den här adaptern låter dig köra dina rutter via API Gateway-handlern.

server.ts
ts
import { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';
const appRouter = /* ... */;
// created for each request
const createContext = ({
event,
context,
}: CreateAWSLambdaContextOptions) => ({}) // no context
type Context = trpc.inferAsyncReturnType<typeof createContext>;
export const handler = awsLambdaRequestHandler({
router: appRouter,
createContext,
})
server.ts
ts
import { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';
const appRouter = /* ... */;
// created for each request
const createContext = ({
event,
context,
}: CreateAWSLambdaContextOptions) => ({}) // no context
type Context = trpc.inferAsyncReturnType<typeof createContext>;
export const handler = awsLambdaRequestHandler({
router: appRouter,
createContext,
})

Bygg och distribuera din kod, använd sedan din API Gateway-URL för att anropa din funktion.

EndpointHTTP URI
getUserGET https://<execution-api-link>/getUser?input=INPUT

where INPUT is a URI-encoded JSON string.

Lite om nyttolastformatversion

API Gateway använder två olika händelsedataformat när det anropar en Lambda. För REST API:er ska version "1.0" användas (APIGatewayProxyEvent), men för HTTP API:er kan du välja mellan version "1.0" eller "2.0".

  • Version 1.0: APIGatewayProxyEvent

  • Version 2.0: APIGatewayProxyEventV2

För att avgöra vilken version du har kan du ange kontexten enligt följande:

function createContext({ event, context, }: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>) { ... } // CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
function createContext({ event, context, }: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>) { ... } // CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>

Läs mer om nyttolastformatversion här