Skip to content

Commit 58f2b4b

Browse files
committed
update all method signatures
1 parent 173ce1d commit 58f2b4b

26 files changed

+307
-129
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ 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-
## [1.0.7]  (2019-09-04)
7+
## [1.0.8]  (2019-09-07)
8+
9+
### Changed
10+
11+
- Update the remaining wrapper signature with Lambda inputs (event, context, callback) for easier testing in applications
12+
13+
## [1.0.7]  (2019-09-07)
814

915
### Changed
1016

@@ -38,6 +44,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/),
3844
- Update older libraries
3945
- Now publish from Git tags instead of master pushes
4046

47+
[1.0.8]: https://github.com/manwaring/lambda-wrapper/compare/v1.0.7...v1.0.8
4148
[1.0.7]: https://github.com/manwaring/lambda-wrapper/compare/v1.0.1...v1.0.7
4249
[1.0.1]: https://github.com/manwaring/lambda-wrapper/compare/v1.0.0...v1.0.1
4350
[1.0.0]: https://github.com/manwaring/lambda-wrapper/compare/v0.3.8...v1.0.0

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@manwaring/lambda-wrapper",
33
"description": "A lambda handler wrapper to abstract common functionality and provide useful defaults",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"scripts": {
66
"publish-please-dry-run": "publish-please --dry-run",
77
"publish-please": "publish-please",

src/api/responses.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('API responses', () => {
3131
it('Handles error response', () => {
3232
const error = errorWrapper(metrics, callback);
3333
error('error');
34-
expect(callback).toHaveBeenCalledWith(new Error('error'));
34+
expect(callback).toHaveBeenCalledWith('error');
3535
});
3636

3737
it('Handles invalid response', () => {

src/api/responses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function invalidWrapper(metrics: Metrics, callback: Callback) {
3131
export function errorWrapper(metrics: Metrics, callback: Callback) {
3232
return function error(error?: any) {
3333
metrics.error(error);
34-
callback(new Error(error));
34+
callback(error);
3535
};
3636
}
3737

src/api/wrapper.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('API wrapper', () => {
2626
const callback = jest.fn((err, result) => (err ? new Error(err) : result));
2727

2828
it('Has expected properties and response functions', () => {
29-
function mockHandler({
29+
function custom({
3030
event,
3131
body,
3232
path,
@@ -52,6 +52,6 @@ describe('API wrapper', () => {
5252
expect(error).toBeInstanceOf(Function);
5353
success('success');
5454
}
55-
api(mockHandler)(requestEvent, context, callback);
55+
api(custom)(requestEvent, context, callback);
5656
});
5757
});

src/api/wrapper.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function api(
1111
return function handler(event: APIGatewayEvent, context: Context, callback: Callback) {
1212
const { body, path, query, auth, headers, testRequest } = new Request(event).getProperties();
1313

14-
const signature = {
14+
return custom({
1515
event,
1616
body,
1717
path,
@@ -23,8 +23,7 @@ export function api(
2323
invalid: invalidWrapper(metrics, callback),
2424
error: errorWrapper(metrics, callback),
2525
redirect: redirectWrapper(metrics, callback)
26-
};
27-
return custom(signature);
26+
});
2827
};
2928
}
3029

src/authorizer/responses.test.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
import { valid, invalid, error } from './responses';
1+
import { validWrapper, invalidWrapper, errorWrapper } from './responses';
2+
import { Metrics } from '../common';
23

34
describe('Lambda Authorizer responses', () => {
5+
const metrics = new Metrics('Lambda Authorizer');
6+
const callback = jest.fn((err, result) => (err ? new Error(err) : result));
7+
8+
beforeEach(() => {
9+
jest.resetAllMocks();
10+
});
11+
412
it('Handles valid response with jwt sub', () => {
13+
const valid = validWrapper(metrics, callback);
514
const jwt = {
615
sub: '1234567890',
716
name: 'John Doe',
817
admin: true
918
};
10-
expect(valid(jwt)).toEqual({
19+
20+
valid(jwt);
21+
expect(callback).toHaveBeenCalledWith(null, {
1122
principalId: '1234567890',
1223
policyDocument: {
1324
Version: '2012-10-17',
@@ -23,12 +34,15 @@ describe('Lambda Authorizer responses', () => {
2334
});
2435

2536
it('Handles valid response with jwt claims', () => {
37+
const valid = validWrapper(metrics, callback);
2638
const jwt = {
2739
claims: 'abcdefghij',
2840
name: 'John Doe',
2941
admin: true
3042
};
31-
expect(valid(jwt)).toEqual({
43+
44+
valid(jwt);
45+
expect(callback).toHaveBeenCalledWith(null, {
3246
principalId: 'abcdefghij',
3347
policyDocument: {
3448
Version: '2012-10-17',
@@ -44,11 +58,14 @@ describe('Lambda Authorizer responses', () => {
4458
});
4559

4660
it('Handles valid response without jwt subs or claims', () => {
61+
const valid = validWrapper(metrics, callback);
4762
const jwt = {
4863
name: 'John Doe',
4964
admin: true
5065
};
51-
expect(valid(jwt)).toEqual({
66+
67+
valid(jwt);
68+
expect(callback).toHaveBeenCalledWith(null, {
5269
principalId: '',
5370
policyDocument: {
5471
Version: '2012-10-17',
@@ -64,10 +81,14 @@ describe('Lambda Authorizer responses', () => {
6481
});
6582

6683
it('Handles invalid response', () => {
67-
expect(() => invalid()).toThrow('Unauthorized');
84+
const invalid = invalidWrapper(metrics, callback);
85+
invalid();
86+
expect(callback).toHaveBeenCalledWith('Unauthorized');
6887
});
6988

7089
it('Handles error response', () => {
71-
expect(() => error('error')).toThrow('error');
90+
const error = errorWrapper(metrics, callback);
91+
error('error');
92+
expect(callback).toHaveBeenCalledWith('error');
7293
});
7394
});

src/authorizer/responses.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
import { Callback } from 'aws-lambda';
12
import { Metrics } from '../common';
23

3-
const metrics = new Metrics('Lambda Authorizer');
4-
5-
export function valid(jwt: any) {
6-
const policy = generatePolicy(jwt);
7-
metrics.valid(policy);
8-
return policy;
4+
export function validWrapper(metrics: Metrics, callback: Callback) {
5+
return function valid(jwt: any) {
6+
const policy = generatePolicy(jwt);
7+
metrics.valid(policy);
8+
callback(null, policy);
9+
};
910
}
1011

11-
export function invalid(message?: any): void {
12-
metrics.invalid(message);
13-
throw new Error('Unauthorized');
12+
export function invalidWrapper(metrics: Metrics, callback: Callback) {
13+
return function invalid(message?: any): void {
14+
metrics.invalid(message);
15+
callback('Unauthorized');
16+
};
1417
}
1518

16-
export function error(error?: any) {
17-
metrics.error(error);
18-
throw new Error(error);
19+
export function errorWrapper(metrics: Metrics, callback: Callback) {
20+
return function error(error?: any) {
21+
metrics.error(error);
22+
callback(error);
23+
};
1924
}
2025

2126
function generatePolicy(jwt: any): any {

src/authorizer/wrapper.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import { authorizer, AuthorizerSignature } from './wrapper';
22

3+
const context = {
4+
callbackWaitsForEmptyEventLoop: false,
5+
functionName: 'function-name',
6+
functionVersion: '$LATEST',
7+
invokedFunctionArn: 'arn:',
8+
memoryLimitInMB: 128,
9+
awsRequestId: 'request',
10+
logGroupName: 'group',
11+
logStreamName: 'stream',
12+
getRemainingTimeInMillis: () => 2,
13+
done: () => {},
14+
fail: () => {},
15+
succeed: () => {}
16+
};
17+
const callback = jest.fn((err, result) => (err ? new Error(err) : result));
18+
319
describe('Stream wrapper', () => {
420
const requestEvent = {
521
type: 'type',
@@ -8,14 +24,14 @@ describe('Stream wrapper', () => {
824
};
925

1026
it('Has expected properties and response funtions', () => {
11-
function mockHandler({ event, token, valid, invalid, error }: AuthorizerSignature) {
27+
function custom({ event, token, valid, invalid, error }: AuthorizerSignature) {
1228
expect(event).toEqual(requestEvent);
1329
expect(token).toEqual('token');
1430
expect(valid).toBeInstanceOf(Function);
1531
expect(invalid).toBeInstanceOf(Function);
1632
expect(error).toBeInstanceOf(Function);
33+
error('error');
1734
}
18-
// @ts-ignore
19-
authorizer(mockHandler)(requestEvent);
35+
authorizer(custom)(requestEvent, context, callback);
2036
});
2137
});

0 commit comments

Comments
 (0)