Hoppa till huvudinnehållet
Version: 10.x

AWS Lambda + API Gateway-adapter

Inofficiell Beta-översättning

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

AWS Lambda-adapter​

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

Exempelapp​

DescriptionLinks
API Gateway with NodeJS client.

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 { 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' };
}),
});
// export type definition of API
export type AppRouter = typeof appRouter;
server.ts
ts
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' };
}),
});
// 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<APIGatewayProxyEventV2>) => ({}) // no context
type Context = Awaited<ReturnType<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<APIGatewayProxyEventV2>) => ({}) // no context
type Context = Awaited<ReturnType<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 har två olika händelsedataformat när den anropar en Lambda. För REST API:er ska de vara version "1.0" (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:

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

Läs mer om nyttolastformatversion här