@aligent/microservice-util-lib 0.1.1

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 (38) hide show
  1. package/README.md +36 -0
  2. package/dist/chunkBy/chunkBy.d.ts +11 -0
  3. package/dist/chunkBy/chunkBy.js +22 -0
  4. package/dist/chunkBy/chunkBy.js.map +1 -0
  5. package/dist/chunkBy/chunkBy.test.d.ts +1 -0
  6. package/dist/chunkBy/chunkBy.test.js +17 -0
  7. package/dist/chunkBy/chunkBy.test.js.map +1 -0
  8. package/dist/fetchSsmParams/fetchSsmParams.d.ts +12 -0
  9. package/dist/fetchSsmParams/fetchSsmParams.js +82 -0
  10. package/dist/fetchSsmParams/fetchSsmParams.js.map +1 -0
  11. package/dist/fetchSsmParams/fetchSsmParams.test.d.ts +1 -0
  12. package/dist/fetchSsmParams/fetchSsmParams.test.js +119 -0
  13. package/dist/fetchSsmParams/fetchSsmParams.test.js.map +1 -0
  14. package/dist/hasPropertiesDefined/hasPropertiesDefined.d.ts +24 -0
  15. package/dist/hasPropertiesDefined/hasPropertiesDefined.js +36 -0
  16. package/dist/hasPropertiesDefined/hasPropertiesDefined.js.map +1 -0
  17. package/dist/hasPropertiesDefined/hasPropertiesDefined.test.d.ts +1 -0
  18. package/dist/hasPropertiesDefined/hasPropertiesDefined.test.js +37 -0
  19. package/dist/hasPropertiesDefined/hasPropertiesDefined.test.js.map +1 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.js +19 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/remap/remap.d.ts +147 -0
  24. package/dist/remap/remap.js +50 -0
  25. package/dist/remap/remap.js.map +1 -0
  26. package/dist/remap/remap.test.d.ts +1 -0
  27. package/dist/remap/remap.test.js +65 -0
  28. package/dist/remap/remap.test.js.map +1 -0
  29. package/dist/retryWrapper/retryWrapper.d.ts +40 -0
  30. package/dist/retryWrapper/retryWrapper.js +121 -0
  31. package/dist/retryWrapper/retryWrapper.js.map +1 -0
  32. package/dist/retryWrapper/retryWrapper.test.d.ts +1 -0
  33. package/dist/retryWrapper/retryWrapper.test.js +234 -0
  34. package/dist/retryWrapper/retryWrapper.test.js.map +1 -0
  35. package/dist/s3/s3.d.ts +47 -0
  36. package/dist/s3/s3.js +196 -0
  37. package/dist/s3/s3.js.map +1 -0
  38. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Microservices Utilities Library
2
+
3
+ This library includes utility functions to simplify & standardise common
4
+ MicroServices tasks.
5
+
6
+ ## Documentation
7
+
8
+ Documentation on each function can be found [here](./docs/modules.md)
9
+
10
+ ## Build
11
+
12
+ This library is written in typescript and can be built using the NPM script:
13
+
14
+ ```sh
15
+ npm install
16
+ npm run build
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ You can locally install this package to your NPM projects by pulling this repo,
22
+ building it, then running:
23
+
24
+ ```sh
25
+ npm install --save ./path/to/this/project
26
+ ```
27
+
28
+ from your project root.
29
+
30
+ ## Testing & Linting
31
+
32
+ Jest tests, linting & type-checking can be run with
33
+
34
+ ```sh
35
+ npm run test
36
+ ```
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Split an array into chunks of a certain size
3
+ * @param source the array to split up
4
+ * @param chunkSize the size of each chunk. (The final chunk will be whatever is remaining)
5
+ * @example
6
+ * ```ts
7
+ * chunkBy([1, 2, 3, 4, 5, 6, 7], 2) // [[1, 2], [3, 4], [5, 6], [7]]
8
+ * ```
9
+ */
10
+ declare function chunkBy<ArrayItem>(source: ArrayItem[], chunkSize: number): ArrayItem[][];
11
+ export default chunkBy;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Split an array into chunks of a certain size
5
+ * @param source the array to split up
6
+ * @param chunkSize the size of each chunk. (The final chunk will be whatever is remaining)
7
+ * @example
8
+ * ```ts
9
+ * chunkBy([1, 2, 3, 4, 5, 6, 7], 2) // [[1, 2], [3, 4], [5, 6], [7]]
10
+ * ```
11
+ */
12
+ function chunkBy(source, chunkSize) {
13
+ if (chunkSize <= 0) {
14
+ throw new Error("Cannot create chunks of size ".concat(chunkSize, " (0 or less)"));
15
+ }
16
+ var numberOfChunks = Math.ceil(source.length / chunkSize);
17
+ return Array.from(new Array(numberOfChunks), function (_, i) {
18
+ return source.slice(i * chunkSize, (i + 1) * chunkSize);
19
+ });
20
+ }
21
+ exports.default = chunkBy;
22
+ //# sourceMappingURL=chunkBy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunkBy.js","sourceRoot":"","sources":["../../src/chunkBy/chunkBy.ts"],"names":[],"mappings":";;AAAA;;;;;;;;GAQG;AACH,SAAS,OAAO,CAAY,MAAmB,EAAE,SAAiB;IAChE,IAAI,SAAS,IAAI,CAAC,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,uCAAgC,SAAS,iBAAc,CAAC,CAAC;KAC1E;IAED,IAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC5D,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,EAAE,UAAC,CAAC,EAAE,CAAC;QAChD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;AACL,CAAC;AAED,kBAAe,OAAO,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
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
+ var chunkBy_1 = __importDefault(require("./chunkBy"));
7
+ describe('chunkBy', function () {
8
+ it('should split up an array into sub-arrays of a certain size', function () {
9
+ var arr = [1, 2, 3, 4];
10
+ expect((0, chunkBy_1.default)(arr, 2)).toEqual([[1, 2], [3, 4]]);
11
+ });
12
+ it('should put leftover items in the last chunk', function () {
13
+ var arr = [1, 2, 3, 4, 5];
14
+ expect((0, chunkBy_1.default)(arr, 2)).toEqual([[1, 2], [3, 4], [5]]);
15
+ });
16
+ });
17
+ //# sourceMappingURL=chunkBy.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunkBy.test.js","sourceRoot":"","sources":["../../src/chunkBy/chunkBy.test.ts"],"names":[],"mappings":";;;;;AAAA,sDAAgC;AAEhC,QAAQ,CAAC,SAAS,EAAE;IAClB,EAAE,CAAC,4DAA4D,EAAE;QAC/D,IAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACzB,MAAM,CAAC,IAAA,iBAAO,EAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACpD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,6CAA6C,EAAE;QAChD,IAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAC5B,MAAM,CAAC,IAAA,iBAAO,EAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,12 @@
1
+ import { SSM } from 'aws-sdk';
2
+ /**
3
+ * Fetch one SSM parameter
4
+ * @param param key of the parameter to fetch
5
+ */
6
+ declare function fetchSsmParams(param: string): Promise<SSM.Parameter>;
7
+ /**
8
+ * Fetch a list of SSM parameters
9
+ * @param params list of parameter keys to fetch
10
+ */
11
+ declare function fetchSsmParams(...params: string[]): Promise<SSM.ParameterList>;
12
+ export default fetchSsmParams;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var aws_sdk_1 = require("aws-sdk");
40
+ var ssm = new aws_sdk_1.SSM();
41
+ /**
42
+ * Fetch SSM Parameters
43
+ * @param params the keys of the parameters to fetch
44
+ */
45
+ function fetchSsmParams() {
46
+ var params = [];
47
+ for (var _i = 0; _i < arguments.length; _i++) {
48
+ params[_i] = arguments[_i];
49
+ }
50
+ return __awaiter(this, void 0, void 0, function () {
51
+ var result_1, result;
52
+ return __generator(this, function (_a) {
53
+ switch (_a.label) {
54
+ case 0:
55
+ if (params.length === 0) {
56
+ throw new Error('No SSM Params supplied');
57
+ }
58
+ if (!(params.length === 1)) return [3 /*break*/, 2];
59
+ return [4 /*yield*/, ssm.getParameter({
60
+ Name: params[0],
61
+ WithDecryption: true
62
+ })
63
+ .promise()];
64
+ case 1:
65
+ result_1 = _a.sent();
66
+ return [2 /*return*/, result_1.Parameter];
67
+ case 2: return [4 /*yield*/, ssm.getParameters({
68
+ Names: params,
69
+ WithDecryption: true
70
+ })
71
+ .promise()];
72
+ case 3:
73
+ result = _a.sent();
74
+ return [2 /*return*/, params.map(function (paramName) {
75
+ return result.Parameters.find(function (param) { return param.Name === paramName; });
76
+ })];
77
+ }
78
+ });
79
+ });
80
+ }
81
+ exports.default = fetchSsmParams;
82
+ //# sourceMappingURL=fetchSsmParams.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetchSsmParams.js","sourceRoot":"","sources":["../../src/fetchSsmParams/fetchSsmParams.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAA8B;AAE9B,IAAM,GAAG,GAAG,IAAI,aAAG,EAAE,CAAC;AAgBtB;;;GAGG;AACH,SAAe,cAAc;IAAC,gBAAmB;SAAnB,UAAmB,EAAnB,qBAAmB,EAAnB,IAAmB;QAAnB,2BAAmB;;;;;;;oBAC/C,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;wBACvB,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;qBAC3C;yBAEG,CAAA,MAAM,CAAC,MAAM,KAAK,CAAC,CAAA,EAAnB,wBAAmB;oBACN,qBAAM,GAAG,CAAC,YAAY,CAAC;4BACpC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;4BACf,cAAc,EAAE,IAAI;yBACrB,CAAC;6BACC,OAAO,EAAE,EAAA;;oBAJN,WAAS,SAIH;oBACZ,sBAAO,QAAM,CAAC,SAAS,EAAC;wBAGX,qBAAM,GAAG,CAAC,aAAa,CAAC;wBACrC,KAAK,EAAE,MAAM;wBACb,cAAc,EAAE,IAAI;qBACrB,CAAC;yBACC,OAAO,EAAE,EAAA;;oBAJN,MAAM,GAAG,SAIH;oBAEZ,sBAAO,MAAM,CAAC,GAAG,CAAC,UAAA,SAAS;4BACzB,OAAO,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,IAAI,KAAK,SAAS,EAAxB,CAAwB,CAAC,CAAC;wBACnE,CAAC,CAAC,EAAC;;;;CACJ;AAED,kBAAe,cAAc,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ var fetchSsmParams_1 = __importDefault(require("./fetchSsmParams"));
43
+ var getParam = jest.fn();
44
+ var getParams = jest.fn();
45
+ jest.mock('aws-sdk', function () {
46
+ return {
47
+ SSM: jest.fn().mockImplementation(function () {
48
+ return {
49
+ getParameter: function () { return ({ promise: getParam }); },
50
+ getParameters: function () { return ({ promise: getParams }); }
51
+ };
52
+ })
53
+ };
54
+ });
55
+ describe('fetchSsmParams', function () {
56
+ afterEach(function () {
57
+ getParam.mockReset();
58
+ getParams.mockReset();
59
+ });
60
+ it('should error when no params are passed', function () { return __awaiter(void 0, void 0, void 0, function () {
61
+ var ex_1;
62
+ return __generator(this, function (_a) {
63
+ switch (_a.label) {
64
+ case 0:
65
+ _a.trys.push([0, 2, , 3]);
66
+ return [4 /*yield*/, (0, fetchSsmParams_1.default)()];
67
+ case 1:
68
+ _a.sent();
69
+ return [3 /*break*/, 3];
70
+ case 2:
71
+ ex_1 = _a.sent();
72
+ expect(ex_1).toBeTruthy();
73
+ return [3 /*break*/, 3];
74
+ case 3: return [2 /*return*/];
75
+ }
76
+ });
77
+ }); });
78
+ it('should fetch a single parameter if only one is supplied', function () { return __awaiter(void 0, void 0, void 0, function () {
79
+ var _a;
80
+ return __generator(this, function (_b) {
81
+ switch (_b.label) {
82
+ case 0:
83
+ _b.trys.push([0, 2, , 3]);
84
+ return [4 /*yield*/, (0, fetchSsmParams_1.default)('')];
85
+ case 1:
86
+ _b.sent();
87
+ return [3 /*break*/, 3];
88
+ case 2:
89
+ _a = _b.sent();
90
+ return [3 /*break*/, 3];
91
+ case 3:
92
+ expect(getParam).toHaveBeenCalledTimes(1);
93
+ expect(getParams).toHaveBeenCalledTimes(0);
94
+ return [2 /*return*/];
95
+ }
96
+ });
97
+ }); });
98
+ it('should fetch a multiple parameters if more than one is supplied', function () { return __awaiter(void 0, void 0, void 0, function () {
99
+ var _a;
100
+ return __generator(this, function (_b) {
101
+ switch (_b.label) {
102
+ case 0:
103
+ _b.trys.push([0, 2, , 3]);
104
+ return [4 /*yield*/, (0, fetchSsmParams_1.default)('', '', '')];
105
+ case 1:
106
+ _b.sent();
107
+ return [3 /*break*/, 3];
108
+ case 2:
109
+ _a = _b.sent();
110
+ return [3 /*break*/, 3];
111
+ case 3:
112
+ expect(getParam).toHaveBeenCalledTimes(0);
113
+ expect(getParams).toHaveBeenCalledTimes(1);
114
+ return [2 /*return*/];
115
+ }
116
+ });
117
+ }); });
118
+ });
119
+ //# sourceMappingURL=fetchSsmParams.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetchSsmParams.test.js","sourceRoot":"","sources":["../../src/fetchSsmParams/fetchSsmParams.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oEAA8C;AAE9C,IAAM,QAAQ,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;AAC3B,IAAM,SAAS,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;AAE5B,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;IACnB,OAAO;QACL,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,kBAAkB,CAAC;YAChC,OAAO;gBACL,YAAY,EAAE,cAAM,OAAA,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,EAAvB,CAAuB;gBAC3C,aAAa,EAAE,cAAM,OAAA,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,EAAxB,CAAwB;aAC9C,CAAC;QACJ,CAAC,CAAC;KACH,CAAC;AACJ,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,gBAAgB,EAAE;IAEzB,SAAS,CAAC;QACR,QAAQ,CAAC,SAAS,EAAE,CAAC;QACrB,SAAS,CAAC,SAAS,EAAE,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wCAAwC,EAAE;;;;;;oBAEzC,qBAAM,IAAA,wBAAc,GAAE,EAAA;;oBAAtB,SAAsB,CAAC;;;;oBAEvB,MAAM,CAAC,IAAE,CAAC,CAAC,UAAU,EAAE,CAAC;;;;;SAE3B,CAAC,CAAC;IAEH,EAAE,CAAC,yDAAyD,EAAE;;;;;;oBAE1D,qBAAM,IAAA,wBAAc,EAAC,EAAE,CAAC,EAAA;;oBAAxB,SAAwB,CAAC;;;;;;oBAG3B,MAAM,CAAC,QAAQ,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;oBAC1C,MAAM,CAAC,SAAS,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;;;;SAC5C,CAAC,CAAC;IAEH,EAAE,CAAC,iEAAiE,EAClE;;;;;;oBAEI,qBAAM,IAAA,wBAAc,EAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAA;;oBAAhC,SAAgC,CAAC;;;;;;oBAGnC,MAAM,CAAC,QAAQ,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;oBAC1C,MAAM,CAAC,SAAS,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;;;;SAC5C,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { SimplifyIntersection } from '../remap/remap';
2
+ /**
3
+ * Ensure that the given properties are defined on the object.
4
+ * @param obj The object to check.
5
+ * @param keys The keys to check.
6
+ * @returns `true` if the object has the given properties defined, `false` otherwise.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * type Foo = { a?: number; b?: number };
11
+ * const foo: Foo = { a: 1, b: 2 };
12
+ * if (hasDefinedProperties(foo, 'a')) {
13
+ * console.log(foo);
14
+ * // ^? const bar: {
15
+ * // a: number;
16
+ * // b?: number;
17
+ * // }
18
+ * }
19
+ * ```
20
+ */
21
+ declare function hasDefinedProperties<T extends {
22
+ [key: string]: any;
23
+ }, K extends keyof T>(obj: T | object, ...keys: K[]): obj is SimplifyIntersection<Required<Pick<T, K>> & Omit<T, K>>;
24
+ export default hasDefinedProperties;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Ensure that the given properties are defined on the object.
5
+ * @param obj The object to check.
6
+ * @param keys The keys to check.
7
+ * @returns `true` if the object has the given properties defined, `false` otherwise.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * type Foo = { a?: number; b?: number };
12
+ * const foo: Foo = { a: 1, b: 2 };
13
+ * if (hasDefinedProperties(foo, 'a')) {
14
+ * console.log(foo);
15
+ * // ^? const bar: {
16
+ * // a: number;
17
+ * // b?: number;
18
+ * // }
19
+ * }
20
+ * ```
21
+ */
22
+ function hasDefinedProperties(obj) {
23
+ var keys = [];
24
+ for (var _i = 1; _i < arguments.length; _i++) {
25
+ keys[_i - 1] = arguments[_i];
26
+ }
27
+ for (var _a = 0, keys_1 = keys; _a < keys_1.length; _a++) {
28
+ var key = keys_1[_a];
29
+ if (obj[key] === undefined || obj[key] === null) {
30
+ return false;
31
+ }
32
+ }
33
+ return true;
34
+ }
35
+ exports.default = hasDefinedProperties;
36
+ //# sourceMappingURL=hasPropertiesDefined.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hasPropertiesDefined.js","sourceRoot":"","sources":["../../src/hasPropertiesDefined/hasPropertiesDefined.ts"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAS,oBAAoB,CAI3B,GAAe;IACf,cAAY;SAAZ,UAAY,EAAZ,qBAAY,EAAZ,IAAY;QAAZ,6BAAY;;IAGZ,KAAkB,UAAI,EAAJ,aAAI,EAAJ,kBAAI,EAAJ,IAAI,EAAE;QAAnB,IAAM,GAAG,aAAA;QACZ,IAAK,GAAS,CAAC,GAAG,CAAC,KAAK,SAAS,IAAK,GAAS,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE;YAC7D,OAAO,KAAK,CAAC;SACd;KACF;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,kBAAe,oBAAoB,CAAC"}
@@ -0,0 +1,37 @@
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
+ var hasPropertiesDefined_1 = __importDefault(require("./hasPropertiesDefined"));
7
+ describe('verifyDefined', function () {
8
+ it('should return the object as-is when checks are met', function () {
9
+ var obj = {
10
+ bar: 7,
11
+ baz: true
12
+ };
13
+ expect((0, hasPropertiesDefined_1.default)(obj, 'bar', 'baz')).toBeTruthy();
14
+ });
15
+ it('should throw an error if a property is missing', function () {
16
+ var obj = {
17
+ bar: 7,
18
+ baz: true
19
+ };
20
+ expect((0, hasPropertiesDefined_1.default)(obj, 'qux')).toBeFalsy();
21
+ });
22
+ it('should list the missing properties when it errors', function () {
23
+ var obj = {
24
+ bar: 7,
25
+ };
26
+ expect((0, hasPropertiesDefined_1.default)(obj, 'qux', 'baz', 'bar')).toBeFalsy();
27
+ });
28
+ it('should still return true if the values are defined but falsey', function () {
29
+ var obj = {
30
+ bar: 0,
31
+ baz: false,
32
+ qux: ''
33
+ };
34
+ expect((0, hasPropertiesDefined_1.default)(obj, 'bar', 'baz', 'qux')).toBeTruthy();
35
+ });
36
+ });
37
+ //# sourceMappingURL=hasPropertiesDefined.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hasPropertiesDefined.test.js","sourceRoot":"","sources":["../../src/hasPropertiesDefined/hasPropertiesDefined.test.ts"],"names":[],"mappings":";;;;;AAAA,gFAA0D;AAQ1D,QAAQ,CAAC,eAAe,EAAE;IACxB,EAAE,CAAC,oDAAoD,EAAE;QACvD,IAAM,GAAG,GAAQ;YACf,GAAG,EAAE,CAAC;YACN,GAAG,EAAE,IAAI;SACV,CAAC;QAEF,MAAM,CAAC,IAAA,8BAAoB,EAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IAC/D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gDAAgD,EAAE;QACnD,IAAM,GAAG,GAAQ;YACf,GAAG,EAAE,CAAC;YACN,GAAG,EAAE,IAAI;SACV,CAAC;QAEF,MAAM,CAAC,IAAA,8BAAoB,EAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IACvD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mDAAmD,EAAE;QACtD,IAAM,GAAG,GAAQ;YACf,GAAG,EAAE,CAAC;SACP,CAAC;QAEF,MAAM,CAAE,IAAA,8BAAoB,EAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IACtE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+DAA+D,EAAE;QAClE,IAAM,GAAG,GAAQ;YACf,GAAG,EAAE,CAAC;YACN,GAAG,EAAE,KAAK;YACV,GAAG,EAAE,EAAE;SACR,CAAC;QAEF,MAAM,CAAC,IAAA,8BAAoB,EAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACtE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import chunkBy from './chunkBy/chunkBy';
2
+ import remap, { Remap, ObjectMap } from './remap/remap';
3
+ import retryWrapper, { RetryConfig } from './retryWrapper/retryWrapper';
4
+ import fetchSsmParams from './fetchSsmParams/fetchSsmParams';
5
+ import S3Dao from './s3/s3';
6
+ import hasDefinedProperties from './hasPropertiesDefined/hasPropertiesDefined';
7
+ export { chunkBy, remap, Remap, ObjectMap, retryWrapper, RetryConfig, fetchSsmParams, S3Dao, hasDefinedProperties };
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
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.hasDefinedProperties = exports.S3Dao = exports.fetchSsmParams = exports.retryWrapper = exports.remap = exports.chunkBy = void 0;
7
+ var chunkBy_1 = __importDefault(require("./chunkBy/chunkBy"));
8
+ exports.chunkBy = chunkBy_1.default;
9
+ var remap_1 = __importDefault(require("./remap/remap"));
10
+ exports.remap = remap_1.default;
11
+ var retryWrapper_1 = __importDefault(require("./retryWrapper/retryWrapper"));
12
+ exports.retryWrapper = retryWrapper_1.default;
13
+ var fetchSsmParams_1 = __importDefault(require("./fetchSsmParams/fetchSsmParams"));
14
+ exports.fetchSsmParams = fetchSsmParams_1.default;
15
+ var s3_1 = __importDefault(require("./s3/s3"));
16
+ exports.S3Dao = s3_1.default;
17
+ var hasPropertiesDefined_1 = __importDefault(require("./hasPropertiesDefined/hasPropertiesDefined"));
18
+ exports.hasDefinedProperties = hasPropertiesDefined_1.default;
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAwC;AAQtC,kBARK,iBAAO,CAQL;AAPT,wDAAwD;AAQtD,gBARK,eAAK,CAQL;AAPP,6EAAwE;AAUtE,uBAVK,sBAAY,CAUL;AATd,mFAA6D;AAW3D,yBAXK,wBAAc,CAWL;AAVhB,+CAA4B;AAW1B,gBAXK,YAAK,CAWL;AAVP,qGAA+E;AAW7E,+BAXK,8BAAoB,CAWL"}
@@ -0,0 +1,147 @@
1
+ /** Turn a type like { foo: number } & { bar: string } into { foo: number; bar: string; } */
2
+ export type SimplifyIntersection<A> = A extends object ? {
3
+ [K in keyof A]: A[K] extends object ? SimplifyIntersection<A[K]> : A[K];
4
+ } : A;
5
+ /** A list of keys to keys, with an optional transformer function */
6
+ type ObjectMap = readonly ((readonly [string, string, ((...args: any[]) => any)?]))[];
7
+ /**
8
+ * Given a Key, a base Object and an ObjectMap, this will return the
9
+ * type of the property referencable by the Key on the base Object, or in
10
+ * the case where the supplied ObjectMap has a transformer function, it will
11
+ * return the return type of that function instead.
12
+ *
13
+ * @example without a transformer function
14
+ * ```
15
+ * const map = [ ['foo', 'bar'] ] as const;
16
+ *
17
+ * type Foo = GetKeyType<'foo', { foo: number }, (typeof map)[number]>;
18
+ * // ^ Foo = number
19
+ * ```
20
+ *
21
+ * @example with a transformer function
22
+ * ```
23
+ * const map = [ ['foo', 'bar', String] ] as const;
24
+ *
25
+ * type Foo = GetKeyType<'foo', { foo: number }, (typeof map)[number]>;
26
+ * // ^ Foo = string
27
+ * ```
28
+ */
29
+ type GetKeyType<Key extends string, O extends {
30
+ [key: string]: any;
31
+ }, M extends ObjectMap[number]> = M extends readonly [string, string, ((...args: any[]) => (infer U))] ? unknown extends U ? Key extends `${infer K}.${infer Rest}` ? GetKeyType<Rest, O[K], M> : O[Key] : U : Key extends `${infer K}.${infer Rest}` ? GetKeyType<Rest, O[K], M> : O[Key];
32
+ /**
33
+ * Given an ObjectMap, return a new ObjectMap with the first index of each
34
+ * tuple replaced with the value of the second index
35
+ */
36
+ type OverrideIndex<M extends ObjectMap[number], V extends string> = readonly [M[0], V, M[2]];
37
+ /**
38
+ * Given a key and a base object, return the type of the property referencable
39
+ * by the key on the base object
40
+ * @example
41
+ * ```
42
+ * type Foo = ConstructTypeFromPropertiesInternal<
43
+ * ['foo.bar', 'foo'],
44
+ * { foo: { bar: number } }
45
+ * >;
46
+ * // ^ Foo = { foo: number }
47
+ * ```
48
+ */
49
+ type ConstructTypeFromPropertiesInternal<M extends ObjectMap[number], O extends {
50
+ [key: string]: any;
51
+ }> = M[1] extends `${infer P}.${infer Rest}` ? {
52
+ [key in P]: ConstructTypeFromPropertiesInternal<OverrideIndex<M, Rest>, O>;
53
+ } : {
54
+ [key in M[1]]: GetKeyType<M[0], O, M>;
55
+ };
56
+ /**
57
+ * Utility type to get the length of a tuple type
58
+ * @example
59
+ * ```
60
+ * type Foo = Length<[null, null]>;
61
+ * // ^ Foo = 2
62
+ * ```
63
+ */
64
+ type Length<T extends any[] | readonly any[]> = T extends {
65
+ length: infer L;
66
+ } ? L : never;
67
+ /**
68
+ * Utility type to return a tuple type of a specified length
69
+ * @example
70
+ * ```
71
+ * type Foo = BuildTuple<3>;
72
+ * // ^ Foo = [any, any, any]
73
+ * ```
74
+ */
75
+ type BuildTuple<L extends number, T extends any[] = []> = T extends {
76
+ length: L;
77
+ } ? T : BuildTuple<L, [...T, any]>;
78
+ /**
79
+ * Utility type which adds two numbers together
80
+ * @example
81
+ * ```
82
+ * type Foo = Add<7, 3>;
83
+ * // ^ Foo = 10
84
+ * ```
85
+ */
86
+ type Add<A extends number, B extends number> = Length<[...BuildTuple<A>, ...BuildTuple<B>]>;
87
+ /**
88
+ * Given an object, and an array which remaps the keys of that object,
89
+ * return an object with the new structure
90
+ * @example
91
+ * ```
92
+ *
93
+ * type Foo = ConstructTypeFromProperties<
94
+ * [ ['foo.bar', 'foo'], ['baz', 'bar'] ],
95
+ * { foo: { bar: number }, baz: string; }
96
+ * >
97
+ * // Foo = {
98
+ * // foo: number;
99
+ * // } & {
100
+ * // bar: string;
101
+ * // }
102
+ * ```
103
+ */
104
+ type ConstructTypeFromProperties<M extends ObjectMap, O extends {
105
+ [key: string]: any;
106
+ }, L extends number = 0> = L extends Length<M> ? unknown : Add<L, 1> extends number ? ConstructTypeFromPropertiesInternal<M[L], O> & ConstructTypeFromProperties<M, O, Add<L, 1>> : never;
107
+ type Remap<MapArray extends ObjectMap, Original extends {
108
+ [key: string]: any;
109
+ }> = SimplifyIntersection<ConstructTypeFromProperties<MapArray, Original>>;
110
+ /**
111
+ * Map one object's values to another structure
112
+ * @param object the object to map from
113
+ * @param map the keys for the mapping
114
+ * @returns the remapped object
115
+ *
116
+ * @example without a transformer function
117
+ * ```ts
118
+ * const map = [
119
+ * ['foo', 'baz'],
120
+ * ['bar', 'qux.0']
121
+ * ] as const;
122
+ * const obj = { foo: 'hi', bar: 7 }
123
+ * remap(obj, map); // { baz: 'hi', qux: [7] }
124
+ * ```
125
+ * @example with a transformer function
126
+ * ```ts
127
+ * const map = [
128
+ * ['foo', 'baz'],
129
+ * ['bar', 'qux.0', (x: number) => x + 1]
130
+ * ] as const;
131
+ * const obj = { foo: 'hi', bar: 7 }
132
+ * remap(obj, map); // { baz: 'hi', qux: [8] }
133
+ * ```
134
+ * @example with an empty initial key
135
+ * ```ts
136
+ * const map = [
137
+ * ['', 'baz', (x: { foo: number, bar: number }) => x.foo + x.bar]
138
+ * ]
139
+ * const obj = { foo: 3, bar: 7 }
140
+ * remap(obj, map); // { baz: 10 }
141
+ * ```
142
+ */
143
+ declare function remap<Original extends {
144
+ [key: string]: any;
145
+ }, MapArray extends ObjectMap>(object: Original, map: MapArray): Remap<MapArray, Original>;
146
+ export { Remap, ObjectMap };
147
+ export default remap;