Skip to content

Commit 344039c

Browse files
committed
update dependencies, add custom api response method, and add lint-staged with eslint
1 parent fa199e7 commit 344039c

File tree

10 files changed

+2640
-2803
lines changed

10 files changed

+2640
-2803
lines changed

.eslintrc.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root: true
2+
3+
parser: '@typescript-eslint/parser'
4+
parserOptions:
5+
ecmaVersion: 2020
6+
sourceType: module
7+
8+
extends:
9+
- plugin:@typescript-eslint/recommended
10+
- prettier/@typescript-eslint
11+
- plugin:prettier/recommended

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ 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.0]  (2020-10-31)
8+
9+
### Added
10+
11+
- Custom response option for api gateway responses
12+
13+
### Changed
14+
15+
- Update dependencies
16+
717
## [4.0.6]  (2020-09-23)
818

919
### Changed
@@ -297,6 +307,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/),
297307
- Update older libraries
298308
- Now publish from Git tags instead of master pushes
299309

310+
[4.1.0]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.6...v4.1.0
300311
[4.0.6]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.5...v4.0.6
301312
[4.0.5]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.4...v4.0.5
302313
[4.0.4]: https://github.com/manwaring/lambda-wrapper/compare/v4.0.3...v4.0.4

jest.config.js

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

package-lock.json

Lines changed: 2553 additions & 2787 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,43 @@
1616
"cfn-response": "^1.0.1"
1717
},
1818
"devDependencies": {
19-
"@babel/core": "^7.11.6",
20-
"@babel/plugin-proposal-class-properties": "^7.10.4",
21-
"@babel/preset-env": "^7.11.5",
22-
"@babel/preset-typescript": "^7.10.4",
23-
"@types/aws-lambda": "^8.10.62",
19+
"@babel/core": "^7.12.3",
20+
"@babel/plugin-proposal-class-properties": "^7.12.1",
21+
"@babel/preset-env": "^7.12.1",
22+
"@babel/preset-typescript": "^7.12.1",
23+
"@types/aws-lambda": "^8.10.64",
2424
"@types/cfn-response": "^1.0.3",
25-
"@types/jest": "^26.0.14",
26-
"@types/node": "^14.11.2",
25+
"@types/jest": "^26.0.15",
26+
"@types/node": "^14.14.6",
27+
"@typescript-eslint/eslint-plugin": "^4.6.0",
28+
"@typescript-eslint/parser": "^4.6.0",
2729
"aws-lambda": "^1.0.6",
28-
"aws-sdk": "^2.761.0",
29-
"babel-jest": "^26.3.0",
30-
"codecov": "^3.7.2",
30+
"aws-sdk": "^2.783.0",
31+
"babel-jest": "^26.6.1",
32+
"codecov": "^3.8.0",
33+
"eslint": "^7.12.1",
34+
"eslint-config-prettier": "^6.15.0",
35+
"eslint-plugin-prettier": "^3.1.4",
3136
"husky": "^4.3.0",
32-
"jest": "^26.4.2",
33-
"publish-please": "^5.5.1",
37+
"jest": "^26.6.1",
38+
"lint-staged": "^10.5.1",
39+
"prettier": "^2.1.2",
40+
"publish-please": "^5.5.2",
3441
"rimraf": "^3.0.2",
3542
"serverless-plugin-test-helper": "^2.4.5",
36-
"typescript": "^4.0.3"
43+
"typescript": "^4.0.5"
3744
},
3845
"husky": {
3946
"hooks": {
40-
"pre-commit": "npm test"
47+
"pre-commit": "lint-staged && npm test"
4148
}
4249
},
50+
"lint-staged": {
51+
"./src/**/*.ts": [
52+
"eslint --fix",
53+
"git add ."
54+
]
55+
},
4356
"publishConfig": {
4457
"access": "public"
4558
},

src/api/shared/responses.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { success, error, invalid, notFound, notAuthorized, redirect } from './responses';
1+
import { success, error, invalid, notFound, notAuthorized, redirect, custom } from './responses';
22

33
describe('API responses', () => {
44
beforeEach(() => {
@@ -146,6 +146,17 @@ describe('API responses', () => {
146146
});
147147
});
148148

149+
it('Handles custom response', () => {
150+
const response = custom({ statusCode: 418 });
151+
expect(response).toEqual({
152+
headers: {
153+
'Access-Control-Allow-Origin': '*',
154+
'Access-Control-Allow-Credentials': true,
155+
},
156+
statusCode: 418,
157+
});
158+
});
159+
149160
it('Handles notFound response', () => {
150161
const response = notFound({ body: 'not found' });
151162
expect(response).toEqual({

src/api/shared/responses.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export function invalid({
4141
return response;
4242
}
4343

44+
export function custom({ body, json = true, cors = true, statusCode, headers }: CustomParameters): ApiResponse {
45+
const response = getResponseFromParameters({ body, json, cors, statusCode, headers });
46+
metrics.custom(response);
47+
return response;
48+
}
49+
4450
const defaultNotFound: ResponseParameters = {
4551
cors: true,
4652
statusCode: 404,
@@ -135,6 +141,14 @@ export interface ResponseParameters {
135141
headers?: { [key: string]: any }; // custom headers to include
136142
}
137143

144+
export interface CustomParameters {
145+
body?: any; // response body
146+
json?: boolean; // indicates if body should be JSON-stringified and content-type header set to application/json, defaults to true
147+
cors?: boolean; // indicates if CORS headers should be added, defaults to true
148+
statusCode: number; // status code to return, defaults by callback (success: 200, invalid: 400, notFound: 404, notAuthorized: 401, redirect: 302, error: 500)
149+
headers?: { [key: string]: any }; // custom headers to include
150+
}
151+
138152
export interface ErrorParameters {
139153
body?: any; // response body
140154
json?: boolean; // indicates if body should be JSON-stringified and content-type header set to application/json, defaults to true

src/api/v1/wrapper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
notAuthorized,
88
error,
99
redirect,
10+
custom,
1011
ApiResponse,
1112
ResponseParameters,
1213
RedirectParameters,
@@ -33,6 +34,7 @@ export function api<T = any>(
3334
notAuthorized,
3435
error,
3536
redirect,
37+
custom,
3638
};
3739
return custom(signature);
3840
};
@@ -53,6 +55,7 @@ export interface ApiSignature<T = any> {
5355
notAuthorized(params?: ResponseParameters): ApiResponse;
5456
redirect(params: RedirectParameters): ApiResponse;
5557
error(params?: ErrorParameters): ApiResponse;
58+
custom(params: ResponseParameters): ApiResponse;
5659
}
5760

5861
export interface WebsocketRequest {

src/api/v2-http/wrapper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
notAuthorized,
99
error,
1010
redirect,
11+
custom,
1112
ApiResponse,
1213
ResponseParameters,
1314
RedirectParameters,
@@ -33,6 +34,7 @@ export function httpApi<T = any>(
3334
notAuthorized,
3435
error,
3536
redirect,
37+
custom,
3638
};
3739
return custom(signature);
3840
};
@@ -52,4 +54,5 @@ export interface HttpApiSignature<T = any> {
5254
notAuthorized(params?: ResponseParameters): ApiResponse;
5355
redirect(params: RedirectParameters): ApiResponse;
5456
error(params?: ErrorParameters): ApiResponse;
57+
custom(params: ResponseParameters): ApiResponse;
5558
}

src/common/metrics.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ export class Metrics {
4545
failure(response?: any): void {
4646
logger.debug(`Failure processing ${this.type} event, responding with`, response);
4747
}
48+
49+
custom(response?: any): void {
50+
logger.debug(`Unknown processing status of ${this.type} event, responding with`, response);
51+
}
4852
}

0 commit comments

Comments
 (0)