@felloh-org/lambda-wrapper 1.0.0 → 1.0.4

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/.eslintignore +5 -0
  2. package/.eslintrc.yml +1 -0
  3. package/.github/workflows/release.yml +2 -0
  4. package/babel.config.js +20 -1
  5. package/cli/seed/create-schema.js +22 -0
  6. package/dist/config/dependencies.js +36 -0
  7. package/dist/dependency-injection/dependency-aware.js +43 -0
  8. package/dist/dependency-injection/dependency-injection.js +135 -0
  9. package/dist/entity/agent-data/booking/index.js +127 -0
  10. package/dist/entity/agent-data/booking-component/index.js +39 -0
  11. package/dist/entity/agent-data/booking-component-type/index.js +39 -0
  12. package/dist/entity/agent-data/index.js +24 -0
  13. package/dist/entity/agent-data/supplier/index.js +39 -0
  14. package/dist/entity/agent-data/supplier-type/index.js +39 -0
  15. package/dist/entity/bank/account/index.js +87 -0
  16. package/dist/entity/bank/beneficiary-organisation/index.js +69 -0
  17. package/dist/entity/bank/beneficiary-provider/index.js +79 -0
  18. package/dist/entity/bank/disbursal/index.js +39 -0
  19. package/dist/entity/bank/index.js +20 -0
  20. package/dist/entity/index.js +22 -0
  21. package/dist/entity/nuapay/account/index.js +96 -0
  22. package/dist/entity/nuapay/balance/index.js +64 -0
  23. package/dist/entity/nuapay/beneficiary/index.js +60 -0
  24. package/dist/entity/nuapay/index.js +20 -0
  25. package/dist/entity/nuapay/transaction/index.js +120 -0
  26. package/dist/entity/payment/index.js +28 -0
  27. package/dist/entity/payment/payment-link/index.js +161 -0
  28. package/dist/entity/payment/transaction/index.js +150 -0
  29. package/dist/entity/payment/transaction-event/index.js +71 -0
  30. package/dist/entity/payment/transaction-event-type/index.js +39 -0
  31. package/dist/entity/payment/transaction-metadata/index.js +140 -0
  32. package/dist/entity/payment/transaction-method/index.js +39 -0
  33. package/dist/entity/payment/transaction-status/index.js +39 -0
  34. package/dist/entity/payment/transaction-type/index.js +39 -0
  35. package/dist/entity/user/feature/index.js +63 -0
  36. package/dist/entity/user/index.js +26 -0
  37. package/dist/entity/user/organisation/index.js +111 -0
  38. package/dist/entity/user/organisation-feature/index.js +69 -0
  39. package/dist/entity/user/role/index.js +107 -0
  40. package/dist/entity/user/user/index.js +154 -0
  41. package/dist/entity/user/user-activity/index.js +82 -0
  42. package/dist/entity/user/user-role/index.js +83 -0
  43. package/dist/index.js +359 -0
  44. package/dist/migration/agent-data/1642121041760-inital.js +15 -0
  45. package/dist/migration/agent-data/1642121610577-organisation-suppliers.js +13 -0
  46. package/dist/migration/agent-data/1642121681610-organisation-suppliers-type.js +13 -0
  47. package/dist/migration/agent-data/1642122014731-booking-component-type.js +13 -0
  48. package/dist/migration/agent-data/1642505882721-booking-fields.js +29 -0
  49. package/dist/migration/agent-data/1642508385880-booking-id.js +19 -0
  50. package/dist/migration/bank/1642313073694-bank-initial.js +25 -0
  51. package/dist/migration/nuapay/1642200051764-initial.js +13 -0
  52. package/dist/migration/nuapay/1642201303413-add-fields-nuapay.js +15 -0
  53. package/dist/migration/nuapay/1642282666475-add-nuapay-account.js +13 -0
  54. package/dist/migration/nuapay/1642283420696-fix-iban-length.js +15 -0
  55. package/dist/migration/nuapay/1642283781684-account-relation.js +15 -0
  56. package/dist/migration/nuapay/1642286393959-beneficiary.js +13 -0
  57. package/dist/migration/nuapay/1642316424390-account-ibans.js +21 -0
  58. package/dist/migration/nuapay/1642318117667-cleanup-beneficaries.js +23 -0
  59. package/dist/migration/nuapay/1642365244026-nullable-bic.js +13 -0
  60. package/dist/migration/nuapay/1642366232568-remove-bic.js +13 -0
  61. package/dist/migration/nuapay/1642370449589-transaction.js +17 -0
  62. package/dist/migration/payment/1642120204899-inital.js +21 -0
  63. package/dist/migration/payment/1642514728358-payment-link-updates.js +39 -0
  64. package/dist/migration/payment/1642516017835-payment-link-add-booking.js +15 -0
  65. package/dist/migration/payment/1642519453365-transaction.js +63 -0
  66. package/dist/migration/payment/1642520774562-transaction-payment-link.js +15 -0
  67. package/dist/migration/payment/1642533711013-transaction-metadata.js +15 -0
  68. package/dist/migration/payment/1642540883807-transaction-event.js +27 -0
  69. package/dist/migration/user/1641774481300-initial.js +27 -0
  70. package/dist/migration/user/1641829697638-add_parent_org.js +25 -0
  71. package/dist/migration/user/1641901624008-add_roles.js +21 -0
  72. package/dist/migration/user/1641903059924-add_roles_kyc.js +13 -0
  73. package/dist/migration/user/1641903425025-roles-defaults.js +15 -0
  74. package/dist/migration/user/1641939221103-feature.js +13 -0
  75. package/dist/migration/user/1642019067334-organisation-feature.js +17 -0
  76. package/dist/migration/user/1642078564295-user-activity-relation.js +16 -0
  77. package/dist/migration/user/1642207858247-fix-user-constraint.js +19 -0
  78. package/dist/model/index.js +45 -0
  79. package/dist/model/response/index.js +150 -0
  80. package/dist/model/status/index.js +82 -0
  81. package/dist/service/http.js +55 -0
  82. package/dist/service/logger.js +236 -0
  83. package/dist/service/request.js +342 -0
  84. package/dist/service/warehouse.js +59 -0
  85. package/dist/util/lambda-termination.js +39 -0
  86. package/dist/util/promisified-delay.js +59 -0
  87. package/dist/wrapper/index.js +165 -0
  88. package/ormconfig.js +25 -0
  89. package/package.json +19 -5
@@ -0,0 +1,21 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class addRoles1641901624008 {
4
+ name = 'addRoles1641901624008'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`CREATE TABLE "user"."role" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "machine_name" character varying(50) NOT NULL, "name" character varying(150) NOT NULL, "description" character varying(250) NOT NULL, "parent_only" boolean NOT NULL, "group" character varying(100) NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_b36bcfe02fc8de3c57a8b2391c2" PRIMARY KEY ("id"))`);
8
+ await queryRunner.query(`CREATE TABLE "user"."user_role" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "user_id" character varying NOT NULL, "organisation_id" character varying(150) NOT NULL, "role_id" uuid NOT NULL, CONSTRAINT "PK_38abbb50a455f9a7da9651ce5d7" PRIMARY KEY ("id", "user_id", "organisation_id", "role_id"))`);
9
+ await queryRunner.query(`ALTER TABLE "user"."user_role" ADD CONSTRAINT "FK_d0e5815877f7395a198a4cb0a46" FOREIGN KEY ("user_id") REFERENCES "user"."user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
10
+ await queryRunner.query(`ALTER TABLE "user"."user_role" ADD CONSTRAINT "FK_fe76718250bdec1ebff3494c99b" FOREIGN KEY ("organisation_id") REFERENCES "user"."organisation"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
11
+ await queryRunner.query(`ALTER TABLE "user"."user_role" ADD CONSTRAINT "FK_32a6fc2fcb019d8e3a8ace0f55f" FOREIGN KEY ("role_id") REFERENCES "user"."role"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
12
+ }
13
+
14
+ async down(queryRunner) {
15
+ await queryRunner.query(`ALTER TABLE "user"."user_role" DROP CONSTRAINT "FK_32a6fc2fcb019d8e3a8ace0f55f"`);
16
+ await queryRunner.query(`ALTER TABLE "user"."user_role" DROP CONSTRAINT "FK_fe76718250bdec1ebff3494c99b"`);
17
+ await queryRunner.query(`ALTER TABLE "user"."user_role" DROP CONSTRAINT "FK_d0e5815877f7395a198a4cb0a46"`);
18
+ await queryRunner.query(`DROP TABLE "user"."user_role"`);
19
+ await queryRunner.query(`DROP TABLE "user"."role"`);
20
+ }
21
+ }
@@ -0,0 +1,13 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class addRolesKyc1641903059924 {
4
+ name = 'addRolesKyc1641903059924'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`ALTER TABLE "user"."role" ADD "kyc_required" boolean NOT NULL`);;
8
+ }
9
+
10
+ async down(queryRunner) {
11
+ await queryRunner.query(`ALTER TABLE "user"."role" DROP COLUMN "kyc_required"`);
12
+ }
13
+ }
@@ -0,0 +1,15 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class rolesDefaults1641903425025 {
4
+ name = 'rolesDefaults1641903425025'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`ALTER TABLE "user"."role" ALTER COLUMN "parent_only" SET DEFAULT false`);
8
+ await queryRunner.query(`ALTER TABLE "user"."role" ALTER COLUMN "kyc_required" SET DEFAULT false`);
9
+ }
10
+
11
+ async down(queryRunner) {
12
+ await queryRunner.query(`ALTER TABLE "user"."role" ALTER COLUMN "kyc_required" DROP DEFAULT`);
13
+ await queryRunner.query(`ALTER TABLE "user"."role" ALTER COLUMN "parent_only" DROP DEFAULT`);
14
+ }
15
+ }
@@ -0,0 +1,13 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class feature1641939221103 {
4
+ name = 'feature1641939221103'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`CREATE TABLE "user"."feature" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "machine_name" character varying(50) NOT NULL, "name" character varying(150) NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_03930932f909ca4be8e33d16a2d" PRIMARY KEY ("id"))`);
8
+ }
9
+
10
+ async down(queryRunner) {
11
+ await queryRunner.query(`DROP TABLE "user"."feature"`);
12
+ }
13
+ }
@@ -0,0 +1,17 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class organisationFeature1642019067334 {
4
+ name = 'organisationFeature1642019067334'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`CREATE TABLE "user"."organisation_feature" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "organisation_id" character varying(150), "feature_id" uuid, CONSTRAINT "PK_ee793662f5baf61d42cf0213a5d" PRIMARY KEY ("id"))`);
8
+ await queryRunner.query(`ALTER TABLE "user"."organisation_feature" ADD CONSTRAINT "FK_65a20c77a2387c9717c02efd4ab" FOREIGN KEY ("organisation_id") REFERENCES "user"."organisation"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
9
+ await queryRunner.query(`ALTER TABLE "user"."organisation_feature" ADD CONSTRAINT "FK_a8920942bf7f3703d9968fa8eae" FOREIGN KEY ("feature_id") REFERENCES "user"."feature"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
10
+ }
11
+
12
+ async down(queryRunner) {
13
+ await queryRunner.query(`ALTER TABLE "user"."organisation_feature" DROP CONSTRAINT "FK_a8920942bf7f3703d9968fa8eae"`);
14
+ await queryRunner.query(`ALTER TABLE "user"."organisation_feature" DROP CONSTRAINT "FK_65a20c77a2387c9717c02efd4ab"`);
15
+ await queryRunner.query(`DROP TABLE "user"."organisation_feature"`);
16
+ }
17
+ }
@@ -0,0 +1,16 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class userActivityRelation1642078564295 {
4
+ name = 'userActivityRelation1642078564295'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`ALTER TABLE "user"."user_activity" DROP COLUMN "user_id"`);
8
+ await queryRunner.query(`ALTER TABLE "user"."user_activity" ADD "user_id" character varying NOT NULL`);
9
+ await queryRunner.query(`ALTER TABLE "user"."user_activity" ADD CONSTRAINT "FK_11108754ec780c670440e32baad" FOREIGN KEY ("user_id") REFERENCES "user"."user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);}
10
+
11
+ async down(queryRunner) {
12
+ await queryRunner.query(`ALTER TABLE "user"."user_activity" DROP CONSTRAINT "FK_11108754ec780c670440e32baad"`);
13
+ await queryRunner.query(`ALTER TABLE "user"."user_activity" DROP COLUMN "user_id"`);
14
+ await queryRunner.query(`ALTER TABLE "user"."user_activity" ADD "user_id" uuid NOT NULL`);
15
+ }
16
+ }
@@ -0,0 +1,19 @@
1
+ const { MigrationInterface, QueryRunner } = require("typeorm");
2
+
3
+ module.exports = class fixUserConstraint1642207858247 {
4
+ name = 'fixUserConstraint1642207858247'
5
+
6
+ async up(queryRunner) {
7
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" DROP CONSTRAINT "FK_81c4608739c8cf71fa1f96e11ae"`);
8
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" DROP CONSTRAINT "FK_d9628e009b6d71bf677c04f8494"`);
9
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" ADD CONSTRAINT "FK_81c4608739c8cf71fa1f96e11ae" FOREIGN KEY ("user_id") REFERENCES "user"."user"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
10
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" ADD CONSTRAINT "FK_d9628e009b6d71bf677c04f8494" FOREIGN KEY ("organisation_id") REFERENCES "user"."organisation"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
11
+ }
12
+
13
+ async down(queryRunner) {
14
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" DROP CONSTRAINT "FK_d9628e009b6d71bf677c04f8494"`);
15
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" DROP CONSTRAINT "FK_81c4608739c8cf71fa1f96e11ae"`);
16
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" ADD CONSTRAINT "FK_d9628e009b6d71bf677c04f8494" FOREIGN KEY ("organisation_id") REFERENCES "user"."organisation"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
17
+ await queryRunner.query(`ALTER TABLE "user"."user_organisation" ADD CONSTRAINT "FK_81c4608739c8cf71fa1f96e11ae" FOREIGN KEY ("user_id") REFERENCES "user"."user"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
18
+ }
19
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _validate = _interopRequireDefault(require("validate.js/validate"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ /* eslint-disable class-methods-use-this */
13
+
14
+ /**
15
+ * Model base class
16
+ */
17
+ class Model {
18
+ /**
19
+ * Instantiate a function with a value if defined
20
+ *
21
+ * @param classFunctionName string
22
+ * @param value mixed
23
+ */
24
+ instantiateFunctionWithDefinedValue(classFunctionName, value) {
25
+ if (typeof value !== 'undefined') {
26
+ this[classFunctionName](value);
27
+ }
28
+ }
29
+ /**
30
+ * Validate values against constraints
31
+ *
32
+ * @param values object
33
+ * @param constraints object
34
+ * @returns {boolean}
35
+ */
36
+
37
+
38
+ validateAgainstConstraints(values, constraints) {
39
+ const validation = (0, _validate.default)(values, constraints);
40
+ return typeof validation === 'undefined';
41
+ }
42
+
43
+ }
44
+
45
+ exports.default = Model;
@@ -0,0 +1,150 @@
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
+
8
+ var _ = _interopRequireDefault(require(".."));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ /**
13
+ *
14
+ * @type {object}
15
+ */
16
+ const RESPONSE_HEADERS = {
17
+ 'Content-Type': 'application/json',
18
+ 'Access-Control-Allow-Origin': '*',
19
+ // Required for CORS support to work
20
+ 'Access-Control-Allow-Credentials': true // Required for cookies, authorization headers with HTTPS
21
+
22
+ };
23
+ /**
24
+ * Default message provided as part of response
25
+ *
26
+ * @type {string}
27
+ */
28
+
29
+ exports.RESPONSE_HEADERS = RESPONSE_HEADERS;
30
+ const DEFAULT_MESSAGE = 'success';
31
+ /**
32
+ * class ResponseModel
33
+ */
34
+
35
+ exports.DEFAULT_MESSAGE = DEFAULT_MESSAGE;
36
+
37
+ class Index extends _.default {
38
+ /**
39
+ * ResponseModel Constructor
40
+ *
41
+ * @param data
42
+ * @param code
43
+ * @param message
44
+ */
45
+ constructor(data = null, code = null, message = null) {
46
+ super();
47
+ this.body = {
48
+ data: data !== null ? data : {},
49
+ message: message !== null ? message : DEFAULT_MESSAGE
50
+ };
51
+ this.code = code !== null ? code : {};
52
+ }
53
+ /**
54
+ * Add or update a body variable
55
+ *
56
+ * @param variable
57
+ * @param value
58
+ */
59
+
60
+
61
+ setBodyVariable(variable, value) {
62
+ this.body[variable] = value;
63
+ }
64
+ /**
65
+ * Set Data
66
+ *
67
+ * @param data
68
+ */
69
+
70
+
71
+ setData(data) {
72
+ this.body.data = data;
73
+ }
74
+ /**
75
+ * Set Status Code
76
+ *
77
+ * @param code
78
+ */
79
+
80
+
81
+ setCode(code) {
82
+ this.code = code;
83
+ }
84
+ /**
85
+ * Get Status Code
86
+ *
87
+ * @returns {*}
88
+ */
89
+
90
+
91
+ getCode() {
92
+ return this.code;
93
+ }
94
+ /**
95
+ * Set message
96
+ *
97
+ * @param message
98
+ */
99
+
100
+
101
+ setMessage(message) {
102
+ this.body.message = message;
103
+ }
104
+ /**
105
+ * Get Message
106
+ *
107
+ * @returns {string|*}
108
+ */
109
+
110
+
111
+ getMessage() {
112
+ return this.body.message;
113
+ }
114
+ /**
115
+ * Geneate a response
116
+ *
117
+ * @returns {object}
118
+ */
119
+
120
+
121
+ generate() {
122
+ return {
123
+ statusCode: this.code,
124
+ headers: RESPONSE_HEADERS,
125
+ body: JSON.stringify(this.body)
126
+ };
127
+ }
128
+ /**
129
+ * Shorthand static method
130
+ * that generates the response immediately
131
+ * if no additional processing is required.
132
+ *
133
+ * Saves only 1 line of code
134
+ * but keeps code terse in a lot of places.
135
+ *
136
+ * @param {*} data
137
+ * @param {*} code
138
+ * @param {*} message
139
+ * @returns {object}
140
+ */
141
+
142
+
143
+ static generate(data = null, code = null, message = null) {
144
+ const response = new this(data, code, message);
145
+ return response.generate();
146
+ }
147
+
148
+ }
149
+
150
+ exports.default = Index;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.STATUS_TYPES = void 0;
7
+
8
+ var _index = _interopRequireDefault(require("../index"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ const STATUS_TYPES = {
13
+ OK: 'OK',
14
+ ACCEPTABLE_FAILURE: 'ACCEPTABLE_FAILURE',
15
+ APPLICATION_FAILURE: 'APPLICATION_FAILURE'
16
+ };
17
+ /**
18
+ * StatusModel Class
19
+ */
20
+
21
+ exports.STATUS_TYPES = STATUS_TYPES;
22
+
23
+ class Index extends _index.default {
24
+ /**
25
+ * StatusModel constructor
26
+ *
27
+ * @param service
28
+ * @param status
29
+ */
30
+ constructor(service, status) {
31
+ super();
32
+ this.setService(service);
33
+ this.setStatus(status);
34
+ }
35
+ /**
36
+ * Get Service
37
+ *
38
+ * @returns {*}
39
+ */
40
+
41
+
42
+ getService() {
43
+ return this.service;
44
+ }
45
+ /**
46
+ * Set Service
47
+ *
48
+ * @param service
49
+ */
50
+
51
+
52
+ setService(service) {
53
+ this.service = service;
54
+ }
55
+ /**
56
+ * Set the status
57
+ *
58
+ * @param status
59
+ */
60
+
61
+
62
+ setStatus(status) {
63
+ if (typeof STATUS_TYPES[status] === 'undefined') {
64
+ throw new TypeError(`${Index.name} - ${status} is not a valid status type`);
65
+ }
66
+
67
+ this.status = status;
68
+ }
69
+ /**
70
+ * Get status
71
+ *
72
+ * @returns {string|*}
73
+ */
74
+
75
+
76
+ getStatus() {
77
+ return this.status;
78
+ }
79
+
80
+ }
81
+
82
+ exports.default = Index;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.DEFAULT_HTTP_TIMEOUT = void 0;
7
+
8
+ var _axios = _interopRequireDefault(require("axios"));
9
+
10
+ var _dependencyAware = _interopRequireDefault(require("../dependency-injection/dependency-aware"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ const DEFAULT_HTTP_TIMEOUT = 10 * 1000;
15
+ /**
16
+ * HTTPService class
17
+ */
18
+
19
+ exports.DEFAULT_HTTP_TIMEOUT = DEFAULT_HTTP_TIMEOUT;
20
+
21
+ class Http extends _dependencyAware.default {
22
+ constructor(di) {
23
+ super(di);
24
+ this.config = {
25
+ timeout: DEFAULT_HTTP_TIMEOUT
26
+ };
27
+ }
28
+ /**
29
+ * Sets the default timeout
30
+ *
31
+ * @param {number} ms
32
+ */
33
+
34
+
35
+ setDefaultTimeout(ms) {
36
+ this.config.timeout = ms;
37
+ }
38
+ /**
39
+ * Performs and HTTP Request
40
+ *
41
+ * @param config
42
+ */
43
+
44
+
45
+ async request(config) {
46
+ return _axios.default.request({
47
+ timeout: this.config.timeout,
48
+ headers: {},
49
+ ...config
50
+ });
51
+ }
52
+
53
+ }
54
+
55
+ exports.default = Http;
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _epsagon = _interopRequireDefault(require("epsagon"));
9
+
10
+ var _winston = _interopRequireDefault(require("winston"));
11
+
12
+ var _dependencyAware = _interopRequireDefault(require("../dependency-injection/dependency-aware"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ /**
17
+ * LoggerService class
18
+ */
19
+ class Logger extends _dependencyAware.default {
20
+ constructor(di) {
21
+ super(di);
22
+ this.winston = null;
23
+ }
24
+ /**
25
+ * Returns a Winston logger object
26
+ * configured for our lambdas.
27
+ *
28
+ * Note:
29
+ *
30
+ * If the lambda is executed
31
+ * in a `serverless-offline` context
32
+ * the log output to console will be pretty printed.
33
+ */
34
+
35
+
36
+ getLogger() {
37
+ const loggerFormats = [_winston.default.format.json({
38
+ replacer: (key, value) => {
39
+ if (value instanceof Buffer) {
40
+ return value.toString('base64');
41
+ }
42
+
43
+ if (value instanceof Error) {
44
+ const error = {};
45
+ Object.getOwnPropertyNames(value).forEach(objectKey => {
46
+ error[objectKey] = value[objectKey];
47
+ });
48
+ return error;
49
+ }
50
+
51
+ return value;
52
+ }
53
+ })];
54
+
55
+ if (this.getContainer().isOffline) {
56
+ loggerFormats.push(_winston.default.format.prettyPrint());
57
+ }
58
+
59
+ return _winston.default.createLogger({
60
+ level: 'info',
61
+ format: _winston.default.format.combine(...loggerFormats),
62
+ transports: [new _winston.default.transports.Console()]
63
+ });
64
+ }
65
+ /**
66
+ * Returns the logger.
67
+ *
68
+ * Uses a cached `Winston` object
69
+ * if it has been already generated,
70
+ * otherwise it generates one.
71
+ */
72
+
73
+
74
+ get logger() {
75
+ if (!this.winston) {
76
+ this.winston = this.getLogger();
77
+ }
78
+
79
+ return this.winston;
80
+ }
81
+ /**
82
+ * While handling an error, lambda wrapper should
83
+ * recognise axios errors and trim down the information.
84
+ *
85
+ * Keep the following keys:
86
+ * - message.config
87
+ * - message.message
88
+ * - message.response?.status
89
+ * - message.response?.data
90
+ *
91
+ * @param {object} error
92
+ */
93
+
94
+
95
+ static processAxiosError(error) {
96
+ const processed = {
97
+ config: error.config,
98
+ message: error.message
99
+ }; // It's pretty common for axios errors
100
+ // to not have.response e.g.when there's
101
+ // a network error or timeout.
102
+ // These errors will have .request but not .response.
103
+
104
+ if (error.response) {
105
+ processed.response = {
106
+ status: error.response.status,
107
+ data: error.response.data
108
+ };
109
+ }
110
+
111
+ return processed;
112
+ }
113
+ /**
114
+ * Transform the original message
115
+ * before it is passed to the winston logger
116
+ *
117
+ * @param {string|object} message
118
+ */
119
+
120
+
121
+ processMessage(message = '') {
122
+ let processed = message;
123
+
124
+ if (processed && processed.isAxiosError) {
125
+ processed = this.constructor.processAxiosError(processed);
126
+ }
127
+
128
+ return processed;
129
+ }
130
+ /**
131
+ * Log Error Message
132
+ *
133
+ * @param error object
134
+ * @param message string
135
+ */
136
+
137
+
138
+ error(error, message = '') {
139
+ if (typeof process.env.EPSAGON_TOKEN === 'string' && process.env.EPSAGON_TOKEN !== 'undefined' && typeof process.env.EPSAGON_SERVICE_NAME === 'string' && process.env.EPSAGON_SERVICE_NAME !== 'undefined' && error instanceof Error) {
140
+ _epsagon.default.setError(error);
141
+ }
142
+
143
+ this.logger.log('error', message, {
144
+ error: this.processMessage(error)
145
+ });
146
+ this.label('error', true);
147
+ this.metric('error', 'error', true);
148
+ }
149
+ /**
150
+ * Log Information Message
151
+ *
152
+ * @param message string
153
+ */
154
+
155
+
156
+ info(message) {
157
+ this.logger.log('info', this.processMessage(message));
158
+ }
159
+ /**
160
+ * Logs an error, using `LoggerService.error`
161
+ * or `LoggerService.info` based on
162
+ * `process.env.LOGGER_SOFT_WARNING`.
163
+ *
164
+ * Please note that `LoggerService.error` and `LoggerService.info`
165
+ * have different signatures. The function uses the shared argument
166
+ * instead of introducing ambiguity.
167
+ *
168
+ * @param error
169
+ */
170
+
171
+
172
+ warning(error) {
173
+ const softWarningValues = ['true', '1'];
174
+
175
+ if (softWarningValues.includes(process.env.LOGGER_SOFT_WARNING)) {
176
+ return this.info(error);
177
+ }
178
+
179
+ return this.error(error);
180
+ }
181
+ /**
182
+ * Add a label
183
+ *
184
+ * @param descriptor string
185
+ * @param silent boolean
186
+ */
187
+
188
+
189
+ label(descriptor, silent = false) {
190
+ if (typeof process.env.EPSAGON_TOKEN === 'string' && process.env.EPSAGON_TOKEN !== 'undefined' && typeof process.env.EPSAGON_SERVICE_NAME === 'string' && process.env.EPSAGON_SERVICE_NAME !== 'undefined') {
191
+ _epsagon.default.label(descriptor);
192
+ }
193
+
194
+ if (silent === false) {
195
+ this.logger.log('info', `label - ${descriptor}`);
196
+ }
197
+ }
198
+ /**
199
+ * Add a metric
200
+ *
201
+ * @param descriptor string
202
+ * @param stat integer | string
203
+ * @param silent boolean
204
+ */
205
+
206
+
207
+ metric(descriptor, stat, silent = false) {
208
+ if (typeof process.env.EPSAGON_TOKEN === 'string' && process.env.EPSAGON_TOKEN !== 'undefined' && typeof process.env.EPSAGON_SERVICE_NAME === 'string' && process.env.EPSAGON_SERVICE_NAME !== 'undefined') {
209
+ _epsagon.default.label(descriptor, stat);
210
+ }
211
+
212
+ if (silent === false) {
213
+ this.logger.log('info', `metric - ${descriptor} - ${stat}`);
214
+ }
215
+ }
216
+ /**
217
+ * Logs an object so that it can be inspected
218
+ *
219
+ * @param action - What are we doing with the object, i.e. 'Processing'
220
+ * @param object - The object to be stored in logs
221
+ * @param level - 'error', 'warning' or 'info'
222
+ */
223
+
224
+
225
+ object(action, object, level = 'info') {
226
+ if (!['error', 'warning', 'info'].includes(level)) {
227
+ throw new Error('Unrecognised log level');
228
+ }
229
+
230
+ const payload = JSON.stringify(object, null, 4);
231
+ return this[level](`${action}: '${payload}'`);
232
+ }
233
+
234
+ }
235
+
236
+ exports.default = Logger;