Skip to content

Commit e461577

Browse files
committed
remove iopipe and epsagon, switch from callback to response/error, and update to 2.0
1 parent 096c722 commit e461577

33 files changed

+2167
-1377
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
loglevel=silent

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
66

7+
## [2.0.0]  (2020-01-04)
8+
9+
### Changed
10+
11+
- Removed references to Epsagon and IOPipe
12+
- Instead of callbacks, response methods use the recommended return syntax
13+
14+
### Added
15+
16+
- V2 of the docs
17+
718
## [1.2.2]  (2019-10-14)
819

920
### Changed

README.md

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
# AWS Lambda wrapper library
1111

12+
### This documentation is for v2 of the library - see [old-docs/v1/README.md for the v1 documentation](old-docs/v1/README.md)
13+
1214
1. [Overview](#overview)
1315
1. [Installation and setup](#installation-and-setup)
1416
- [Optional configuration](#optional-configuration)
@@ -31,7 +33,7 @@ This library provides custom Lambda function wrappers which expose standard, abs
3133

3234
### Rationale and motivation
3335

34-
AWS Lambda supports a wide variety of event triggers, each with unique payloads and expected responses. The Lambda execution environment, however, only provides the raw events and has no included mechanisms for simplifying response object creation. For example, API Gateway events include only the raw request body, leaving it up to developers to implement parsing themselves. Similarly, the developer is responsible for creating a response object which includes the correct HTTP Status Code and headers. Given the standard nature of these kinds of concerns, this library exposes helpful abstractions like parsed HTTP bodies based on content-type headers, and success response functions which apply correct status codes and headers before invoking the Lambda callback.
36+
AWS Lambda supports a wide variety of event triggers, each with unique payloads and expected responses. The Lambda execution environment, however, only provides the raw events and has no included mechanisms for simplifying response object creation. For example, API Gateway events include only the raw request body, leaving it up to developers to implement parsing themselves. Similarly, the developer is responsible for creating a response object which includes the correct HTTP status code and headers. Given the standard nature of these kinds of concerns, this library exposes helpful abstractions like parsed HTTP bodies based on content-type headers, and success response functions which create response objects with the correct status codes and headers for returning.
3537

3638
# Installation and setup
3739

@@ -71,9 +73,9 @@ export const handler = api(async ({ body, path, success, error }) => {
7173
try {
7274
const { pathParam1, pathParam2 } = path;
7375
const results = await doSomething(body, pathParam1, pathParam2);
74-
success(results);
76+
return success(results);
7577
} catch (err) {
76-
error(err);
78+
return error(err);
7779
}
7880
});
7981
```
@@ -89,10 +91,17 @@ interface ApiSignature {
8991
headers: { [name: string]: string }; // headers param payload as key-value pairs
9092
testRequest: boolean; // indicates if this is a test request, based on presence of headers matching 'Test-Request' or process.env.TEST_REQUEST_HEADER
9193
auth: any; // auth context from custom authorizer
92-
success(payload?: any, replacer?: (this: any, key: string, value: any) => any): void; // returns 200 status with payload
93-
invalid(errors?: string[]): void; // returns 400 status with errors
94-
redirect(url: string): void; // returns 302 redirect with new url
95-
error(error?: any): void; // returns 500 status with error
94+
success(payload?: any, replacer?: (this: any, key: string, value: any) => any): ApiResponse; // returns 200 status with payload
95+
invalid(errors?: string[]): ApiResponse; // returns 400 status with errors
96+
notFound(message?: string): ApiResponse; // returns 404 status with message
97+
redirect(url: string): ApiResponse; // returns 302 redirect with new url
98+
error(error?: any): ApiResponse; // returns 500 status with error
99+
}
100+
101+
interface ApiResponse {
102+
statusCode: number;
103+
headers: { [name: string]: string | boolean };
104+
body?: string;
96105
}
97106
```
98107

@@ -108,9 +117,9 @@ import { cloudFormation } from '@manwaring/lambda-wrapper';
108117
export const handler = cloudFormation(({ event, success, failure }) => {
109118
try {
110119
const { BucketName } = event.ResourceProperties;
111-
success();
120+
return success();
112121
} catch (err) {
113-
failure(err);
122+
return failure(err);
114123
}
115124
});
116125
```
@@ -152,8 +161,8 @@ interface DynamoDBStreamSignature {
152161
newVersions: any[]; // array of all unmarshalled javascript objects of new images
153162
oldVersions: any[]; // array of all unmarshalled javascript objects of old images
154163
versions: Version[]; // array of full version object (new image, old image, etc - see Version interface)
155-
success(message?: any): void; // invokes lambda callback with success
156-
error(error?: any): void; // invokes lambda callback with error
164+
success(message?: any): any; // logs and returns the message
165+
error(error?: any): void; // logs the error and throws it
157166
}
158167

159168
interface Version {
@@ -180,9 +189,9 @@ export const handler = authorizer(async ({ token, valid, invalid }) => {
180189
return invalid('Missing token');
181190
}
182191
const jwt = await verifier.verifyAccessToken(token);
183-
valid(jwt);
192+
return valid(jwt);
184193
} catch (err) {
185-
invalid(err);
194+
return invalid(err);
186195
}
187196
});
188197
```
@@ -193,9 +202,21 @@ export const handler = authorizer(async ({ token, valid, invalid }) => {
193202
interface AuthorizerSignature {
194203
event: CustomAuthorizerEvent; // original event
195204
token: string; // authorizer token from original event
196-
valid(jwt: any): void; // creates AWS policy to authenticate request, and adds auth context if available
197-
invalid(message?: string[]): void; // returns 401 unauthorized
198-
error(error?: any): void; // records error information and returns 401 unauthorized
205+
valid(jwt: any): Policy; // returns AWS policy to authenticate request, and adds auth context if available
206+
invalid(message?: any): void; // records invalid information and throws 401 unauthorized
207+
error(error?: any): void; // records error information and throws 401 unauthorized
208+
}
209+
210+
interface Policy {
211+
principalId: string;
212+
policyDocument: {
213+
Version: string;
214+
Statement: {
215+
Action: string;
216+
Effect: string;
217+
Resource: string;
218+
}[];
219+
};
199220
}
200221
```
201222

@@ -221,9 +242,9 @@ export const handler = sns(async ({ message, success, error }) => {
221242
```ts
222243
interface SnsSignature {
223244
event: SNSEvent; // original event
224-
message: any; // JSON-parsed message from event (or raw message if not JSON)
225-
success(payload?: any): void; // invokes lambda callback with success
226-
error(error?: any): void; // invokes lambda callback with error
245+
message: any; // JSON-parsed message from event
246+
success(message?: any): any; // logs and returns the message
247+
error(error?: any): void; // logs the error and throws
227248
}
228249
```
229250

@@ -238,9 +259,9 @@ export const handler = wrapper(async ({ event, success, error }) => {
238259
try {
239260
const { value1, value2 } = event;
240261
const results = await doSomething(value1, value2);
241-
success(results);
262+
return success(results);
242263
} catch (err) {
243-
error(err);
264+
return error(err);
244265
}
245266
});
246267
```
@@ -250,8 +271,8 @@ export const handler = wrapper(async ({ event, success, error }) => {
250271
```ts
251272
interface WrapperSignature {
252273
event: any; // original event
253-
success(payload?: any): void; // invokes lambda callback with success response
254-
error(error?: any): void; // invokes lambda callback with error response
274+
success(message?: any): any; // logs and returns the message
275+
error(error?: any): void; // logs the error and throws
255276
}
256277
```
257278

examples/ts/app/cloudformation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import 'source-map-support/register';
33

44
export const handler = cloudFormation(({ event, success, failure }) => {
55
try {
6-
success(event.ResourceProperties.BucketName);
6+
return success(event.ResourceProperties.BucketName);
77
} catch (err) {
8-
failure(err);
8+
return failure(err);
99
}
1010
});

examples/ts/app/generic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import 'source-map-support/register';
33

44
export const handler = wrapper(async ({ event, success, error }) => {
55
try {
6-
success(event);
6+
return success(event);
77
} catch (err) {
8-
error(err);
8+
return error(err);
99
}
1010
});

0 commit comments

Comments
 (0)