@comicrelief/lambda-wrapper 1.10.2 → 2.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +196 -50
  2. package/dist/core/DependencyAwareClass.d.ts +16 -0
  3. package/dist/core/DependencyAwareClass.d.ts.map +1 -0
  4. package/dist/core/DependencyAwareClass.js +20 -0
  5. package/dist/core/DependencyAwareClass.js.map +1 -0
  6. package/dist/core/DependencyInjection.d.ts +62 -0
  7. package/dist/core/DependencyInjection.d.ts.map +1 -0
  8. package/dist/core/DependencyInjection.js +105 -0
  9. package/dist/core/DependencyInjection.js.map +1 -0
  10. package/dist/core/LambdaWrapper.d.ts +73 -0
  11. package/dist/core/LambdaWrapper.d.ts.map +1 -0
  12. package/dist/core/LambdaWrapper.js +170 -0
  13. package/dist/core/LambdaWrapper.js.map +1 -0
  14. package/dist/core/config.d.ts +20 -0
  15. package/dist/core/config.d.ts.map +1 -0
  16. package/dist/core/config.js +21 -0
  17. package/dist/core/config.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +57 -143
  21. package/dist/index.js.map +1 -0
  22. package/dist/models/ResponseModel.d.ts +73 -0
  23. package/dist/models/ResponseModel.d.ts.map +1 -0
  24. package/dist/models/ResponseModel.js +100 -0
  25. package/dist/models/ResponseModel.js.map +1 -0
  26. package/dist/models/SQSMessageModel.d.ts +46 -0
  27. package/dist/models/SQSMessageModel.d.ts.map +1 -0
  28. package/dist/models/SQSMessageModel.js +65 -0
  29. package/dist/models/SQSMessageModel.js.map +1 -0
  30. package/dist/services/BaseConfigService.d.ts +101 -0
  31. package/dist/services/BaseConfigService.d.ts.map +1 -0
  32. package/dist/services/BaseConfigService.js +180 -0
  33. package/dist/services/BaseConfigService.js.map +1 -0
  34. package/dist/services/HTTPService.d.ts +29 -0
  35. package/dist/services/HTTPService.d.ts.map +1 -0
  36. package/dist/services/HTTPService.js +55 -0
  37. package/dist/services/HTTPService.js.map +1 -0
  38. package/dist/services/LoggerService.d.ts +102 -0
  39. package/dist/services/LoggerService.d.ts.map +1 -0
  40. package/dist/services/LoggerService.js +249 -0
  41. package/dist/services/LoggerService.js.map +1 -0
  42. package/dist/services/RequestService.d.ts +119 -0
  43. package/dist/services/RequestService.d.ts.map +1 -0
  44. package/dist/services/RequestService.js +293 -0
  45. package/dist/services/RequestService.js.map +1 -0
  46. package/dist/services/SQSService.d.ts +263 -0
  47. package/dist/services/SQSService.d.ts.map +1 -0
  48. package/dist/services/SQSService.js +411 -0
  49. package/dist/services/SQSService.js.map +1 -0
  50. package/dist/services/TimerService.d.ts +22 -0
  51. package/dist/services/TimerService.d.ts.map +1 -0
  52. package/dist/services/TimerService.js +40 -0
  53. package/dist/services/TimerService.js.map +1 -0
  54. package/dist/types/Status.d.ts +6 -0
  55. package/dist/types/Status.d.ts.map +1 -0
  56. package/dist/types/Status.js +3 -0
  57. package/dist/types/Status.js.map +1 -0
  58. package/dist/utils/LambdaTermination.d.ts +14 -0
  59. package/dist/utils/LambdaTermination.d.ts.map +1 -0
  60. package/dist/utils/LambdaTermination.js +23 -0
  61. package/dist/utils/LambdaTermination.js.map +1 -0
  62. package/dist/utils/PromisifiedDelay.d.ts +17 -0
  63. package/dist/utils/PromisifiedDelay.d.ts.map +1 -0
  64. package/dist/utils/PromisifiedDelay.js +48 -0
  65. package/dist/utils/PromisifiedDelay.js.map +1 -0
  66. package/package.json +36 -26
  67. package/.eslintrc.yml +0 -16
  68. package/.github/dependabot.yml +0 -11
  69. package/.github/workflows/main.yml +0 -74
  70. package/babel.config.js +0 -13
  71. package/dist/Config/Dependencies.js +0 -33
  72. package/dist/DependencyInjection/DependencyAware.class.js +0 -40
  73. package/dist/DependencyInjection/DependencyInjection.class.js +0 -115
  74. package/dist/Model/CloudEvent.model.js +0 -138
  75. package/dist/Model/Model.model.js +0 -39
  76. package/dist/Model/Response.model.js +0 -135
  77. package/dist/Model/SQS/MarketingPreference.constraints.json +0 -28
  78. package/dist/Model/SQS/MarketingPreference.model.js +0 -583
  79. package/dist/Model/SQS/Message.model.js +0 -92
  80. package/dist/Model/Status.model.js +0 -71
  81. package/dist/Service/BaseConfig.service.js +0 -204
  82. package/dist/Service/HTTP.service.js +0 -57
  83. package/dist/Service/Logger.service.js +0 -256
  84. package/dist/Service/Request.service.js +0 -294
  85. package/dist/Service/SQS.service.js +0 -367
  86. package/dist/Service/Timer.service.js +0 -46
  87. package/dist/Wrapper/LambdaTermination.js +0 -34
  88. package/dist/Wrapper/LambdaWrapper.js +0 -143
  89. package/dist/Wrapper/PromisifiedDelay.js +0 -55
package/package.json CHANGED
@@ -1,50 +1,60 @@
1
1
  {
2
2
  "name": "@comicrelief/lambda-wrapper",
3
- "version": "1.10.2",
3
+ "version": "2.0.0-beta.10",
4
4
  "description": "Lambda wrapper for all Comic Relief Serverless Projects",
5
5
  "main": "dist/index.js",
6
+ "author": "Adam Clark",
7
+ "license": "ISC",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/comicrelief/lambda-wrapper.git"
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
6
15
  "scripts": {
16
+ "prepare": "yarn clean && yarn build",
17
+ "build": "tsc -p tsconfig-build.json",
18
+ "clean": "rm -rf dist",
7
19
  "lint": "eslint src tests",
8
- "test": "jest --coverage",
9
- "build": "babel src --out-dir dist --copy-files",
10
- "prepublish": "yarn build"
20
+ "test": "jest",
21
+ "coverage": "yarn test --coverage"
11
22
  },
12
- "author": "Adam Clark",
13
- "license": "ISC",
14
23
  "devDependencies": {
15
- "@babel/cli": "^7.22.9",
16
- "@babel/core": "^7.22.9",
17
- "@babel/eslint-parser": "^7.22.9",
18
- "@babel/node": "^7.22.6",
19
- "@babel/plugin-syntax-flow": "^7.22.5",
20
- "@babel/plugin-transform-flow-strip-types": "^7.22.5",
21
- "@babel/plugin-transform-react-jsx": "^7.22.5",
22
- "@babel/preset-env": "^7.22.9",
23
24
  "@comicrelief/eslint-config": "^2.0.3",
24
- "@types/jest": "^28.1.8",
25
- "aws-sdk": "^2.1427.0",
26
- "babel-jest": "^28.1.3",
27
- "eslint": "^8.22.0",
28
- "eslint-plugin-flowtype": "^8.0.3",
25
+ "@istanbuljs/nyc-config-typescript": "^1.0.2",
26
+ "@types/async": "^3.2.20",
27
+ "@types/jest": "^29.5.4",
28
+ "@types/node": "14",
29
+ "@types/useragent": "^2.3.2",
30
+ "@types/uuid": "^9.0.3",
31
+ "@types/xml2js": "^0.4.12",
32
+ "@typescript-eslint/eslint-plugin": "^5.33.0",
33
+ "@typescript-eslint/parser": "^5.33.0",
34
+ "aws-sdk": "^2.1456.0",
35
+ "eslint": "^8.49.0",
29
36
  "eslint-plugin-import": "^2.25.2",
30
37
  "eslint-plugin-jsdoc": "^39.3.2",
31
- "eslint-plugin-sonarjs": "^0.13.0",
32
- "eslint-plugin-unicorn": "^42.0.0",
33
- "jest": "^29.6.2",
38
+ "jest": "^29.7.0",
34
39
  "nyc": "^15.1.0",
35
- "semantic-release": "^19.0.5"
40
+ "semantic-release": "^19.0.5",
41
+ "ts-jest": "^29.1.1",
42
+ "ts-node": "^10.9.1",
43
+ "tsconfig-paths": "^4.2.0",
44
+ "typescript": "^4.7.4"
36
45
  },
37
46
  "peerDependencies": {
38
47
  "aws-sdk": "^2.831.0"
39
48
  },
40
49
  "dependencies": {
41
- "@sentry/node": "^6.19.7",
50
+ "@lumigo/tracer": "^1.87.0",
51
+ "@sentry/node": "^6.0.1",
52
+ "@types/aws-lambda": "^8.10.120",
42
53
  "alai": "1.0.3",
43
54
  "async": "^3.2.4",
44
55
  "axios": "^0.27.2",
45
- "epsagon": "^1.123.3",
46
56
  "useragent": "2.3.0",
47
- "uuid": "^8.3.2",
57
+ "uuid": "^9.0.1",
48
58
  "validate.js": "0.13.1",
49
59
  "winston": "^3.10.0",
50
60
  "xml2js": "^0.6.2"
package/.eslintrc.yml DELETED
@@ -1,16 +0,0 @@
1
- root: true
2
-
3
- extends:
4
- - '@comicrelief/eslint-config'
5
- - '@comicrelief/eslint-config/mixins/jsdoc'
6
-
7
- parser: '@babel/eslint-parser'
8
-
9
- ignorePatterns:
10
- - node_modules
11
- - dist
12
- - coverage
13
- - babel.config.js
14
-
15
- rules:
16
- unicorn/prefer-node-protocol: off
@@ -1,11 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: npm
4
- directory: "/"
5
- schedule:
6
- interval: daily
7
- open-pull-requests-limit: 10
8
- commit-message:
9
- prefix: "fix"
10
- prefix-development: "chore"
11
- include: "scope"
@@ -1,74 +0,0 @@
1
- name: test-and-publish
2
-
3
- on: [push]
4
-
5
- jobs:
6
- lint:
7
- name: Lint
8
- runs-on: ubuntu-latest
9
-
10
- steps:
11
- - uses: actions/checkout@v2
12
-
13
- - uses: actions/setup-node@v1
14
- with:
15
- node-version: 14
16
-
17
- - name: Restore cache
18
- uses: actions/cache@v2
19
- with:
20
- path: '**/node_modules'
21
- key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
22
-
23
- - run: yarn install
24
-
25
- - run: yarn lint
26
- env:
27
- CI: true
28
-
29
- unit-test:
30
- name: Run Unit Tests
31
- runs-on: ubuntu-latest
32
-
33
- steps:
34
- - uses: actions/checkout@v2
35
-
36
- - uses: actions/setup-node@v1
37
- with:
38
- node-version: 14
39
-
40
- - name: Restore cache
41
- uses: actions/cache@v2
42
- with:
43
- path: '**/node_modules'
44
- key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
45
-
46
- - run: yarn install
47
-
48
- - run: yarn test
49
- env:
50
- CI: true
51
-
52
- publish:
53
- name: Publish
54
- runs-on: ubuntu-latest
55
- needs: unit-test
56
- if: github.ref == 'refs/heads/master'
57
- steps:
58
- - name: Checkout
59
- uses: actions/checkout@v2
60
-
61
- - name: Restore cache
62
- uses: actions/cache@v2
63
- with:
64
- path: '**/node_modules'
65
- key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
66
-
67
- - name: Build and publish release.
68
- run: yarn install
69
-
70
- - name: Publish via Semantic Release
71
- run: yarn semantic-release
72
- env:
73
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74
- NPM_TOKEN: ${{ secrets.NPM_PUBLISHING_TOKEN }}
package/babel.config.js DELETED
@@ -1,13 +0,0 @@
1
- module.exports = {
2
- presets: [
3
- [
4
- '@babel/preset-env',
5
- {
6
- targets: {
7
- node: 'current',
8
- },
9
- },
10
- ],
11
- ],
12
- plugins: ['@babel/plugin-syntax-flow', '@babel/plugin-transform-flow-strip-types'],
13
- };
@@ -1,33 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.DEPENDENCIES = exports.DEFINITIONS = void 0;
7
- var _HTTP = _interopRequireDefault(require("../Service/HTTP.service"));
8
- var _Logger = _interopRequireDefault(require("../Service/Logger.service"));
9
- var _Request = _interopRequireDefault(require("../Service/Request.service"));
10
- var _SQS = _interopRequireDefault(require("../Service/SQS.service"));
11
- var _Timer = _interopRequireDefault(require("../Service/Timer.service"));
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
- const DEFINITIONS = {
14
- HTTP: 'HTTP',
15
- LOGGER: 'LOGGER',
16
- REQUEST: 'REQUEST',
17
- SQS: 'SQS',
18
- TIMER: 'TIMER'
19
- };
20
- exports.DEFINITIONS = DEFINITIONS;
21
- const DEPENDENCIES = {
22
- [DEFINITIONS.HTTP]: _HTTP.default,
23
- [DEFINITIONS.LOGGER]: _Logger.default,
24
- [DEFINITIONS.REQUEST]: _Request.default,
25
- [DEFINITIONS.SQS]: _SQS.default,
26
- [DEFINITIONS.TIMER]: _Timer.default
27
- };
28
- exports.DEPENDENCIES = DEPENDENCIES;
29
- var _default = {
30
- DEFINITIONS,
31
- DEPENDENCIES
32
- };
33
- exports.default = _default;
@@ -1,40 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _DependencyInjection = _interopRequireDefault(require("./DependencyInjection.class"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- /**
10
- * DependencyAwareClass Class
11
- */
12
- class DependencyAwareClass {
13
- /**
14
- * DependencyAwareClass constructor
15
- *
16
- * @param {DependencyInjection} di
17
- */
18
- constructor(di) {
19
- this.di = di;
20
- }
21
-
22
- /**
23
- * Get Dependency Injection Container
24
- *
25
- * @returns {DependencyInjection}
26
- */
27
- getContainer() {
28
- return this.di;
29
- }
30
-
31
- /**
32
- * Shortcut for `this.getContainer().definitions`
33
- *
34
- * @returns {object}
35
- */
36
- get definitions() {
37
- return this.getContainer().definitions;
38
- }
39
- }
40
- exports.default = DependencyAwareClass;
@@ -1,115 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _Dependencies = require("../Config/Dependencies");
8
- /**
9
- * DependencyInjection class
10
- */
11
- class DependencyInjection {
12
- /**
13
- * DependencyInjection constructor
14
- *
15
- * @param configuration
16
- * @param event
17
- * @param context
18
- */
19
- constructor(configuration, event, context) {
20
- this.event = event;
21
- this.context = context;
22
- this.dependencies = {};
23
- this.configuration = configuration;
24
- for (let x = 0; x <= 1; x += 1) {
25
- // Iterate over lapper dependencies and add to container
26
- Object.keys(_Dependencies.DEFINITIONS).forEach(dependencyKey => {
27
- this.dependencies[dependencyKey] = new _Dependencies.DEPENDENCIES[dependencyKey](this);
28
- });
29
-
30
- // Iterate over child dependencies and add to container
31
- if (typeof configuration.DEPENDENCIES !== 'undefined') {
32
- Object.keys(configuration.DEPENDENCIES).forEach(dependencyKey => {
33
- this.dependencies[dependencyKey] = new configuration.DEPENDENCIES[dependencyKey](this);
34
- });
35
- }
36
- }
37
- }
38
-
39
- /**
40
- * Get Dependency
41
- *
42
- * @param definition
43
- * @returns {*}
44
- */
45
- get(definition) {
46
- if (typeof this.dependencies[definition] === 'undefined') {
47
- throw new TypeError(`${definition} does not exist in di container`);
48
- }
49
- return this.dependencies[definition];
50
- }
51
-
52
- /**
53
- * Get Event
54
- *
55
- * @returns {*}
56
- */
57
- getEvent() {
58
- return this.event;
59
- }
60
-
61
- /**
62
- * Get Context
63
- *
64
- * @returns {*}
65
- */
66
- getContext() {
67
- return this.context;
68
- }
69
-
70
- /**
71
- * Get Configuration
72
- *
73
- * @param definition string
74
- * @returns {*}
75
- */
76
- getConfiguration(definition = null) {
77
- if (definition !== null && typeof this.configuration[definition] === 'undefined') {
78
- return null;
79
- }
80
- if (typeof this.configuration[definition] !== 'undefined') {
81
- return this.configuration[definition];
82
- }
83
- return this.configuration;
84
- }
85
-
86
- /**
87
- * Check whether the function
88
- * is being executed in a serverless-offline context
89
- *
90
- * @returns {boolean}
91
- */
92
- get isOffline() {
93
- const context = this.getContext() || {};
94
- if (!Object.prototype.hasOwnProperty.call(context, 'invokedFunctionArn')) {
95
- return true;
96
- }
97
- if (context.invokedFunctionArn.includes('offline')) {
98
- return true;
99
- }
100
- return !!process.env.USE_SERVERLESS_OFFLINE;
101
- }
102
-
103
- /**
104
- * Returns the definitions
105
- * associated to this DependencyInjection
106
- * so that services can refer to them
107
- * without causing circular imports.
108
- *
109
- * @returns {object}
110
- */
111
- get definitions() {
112
- return this.configuration.DEFINITIONS;
113
- }
114
- }
115
- exports.default = DependencyInjection;
@@ -1,138 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _uuid = require("uuid");
8
- var _Model = _interopRequireDefault(require("./Model.model"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- /**
11
- * CloudEventModel class
12
- * Class to implement cloud events - https://github.com/cloudevents/spec/blob/master/spec.md
13
- */
14
- class CloudEventModel extends _Model.default {
15
- /**
16
- * CloudEventModel constructor
17
- */
18
- constructor() {
19
- super();
20
- this.cloudEventsVersion = '0.1';
21
- this.eventType = '';
22
- this.source = '';
23
- this.eventID = (0, _uuid.v4)();
24
- this.eventTime = new Date().toISOString();
25
- this.extensions = {};
26
- this.contentType = 'application/json';
27
- this.data = {};
28
- }
29
-
30
- /**
31
- * Get Cloud Events Version
32
- *
33
- * @returns {number}
34
- */
35
- getCloudEventsVersion() {
36
- return this.cloudEventsVersion;
37
- }
38
-
39
- /**
40
- * Get event type
41
- *
42
- * @returns {string|*}
43
- */
44
- getEventType() {
45
- return this.eventType;
46
- }
47
-
48
- /**
49
- * Set event type
50
- *
51
- * @param value string
52
- */
53
- setEventType(value) {
54
- this.eventType = value;
55
- }
56
-
57
- /**
58
- * Get source
59
- *
60
- * @returns {string|*}
61
- */
62
- getSource() {
63
- return this.source;
64
- }
65
-
66
- /**
67
- * Set source
68
- *
69
- * @param value string
70
- */
71
- setSource(value) {
72
- this.source = value;
73
- }
74
-
75
- /**
76
- * Get event id
77
- *
78
- * @returns {*|string}
79
- */
80
- getEventID() {
81
- return this.eventID;
82
- }
83
-
84
- /**
85
- * Get event time
86
- *
87
- * @returns {*|string}
88
- */
89
- getEventTime() {
90
- return this.eventTime;
91
- }
92
-
93
- /**
94
- * Get extensions
95
- *
96
- * @returns {{}|*}
97
- */
98
- getExtensions() {
99
- return this.extensions;
100
- }
101
-
102
- /**
103
- * Set extensions
104
- *
105
- * @param value object
106
- */
107
- setExtensions(value) {
108
- this.extensions = value;
109
- }
110
-
111
- /**
112
- * Get content type
113
- *
114
- * @returns {string}
115
- */
116
- getContentType() {
117
- return this.contentType;
118
- }
119
-
120
- /**
121
- * Get data
122
- *
123
- * @returns {{}|*}
124
- */
125
- getData() {
126
- return this.data;
127
- }
128
-
129
- /**
130
- * Set data
131
- *
132
- * @param value object
133
- */
134
- setData(value) {
135
- this.data = value;
136
- }
137
- }
138
- exports.default = CloudEventModel;
@@ -1,39 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _validate = _interopRequireDefault(require("validate.js/validate"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- /* eslint-disable class-methods-use-this */
10
-
11
- /**
12
- * Model base class
13
- */
14
- class Model {
15
- /**
16
- * Instantiate a function with a value if defined
17
- *
18
- * @param classFunctionName string
19
- * @param value mixed
20
- */
21
- instantiateFunctionWithDefinedValue(classFunctionName, value) {
22
- if (typeof value !== 'undefined') {
23
- this[classFunctionName](value);
24
- }
25
- }
26
-
27
- /**
28
- * Validate values against constraints
29
- *
30
- * @param values object
31
- * @param constraints object
32
- * @returns {boolean}
33
- */
34
- validateAgainstConstraints(values, constraints) {
35
- const validation = (0, _validate.default)(values, constraints);
36
- return typeof validation === 'undefined';
37
- }
38
- }
39
- exports.default = Model;
@@ -1,135 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.RESPONSE_HEADERS = exports.DEFAULT_MESSAGE = void 0;
7
- var _Model = _interopRequireDefault(require("./Model.model"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- /**
10
- *
11
- * @type {object}
12
- */
13
- const RESPONSE_HEADERS = {
14
- 'Content-Type': 'application/json',
15
- 'Access-Control-Allow-Origin': '*',
16
- // Required for CORS support to work
17
- 'Access-Control-Allow-Credentials': true // Required for cookies, authorization headers with HTTPS
18
- };
19
-
20
- /**
21
- * Default message provided as part of response
22
- *
23
- * @type {string}
24
- */
25
- exports.RESPONSE_HEADERS = RESPONSE_HEADERS;
26
- const DEFAULT_MESSAGE = 'success';
27
-
28
- /**
29
- * class ResponseModel
30
- */
31
- exports.DEFAULT_MESSAGE = DEFAULT_MESSAGE;
32
- class ResponseModel extends _Model.default {
33
- /**
34
- * ResponseModel Constructor
35
- *
36
- * @param data
37
- * @param code
38
- * @param message
39
- */
40
- constructor(data = null, code = null, message = null) {
41
- super();
42
- this.body = {
43
- data: data !== null ? data : {},
44
- message: message !== null ? message : DEFAULT_MESSAGE
45
- };
46
- this.code = code !== null ? code : {};
47
- }
48
-
49
- /**
50
- * Add or update a body variable
51
- *
52
- * @param variable
53
- * @param value
54
- */
55
- setBodyVariable(variable, value) {
56
- this.body[variable] = value;
57
- }
58
-
59
- /**
60
- * Set Data
61
- *
62
- * @param data
63
- */
64
- setData(data) {
65
- this.body.data = data;
66
- }
67
-
68
- /**
69
- * Set Status Code
70
- *
71
- * @param code
72
- */
73
- setCode(code) {
74
- this.code = code;
75
- }
76
-
77
- /**
78
- * Get Status Code
79
- *
80
- * @returns {*}
81
- */
82
- getCode() {
83
- return this.code;
84
- }
85
-
86
- /**
87
- * Set message
88
- *
89
- * @param message
90
- */
91
- setMessage(message) {
92
- this.body.message = message;
93
- }
94
-
95
- /**
96
- * Get Message
97
- *
98
- * @returns {string|*}
99
- */
100
- getMessage() {
101
- return this.body.message;
102
- }
103
-
104
- /**
105
- * Geneate a response
106
- *
107
- * @returns {object}
108
- */
109
- generate() {
110
- return {
111
- statusCode: this.code,
112
- headers: RESPONSE_HEADERS,
113
- body: JSON.stringify(this.body)
114
- };
115
- }
116
-
117
- /**
118
- * Shorthand static method
119
- * that generates the response immediately
120
- * if no additional processing is required.
121
- *
122
- * Saves only 1 line of code
123
- * but keeps code terse in a lot of places.
124
- *
125
- * @param {*} data
126
- * @param {*} code
127
- * @param {*} message
128
- * @returns {object}
129
- */
130
- static generate(data = null, code = null, message = null) {
131
- const response = new this(data, code, message);
132
- return response.generate();
133
- }
134
- }
135
- exports.default = ResponseModel;