Skip to content

Commit 3e4e1cd

Browse files
committed
update documentation and tests
1 parent 1c9ad58 commit 3e4e1cd

File tree

9 files changed

+191
-20
lines changed

9 files changed

+191
-20
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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+
## [4.1.1]  (2020-10-31)
8+
9+
### Changed
10+
11+
- Update documentation
12+
713
## [4.1.0]  (2020-10-31)
814

915
### Added
@@ -307,6 +313,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/),
307313
- Update older libraries
308314
- Now publish from Git tags instead of master pushes
309315

316+
[4.1.1]: https://github.com/manwaring/lambda-wrapper/compare/v4.1.0...v4.1.1
310317
[4.1.0]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.6...v4.1.0
311318
[4.0.6]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.5...v4.0.6
312319
[4.0.5]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.4...v4.0.5

README.md

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export interface ApiSignature<T = any> {
152152
notAuthorized(params?: ResponseParameters): ApiResponse;
153153
redirect(params: RedirectParameters): ApiResponse;
154154
error(params?: ErrorParameters): ApiResponse;
155+
custom(params: CustomParameters): ApiResponse;
155156
}
156157
```
157158

@@ -215,6 +216,22 @@ interface ErrorParameters {
215216

216217
</details>
217218

219+
<details>
220+
<summary>CustomParameters</summary>
221+
222+
```ts
223+
interface CustomParameters {
224+
body?: any; // response body
225+
json?: boolean; // indicates if body should be JSON-stringified and content-type header set to application/json, defaults to true
226+
cors?: boolean; // indicates if CORS headers should be added, defaults to true
227+
statusCode: number; // status code to return
228+
headers?: { [key: string]: any }; // custom headers to include
229+
err?: Error; // optional Error object for automatic logging
230+
}
231+
```
232+
233+
</details>
234+
218235
<details>
219236
<summary>WebsocketRequest</summary>
220237

@@ -543,7 +560,7 @@ return redirect({ url, statusCode: 308, cors: false });
543560
cors?: boolean,
544561
statusCode?: number,
545562
headers?: { [key: string]: any},
546-
err?: Error;
563+
err?: Error
547564
}
548565
```
549566

@@ -596,6 +613,65 @@ console.debug(err);
596613

597614
</details>
598615

616+
<details>
617+
<summary>Custom</summary>
618+
619+
### Available parameters
620+
621+
```ts
622+
{
623+
body?: any,
624+
json?: boolean,
625+
cors?: boolean,
626+
statusCode: number,
627+
headers?: { [key: string]: any},
628+
err?: Error
629+
}
630+
```
631+
632+
### Default parameters
633+
634+
```ts
635+
{
636+
json: true,
637+
cors: true,
638+
}
639+
```
640+
641+
### Invocation with defaults
642+
643+
```ts
644+
return custom({ statusCode: 418 });
645+
646+
// returns
647+
{
648+
statusCode: 418,
649+
headers: {
650+
'Access-Control-Allow-Origin': '*',
651+
'Access-Control-Allow-Credentials': true,
652+
}
653+
}
654+
```
655+
656+
### Invocation overriding defaults
657+
658+
```ts
659+
const body = { message: 'Custom response' };
660+
return custom({ body, statusCode: 418 });
661+
662+
// returns
663+
{
664+
body: "{\"message\": \"Custom response\"}",
665+
statusCode: 418,
666+
headers: {
667+
'Access-Control-Allow-Origin': '*',
668+
'Access-Control-Allow-Credentials': true,
669+
}
670+
}
671+
```
672+
673+
</details>
674+
599675
# API Gateway HTTP API
600676

601677
Other than the raw payload from AWS the HTTP API method signature and response functions match the API Gateway signature and functions. Hooray for wrappers! Note that you still need to provide the correct wrapper function so that the library can parse the AWS event correctly.
@@ -667,6 +743,7 @@ export interface HttpApiSignature<T = any> {
667743
notAuthorized(params?: ResponseParameters): ApiResponse;
668744
redirect(params: RedirectParameters): ApiResponse;
669745
error(params?: ErrorParameters): ApiResponse;
746+
custom(params: CustomParameters): ApiResponse;
670747
}
671748
```
672749

@@ -730,6 +807,22 @@ interface ErrorParameters {
730807

731808
</details>
732809

810+
<details>
811+
<summary>CustomParameters</summary>
812+
813+
```ts
814+
interface CustomParameters {
815+
body?: any; // response body
816+
json?: boolean; // indicates if body should be JSON-stringified and content-type header set to application/json, defaults to true
817+
cors?: boolean; // indicates if CORS headers should be added, defaults to true
818+
statusCode: number; // status code to return
819+
headers?: { [key: string]: any }; // custom headers to include
820+
err?: Error; // optional Error object for automatic logging
821+
}
822+
```
823+
824+
</details>
825+
733826
<details>
734827
<summary>HttpApiEvent</summary>
735828

@@ -1127,6 +1220,65 @@ console.debug(err);
11271220

11281221
</details>
11291222

1223+
<details>
1224+
<summary>Custom</summary>
1225+
1226+
### Available parameters
1227+
1228+
```ts
1229+
{
1230+
body?: any,
1231+
json?: boolean,
1232+
cors?: boolean,
1233+
statusCode: number,
1234+
headers?: { [key: string]: any},
1235+
err?: Error
1236+
}
1237+
```
1238+
1239+
### Default parameters
1240+
1241+
```ts
1242+
{
1243+
json: true,
1244+
cors: true,
1245+
}
1246+
```
1247+
1248+
### Invocation with defaults
1249+
1250+
```ts
1251+
return custom({ statusCode: 418 });
1252+
1253+
// returns
1254+
{
1255+
statusCode: 418,
1256+
headers: {
1257+
'Access-Control-Allow-Origin': '*',
1258+
'Access-Control-Allow-Credentials': true,
1259+
}
1260+
}
1261+
```
1262+
1263+
### Invocation overriding defaults
1264+
1265+
```ts
1266+
const body = { message: 'Custom response' };
1267+
return custom({ body, statusCode: 418 });
1268+
1269+
// returns
1270+
{
1271+
body: "{\"message\": \"Custom response\"}",
1272+
statusCode: 418,
1273+
headers: {
1274+
'Access-Control-Allow-Origin': '*',
1275+
'Access-Control-Allow-Credentials': true,
1276+
}
1277+
}
1278+
```
1279+
1280+
</details>
1281+
11301282
# CloudFormation Custom Resource
11311283

11321284
## Sample TypeScript implementation

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
collectCoverageFrom: ['src/**/*.ts', '!src/**/*index.ts'],
3-
coverageThreshold: { global: { lines: 90 }}
3+
coverageThreshold: { global: { lines: 90 } },
44
};

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": "4.1.0",
4+
"version": "4.1.1",
55
"scripts": {
66
"publish-please-dry-run": "publish-please --dry-run",
77
"publish-please": "publish-please",

src/api/v1/wrapper.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-empty-function */
12
import { apiGatewayEvent } from 'serverless-plugin-test-helper';
23
import { api, ApiSignature } from './wrapper';
34

@@ -28,7 +29,7 @@ describe('API wrapper', () => {
2829
const callback = jest.fn((err, result) => (err ? new Error(err) : result));
2930

3031
it('Has expected properties and response functions', () => {
31-
function custom({
32+
function customHandler({
3233
event,
3334
websocket,
3435
body,
@@ -43,6 +44,7 @@ describe('API wrapper', () => {
4344
invalid,
4445
redirect,
4546
error,
47+
custom,
4648
}: ApiSignature) {
4749
expect(event).toEqual(requestEvent);
4850
expect(websocket.connectionId).toEqual('abc-123');
@@ -58,17 +60,18 @@ describe('API wrapper', () => {
5860
expect(invalid).toBeInstanceOf(Function);
5961
expect(redirect).toBeInstanceOf(Function);
6062
expect(error).toBeInstanceOf(Function);
61-
success('success');
63+
expect(custom).toBeInstanceOf(Function);
64+
success({ body: 'success' });
6265
}
63-
api(custom)(requestEvent, context, callback);
66+
api(customHandler)(requestEvent, context, callback);
6467
});
6568

6669
it('Has expected properties and response functions with optional generic type', () => {
6770
interface CustomType {
6871
Message: string;
6972
Id: number;
7073
}
71-
function custom({
74+
function customHandler({
7275
event,
7376
websocket,
7477
body,
@@ -83,6 +86,7 @@ describe('API wrapper', () => {
8386
invalid,
8487
redirect,
8588
error,
89+
custom,
8690
}: ApiSignature<CustomType>) {
8791
expect(event).toEqual(requestEvent);
8892
expect(websocket.connectionId).toEqual('abc-123');
@@ -98,8 +102,9 @@ describe('API wrapper', () => {
98102
expect(invalid).toBeInstanceOf(Function);
99103
expect(redirect).toBeInstanceOf(Function);
100104
expect(error).toBeInstanceOf(Function);
101-
success('success');
105+
expect(custom).toBeInstanceOf(Function);
106+
success({ body: 'success' });
102107
}
103-
api(custom)(requestEvent, context, callback);
108+
api(customHandler)(requestEvent, context, callback);
104109
});
105110
});

src/api/v1/wrapper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import {
1212
ResponseParameters,
1313
RedirectParameters,
1414
ErrorParameters,
15+
CustomParameters,
1516
} from '../shared';
1617

1718
export function api<T = any>(
18-
custom: (props: ApiSignature<T>) => any
19+
customHandler: (props: ApiSignature<T>) => any
1920
): (event: APIGatewayEvent, context: Context, callback: Callback) => any {
2021
return function handler(event: APIGatewayEvent) {
2122
const { body, websocket, path, query, auth, headers, testRequest } = new Request(event).getProperties();
@@ -36,7 +37,7 @@ export function api<T = any>(
3637
redirect,
3738
custom,
3839
};
39-
return custom(signature);
40+
return customHandler(signature);
4041
};
4142
}
4243

@@ -55,7 +56,7 @@ export interface ApiSignature<T = any> {
5556
notAuthorized(params?: ResponseParameters): ApiResponse;
5657
redirect(params: RedirectParameters): ApiResponse;
5758
error(params?: ErrorParameters): ApiResponse;
58-
custom(params: ResponseParameters): ApiResponse;
59+
custom(params: CustomParameters): ApiResponse;
5960
}
6061

6162
export interface WebsocketRequest {

0 commit comments

Comments
 (0)