|
36 | 36 | - [Optional configuration](#optional-configuration)
|
37 | 37 | 1. [Supported events](#supported-events)
|
38 | 38 | - [API Gateway](#api-gateway)
|
| 39 | + - [API Gateway HTTP API](#api-gateway-http-api) |
39 | 40 | - [CloudFormation Custom Resource](#cloudformation-custom-resource)
|
40 | 41 | - [DynamoDB Stream](#dynamodb-stream)
|
41 | 42 | - [Lambda Authorizer](#lambda-authorizer)
|
@@ -74,6 +75,7 @@ If you want each invocation to be tagged with the AWS region, environment/, and
|
74 | 75 | All of the events bellow have a corresponding wrapper which provides a deconstructed method signature exposing parsed/unmarshalled request parameters and helper response methods.
|
75 | 76 |
|
76 | 77 | 1. [API Gateway](#api-gateway) with support for cors headers and 200, 302, 400, and 500 responses
|
| 78 | +1. [API Gateway HTTP API](#api-gateway-http-api) with support for cors headers and 200, 302, 400, and 500 responses |
77 | 79 | 1. [CloudFormation Custom Resource](#cloudformation-custom-resource) with support for CloudFormation successes and failures
|
78 | 80 | 1. [DynamoDB Stream](#dynamodb-stream) with support for success and failure responses
|
79 | 81 | 1. [Lambda Authorizer](#lambda-authorizer) with support for creating access policies for successfully authorized requests
|
@@ -151,7 +153,55 @@ interface ApiResponse {
|
151 | 153 | }
|
152 | 154 | ```
|
153 | 155 |
|
154 |
| -\*Note that each callback helper functions (success, invalid, redirect, error) includes CORS-enabling header information |
| 156 | +\*Note that each callback helper function (success, invalid, redirect, error) includes CORS-enabling header information |
| 157 | + |
| 158 | +## API Gateway HTTP API |
| 159 | + |
| 160 | +### Sample implementation |
| 161 | + |
| 162 | +```ts |
| 163 | +import { httpApi } from '@manwaring/lambda-wrapper'; |
| 164 | +import { CustomInterface } from './custom-interface'; |
| 165 | + |
| 166 | +// By passing in CustomInterface as a generic the async method signature will correctly identify newVersions as an array of CustomInterface, making TypeScript development easier (note that the generic is not required in JavaScript projects) |
| 167 | +export const handler = httpApi<CustomInterface>(async ({ body, path, success, error }) => { |
| 168 | + try { |
| 169 | + const { pathParam1, pathParam2 } = path; |
| 170 | + const results = await doSomething(body, pathParam1, pathParam2); |
| 171 | + return success(results); |
| 172 | + } catch (err) { |
| 173 | + return error(err); |
| 174 | + } |
| 175 | +}); |
| 176 | +``` |
| 177 | + |
| 178 | +### Properties and methods available on wrapper signature |
| 179 | + |
| 180 | +```ts |
| 181 | +export interface HttpApiSignature<T = any> { |
| 182 | + event: HttpApiEvent; // original event from https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.proxy-format |
| 183 | + body: T; // JSON parsed body payload if exists (otherwise undefined) |
| 184 | + path: { [name: string]: string }; // path param payload as key-value pairs if exists (otherwise undefined) |
| 185 | + query: { [name: string]: string }; // query param payload as key-value pairs if exists (otherwise undefined) |
| 186 | + headers: { [name: string]: string }; // header payload as key-value pairs if exists (otherwise undefined) |
| 187 | + testRequest: boolean; // indicates if this is a test request - looks for a header matching process.env.TEST_REQUEST_HEADER (dynamic from application) or 'Test-Request' (default) |
| 188 | + auth: any; // auth context from custom authorizer if exists (otherwise undefined) |
| 189 | + success(payload?: any, replacer?: (this: any, key: string, value: any) => any): ApiResponse; // returns 200 status code with optional payload as body |
| 190 | + invalid(errors?: string[]): ApiResponse; // returns 400 status code with optional errors as body |
| 191 | + notFound(message?: string): ApiResponse; // returns 404 status code with optional message as body |
| 192 | + notAuthorized(message?: string): ApiResponse; // returns 403 status code with optional message as body |
| 193 | + redirect(url: string): ApiResponse; // returns 302 status code (redirect) with new url |
| 194 | + error(error?: any): ApiResponse; // returns 500 status code with optional error as body |
| 195 | +} |
| 196 | + |
| 197 | +interface ApiResponse { |
| 198 | + statusCode: number; |
| 199 | + headers: { [name: string]: string | boolean }; |
| 200 | + body?: string; |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +\*Note that each callback helper function (success, invalid, redirect, error) includes CORS-enabling header information |
155 | 205 |
|
156 | 206 | ## CloudFormation Custom Resource
|
157 | 207 |
|
|
0 commit comments