@sailplane/lambda-utils
Version:
Use middleware to remove redundancy in AWS Lambda handlers.
149 lines (142 loc) • 7.06 kB
text/typescript
import { APIGatewayProxyEvent as APIGatewayProxyEvent$1, APIGatewayProxyEventV2 as APIGatewayProxyEventV2$1, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, Context, Callback } from 'aws-lambda';
import middy from '@middy/core';
/**
* Casted interface for APIGatewayProxyEvents as converted through the middleware
*/
interface APIGatewayProxyEvent extends APIGatewayProxyEvent$1 {
/**
* HTTP Request body, parsed from a JSON string into an object.
*/
body: any | null;
/**
* HTTP Path Parameters, always defined, never null
*/
pathParameters: {
[name: string]: string;
};
/**
* HTTP URL query string parameters, always defined, never null
*/
queryStringParameters: {
[name: string]: string;
};
}
type APIGatewayProxyEventV1 = APIGatewayProxyEvent;
/**
* Casted interface for APIGatewayProxyEventsV2 as converted through the middleware
*/
interface APIGatewayProxyEventV2 extends APIGatewayProxyEventV2$1 {
/**
* HTTP Request body, parsed from a JSON string into an object.
*/
body: any | null;
/**
* HTTP Path Parameters, always defined, never null
*/
pathParameters: {
[name: string]: string;
};
/**
* HTTP URL query string parameters, always defined, never null
*/
queryStringParameters: {
[name: string]: string;
};
}
type APIGatewayProxyEventAnyVersion = APIGatewayProxyEvent$1 | APIGatewayProxyEvent | APIGatewayProxyEventV2$1 | APIGatewayProxyEventV2;
type APIGatewayProxyResultAnyVersion = APIGatewayProxyResult | APIGatewayProxyStructuredResultV2;
/** LambdaUtils version of ProxyHandler for API Gateway v1 payload format */
type AsyncProxyHandlerV1 = (event: APIGatewayProxyEvent, context: Context, callback?: Callback<APIGatewayProxyResult>) => Promise<APIGatewayProxyResult | object | void>;
/** LambdaUtils version of an API Gateway v1 payload handler wrapped with middy */
type AsyncMiddyifedHandlerV1 = middy.MiddyfiedHandler<APIGatewayProxyEvent$1, APIGatewayProxyResult | object | void>;
/** LambdaUtils version of ProxyHandler for API Gateway v2 payload format */
type AsyncProxyHandlerV2 = (event: APIGatewayProxyEventV2, context: Context, callback?: Callback<APIGatewayProxyStructuredResultV2>) => Promise<APIGatewayProxyStructuredResultV2 | object | void>;
/** LambdaUtils version of an API Gateway v2 payload handler wrapped with middy */
type AsyncMiddyifedHandlerV2 = middy.MiddyfiedHandler<APIGatewayProxyEventV2$1, APIGatewayProxyStructuredResultV2 | object | void>;
/**
* Wrap an API Gateway V1 format proxy lambda function handler to add features:
* - Set CORS headers.
* - Normalize incoming headers to lowercase
* - If incoming content is JSON text, replace event.body with parsed object.
* - Ensures that event.queryStringParameters and event.pathParameters are defined,
* to avoid TypeErrors.
* - Ensures that handler response is formatted properly as a successful
* API Gateway result.
* - Catch http-errors exceptions into proper HTTP responses.
* - Catch other exceptions and return as HTTP 500
* - Set Lambda invocation and API request context in @sailplane/logger
*
* This wrapper includes commonly useful middleware. You may further wrap it
* with your own function that adds additional middleware, or just use it as
* an example.
*
* @param handler async function to wrap
* @see https://middy.js.org/#:~:text=available%20middlewares
* @see https://www.npmjs.com/package/http-errors
*/
declare function wrapApiHandler(handler: AsyncProxyHandlerV1): AsyncMiddyifedHandlerV1;
declare const wrapApiHandlerV1: typeof wrapApiHandler;
/**
* Wrap an API Gateway V2 format proxy lambda function handler to add features:
* - Set CORS headers.
* - Normalize incoming headers to lowercase
* - If incoming content is JSON text, replace event.body with parsed object.
* - Ensures that event.queryStringParameters and event.pathParameters are defined,
* to avoid TypeErrors.
* - Ensures that handler response is formatted properly as a successful
* API Gateway result.
* - Catch http-errors exceptions into proper HTTP responses.
* - Catch other exceptions and return as HTTP 500
* - Set Lambda invocation and API request context in @sailplane/logger
*
* This wrapper includes commonly useful middleware. You may further wrap it
* with your own function that adds additional middleware, or just use it as
* an example.
*
* @param handler async function to wrap
* @see https://middy.js.org/#:~:text=available%20middlewares
* @see https://www.npmjs.com/package/http-errors
*/
declare function wrapApiHandlerV2(handler: AsyncProxyHandlerV2): AsyncMiddyifedHandlerV2;
/**
* Construct the object that API Gateway payload format v1 wants back
* upon a successful run. (HTTP 200 Ok)
*
* This normally is not needed. If the response is simply the content to return as the
* body of the HTTP response, you may simply return it from the handler given to
* #wrapApiHandler(handler). It will automatically transform the result.
*
* @param result object to serialize into JSON as the response body
* @returns {APIGatewayProxyResult}
*/
declare function apiSuccess(result?: any): APIGatewayProxyResult;
/**
* Construct the object that API Gateway payload format v1 wants back upon a failed run.
*
* Often, it is simpler to throw a http-errors exception from your #wrapApiHandler
* handler.
*
* @see https://www.npmjs.com/package/http-errors
* @param statusCode HTTP status code, between 400 and 599.
* @param message string to return in the response body
* @returns {APIGatewayProxyResult}
*/
declare function apiFailure(statusCode: number, message?: string): APIGatewayProxyResult;
/**
* Middleware for LambdaUtils to set request context in Logger
*/
declare const loggerContextMiddleware: () => middy.MiddlewareObj<APIGatewayProxyEventAnyVersion>;
/**
* Middleware to allow an async handler to return its exact response body.
* This middleware will wrap it up as an APIGatewayProxyResult.
* Must be registered as the last (thus first to run) "after" middleware.
*/
declare const resolvedPromiseIsSuccessMiddleware: () => middy.MiddlewareObj<APIGatewayProxyEventAnyVersion, APIGatewayProxyResultAnyVersion>;
/**
* Middleware to handle any otherwise unhandled exception by logging it and generating
* an HTTP 500 response.
*
* Fine-tuned to work better than the Middy version, and uses @sailplane/logger.
*/
declare const unhandledExceptionMiddleware: () => middy.MiddlewareObj<APIGatewayProxyEventAnyVersion, APIGatewayProxyResultAnyVersion>;
export { type APIGatewayProxyEvent, type APIGatewayProxyEventAnyVersion, type APIGatewayProxyEventV1, type APIGatewayProxyEventV2, type APIGatewayProxyResultAnyVersion, type AsyncMiddyifedHandlerV1, type AsyncMiddyifedHandlerV2, type AsyncProxyHandlerV1, type AsyncProxyHandlerV2, apiFailure, apiSuccess, loggerContextMiddleware, resolvedPromiseIsSuccessMiddleware, unhandledExceptionMiddleware, wrapApiHandler, wrapApiHandlerV1, wrapApiHandlerV2 };