@comicrelief/lambda-wrapper 1.10.1 → 2.0.0-beta.2

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 +172 -47
  2. package/dist/core/DependencyAwareClass.d.ts +15 -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 +83 -0
  9. package/dist/core/DependencyInjection.js.map +1 -0
  10. package/dist/core/LambdaWrapper.d.ts +61 -0
  11. package/dist/core/LambdaWrapper.d.ts.map +1 -0
  12. package/dist/core/LambdaWrapper.js +126 -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 +59 -162
  21. package/dist/index.js.map +1 -0
  22. package/dist/models/ResponseModel.d.ts +91 -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/models/StatusModel.d.ts +40 -0
  31. package/dist/models/StatusModel.d.ts.map +1 -0
  32. package/dist/models/StatusModel.js +50 -0
  33. package/dist/models/StatusModel.js.map +1 -0
  34. package/dist/services/BaseConfigService.d.ts +101 -0
  35. package/dist/services/BaseConfigService.d.ts.map +1 -0
  36. package/dist/services/BaseConfigService.js +180 -0
  37. package/dist/services/BaseConfigService.js.map +1 -0
  38. package/dist/services/HTTPService.d.ts +29 -0
  39. package/dist/services/HTTPService.d.ts.map +1 -0
  40. package/dist/services/HTTPService.js +55 -0
  41. package/dist/services/HTTPService.js.map +1 -0
  42. package/dist/services/LoggerService.d.ts +102 -0
  43. package/dist/services/LoggerService.d.ts.map +1 -0
  44. package/dist/services/LoggerService.js +258 -0
  45. package/dist/services/LoggerService.js.map +1 -0
  46. package/dist/services/RequestService.d.ts +119 -0
  47. package/dist/services/RequestService.d.ts.map +1 -0
  48. package/dist/services/RequestService.js +293 -0
  49. package/dist/services/RequestService.js.map +1 -0
  50. package/dist/services/SQSService.d.ts +181 -0
  51. package/dist/services/SQSService.d.ts.map +1 -0
  52. package/dist/services/SQSService.js +366 -0
  53. package/dist/services/SQSService.js.map +1 -0
  54. package/dist/services/TimerService.d.ts +22 -0
  55. package/dist/services/TimerService.d.ts.map +1 -0
  56. package/dist/services/TimerService.js +40 -0
  57. package/dist/services/TimerService.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 +29 -19
  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 -40
  72. package/dist/DependencyInjection/DependencyAware.class.js +0 -47
  73. package/dist/DependencyInjection/DependencyInjection.class.js +0 -131
  74. package/dist/Model/CloudEvent.model.js +0 -156
  75. package/dist/Model/Model.model.js +0 -45
  76. package/dist/Model/Response.model.js +0 -150
  77. package/dist/Model/SQS/MarketingPreference.constraints.json +0 -28
  78. package/dist/Model/SQS/MarketingPreference.model.js +0 -641
  79. package/dist/Model/SQS/Message.model.js +0 -104
  80. package/dist/Model/Status.model.js +0 -82
  81. package/dist/Service/BaseConfig.service.js +0 -225
  82. package/dist/Service/HTTP.service.js +0 -70
  83. package/dist/Service/Logger.service.js +0 -293
  84. package/dist/Service/Request.service.js +0 -341
  85. package/dist/Service/SQS.service.js +0 -420
  86. package/dist/Service/Timer.service.js +0 -55
  87. package/dist/Wrapper/LambdaTermination.js +0 -39
  88. package/dist/Wrapper/LambdaWrapper.js +0 -164
  89. package/dist/Wrapper/PromisifiedDelay.js +0 -59
@@ -0,0 +1,40 @@
1
+ export declare const STATUS_TYPES: {
2
+ OK: string;
3
+ ACCEPTABLE_FAILURE: string;
4
+ APPLICATION_FAILURE: string;
5
+ };
6
+ /**
7
+ * Model for our status check endpoints.
8
+ */
9
+ export default class StatusModel {
10
+ /**
11
+ * Service name.
12
+ */
13
+ service: string;
14
+ /**
15
+ * One of the `STATUS_TYPES` values.
16
+ */
17
+ status: string;
18
+ constructor(service: string, status: string);
19
+ /**
20
+ * Get the service name.
21
+ */
22
+ getService(): string;
23
+ /**
24
+ * Set the service name.
25
+ *
26
+ * @param service
27
+ */
28
+ setService(service: string): void;
29
+ /**
30
+ * Set the status.
31
+ *
32
+ * @param status
33
+ */
34
+ setStatus(status: string): void;
35
+ /**
36
+ * Get the status.
37
+ */
38
+ getStatus(): string;
39
+ }
40
+ //# sourceMappingURL=StatusModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StatusModel.d.ts","sourceRoot":"","sources":["../../src/models/StatusModel.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;;;;CAIxB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAC9B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;gBAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAK3C;;OAEG;IACH,UAAU,IAAI,MAAM;IAIpB;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,MAAM;IAI1B;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM;IAQxB;;OAEG;IACH,SAAS,IAAI,MAAM;CAGpB"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.STATUS_TYPES = void 0;
4
+ exports.STATUS_TYPES = {
5
+ OK: 'OK',
6
+ ACCEPTABLE_FAILURE: 'ACCEPTABLE_FAILURE',
7
+ APPLICATION_FAILURE: 'APPLICATION_FAILURE',
8
+ };
9
+ /**
10
+ * Model for our status check endpoints.
11
+ */
12
+ class StatusModel {
13
+ constructor(service, status) {
14
+ this.service = service;
15
+ this.status = status;
16
+ }
17
+ /**
18
+ * Get the service name.
19
+ */
20
+ getService() {
21
+ return this.service;
22
+ }
23
+ /**
24
+ * Set the service name.
25
+ *
26
+ * @param service
27
+ */
28
+ setService(service) {
29
+ this.service = service;
30
+ }
31
+ /**
32
+ * Set the status.
33
+ *
34
+ * @param status
35
+ */
36
+ setStatus(status) {
37
+ if (!(status in exports.STATUS_TYPES)) {
38
+ throw new TypeError(`${StatusModel.name} - ${status} is not a valid status type`);
39
+ }
40
+ this.status = status;
41
+ }
42
+ /**
43
+ * Get the status.
44
+ */
45
+ getStatus() {
46
+ return this.status;
47
+ }
48
+ }
49
+ exports.default = StatusModel;
50
+ //# sourceMappingURL=StatusModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StatusModel.js","sourceRoot":"","sources":["../../src/models/StatusModel.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC1B,EAAE,EAAE,IAAI;IACR,kBAAkB,EAAE,oBAAoB;IACxC,mBAAmB,EAAE,qBAAqB;CAC3C,CAAC;AAEF;;GAEG;AACH,MAAqB,WAAW;IAW9B,YAAY,OAAe,EAAE,MAAc;QACzC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,OAAe;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACH,SAAS,CAAC,MAAc;QACtB,IAAI,CAAC,CAAC,MAAM,IAAI,oBAAY,CAAC,EAAE;YAC7B,MAAM,IAAI,SAAS,CAAC,GAAG,WAAW,CAAC,IAAI,MAAM,MAAM,6BAA6B,CAAC,CAAC;SACnF;QAED,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAnDD,8BAmDC"}
@@ -0,0 +1,101 @@
1
+ import { S3 } from 'aws-sdk';
2
+ import DependencyAwareClass from '../core/DependencyAwareClass';
3
+ /**
4
+ * `error.code` for S3 404 errors.
5
+ */
6
+ export declare const S3_NO_SUCH_KEY_ERROR_CODE = "NoSuchKey";
7
+ /**
8
+ * Represents the service states.
9
+ */
10
+ export declare const ServiceStates: {
11
+ OK: string;
12
+ TEMPORARILY_PAUSED: string;
13
+ INDEFINITELY_PAUSED: string;
14
+ };
15
+ /**
16
+ * Maps service states to HTTP codes.
17
+ */
18
+ export declare const ServiceStatesHttpCodes: {
19
+ [x: string]: number;
20
+ };
21
+ /**
22
+ * This class is to be extended by the implementing services so that
23
+ * `defaultConfig` and possibly `s3Config` can be overriden / extended.
24
+ *
25
+ * Config is typed as `unknown` since you shouldn't trust what's in the bucket.
26
+ * Override the `get` and `put` methods to pass the results through some
27
+ * validation to ensure the config is valid and can safely be typed.
28
+ */
29
+ export default class BaseConfigService extends DependencyAwareClass {
30
+ /**
31
+ * Returns the basic config.
32
+ *
33
+ * This getter is used to set the default config should the service not find
34
+ * any on the configured S3 Bucket.
35
+ *
36
+ * See `getOrCreate` and `patch` methods.
37
+ */
38
+ static get defaultConfig(): {
39
+ state: string;
40
+ };
41
+ /**
42
+ * Returns the S3 configuration used to retrieve or update the service
43
+ * configuration.
44
+ */
45
+ static get s3config(): {
46
+ Bucket: string;
47
+ Key: string;
48
+ };
49
+ /**
50
+ * Returns an S3 client.
51
+ */
52
+ static get client(): S3;
53
+ /**
54
+ * Returns an S3 client
55
+ */
56
+ get client(): S3;
57
+ /**
58
+ * Deletes the configuration stored on S3. Helpful in feature tests.
59
+ */
60
+ delete(): Promise<import("aws-sdk/lib/request").PromiseResult<S3.DeleteObjectOutput, import("aws-sdk").AWSError>>;
61
+ /**
62
+ * Puts the given configuration on S3.
63
+ *
64
+ * @param config
65
+ */
66
+ put<T>(config: T): Promise<T>;
67
+ /**
68
+ * Gets the service configuration.
69
+ */
70
+ get(): Promise<unknown>;
71
+ /**
72
+ * Gets or creates the service configuration.
73
+ *
74
+ * If the configuration is not found on S3 the default configuration is
75
+ * uploaded and returned instead.
76
+ */
77
+ getOrCreate(): Promise<unknown>;
78
+ /**
79
+ * Patches the existing configuration
80
+ * or the default configuration
81
+ * with the provided partial configuration
82
+ *
83
+ * @param partialConfig
84
+ */
85
+ patch(partialConfig: any): Promise<unknown>;
86
+ /**
87
+ * Performs a health check given the current config.
88
+ *
89
+ * If `currentConfig` is not supplied it uses `getOrCreate` to fetch it.
90
+ *
91
+ * @param currentConfig
92
+ */
93
+ healthCheck(currentConfig?: any): Promise<number>;
94
+ /**
95
+ * Ensures that the application is healthy or throws a `LambdaTermination`.
96
+ *
97
+ * @param currentConfig
98
+ */
99
+ ensureHealthy(currentConfig?: any): Promise<number>;
100
+ }
101
+ //# sourceMappingURL=BaseConfigService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseConfigService.d.ts","sourceRoot":"","sources":["../../src/services/BaseConfigService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAE7B,OAAO,oBAAoB,MAAM,8BAA8B,CAAC;AAGhE;;GAEG;AACH,eAAO,MAAM,yBAAyB,cAAc,CAAC;AAErD;;GAEG;AACH,eAAO,MAAM,aAAa;;;;CAIzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB;;CAIlC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAkB,SAAQ,oBAAoB;IACjE;;;;;;;OAOG;IACH,MAAM,KAAK,aAAa;;MAIvB;IAED;;;OAGG;IACH,MAAM,KAAK,QAAQ,IAAI;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;KAAE,CAKtD;IAED;;OAEG;IACH,MAAM,KAAK,MAAM,OAIhB;IAED;;OAEG;IACH,IAAI,MAAM,OAET;IAED;;OAEG;IACG,MAAM;IAMZ;;;;OAIG;IACG,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IASnC;;OAEG;IACG,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC;IAkB7B;;;;;OAKG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAarC;;;;;;OAMG;IACG,KAAK,CAAC,aAAa,EAAE,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC;IAqBjD;;;;;;OAMG;IACG,WAAW,CAAC,aAAa,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;IAMvD;;;;OAIG;IACG,aAAa,CAAC,aAAa,GAAE,GAAU;CAW9C"}
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ServiceStatesHttpCodes = exports.ServiceStates = exports.S3_NO_SUCH_KEY_ERROR_CODE = void 0;
7
+ const aws_sdk_1 = require("aws-sdk");
8
+ const DependencyAwareClass_1 = __importDefault(require("../core/DependencyAwareClass"));
9
+ const LambdaTermination_1 = __importDefault(require("../utils/LambdaTermination"));
10
+ /**
11
+ * `error.code` for S3 404 errors.
12
+ */
13
+ exports.S3_NO_SUCH_KEY_ERROR_CODE = 'NoSuchKey';
14
+ /**
15
+ * Represents the service states.
16
+ */
17
+ exports.ServiceStates = {
18
+ OK: 'OK',
19
+ TEMPORARILY_PAUSED: 'TEMPORARILY_PAUSED',
20
+ INDEFINITELY_PAUSED: 'INDEFINITELY_PAUSED',
21
+ };
22
+ /**
23
+ * Maps service states to HTTP codes.
24
+ */
25
+ exports.ServiceStatesHttpCodes = {
26
+ [exports.ServiceStates.OK]: 200,
27
+ [exports.ServiceStates.TEMPORARILY_PAUSED]: 409,
28
+ [exports.ServiceStates.INDEFINITELY_PAUSED]: 409,
29
+ };
30
+ /**
31
+ * This class is to be extended by the implementing services so that
32
+ * `defaultConfig` and possibly `s3Config` can be overriden / extended.
33
+ *
34
+ * Config is typed as `unknown` since you shouldn't trust what's in the bucket.
35
+ * Override the `get` and `put` methods to pass the results through some
36
+ * validation to ensure the config is valid and can safely be typed.
37
+ */
38
+ class BaseConfigService extends DependencyAwareClass_1.default {
39
+ /**
40
+ * Returns the basic config.
41
+ *
42
+ * This getter is used to set the default config should the service not find
43
+ * any on the configured S3 Bucket.
44
+ *
45
+ * See `getOrCreate` and `patch` methods.
46
+ */
47
+ static get defaultConfig() {
48
+ return {
49
+ state: exports.ServiceStates.OK,
50
+ };
51
+ }
52
+ /**
53
+ * Returns the S3 configuration used to retrieve or update the service
54
+ * configuration.
55
+ */
56
+ static get s3config() {
57
+ return {
58
+ Bucket: process.env.SERVICE_CONFIG_S3_BUCKET || '',
59
+ Key: process.env.SERVICE_CONFIG_S3_KEY || '',
60
+ };
61
+ }
62
+ /**
63
+ * Returns an S3 client.
64
+ */
65
+ static get client() {
66
+ return new aws_sdk_1.S3({
67
+ region: process.env.REGION,
68
+ });
69
+ }
70
+ /**
71
+ * Returns an S3 client
72
+ */
73
+ get client() {
74
+ return this.constructor.client;
75
+ }
76
+ /**
77
+ * Deletes the configuration stored on S3. Helpful in feature tests.
78
+ */
79
+ async delete() {
80
+ return this.client.deleteObject(this.constructor.s3config).promise();
81
+ }
82
+ /**
83
+ * Puts the given configuration on S3.
84
+ *
85
+ * @param config
86
+ */
87
+ async put(config) {
88
+ await this.client.putObject({
89
+ ...this.constructor.s3config,
90
+ Body: JSON.stringify(config),
91
+ }).promise();
92
+ return config;
93
+ }
94
+ /**
95
+ * Gets the service configuration.
96
+ */
97
+ async get() {
98
+ const response = await this.client.getObject(this.constructor.s3config).promise();
99
+ const body = String(response.Body);
100
+ if (!body) {
101
+ // Empty strings are not valid configurations
102
+ throw new Error('Configuration file is empty');
103
+ }
104
+ try {
105
+ return JSON.parse(body);
106
+ }
107
+ catch {
108
+ throw new Error('Invalid configuration file');
109
+ }
110
+ }
111
+ /**
112
+ * Gets or creates the service configuration.
113
+ *
114
+ * If the configuration is not found on S3 the default configuration is
115
+ * uploaded and returned instead.
116
+ */
117
+ async getOrCreate() {
118
+ try {
119
+ return await this.get();
120
+ }
121
+ catch (error) {
122
+ if (error.code !== exports.S3_NO_SUCH_KEY_ERROR_CODE) {
123
+ // Throw directly any other error
124
+ throw error;
125
+ }
126
+ return this.put(this.constructor.defaultConfig);
127
+ }
128
+ }
129
+ /**
130
+ * Patches the existing configuration
131
+ * or the default configuration
132
+ * with the provided partial configuration
133
+ *
134
+ * @param partialConfig
135
+ */
136
+ async patch(partialConfig) {
137
+ let base = this.constructor.defaultConfig;
138
+ try {
139
+ base = await this.get();
140
+ }
141
+ catch (error) {
142
+ if (error.code !== exports.S3_NO_SUCH_KEY_ERROR_CODE) {
143
+ // Throw directly any other error
144
+ throw error;
145
+ }
146
+ // Config doesn't exist
147
+ // keep using `this.constructor.defaultConfig`
148
+ }
149
+ return this.put({
150
+ ...base,
151
+ ...partialConfig,
152
+ });
153
+ }
154
+ /**
155
+ * Performs a health check given the current config.
156
+ *
157
+ * If `currentConfig` is not supplied it uses `getOrCreate` to fetch it.
158
+ *
159
+ * @param currentConfig
160
+ */
161
+ async healthCheck(currentConfig) {
162
+ const config = currentConfig || await this.getOrCreate();
163
+ return exports.ServiceStatesHttpCodes[config.state] || 500;
164
+ }
165
+ /**
166
+ * Ensures that the application is healthy or throws a `LambdaTermination`.
167
+ *
168
+ * @param currentConfig
169
+ */
170
+ async ensureHealthy(currentConfig = null) {
171
+ const statusCode = await this.healthCheck(currentConfig);
172
+ if (statusCode < 400) {
173
+ return statusCode;
174
+ }
175
+ const message = 'Application is not healthy.';
176
+ throw new LambdaTermination_1.default(message, statusCode, message, message);
177
+ }
178
+ }
179
+ exports.default = BaseConfigService;
180
+ //# sourceMappingURL=BaseConfigService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseConfigService.js","sourceRoot":"","sources":["../../src/services/BaseConfigService.ts"],"names":[],"mappings":";;;;;;AAAA,qCAA6B;AAE7B,wFAAgE;AAChE,mFAA2D;AAE3D;;GAEG;AACU,QAAA,yBAAyB,GAAG,WAAW,CAAC;AAErD;;GAEG;AACU,QAAA,aAAa,GAAG;IAC3B,EAAE,EAAE,IAAI;IACR,kBAAkB,EAAE,oBAAoB;IACxC,mBAAmB,EAAE,qBAAqB;CAC3C,CAAC;AAEF;;GAEG;AACU,QAAA,sBAAsB,GAAG;IACpC,CAAC,qBAAa,CAAC,EAAE,CAAC,EAAE,GAAG;IACvB,CAAC,qBAAa,CAAC,kBAAkB,CAAC,EAAE,GAAG;IACvC,CAAC,qBAAa,CAAC,mBAAmB,CAAC,EAAE,GAAG;CACzC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAqB,iBAAkB,SAAQ,8BAAoB;IACjE;;;;;;;OAOG;IACH,MAAM,KAAK,aAAa;QACtB,OAAO;YACL,KAAK,EAA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CAAC,aAAa,CAAC,CAAC;QAEzD,IAAI,UAAU,GAAG,GAAG,EAAE;YACpB,OAAO,UAAU,CAAC;SACnB;QAED,MAAM,OAAO,GAAG,6BAA6B,CAAC;QAE9C,MAAM,IAAI,2BAAiB,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACrE,CAAC;CACF;AAlKD,oCAkKC"}
@@ -0,0 +1,29 @@
1
+ import { AxiosRequestConfig, AxiosResponse } from 'axios';
2
+ import DependencyAwareClass from '../core/DependencyAwareClass';
3
+ import DependencyInjection from '../core/DependencyInjection';
4
+ export declare const COMICRELIEF_TEST_METADATA_HEADER = "x-comicrelief-test-metadata";
5
+ export declare const DEFAULT_HTTP_TIMEOUT: number;
6
+ /**
7
+ * Wrapper for `axios.request` that:
8
+ *
9
+ * - sets a default timeout of 10 seconds
10
+ * - forwards a `x-comicrelief-test-metadata` header if one was provided in
11
+ * the request from upstream
12
+ */
13
+ export default class HTTPService extends DependencyAwareClass {
14
+ config: AxiosRequestConfig;
15
+ constructor(di: DependencyInjection);
16
+ /**
17
+ * Set the default timeout.
18
+ *
19
+ * @param ms
20
+ */
21
+ setDefaultTimeout(ms: number): void;
22
+ /**
23
+ * Perform an HTTP request.
24
+ *
25
+ * @param config
26
+ */
27
+ request(config: AxiosRequestConfig): Promise<AxiosResponse>;
28
+ }
29
+ //# sourceMappingURL=HTTPService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HTTPService.d.ts","sourceRoot":"","sources":["../../src/services/HTTPService.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEjE,OAAO,oBAAoB,MAAM,8BAA8B,CAAC;AAChE,OAAO,mBAAmB,MAAM,6BAA6B,CAAC;AAG9D,eAAO,MAAM,gCAAgC,gCAAgC,CAAC;AAC9E,eAAO,MAAM,oBAAoB,QAAY,CAAC;AAE9C;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,oBAAoB;IAC3D,MAAM,EAAE,kBAAkB,CAAC;gBAEf,EAAE,EAAE,mBAAmB;IAQnC;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,EAAE,MAAM;IAI5B;;;;OAIG;IACG,OAAO,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,aAAa,CAAC;CAiBlE"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DEFAULT_HTTP_TIMEOUT = exports.COMICRELIEF_TEST_METADATA_HEADER = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const DependencyAwareClass_1 = __importDefault(require("../core/DependencyAwareClass"));
9
+ const RequestService_1 = __importDefault(require("./RequestService"));
10
+ exports.COMICRELIEF_TEST_METADATA_HEADER = 'x-comicrelief-test-metadata';
11
+ exports.DEFAULT_HTTP_TIMEOUT = 10 * 1000;
12
+ /**
13
+ * Wrapper for `axios.request` that:
14
+ *
15
+ * - sets a default timeout of 10 seconds
16
+ * - forwards a `x-comicrelief-test-metadata` header if one was provided in
17
+ * the request from upstream
18
+ */
19
+ class HTTPService extends DependencyAwareClass_1.default {
20
+ constructor(di) {
21
+ super(di);
22
+ this.config = {
23
+ timeout: exports.DEFAULT_HTTP_TIMEOUT,
24
+ };
25
+ }
26
+ /**
27
+ * Set the default timeout.
28
+ *
29
+ * @param ms
30
+ */
31
+ setDefaultTimeout(ms) {
32
+ this.config.timeout = ms;
33
+ }
34
+ /**
35
+ * Perform an HTTP request.
36
+ *
37
+ * @param config
38
+ */
39
+ async request(config) {
40
+ const mergedConfig = {
41
+ timeout: this.config.timeout,
42
+ headers: {},
43
+ ...config,
44
+ };
45
+ const request = this.di.get(RequestService_1.default);
46
+ const testMetadata = request.getHeader(exports.COMICRELIEF_TEST_METADATA_HEADER);
47
+ if (testMetadata) {
48
+ mergedConfig.headers = mergedConfig.headers || {};
49
+ mergedConfig.headers[exports.COMICRELIEF_TEST_METADATA_HEADER] = testMetadata;
50
+ }
51
+ return axios_1.default.request(mergedConfig);
52
+ }
53
+ }
54
+ exports.default = HTTPService;
55
+ //# sourceMappingURL=HTTPService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HTTPService.js","sourceRoot":"","sources":["../../src/services/HTTPService.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAiE;AAEjE,wFAAgE;AAEhE,sEAA8C;AAEjC,QAAA,gCAAgC,GAAG,6BAA6B,CAAC;AACjE,QAAA,oBAAoB,GAAG,EAAE,GAAG,IAAI,CAAC;AAE9C;;;;;;GAMG;AACH,MAAqB,WAAY,SAAQ,8BAAoB;IAG3D,YAAY,EAAuB;QACjC,KAAK,CAAC,EAAE,CAAC,CAAC;QAEV,IAAI,CAAC,MAAM,GAAG;YACZ,OAAO,EAAE,4BAAoB;SAC9B,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,iBAAiB,CAAC,EAAU;QAC1B,IAAI,CAAC,MAAM,CAAC,OAAO,GAAG,EAAE,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,OAAO,CAAC,MAA0B;QACtC,MAAM,YAAY,GAAG;YACnB,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO;YAC5B,OAAO,EAAE,EAAE;YACX,GAAG,MAAM;SACV,CAAC;QAEF,MAAM,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,wBAAc,CAAC,CAAC;QAC5C,MAAM,YAAY,GAAG,OAAO,CAAC,SAAS,CAAC,wCAAgC,CAAC,CAAC;QAEzE,IAAI,YAAY,EAAE;YAChB,YAAY,CAAC,OAAO,GAAG,YAAY,CAAC,OAAO,IAAI,EAAE,CAAC;YAClD,YAAY,CAAC,OAAO,CAAC,wCAAgC,CAAC,GAAG,YAAY,CAAC;SACvE;QAED,OAAO,eAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IACrC,CAAC;CACF;AA1CD,8BA0CC"}
@@ -0,0 +1,102 @@
1
+ import * as Sentry from '@sentry/node';
2
+ import { AxiosError } from 'axios';
3
+ import Winston from 'winston';
4
+ import DependencyAwareClass from '../core/DependencyAwareClass';
5
+ import DependencyInjection from '../core/DependencyInjection';
6
+ /**
7
+ * Provides logging and integrations with our monitoring tools.
8
+ *
9
+ * For logging we use [Winston](https://github.com/winstonjs/winston).
10
+ * Errors will also be sent to [Sentry](https://sentry.io/) and
11
+ * [Epsagon](https://epsagon.com/) if those are available.
12
+ */
13
+ export default class LoggerService extends DependencyAwareClass {
14
+ private sentry;
15
+ private winston;
16
+ constructor(di: DependencyInjection);
17
+ /**
18
+ * Returns a Winston logger configured for our lambdas.
19
+ *
20
+ * Note: If the lambda is executed in a `serverless-offline` context, the
21
+ * log output to console will be pretty-printed.
22
+ */
23
+ getLogger(): Winston.Logger;
24
+ /**
25
+ * Returns the logger.
26
+ *
27
+ * Uses a cached Winston logger if it has been already created, otherwise it
28
+ * creates one.
29
+ */
30
+ get logger(): Winston.Logger;
31
+ /**
32
+ * Get Sentry client.
33
+ */
34
+ getSentry(): typeof Sentry | null;
35
+ /**
36
+ * While logging an error, we should recognise axios errors and trim down the
37
+ * information to only what is useful for debugging.
38
+ *
39
+ * Keep the following keys:
40
+ * - message.config
41
+ * - message.message
42
+ * - message.response?.status
43
+ * - message.response?.data
44
+ *
45
+ * @param {object} error
46
+ */
47
+ static processAxiosError(error: AxiosError): any;
48
+ /**
49
+ * Transform the original message before it is passed to the logger.
50
+ *
51
+ * @param message
52
+ */
53
+ static processMessage(message: any): any;
54
+ /**
55
+ * Log Error Message
56
+ *
57
+ * @param error object
58
+ * @param message string
59
+ */
60
+ error(error: any, message?: string): void;
61
+ /**
62
+ * Log an informational message.
63
+ *
64
+ * @param message
65
+ */
66
+ info(message: any): void;
67
+ /**
68
+ * Log an error, using `LoggerService.error` or `LoggerService.info` based
69
+ * on `process.env.LOGGER_SOFT_WARNING`.
70
+ *
71
+ * Please note that `LoggerService.error` and `LoggerService.info` have
72
+ * different signatures. The function uses the shared argument instead of
73
+ * introducing ambiguity.
74
+ *
75
+ * @param error
76
+ */
77
+ warning(error: any): void;
78
+ /**
79
+ * Add a label to the function's Epsagon trace.
80
+ *
81
+ * @param descriptor
82
+ * @param silent If `false`, the label will also be logged. (default: false)
83
+ */
84
+ label(descriptor: string, silent?: boolean): void;
85
+ /**
86
+ * Add a metric to the function's Epsagon trace.
87
+ *
88
+ * @param descriptor
89
+ * @param stat
90
+ * @param silent If `false`, the metric will also be logged. (default: false)
91
+ */
92
+ metric(descriptor: string, stat: number | string, silent?: boolean): void;
93
+ /**
94
+ * Log an object so that it can be inspected.
95
+ *
96
+ * @param action What are we doing with the object, e.g. 'Processing'
97
+ * @param object The object to be stored in logs
98
+ * @param level 'error', 'warning' or 'info'
99
+ */
100
+ object(action: string, object: any, level?: 'error' | 'warning' | 'info'): void;
101
+ }
102
+ //# sourceMappingURL=LoggerService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LoggerService.d.ts","sourceRoot":"","sources":["../../src/services/LoggerService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,oBAAoB,MAAM,8BAA8B,CAAC;AAChE,OAAO,mBAAmB,MAAM,6BAA6B,CAAC;AAa9D;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,oBAAoB;IAC7D,OAAO,CAAC,MAAM,CAAuB;IAErC,OAAO,CAAC,OAAO,CAAwB;gBAE3B,EAAE,EAAE,mBAAmB;IA6BnC;;;;;OAKG;IACH,SAAS;IA6BT;;;;;OAKG;IACH,IAAI,MAAM,mBAMT;IAED;;OAEG;IACH,SAAS;IAIT;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,iBAAiB,CAAC,KAAK,EAAE,UAAU;IAkB1C;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,GAAG;IAUlC;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,SAAK;IAoB9B;;;;OAIG;IACH,IAAI,CAAC,OAAO,EAAE,GAAG;IAIjB;;;;;;;;;OASG;IACH,OAAO,CAAC,KAAK,EAAE,GAAG;IAUlB;;;;;OAKG;IACH,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,UAAQ;IAexC;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,UAAQ;IAehE;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,GAAE,OAAO,GAAG,SAAS,GAAG,MAAe;CASjF"}