@creator.co/wapi 1.2.0-alpha1 → 1.2.0-alpha2

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.
package/README.md CHANGED
@@ -6,3 +6,4 @@ TODOs:
6
6
  - js-docs
7
7
  - readme docs
8
8
  - improve types
9
+ - cleanup deps
package/dist/index.d.ts CHANGED
@@ -3,8 +3,8 @@ import Utils from "./src/API/Utils";
3
3
  import EventProcessor from "./src/BaseEvent/EventProcessor";
4
4
  import Process from "./src/BaseEvent/Process";
5
5
  import Transaction from "./src/BaseEvent/Transaction";
6
+ import Configuration from "./src/Config/Configuration";
6
7
  import Crypto from "./src/Crypto/Crypto";
7
8
  import Mailer from "./src/Mailer/Mailer";
8
- import Publisher from "./src/Publisher/Publisher";
9
9
  import Router from "./src/Server/Router";
10
- export { Transaction, Process, EventProcessor, Router, Publisher, Mailer, Crypto, Response, Utils, };
10
+ export { Transaction, Process, EventProcessor, Router, Mailer, Crypto, Configuration, Response, Utils, };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Utils = exports.Response = exports.Crypto = exports.Mailer = exports.Publisher = exports.Router = exports.EventProcessor = exports.Process = exports.Transaction = void 0;
3
+ exports.Utils = exports.Response = exports.Configuration = exports.Crypto = exports.Mailer = exports.Router = exports.EventProcessor = exports.Process = exports.Transaction = void 0;
4
4
  var Response_1 = require("./src/API/Response");
5
5
  exports.Response = Response_1.default;
6
6
  var Utils_1 = require("./src/API/Utils");
@@ -11,12 +11,12 @@ var Process_1 = require("./src/BaseEvent/Process");
11
11
  exports.Process = Process_1.default;
12
12
  var Transaction_1 = require("./src/BaseEvent/Transaction");
13
13
  exports.Transaction = Transaction_1.default;
14
+ var Configuration_1 = require("./src/Config/Configuration");
15
+ exports.Configuration = Configuration_1.default;
14
16
  var Crypto_1 = require("./src/Crypto/Crypto");
15
17
  exports.Crypto = Crypto_1.default;
16
18
  var Mailer_1 = require("./src/Mailer/Mailer");
17
19
  exports.Mailer = Mailer_1.default;
18
- var Publisher_1 = require("./src/Publisher/Publisher");
19
- exports.Publisher = Publisher_1.default;
20
20
  var Router_1 = require("./src/Server/Router");
21
21
  exports.Router = Router_1.default;
22
22
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,+CAAyC;AAqBvC,mBArBK,kBAAQ,CAqBL;AApBV,yCAAmC;AAsBjC,gBAtBK,eAAK,CAsBL;AArBP,iEAA2D;AAYzD,yBAZK,wBAAc,CAYL;AAXhB,mDAA6C;AAU3C,kBAVK,iBAAO,CAUL;AATT,2DAAqD;AAQnD,sBARK,qBAAW,CAQL;AAPb,8CAAwC;AActC,iBAdK,gBAAM,CAcL;AAbR,8CAAwC;AAYtC,iBAZK,gBAAM,CAYL;AAXR,uDAAiD;AAU/C,oBAVK,mBAAS,CAUL;AATX,8CAAwC;AAOtC,iBAPK,gBAAM,CAOL"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,+CAAyC;AAqBvC,mBArBK,kBAAQ,CAqBL;AApBV,yCAAmC;AAsBjC,gBAtBK,eAAK,CAsBL;AArBP,iEAA2D;AAYzD,yBAZK,wBAAc,CAYL;AAXhB,mDAA6C;AAU3C,kBAVK,iBAAO,CAUL;AATT,2DAAqD;AAQnD,sBARK,qBAAW,CAQL;AAPb,4DAAsD;AAcpD,wBAdK,uBAAa,CAcL;AAbf,8CAAwC;AAYtC,iBAZK,gBAAM,CAYL;AAXR,8CAAwC;AAUtC,iBAVK,gBAAM,CAUL;AATR,8CAAwC;AAOtC,iBAPK,gBAAM,CAOL"}
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creator.co/wapi",
3
- "version": "1.2.0-alpha1",
3
+ "version": "1.2.0-alpha2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,6 +27,8 @@
27
27
  "email-templates": "^11.1.1",
28
28
  "express": "^4.18.2",
29
29
  "json-stringify-safe": "^5.0.1",
30
+ "node-cache": "^5.1.2",
31
+ "parse-duration": "^1.1.0",
30
32
  "sha1": "^1.1.1",
31
33
  "stack-trace": "0.0.10",
32
34
  "unflatten": "^1.0.4",
@@ -1,9 +1,7 @@
1
- import Logger, { LoggerConfig } from "../Logger/Logger";
2
- import Publisher, { PublisherConfig } from "../Publisher/Publisher";
3
- export type ProcessConfig = {
4
- logger?: LoggerConfig;
5
- publisher?: PublisherConfig;
6
- };
1
+ import { TransactionConfig } from "./Transaction";
2
+ import Logger from "../Logger/Logger";
3
+ import Publisher from "../Publisher/Publisher";
4
+ export type ProcessConfig = Omit<TransactionConfig, "throwOnErrors" | "syncReturn">;
7
5
  export default class Process {
8
6
  private _interval;
9
7
  readonly logger: Logger;
@@ -1 +1 @@
1
- {"version":3,"file":"Process.js","sourceRoot":"","sources":["../../../src/BaseEvent/Process.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAuD;AACvD,oDAAmE;AAQnE;IAKE,iBAAY,MAAqB,EAAE,QAAgB;QACjD,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;QACzB,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAA;QAC/D,IAAI,CAAC,SAAS,GAAG,IAAI,mBAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IAClD,CAAC;IAED,gBAAgB;IACV,yBAAO,GAAb,UAAc,aAAa;;;;gBACzB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;gBAC/C,YAAY;gBACZ,wCAAwC;gBACxC,cAAc;gBACd,WAAW,CAAC;;;oCACV,qBAAM,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAA;;gCAAlC,SAAkC,CAAA;;;;qBACnC,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;;;;KACnB;IACD,YAAY;IACN,0BAAQ,GAAd,UAAe,aAAa;;;;;;wBACtB,eAAe,GAAG,IAAI,CAAC,wBAAwB;wBAAzB,CAAA;;;;wBAGxB,sBAAsB;wBACtB,iDAAiD;wBACjD,SAAS;wBACT,qBAAM,aAAa,CAAC,IAAI,CAAC;4BACzB,WAAW;4BACX,uCAAuC;4BACvC,EAAE;0BAHuB;;wBAHzB,sBAAsB;wBACtB,iDAAiD;wBACjD,SAAS;wBACT,SAAyB,CAAA;wBACzB,WAAW;wBACX,uCAAuC;wBACvC,EAAE;wBACF,eAAe,GAAG,KAAK,CAAA;;;;wBAEvB,kBAAkB;wBAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CACf,8DAA8D,CAC/D,CAAA;wBACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAC,CAAC,CAAA;;4BAI1B,sBAAO,eAAe,EAAA;;;;KACvB;IACH,cAAC;AAAD,CAAC,AA7CD,IA6CC"}
1
+ {"version":3,"file":"Process.js","sourceRoot":"","sources":["../../../src/BaseEvent/Process.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2CAAqC;AACrC,oDAA8C;AAQ9C;IAKE,iBAAY,MAAqB,EAAE,QAAgB;QACjD,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;QACzB,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAA;QAC/D,IAAI,CAAC,SAAS,GAAG,IAAI,mBAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IAClD,CAAC;IAED,gBAAgB;IACV,yBAAO,GAAb,UAAc,aAAa;;;;gBACzB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;gBAC/C,YAAY;gBACZ,wCAAwC;gBACxC,cAAc;gBACd,WAAW,CAAC;;;oCACV,qBAAM,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAA;;gCAAlC,SAAkC,CAAA;;;;qBACnC,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;;;;KACnB;IACD,YAAY;IACN,0BAAQ,GAAd,UAAe,aAAa;;;;;;wBACtB,eAAe,GAAG,IAAI,CAAC,wBAAwB;wBAAzB,CAAA;;;;wBAGxB,sBAAsB;wBACtB,iDAAiD;wBACjD,SAAS;wBACT,qBAAM,aAAa,CAAC,IAAI,CAAC;4BACzB,WAAW;4BACX,uCAAuC;4BACvC,EAAE;0BAHuB;;wBAHzB,sBAAsB;wBACtB,iDAAiD;wBACjD,SAAS;wBACT,SAAyB,CAAA;wBACzB,WAAW;wBACX,uCAAuC;wBACvC,EAAE;wBACF,eAAe,GAAG,KAAK,CAAA;;;;wBAEvB,kBAAkB;wBAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CACf,8DAA8D,CAC/D,CAAA;wBACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAC,CAAC,CAAA;;4BAI1B,sBAAO,eAAe,EAAA;;;;KACvB;IACH,cAAC;AAAD,CAAC,AA7CD,IA6CC"}
@@ -0,0 +1,28 @@
1
+ import { EnvironmentType } from "./EnvironmentVar";
2
+ export type ConfigurationSchema = {
3
+ [name: string]: {
4
+ type: EnvironmentType;
5
+ required?: boolean;
6
+ cachingPolicy?: string;
7
+ };
8
+ };
9
+ type ExtractRemote<Type> = keyof {
10
+ [Property in keyof Type]: Type[Property] extends {
11
+ type: EnvironmentType.PlainRemote | EnvironmentType.SecureRemote;
12
+ } ? Property : never;
13
+ };
14
+ type ExtractLocal<Type> = keyof {
15
+ [Property in keyof Type]: Type[Property] extends {
16
+ type: EnvironmentType.Local;
17
+ } ? Property : never;
18
+ };
19
+ export default class Configuration<T extends ConfigurationSchema> {
20
+ private readonly schema;
21
+ private readonly remotePrefix;
22
+ constructor(schema: T, remotePrefix: string);
23
+ get(propName: ExtractLocal<T>): any;
24
+ asyncGet(propName: ExtractRemote<T>): Promise<any>;
25
+ private getCachedValue;
26
+ private cacheValue;
27
+ }
28
+ export {};
@@ -0,0 +1,93 @@
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 MemCache = require("node-cache");
40
+ var DurationParser = require("parse-duration");
41
+ var EnvironmentVar_1 = require("./EnvironmentVar");
42
+ // Hold cached values at nodeVM level
43
+ var cacheStore = new MemCache();
44
+ var Configuration = /** @class */ (function () {
45
+ function Configuration(schema, remotePrefix) {
46
+ this.schema = schema;
47
+ this.remotePrefix = remotePrefix;
48
+ }
49
+ Configuration.prototype.get = function (propName) {
50
+ propName = propName; // TODO: find a better way, this seems to come from keyof
51
+ var propSchema = this.schema[propName];
52
+ var v = this.getCachedValue(propName);
53
+ v =
54
+ v ||
55
+ new EnvironmentVar_1.default(propName, propSchema.type, propSchema.required, this.remotePrefix).syncResolve();
56
+ this.cacheValue(propName, v, propSchema.cachingPolicy);
57
+ return v;
58
+ };
59
+ Configuration.prototype.asyncGet = function (propName) {
60
+ return __awaiter(this, void 0, void 0, function () {
61
+ var propSchema, v, _a;
62
+ return __generator(this, function (_b) {
63
+ switch (_b.label) {
64
+ case 0:
65
+ propName = propName; // TODO: find a better way, this seems to come from keyof
66
+ propSchema = this.schema[propName];
67
+ v = this.getCachedValue(propName);
68
+ _a = v;
69
+ if (_a) return [3 /*break*/, 2];
70
+ return [4 /*yield*/, new EnvironmentVar_1.default(propName, propSchema.type, propSchema.required, this.remotePrefix).resolve()];
71
+ case 1:
72
+ _a = (_b.sent());
73
+ _b.label = 2;
74
+ case 2:
75
+ v = _a;
76
+ this.cacheValue(propName, v, propSchema.cachingPolicy);
77
+ return [2 /*return*/, v];
78
+ }
79
+ });
80
+ });
81
+ };
82
+ // caching layer
83
+ Configuration.prototype.getCachedValue = function (valueKey) {
84
+ return cacheStore.get(valueKey);
85
+ };
86
+ Configuration.prototype.cacheValue = function (valueKey, value, policy) {
87
+ if (policy === void 0) { policy = "1d"; }
88
+ cacheStore.set(valueKey, value, DurationParser(policy, "s"));
89
+ };
90
+ return Configuration;
91
+ }());
92
+ exports.default = Configuration;
93
+ //# sourceMappingURL=Configuration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Configuration.js","sourceRoot":"","sources":["../../../src/Config/Configuration.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAsC;AACtC,+CAAgD;AAEhD,mDAAkE;AAElE,qCAAqC;AACrC,IAAM,UAAU,GAAG,IAAI,QAAQ,EAAE,CAAA;AA2BjC;IAGE,uBAAY,MAAS,EAAE,YAAoB;QACzC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAA;IAClC,CAAC;IAEM,2BAAG,GAAV,UAAW,QAAyB;QAClC,QAAQ,GAAG,QAAkB,CAAA,CAAC,yDAAyD;QACvF,IAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;QACxC,IAAI,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAA;QACrC,CAAC;YACC,CAAC;gBACD,IAAI,wBAAc,CAChB,QAAQ,EACR,UAAU,CAAC,IAAI,EACf,UAAU,CAAC,QAAQ,EACnB,IAAI,CAAC,YAAY,CAClB,CAAC,WAAW,EAAE,CAAA;QACjB,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,EAAE,UAAU,CAAC,aAAa,CAAC,CAAA;QACtD,OAAO,CAAC,CAAA;IACV,CAAC;IACY,gCAAQ,GAArB,UAAsB,QAA0B;;;;;;wBAC9C,QAAQ,GAAG,QAAkB,CAAA,CAAC,yDAAyD;wBACjF,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;wBACpC,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAA;wBAEnC,KAAA,CAAC,CAAA;gCAAD,wBAAC;wBACA,qBAAM,IAAI,wBAAc,CACvB,QAAQ,EACR,UAAU,CAAC,IAAI,EACf,UAAU,CAAC,QAAQ,EACnB,IAAI,CAAC,YAAY,CAClB,CAAC,OAAO,EAAE,EAAA;;wBALX,KAAA,CAAC,SAKU,CAAC,CAAA;;;wBAPd,CAAC,KAOa,CAAA;wBACd,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,EAAE,UAAU,CAAC,aAAa,CAAC,CAAA;wBACtD,sBAAO,CAAC,EAAA;;;;KACT;IACD,gBAAgB;IACR,sCAAc,GAAtB,UAAuB,QAAgB;QACrC,OAAO,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IACjC,CAAC;IACO,kCAAU,GAAlB,UACE,QAAgB,EAChB,KAAU,EACV,MAAqB;QAArB,uBAAA,EAAA,aAAqB;QAErB,UAAU,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,cAAc,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;IAC9D,CAAC;IACH,oBAAC;AAAD,CAAC,AAjDD,IAiDC"}
package/index.ts CHANGED
@@ -3,23 +3,23 @@ import Utils from "./src/API/Utils"
3
3
  import EventProcessor from "./src/BaseEvent/EventProcessor"
4
4
  import Process from "./src/BaseEvent/Process"
5
5
  import Transaction from "./src/BaseEvent/Transaction"
6
+ import Configuration from "./src/Config/Configuration"
6
7
  import Crypto from "./src/Crypto/Crypto"
7
8
  import Mailer from "./src/Mailer/Mailer"
8
- import Publisher from "./src/Publisher/Publisher"
9
9
  import Router from "./src/Server/Router"
10
10
 
11
11
  export {
12
- //Base Events
12
+ // Base Events
13
13
  Transaction,
14
14
  Process,
15
15
  EventProcessor,
16
16
  Router,
17
- //Client
18
- Publisher,
17
+ // Externally initialized clients
19
18
  Mailer,
20
19
  Crypto,
21
- //Responses
20
+ Configuration,
21
+ // Responses
22
22
  Response,
23
- //Helpers
23
+ // Helpers
24
24
  Utils,
25
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creator.co/wapi",
3
- "version": "1.2.0-alpha1",
3
+ "version": "1.2.0-alpha2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,6 +27,8 @@
27
27
  "email-templates": "^11.1.1",
28
28
  "express": "^4.18.2",
29
29
  "json-stringify-safe": "^5.0.1",
30
+ "node-cache": "^5.1.2",
31
+ "parse-duration": "^1.1.0",
30
32
  "sha1": "^1.1.1",
31
33
  "stack-trace": "0.0.10",
32
34
  "unflatten": "^1.0.4",
@@ -1,11 +1,12 @@
1
- import Logger, { LoggerConfig } from "../Logger/Logger"
2
- import Publisher, { PublisherConfig } from "../Publisher/Publisher"
1
+ import { TransactionConfig } from "./Transaction"
2
+ import Logger from "../Logger/Logger"
3
+ import Publisher from "../Publisher/Publisher"
3
4
 
4
5
  // Config
5
- export type ProcessConfig = {
6
- logger?: LoggerConfig
7
- publisher?: PublisherConfig
8
- }
6
+ export type ProcessConfig = Omit<
7
+ TransactionConfig,
8
+ "throwOnErrors" | "syncReturn"
9
+ >
9
10
 
10
11
  export default class Process {
11
12
  private _interval: number
@@ -0,0 +1,83 @@
1
+ import * as MemCache from "node-cache"
2
+ import * as DurationParser from "parse-duration"
3
+
4
+ import EnvironmentVar, { EnvironmentType } from "./EnvironmentVar"
5
+
6
+ // Hold cached values at nodeVM level
7
+ const cacheStore = new MemCache()
8
+
9
+ export type ConfigurationSchema = {
10
+ [name: string]: {
11
+ type: EnvironmentType
12
+ required?: boolean
13
+ cachingPolicy?: string
14
+ }
15
+ }
16
+
17
+ // runtime type infer to ConfigurationSchema keys, which type is remote
18
+ type ExtractRemote<Type> = keyof {
19
+ [Property in keyof Type]: Type[Property] extends {
20
+ type: EnvironmentType.PlainRemote | EnvironmentType.SecureRemote
21
+ }
22
+ ? Property
23
+ : never
24
+ }
25
+ // runtime type infer to ConfigurationSchema keys, which type is local
26
+ type ExtractLocal<Type> = keyof {
27
+ [Property in keyof Type]: Type[Property] extends {
28
+ type: EnvironmentType.Local
29
+ }
30
+ ? Property
31
+ : never
32
+ }
33
+
34
+ export default class Configuration<T extends ConfigurationSchema> {
35
+ private readonly schema: T
36
+ private readonly remotePrefix: string
37
+ constructor(schema: T, remotePrefix: string) {
38
+ this.schema = schema
39
+ this.remotePrefix = remotePrefix
40
+ }
41
+
42
+ public get(propName: ExtractLocal<T>): any {
43
+ propName = propName as string // TODO: find a better way, this seems to come from keyof
44
+ const propSchema = this.schema[propName]
45
+ let v = this.getCachedValue(propName)
46
+ v =
47
+ v ||
48
+ new EnvironmentVar<string>(
49
+ propName,
50
+ propSchema.type,
51
+ propSchema.required,
52
+ this.remotePrefix,
53
+ ).syncResolve()
54
+ this.cacheValue(propName, v, propSchema.cachingPolicy)
55
+ return v
56
+ }
57
+ public async asyncGet(propName: ExtractRemote<T>): Promise<any> {
58
+ propName = propName as string // TODO: find a better way, this seems to come from keyof
59
+ const propSchema = this.schema[propName]
60
+ let v = this.getCachedValue(propName)
61
+ v =
62
+ v ||
63
+ (await new EnvironmentVar<string>(
64
+ propName,
65
+ propSchema.type,
66
+ propSchema.required,
67
+ this.remotePrefix,
68
+ ).resolve())
69
+ this.cacheValue(propName, v, propSchema.cachingPolicy)
70
+ return v
71
+ }
72
+ // caching layer
73
+ private getCachedValue(valueKey: string): any {
74
+ return cacheStore.get(valueKey)
75
+ }
76
+ private cacheValue(
77
+ valueKey: string,
78
+ value: any,
79
+ policy: string = "1d",
80
+ ): void {
81
+ cacheStore.set(valueKey, value, DurationParser(policy, "s"))
82
+ }
83
+ }