@trunkrs/common 1.5.14 → 1.6.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trunkrs/common",
3
- "version": "1.5.14",
3
+ "version": "1.6.0",
4
4
  "description": "Common standards library for development in Trunkrs",
5
5
  "types": "./index.d.ts",
6
6
  "repository": "https://github.com/Trunkrs/common.git",
@@ -33,7 +33,8 @@
33
33
  "jest": "^27.0.5",
34
34
  "prettier": "^2.1.2",
35
35
  "ts-jest": "^27.0.5",
36
- "typescript": "^4.2.4"
36
+ "typescript": "^4.2.4",
37
+ "redis": "^4.0.4"
37
38
  },
38
39
  "publishConfig": {
39
40
  "access": "public"
@@ -47,6 +48,7 @@
47
48
  "peerDependencies": {
48
49
  "@typescript-eslint/eslint-plugin": ">=4 || <5",
49
50
  "@typescript-eslint/parser": ">=4 || <5",
51
+ "redis": "^4.0.4",
50
52
  "aws-sdk": ">2.1000.0 || <3",
51
53
  "date-fns": ">=2 || <3",
52
54
  "eslint": ">=7.2.0",
@@ -0,0 +1,12 @@
1
+ import { RedisClientType } from 'redis';
2
+ import SetOptions from './types/SetOptions';
3
+ declare class RedisClient {
4
+ private readonly client;
5
+ constructor(client: RedisClientType);
6
+ set(key: string, value: string, options?: SetOptions): Promise<void>;
7
+ get(key: string): Promise<string | null>;
8
+ getOrSet(key: string, factory: () => Promise<string>, setOptions?: SetOptions): Promise<string>;
9
+ delete(...keys: string[]): Promise<void>;
10
+ clear(): Promise<void>;
11
+ }
12
+ export default RedisClient;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class RedisClient {
4
+ constructor(client) {
5
+ this.client = client;
6
+ }
7
+ async set(key, value, options) {
8
+ const commandOptions = {};
9
+ if (options === null || options === void 0 ? void 0 : options.expiresAt) {
10
+ Object.assign(commandOptions, {
11
+ PXAT: options.expiresAt.getTime(),
12
+ });
13
+ }
14
+ if (options === null || options === void 0 ? void 0 : options.setCondition) {
15
+ Object.assign(commandOptions, {
16
+ [options.setCondition]: true,
17
+ });
18
+ }
19
+ await this.client.connect();
20
+ await this.client.set(key, value, commandOptions);
21
+ await this.client.disconnect();
22
+ }
23
+ async get(key) {
24
+ await this.client.connect();
25
+ const item = await this.client.get(key);
26
+ await this.client.disconnect();
27
+ return item;
28
+ }
29
+ async getOrSet(key, factory, setOptions) {
30
+ await this.client.connect();
31
+ let value = await this.get(key);
32
+ if (!value) {
33
+ value = await factory();
34
+ await this.set(key, value, setOptions);
35
+ }
36
+ await this.client.disconnect();
37
+ return value;
38
+ }
39
+ async delete(...keys) {
40
+ await this.client.connect();
41
+ await this.client.del(keys);
42
+ await this.client.disconnect();
43
+ }
44
+ async clear() {
45
+ await this.client.connect();
46
+ await this.client.FLUSHALL();
47
+ await this.client.disconnect();
48
+ }
49
+ }
50
+ exports.default = RedisClient;
51
+ //# sourceMappingURL=RedisClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RedisClient.js","sourceRoot":"","sources":["../../../services/redis/RedisClient.ts"],"names":[],"mappings":";;AAIA,MAAM,WAAW;IACf,YAA6B,MAAuB;QAAvB,WAAM,GAAN,MAAM,CAAiB;IAAG,CAAC;IASjD,KAAK,CAAC,GAAG,CACd,GAAW,EACX,KAAa,EACb,OAAoB;QAEpB,MAAM,cAAc,GAAG,EAAE,CAAA;QAEzB,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,SAAS,EAAE;YACtB,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE;gBAE5B,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE;aAClC,CAAC,CAAA;SACH;QAED,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,EAAE;YACzB,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE;gBAC5B,CAAC,OAAO,CAAC,YAAY,CAAC,EAA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AM,CAAC,OAAO,EAAE,CAAA;QAC3B,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAA;QAC5B,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAA;IAChC,CAAC;CACF;AAED,kBAAe,WAAW,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { default as RedisClient } from './RedisClient';
2
+ export { default as SetCondition } from './types/SetCondition';
3
+ export { default as SetOptions } from './types/SetOptions';
@@ -0,0 +1,11 @@
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.SetCondition = exports.RedisClient = void 0;
7
+ var RedisClient_1 = require("./RedisClient");
8
+ Object.defineProperty(exports, "RedisClient", { enumerable: true, get: function () { return __importDefault(RedisClient_1).default; } });
9
+ var SetCondition_1 = require("./types/SetCondition");
10
+ Object.defineProperty(exports, "SetCondition", { enumerable: true, get: function () { return __importDefault(SetCondition_1).default; } });
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../services/redis/index.ts"],"names":[],"mappings":";;;;;;AAAA,6CAAsD;AAA7C,2HAAA,OAAO,OAAe;AAC/B,qDAA8D;AAArD,6HAAA,OAAO,OAAgB"}
@@ -0,0 +1,5 @@
1
+ declare enum SetCondition {
2
+ IfNotExists = "NX",
3
+ IfExists = "XX"
4
+ }
5
+ export default SetCondition;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var SetCondition;
4
+ (function (SetCondition) {
5
+ SetCondition["IfNotExists"] = "NX";
6
+ SetCondition["IfExists"] = "XX";
7
+ })(SetCondition || (SetCondition = {}));
8
+ exports.default = SetCondition;
9
+ //# sourceMappingURL=SetCondition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SetCondition.js","sourceRoot":"","sources":["../../../../services/redis/types/SetCondition.ts"],"names":[],"mappings":";;AAAA,IAAK,YAGJ;AAHD,WAAK,YAAY;IACf,kCAAkB,CAAA;IAClB,+BAAe,CAAA;AACjB,CAAC,EAHI,YAAY,KAAZ,YAAY,QAGhB;AAED,kBAAe,YAAY,CAAA"}
@@ -0,0 +1,6 @@
1
+ import SetCondition from './SetCondition';
2
+ interface SetOptions {
3
+ expiresAt?: Date;
4
+ setCondition?: SetCondition;
5
+ }
6
+ export default SetOptions;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=SetOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SetOptions.js","sourceRoot":"","sources":["../../../../services/redis/types/SetOptions.ts"],"names":[],"mappings":""}
@@ -0,0 +1,15 @@
1
+ import Cache from './Cache';
2
+ import RedisClient from '../../services/redis/RedisClient';
3
+ import { Serializer } from '../serialization';
4
+ declare class RedisCache extends Cache {
5
+ private readonly client;
6
+ private readonly serializer;
7
+ constructor(stalenessTimeout: number, client: RedisClient, serializer: Serializer);
8
+ add<TValue>(key: string, value: TValue): Promise<void>;
9
+ clear(): Promise<void>;
10
+ get<TValue>(key: string): Promise<TValue | null>;
11
+ getOrAdd<TValue>(key: string, factory: () => Promise<TValue>): Promise<TValue>;
12
+ hasKey(key: string): Promise<boolean>;
13
+ remove(key: string): Promise<void>;
14
+ }
15
+ export default RedisCache;
@@ -0,0 +1,48 @@
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
+ const Cache_1 = __importDefault(require("./Cache"));
7
+ class RedisCache extends Cache_1.default {
8
+ constructor(stalenessTimeout, client, serializer) {
9
+ super(stalenessTimeout);
10
+ this.client = client;
11
+ this.serializer = serializer;
12
+ }
13
+ async add(key, value) {
14
+ const item = this.createItem(value);
15
+ const serializedItem = this.serializer.serialize(item, 'string');
16
+ await this.client.set(key, serializedItem, { expiresAt: item.expiration });
17
+ }
18
+ async clear() {
19
+ await this.client.clear();
20
+ }
21
+ async get(key) {
22
+ const item = await this.client.get(key);
23
+ if (!item) {
24
+ return null;
25
+ }
26
+ const deserializedItem = this.serializer.deserialize(item);
27
+ return deserializedItem.value;
28
+ }
29
+ async getOrAdd(key, factory) {
30
+ const item = await this.client.get(key);
31
+ if (!item) {
32
+ const newValue = await factory();
33
+ await this.add(key, newValue);
34
+ return newValue;
35
+ }
36
+ const deserializedItem = this.serializer.deserialize(item);
37
+ return deserializedItem.value;
38
+ }
39
+ async hasKey(key) {
40
+ const item = await this.client.get(key);
41
+ return !!item;
42
+ }
43
+ async remove(key) {
44
+ await this.client.delete(key);
45
+ }
46
+ }
47
+ exports.default = RedisCache;
48
+ //# sourceMappingURL=RedisCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RedisCache.js","sourceRoot":"","sources":["../../../utils/caching/RedisCache.ts"],"names":[],"mappings":";;;;;AAAA,oDAA0C;AAI1C,MAAM,UAAW,SAAQ,eAAK;IAC5B,YACE,gBAAwB,EACP,MAAmB,EACnB,UAAsB;QAEvC,KAAK,CAAC,gBAAgB,CAAC,CAAA;QAHN,WAAM,GAAN,MAAM,CAAa;QACnB,eAAU,GAAV,UAAU,CAAY;IAGzC,CAAC;IAEM,KAAK,CAAC,GAAG,CAAS,GAAW,EAAE,KAAa;QACjD,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QACnC,MAAM,cAAc,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QAEhE,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,cAAc,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAA;IAC5E,CAAC;IAEM,KAAK,C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ache';
5
5
  export { default as SecretCache } from './SecretCache';
6
6
  export { default as S3Cache } from './S3Cache';
7
+ export { default as RedisCache } from './RedisCache';
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.S3Cache = exports.SecretCache = exports.MemoryCache = exports.GlobalAtomicCache = exports.FileCache = exports.Cache = void 0;
6
+ exports.RedisCache = exports.S3Cache = exports.SecretCache = exports.MemoryCache = exports.GlobalAtomicCache = exports.FileCache = exports.Cache = void 0;
7
7
  var Cache_1 = require("./Cache");
8
8
  Object.defineProperty(exports, "Cache", { enumerable: true, get: function () { return __importDefault(Cache_1).default; } });
9
9
  var FileCache_1 = require("./FileCache");
@@ -16,4 +16,6 @@ var SecretCache_1 = require("./SecretCache");
16
16
  Object.defineProperty(exports, "SecretCache", { enumerable: true, get: function () { return __importDefault(SecretCache_1).default; } });
17
17
  var S3Cache_1 = require("./S3Cache");
18
18
  Object.defineProperty(exports, "S3Cache", { enumerable: true, get: function () { return __importDefault(S3Cache_1).default; } });
19
+ var RedisCache_1 = require("./RedisCache");
20
+ Object.defineProperty(exports, "RedisCache", { enumerable: true, get: function () { return __importDefault(RedisCache_1).default; } });
19
21
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../utils/caching/index.ts"],"names":[],"mappings":";;;;;;AAAA,iCAA0C;AAAjC,+GAAA,OAAO,OAAS;AAEzB,yCAAkD;AAAzC,uHAAA,OAAO,OAAa;AAC7B,yDAAkE;AAAzD,uIAAA,OAAO,OAAqB;AACrC,6CAAsD;AAA7C,2HAAA,OAAO,OAAe;AAC/B,6CAAsD;AAA7C,2HAAA,OAAO,OAAe;AAC/B,qCAA8C;AAArC,mHAAA,OAAO,OAAW"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../utils/caching/index.ts"],"names":[],"mappings":";;;;;;AAAA,iCAA0C;AAAjC,+GAAA,OAAO,OAAS;AAEzB,yCAAkD;AAAzC,uHAAA,OAAO,OAAa;AAC7B,yDAAkE;AAAzD,uIAAA,OAAO,OAAqB;AACrC,6CAAsD;AAA7C,2HAAA,OAAO,OAAe;AAC/B,6CAAsD;AAA7C,2HAAA,OAAO,OAAe;AAC/B,qCAA8C;AAArC,mHAAA,OAAO,OAAW;AAC3B,2CAAoD;AAA3C,yHAAA,OAAO,OAAc"}