smithy-typescript/server-apigateway
This package provides glue code to enable using a server sdk inside of
apigateway.
Usage
Example
import { HttpRequest } from "@aws-sdk/protocol-http";
import {
GreetingService as __GreetingService,
SayHelloInput,
SayHelloOutput,
getGreetingServiceHandler,
} from "@greeting-service/service-greeting";
import { convertEvent, convertResponse } from "@aws-smithy/server-apigateway";
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2, APIGatewayProxyHandlerV2 } from "aws-lambda";
class GreetingService implements __GreetingService {
SayHello(input: SayHelloInput, request: HttpRequest): SayHelloOutput {
return {
greeting: `Hello ${input.name}! How is ${input.city}?`,
};
}
}
const serviceHandler = getGreetingServiceHandler(new GreetingService());
export const lambdaHandler: APIGatewayProxyHandlerV2 = async (
event: APIGatewayProxyEventV2
): Promise<APIGatewayProxyResultV2> => {
console.log(`Received event: ${JSON.stringify(event)}`);
const convertedEvent = convertEvent(event);
let rawResponse = await serviceHandler.handle(convertedEvent);
const convertedResponse = convertResponse(rawResponse);
console.log(`Returning response: ${JSON.stringify(convertedResponse)}`);
return convertedResponse;
};