Skip to content

Commit 0094691

Browse files
committed
Add config for default value
- Create a new method to set the default value for `headers`, `body` and `statusCodes`. - Also it should not break the old implementation. - Move to ES6 codestyle. Fix #2
1 parent 4267bae commit 0094691

File tree

4 files changed

+314
-95
lines changed

4 files changed

+314
-95
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2016
3+
Copyright (c) 2017
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ A wrap around response for aws lambda integration proxy
88

99
## Response creation
1010
1. Response can be created using `response` method
11-
```
12-
var lambdaProxy = require('lambda-proxy-reponse');
11+
```javascript
12+
const lambdaProxy = require('lambda-proxy-reponse');
1313

1414
exports.handler = (event, context, callback) => {
1515
// response method needs at least 3 parameters
1616
// statusCode, headers, body
17-
var response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
17+
const response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
1818

1919
// -> response = {
2020
// statusCodes: 200,
@@ -39,6 +39,82 @@ A wrap around response for aws lambda integration proxy
3939
- `lambdaProxy.notFound = lambdaProxy.response(404, ...)`
4040
- `lambdaProxy.serverError = lambdaProxy.response(500, ...)`
4141

42+
## Options
43+
From version 2.0, new config option has been introduced to set default values for response.
44+
45+
### body
46+
Default body for every response. Will be overriden if you support new body in the call
47+
```javascript
48+
// ....
49+
lambdaProxy.config({ body: { bodyData: 'yourDefaultBodyData' } });
50+
51+
// ......
52+
let response = lambdaProxy.response(200, { 'X-header': 'Your headers value' });
53+
54+
// -> response = {
55+
// statusCodes: 200,
56+
// headers: {'X-header': 'Your headers value'},
57+
// body: '{ "bodyData": "yourDefaultBodyData" }'
58+
// }
59+
60+
response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
61+
// -> response = {
62+
// statusCodes: 200,
63+
// headers: {'X-header': 'Your headers value'},
64+
// body: '{ "bodyData": "yourBodyData" }'
65+
// }
66+
```
67+
68+
### headers
69+
Default headers for every response. By default, it will extend whatever headers you provide inside
70+
your response. Can be turned off by setting `extendHeader` in options.
71+
```javascript
72+
// ....
73+
lambdaProxy.config({ header: { 'X-Default-Header': 'Your default header' } });
74+
75+
// ......
76+
let response = lambdaProxy.response(200, null, { bodyData: 'yourBodyData' });
77+
78+
// -> response = {
79+
// statusCodes: 200,
80+
// headers: { 'X-Default-Header': 'Your default header' },
81+
// body: '{ "bodyData": "yourBodyData" }'
82+
// }
83+
84+
// by default it will extend your header
85+
response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
86+
// -> response = {
87+
// statusCodes: 200,
88+
// headers: { 'X-header': 'Your headers value', 'X-Default-Header': 'Your default header' },
89+
// body: '{ "bodyData": "yourBodyData" }'
90+
// }
91+
92+
// you can turn it off by setting config
93+
lambdaProxy.config({ header: { 'X-Default-Header': 'Your default header' }, extendHeader: false });
94+
95+
// .......
96+
response = lambdaProxy.response(200, { 'X-header': 'Your headers value' }, { 'bodyData': 'yourBodyData' });
97+
// -> response = {
98+
// statusCodes: 200,
99+
// headers: { 'X-header': 'Your headers value' },
100+
// body: '{ "bodyData": "yourBodyData" }'
101+
// }
102+
```
103+
### status
104+
Default status code.
105+
```javascript
106+
// ....
107+
lambdaProxy.config({ status: 200 });
108+
109+
// ....
110+
response = lambdaProxy.response();
111+
// -> response = {
112+
// statusCodes: 200,
113+
// headers: {},
114+
// body: '{}'
115+
// }
116+
```
117+
42118
## Note
43119
This response template is only for used with newly aws lambda proxy integration.
44120

index.js

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,93 @@
11
'use strict';
2-
exports.response = function response(status, headers, body, cb) {
3-
// hearders must be an object, if not APIGateway will throw error
4-
if (typeof headers !== 'object') {
5-
headers = {};
2+
class LambdaProxyResponse {
3+
constructor(options) {
4+
this.options = options || { headers: {}, body: {}, status: null, extendHeader: true };
65
}
76

8-
if (typeof body === 'undefined' || typeof body === 'null') {
9-
body = {};
7+
config(options) {
8+
if (typeof options === 'object' && options !== null) {
9+
Object.assign(this.options, options);
10+
}
1011
}
1112

12-
status = status || 400;
13-
var responseTemplate = createResponseTemplate(status, headers, body);
13+
response(status, headers, body, cb, options) {
14+
let responseStatus = {};
15+
let responseHeader = {};
16+
let responseBody = {};
1417

15-
// call cb
16-
if (cb) {
17-
cb(null, responseTemplate);
18+
responseStatus = status || this.options.status || 400;
19+
20+
// set headers
21+
if (typeof headers !== 'object' || headers === null ) {
22+
if (this.options && this.options.headers) {
23+
responseHeader = this.options.headers;
24+
} else {
25+
responseHeader = {};
26+
}
27+
} else {
28+
if (this.options.headers && this.options.extendHeader) {
29+
responseHeader = Object.assign(responseHeader, headers, this.options.headers);
30+
} else {
31+
responseHeader = headers;
32+
}
33+
}
34+
35+
// set body
36+
if (typeof body === 'undefined' || body === null) {
37+
if (this.options && this.options.body) {
38+
responseBody = this.options.body;
39+
} else {
40+
responseBody = {};
41+
}
42+
} else {
43+
responseBody = body;
44+
}
45+
46+
const responseTemplate = this._createResponseTemplate(responseStatus, responseHeader, responseBody);
47+
48+
// call cb
49+
if (typeof cb === 'function') {
50+
cb(null, responseTemplate);
51+
}
52+
53+
return responseTemplate;
1854
}
1955

20-
return responseTemplate;
21-
};
56+
ok(headers, body, cb, options) {
57+
return this.response(200, headers, body, cb, options || {});
58+
}
2259

23-
exports.ok = function ok(headers, body, cb) {
24-
return exports.response(200, headers, body, cb);
25-
}
60+
created(headers, body, cb, options) {
61+
return this.response(201, headers, body, cb, options || {});
62+
}
2663

27-
exports.created = function created(headers, body, cb) {
28-
return exports.response(201, headers, body, cb);
29-
}
64+
badRequest(headers, body, cb, options) {
65+
return this.response(400, headers, body, cb, options || {});
66+
}
3067

31-
exports.badRequest = function badRequest(headers, body, cb) {
32-
return exports.response(400, headers, body, cb);
33-
}
68+
notAuthorized(headers, body, cb, options) {
69+
return this.response(401, headers, body, cb, options || {});
70+
}
3471

35-
exports.notAuthorized = function notAuthorized(headers, body, cb) {
36-
return exports.response(401, headers, body, cb);
37-
}
72+
forbidden(headers, body, cb, options) {
73+
return this.response(403, headers, body, cb, options || {});
74+
}
3875

39-
exports.forbidden = function forbidden(headers, body, cb) {
40-
return exports.response(403, headers, body, cb);
41-
}
76+
notFound(headers, body, cb, options) {
77+
return this.response(404, headers, body, cb, options || {});
78+
}
4279

43-
exports.notFound = function notFound(headers, body, cb) {
44-
return exports.response(404, headers, body, cb);
45-
}
80+
serverError(headers, body, cb, options) {
81+
return this.response(500, headers, body, cb, options || {});
82+
}
4683

47-
exports.serverError = function serverError(headers, body, cb) {
48-
return exports.response(500, headers, body, cb);
84+
_createResponseTemplate(status, headers, body) {
85+
return {
86+
statusCode: status,
87+
body: JSON.stringify(body),
88+
headers: headers
89+
};
90+
}
4991
}
5092

51-
function createResponseTemplate(status, headers, body) {
52-
return {
53-
statusCode: status,
54-
body: JSON.stringify(body),
55-
headers: headers
56-
};
57-
}
93+
module.exports = new LambdaProxyResponse();

0 commit comments

Comments
 (0)