@aws-smithy/server-apigateway
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.10 • Public • Published

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)}`);

  // Convert apigateway's lambda event to an HttpRequest.
  const convertedEvent = convertEvent(event);

  // Call the service handler, which will route the request to the GreetingService
  // implementation and then serialize the response to an HttpResponse.
  let rawResponse = await serviceHandler.handle(convertedEvent);

  // Convert the HttpResponse to apigateway's expected format.
  const convertedResponse = convertResponse(rawResponse);
  console.log(`Returning response: ${JSON.stringify(convertedResponse)}`);
  return convertedResponse;
};

/@aws-smithy/server-apigateway/

    Package Sidebar

    Install

    npm i @aws-smithy/server-apigateway

    Weekly Downloads

    1,584

    Version

    1.0.0-alpha.10

    License

    Apache-2.0

    Unpacked Size

    19.5 kB

    Total Files

    7

    Last publish

    Collaborators

    • pose
    • smithy-team
    • chasecoa
    • hpmellema
    • syall
    • mattsb42-aws
    • jordonphillips
    • amzn-oss
    • adamthom
    • gosar