|
1 | 1 | '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 }; |
6 | 5 | } |
7 | 6 |
|
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 | + } |
10 | 11 | } |
11 | 12 |
|
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 = {}; |
14 | 17 |
|
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; |
18 | 54 | } |
19 | 55 |
|
20 | | - return responseTemplate; |
21 | | -}; |
| 56 | + ok(headers, body, cb, options) { |
| 57 | + return this.response(200, headers, body, cb, options || {}); |
| 58 | + } |
22 | 59 |
|
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 | + } |
26 | 63 |
|
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 | + } |
30 | 67 |
|
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 | + } |
34 | 71 |
|
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 | + } |
38 | 75 |
|
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 | + } |
42 | 79 |
|
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 | + } |
46 | 83 |
|
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 | + } |
49 | 91 | } |
50 | 92 |
|
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