@tsed/platform-cache 7.0.0-alpha.1 → 7.0.0-beta.10

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 (48) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/cjs/decorators/useCache.js +11 -0
  3. package/lib/cjs/decorators/useCache.js.map +1 -0
  4. package/lib/cjs/index.js +13 -0
  5. package/lib/cjs/index.js.map +1 -0
  6. package/lib/cjs/interceptors/PlatformCacheInterceptor.js +150 -0
  7. package/lib/cjs/interceptors/PlatformCacheInterceptor.js.map +1 -0
  8. package/lib/cjs/interfaces/PlatformCacheOptions.js +3 -0
  9. package/lib/cjs/interfaces/PlatformCacheOptions.js.map +1 -0
  10. package/lib/cjs/interfaces/PlatformCachedObject.js +3 -0
  11. package/lib/cjs/interfaces/PlatformCachedObject.js.map +1 -0
  12. package/lib/cjs/interfaces/interfaces.js +3 -0
  13. package/lib/cjs/interfaces/interfaces.js.map +1 -0
  14. package/lib/cjs/package.json +3 -0
  15. package/lib/cjs/services/PlatformCache.js +147 -0
  16. package/lib/cjs/services/PlatformCache.js.map +1 -0
  17. package/lib/esm/decorators/useCache.js +7 -0
  18. package/lib/esm/decorators/useCache.js.map +1 -0
  19. package/lib/esm/index.js +10 -0
  20. package/lib/esm/index.js.map +1 -0
  21. package/lib/esm/interceptors/PlatformCacheInterceptor.js +147 -0
  22. package/lib/esm/interceptors/PlatformCacheInterceptor.js.map +1 -0
  23. package/lib/esm/interfaces/PlatformCacheOptions.js +2 -0
  24. package/lib/esm/interfaces/PlatformCacheOptions.js.map +1 -0
  25. package/lib/esm/interfaces/PlatformCachedObject.js +2 -0
  26. package/lib/esm/interfaces/PlatformCachedObject.js.map +1 -0
  27. package/lib/esm/interfaces/interfaces.js +2 -0
  28. package/lib/esm/interfaces/interfaces.js.map +1 -0
  29. package/lib/esm/package.json +3 -0
  30. package/lib/esm/services/PlatformCache.js +144 -0
  31. package/lib/esm/services/PlatformCache.js.map +1 -0
  32. package/lib/{decorators → types/decorators}/useCache.d.ts +0 -0
  33. package/lib/types/index.d.ts +9 -0
  34. package/lib/{interceptors → types/interceptors}/PlatformCacheInterceptor.d.ts +10 -2
  35. package/lib/{interfaces → types/interfaces}/PlatformCacheOptions.d.ts +2 -1
  36. package/lib/{interfaces → types/interfaces}/PlatformCachedObject.d.ts +0 -0
  37. package/lib/{interfaces/index.d.ts → types/interfaces/interfaces.d.ts} +6 -3
  38. package/lib/types/services/PlatformCache.d.ts +36 -0
  39. package/package.json +40 -22
  40. package/readme.md +2 -3
  41. package/tsconfig.compile.esm.json +12 -0
  42. package/lib/PlatformCacheModule.d.ts +0 -5
  43. package/lib/index.d.ts +0 -4
  44. package/lib/index.js +0 -348
  45. package/lib/index.js.map +0 -1
  46. package/lib/index.modern.js +0 -345
  47. package/lib/index.modern.js.map +0 -1
  48. package/lib/services/PlatformCache.d.ts +0 -26
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=PlatformCachedObject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformCachedObject.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformCachedObject.ts"],"names":[],"mappings":"","sourcesContent":["export interface PlatformCachedObject {\n ttl: number;\n args: any[];\n data: any;\n expires: number;\n headers: Record<string, number | string | string[]>;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import type {BaseContext} from \"@tsed/di\";\nimport type {Cache, CacheOptions, CachingConfig, Store} from \"cache-manager\";\nimport type {CacheManager} from \"../services/PlatformCache\";\n\nexport interface PlatformCacheSettings extends CacheOptions, CachingConfig {\n caches?: Cache[];\n store?:\n | \"memory\"\n | \"none\"\n | Store\n | {\n create(...args: any[]): Store;\n }\n | (() => any);\n max?: number;\n keyResolver?: (args: any[], ctx: BaseContext) => string;\n\n /**\n * You may pass in any other arguments these will be passed on to the `create` method of your store,\n * otherwise they will be ignored.\n */\n [key: string]: any;\n}\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace TsED {\n interface Configuration {\n cache?: PlatformCacheSettings | false;\n }\n }\n}\n\nexport interface OnCreateCacheManager {\n $onCreateCacheManager(cache: CacheManager): void | Promise<void>;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,144 @@
1
+ var _PlatformCache_cache;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet, __decorate, __metadata } from "tslib";
3
+ import { isClass, isFunction } from "@tsed/core";
4
+ import { Configuration, Inject, InjectorService, Module } from "@tsed/di";
5
+ import { deserialize, serialize } from "@tsed/json-mapper";
6
+ import { Logger } from "@tsed/logger";
7
+ const defaultKeyResolver = (args) => {
8
+ return args.map((arg) => (isClass(arg) ? JSON.stringify(serialize(arg)) : arg)).join(":");
9
+ };
10
+ /**
11
+ * @platform
12
+ */
13
+ let PlatformCache = class PlatformCache {
14
+ constructor() {
15
+ _PlatformCache_cache.set(this, void 0);
16
+ }
17
+ get cache() {
18
+ return __classPrivateFieldGet(this, _PlatformCache_cache, "f");
19
+ }
20
+ async $onInit() {
21
+ const settings = this.settings.get("cache");
22
+ if (settings) {
23
+ __classPrivateFieldSet(this, _PlatformCache_cache, await this.createCacheManager(settings), "f");
24
+ await this.injector.emit("$onCreateCacheManager", __classPrivateFieldGet(this, _PlatformCache_cache, "f"));
25
+ }
26
+ }
27
+ disabled() {
28
+ return !this.settings.get("cache");
29
+ }
30
+ defaultKeyResolver() {
31
+ return this.settings.get("cache.keyResolver", defaultKeyResolver);
32
+ }
33
+ defaultTtl() {
34
+ return this.settings.get("cache.ttl");
35
+ }
36
+ calculateTTL(result, currentTtl) {
37
+ const ttl = currentTtl === undefined ? this.defaultTtl() : currentTtl;
38
+ return isFunction(ttl) ? ttl(result) : ttl;
39
+ }
40
+ async ttl(key) {
41
+ if (this.cache && "store" in this.cache && this.cache.store.ttl) {
42
+ return this.cache.store.ttl(key);
43
+ }
44
+ }
45
+ wrap(key, fetch, options) {
46
+ var _a;
47
+ if (!this.cache) {
48
+ return fetch();
49
+ }
50
+ return (_a = this.cache) === null || _a === void 0 ? void 0 : _a.wrap(key, fetch, options);
51
+ }
52
+ async get(key, options = {}) {
53
+ var _a;
54
+ return deserialize((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(key), options);
55
+ }
56
+ async set(key, value, options) {
57
+ var _a;
58
+ return (_a = this.cache) === null || _a === void 0 ? void 0 : _a.set(key, value, options);
59
+ }
60
+ async getCachedObject(key) {
61
+ try {
62
+ return await this.get(key);
63
+ }
64
+ catch (er) {
65
+ this.logger.error({
66
+ event: "CACHE_ERROR",
67
+ method: "getCachedObject",
68
+ error: er
69
+ });
70
+ }
71
+ }
72
+ async setCachedObject(key, data, opts) {
73
+ try {
74
+ await this.set(key, {
75
+ ...opts,
76
+ data: JSON.stringify(data)
77
+ }, {
78
+ ttl: opts.ttl
79
+ });
80
+ }
81
+ catch (er) {
82
+ this.logger.error({
83
+ event: "CACHE_ERROR",
84
+ method: "setCachedObject",
85
+ error: er
86
+ });
87
+ }
88
+ }
89
+ async del(key) {
90
+ var _a;
91
+ await ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.del(key));
92
+ }
93
+ async reset() {
94
+ var _a;
95
+ // @ts-ignore
96
+ await ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.reset());
97
+ }
98
+ async keys(...args) {
99
+ if (this.cache && "store" in this.cache && this.cache.store.keys) {
100
+ return this.cache.store.keys(...args);
101
+ }
102
+ // istanbul ignore next
103
+ return [];
104
+ }
105
+ async getMatchingKeys(patterns) {
106
+ const [keys, { default: micromatch }] = await Promise.all([this.keys(), import("micromatch")]);
107
+ return micromatch(keys, patterns);
108
+ }
109
+ async deleteMatchingKeys(patterns) {
110
+ const keys = await this.getMatchingKeys(patterns);
111
+ await Promise.all(keys.map((key) => this.del(key)));
112
+ return keys;
113
+ }
114
+ async createCacheManager(settings) {
115
+ const { caches, store = "memory", ttl, ...props } = settings;
116
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
117
+ const { default: cacheManager } = await import("cache-manager");
118
+ return (caches === null || caches === void 0 ? void 0 : caches.length)
119
+ ? cacheManager.multiCaching(caches, { ...props })
120
+ : cacheManager.caching({
121
+ ...props,
122
+ ttl,
123
+ store: isFunction(store) ? await store() : store
124
+ });
125
+ }
126
+ };
127
+ _PlatformCache_cache = new WeakMap();
128
+ __decorate([
129
+ Configuration(),
130
+ __metadata("design:type", Object)
131
+ ], PlatformCache.prototype, "settings", void 0);
132
+ __decorate([
133
+ Inject(),
134
+ __metadata("design:type", InjectorService)
135
+ ], PlatformCache.prototype, "injector", void 0);
136
+ __decorate([
137
+ Inject(),
138
+ __metadata("design:type", Logger)
139
+ ], PlatformCache.prototype, "logger", void 0);
140
+ PlatformCache = __decorate([
141
+ Module()
142
+ ], PlatformCache);
143
+ export { PlatformCache };
144
+ //# sourceMappingURL=PlatformCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformCache.js","sourceRoot":"","sources":["../../../src/services/PlatformCache.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,OAAO,EAAE,UAAU,EAAC,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAC,aAAa,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AACxE,OAAO,EAAC,WAAW,EAA2B,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAIlF,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAEpC,MAAM,kBAAkB,GAAG,CAAC,IAAW,EAAE,EAAE;IACzC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,S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C;QAElD,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAE5D,OAAO,IAAI,CAAC;IACd,CAAC;IAES,KAAK,CAAC,kBAAkB,CAAC,QAA+B;QAChE,MAAM,EAAC,MAAM,EAAE,KAAK,GAAG,QAAQ,EAAE,GAAG,EAAE,GAAG,KAAK,EAAC,GAAG,QAAQ,CAAC;QAC3D,6DAA6D;QAC7D,MAAM,EAAC,OAAO,EAAE,YAAY,EAAC,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,CAAC;QAE9D,OAAO,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM;YACnB,CAAC,CAAC,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,EAAC,GAAG,KAAK,EAAC,CAAC;YAC/C,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC;gBACnB,GAAG,KAAK;gBACR,GAAG;gBACH,KAAK,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK;aACjD,CAAC,CAAC;IACT,CAAC;CACF,CAAA;;AA9IC;IADC,aAAa,EAAE;;+CACkB;AAGlC;IADC,MAAM,EAAE;8BACW,eAAe;+CAAC;AAGpC;IADC,MAAM,EAAE;8BACS,MAAM;6CAAC;AARd,aAAa;IADzB,MAAM,EAAE;GACI,aAAa,CAgJzB;SAhJY,aAAa","sourcesContent":["import {isClass, isFunction} from \"@tsed/core\";\nimport {Configuration, Inject, InjectorService, Module} from \"@tsed/di\";\nimport {deserialize, JsonDeserializerOptions, serialize} from \"@tsed/json-mapper\";\nimport type {Cache, CachingConfig, MultiCache, TtlFunction} from \"cache-manager\";\nimport {PlatformCachedObject} from \"../interfaces/PlatformCachedObject\";\nimport {PlatformCacheSettings} from \"../interfaces/interfaces\";\nimport {Logger} from \"@tsed/logger\";\n\nconst defaultKeyResolver = (args: any[]) => {\n return args.map((arg: any) => (isClass(arg) ? JSON.stringify(serialize(arg)) : arg)).join(\":\");\n};\n\nexport type CacheManager = Cache | MultiCache;\n\n/**\n * @platform\n */\n@Module()\nexport class PlatformCache {\n @Configuration()\n protected settings: Configuration;\n\n @Inject()\n protected injector: InjectorService;\n\n @Inject()\n protected logger: Logger;\n\n #cache: CacheManager | undefined;\n\n get cache() {\n return this.#cache;\n }\n\n async $onInit() {\n const settings = this.settings.get<PlatformCacheSettings>(\"cache\");\n\n if (settings) {\n this.#cache = await this.createCacheManager(settings);\n\n await this.injector.emit(\"$onCreateCacheManager\", this.#cache);\n }\n }\n\n disabled(): boolean {\n return !this.settings.get<PlatformCacheSettings>(\"cache\");\n }\n\n defaultKeyResolver() {\n return this.settings.get<(args: any[], ctx?: any) => string>(\"cache.keyResolver\", defaultKeyResolver);\n }\n\n defaultTtl() {\n return this.settings.get<number | TtlFunction>(\"cache.ttl\");\n }\n\n calculateTTL(result?: any, currentTtl?: number | TtlFunction) {\n const ttl = currentTtl === undefined ? this.defaultTtl() : currentTtl;\n\n return isFunction(ttl) ? ttl(result) : ttl;\n }\n\n async ttl(key: string) {\n if (this.cache && \"store\" in this.cache && this.cache.store.ttl) {\n return this.cache.store.ttl(key);\n }\n }\n\n wrap<T>(key: string, fetch: () => Promise<T>, options?: CachingConfig): Promise<T> {\n if (!this.cache) {\n return fetch();\n }\n\n return this.cache?.wrap<T>(key, fetch, options as any);\n }\n\n async get<T>(key: string, options: JsonDeserializerOptions = {}): Promise<T | undefined> {\n return deserialize(this.cache?.get<T>(key), options);\n }\n\n async set<T>(key: string, value: any, options?: CachingConfig): Promise<T | undefined> {\n return this.cache?.set<T>(key, value, options);\n }\n\n async getCachedObject(key: string) {\n try {\n return await this.get<PlatformCachedObject>(key);\n } catch (er) {\n this.logger.error({\n event: \"CACHE_ERROR\",\n method: \"getCachedObject\",\n error: er\n });\n }\n }\n\n async setCachedObject(key: string, data: any, opts: {ttl: number} & Record<string, any>) {\n try {\n await this.set<PlatformCachedObject>(\n key,\n {\n ...opts,\n data: JSON.stringify(data)\n },\n {\n ttl: opts.ttl\n }\n );\n } catch (er) {\n this.logger.error({\n event: \"CACHE_ERROR\",\n method: \"setCachedObject\",\n error: er\n });\n }\n }\n\n async del(key: string): Promise<void> {\n await this.cache?.del(key);\n }\n\n async reset(): Promise<void> {\n // @ts-ignore\n await this.cache?.reset();\n }\n\n async keys(...args: any[]): Promise<string[]> {\n if (this.cache && \"store\" in this.cache && this.cache.store.keys) {\n return this.cache.store.keys(...args);\n }\n\n // istanbul ignore next\n return [];\n }\n\n async getMatchingKeys(patterns: string): Promise<string[]> {\n const [keys, {default: micromatch}] = await Promise.all([this.keys(), import(\"micromatch\")]);\n\n return micromatch(keys, patterns);\n }\n\n async deleteMatchingKeys(patterns: string): Promise<string[]> {\n const keys = await this.getMatchingKeys(patterns);\n\n await Promise.all(keys.map((key: string) => this.del(key)));\n\n return keys;\n }\n\n protected async createCacheManager(settings: PlatformCacheSettings) {\n const {caches, store = \"memory\", ttl, ...props} = settings;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const {default: cacheManager} = await import(\"cache-manager\");\n\n return caches?.length\n ? cacheManager.multiCaching(caches, {...props})\n : cacheManager.caching({\n ...props,\n ttl,\n store: isFunction(store) ? await store() : store\n });\n }\n}\n"]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./decorators/useCache";
5
+ export * from "./interceptors/PlatformCacheInterceptor";
6
+ export * from "./interfaces/PlatformCacheOptions";
7
+ export * from "./interfaces/PlatformCachedObject";
8
+ export * from "./interfaces/interfaces";
9
+ export * from "./services/PlatformCache";
@@ -8,8 +8,15 @@ import { PlatformCache } from "../services/PlatformCache";
8
8
  export declare class PlatformCacheInterceptor implements InterceptorMethods {
9
9
  protected cache: PlatformCache;
10
10
  intercept(context: InterceptorContext<any, PlatformCacheOptions>, next: InterceptorNext): Promise<any>;
11
+ canRefreshInBackground(key: string, { refreshThreshold, ttl }: {
12
+ refreshThreshold?: number;
13
+ ttl: any;
14
+ }, next: Function): Promise<void>;
15
+ cacheMethod(context: InterceptorContext<any, PlatformCacheOptions>, next: InterceptorNext): Promise<any>;
16
+ cacheResponse(context: InterceptorContext<any, PlatformCacheOptions>, next: InterceptorNext): Promise<any>;
11
17
  protected getArgs(context: InterceptorContext<unknown, PlatformCacheOptions>): any;
12
18
  protected getOptions(context: InterceptorContext<any, PlatformCacheOptions>): {
19
+ refreshThreshold: number | undefined;
13
20
  ttl: number | import("cache-manager").TtlFunction | undefined;
14
21
  type: any;
15
22
  args: any;
@@ -17,7 +24,8 @@ export declare class PlatformCacheInterceptor implements InterceptorMethods {
17
24
  keyArgs: string;
18
25
  };
19
26
  protected isEndpoint({ target, propertyKey }: InterceptorContext<any, PlatformCacheOptions>): boolean;
20
- protected cacheMethod(context: InterceptorContext<any, PlatformCacheOptions>, next: InterceptorNext): Promise<any>;
21
- protected cacheResponse(context: InterceptorContext<any, PlatformCacheOptions>, next: InterceptorNext): Promise<any>;
27
+ protected hasKeyInQueue(key: string): Promise<boolean>;
28
+ protected addKeyToQueue(key: string): Promise<void>;
29
+ protected deleteKeyFromQueue(key: string): Promise<void>;
22
30
  protected sendResponse(cachedObject: PlatformCachedObject, $ctx: BaseContext): any;
23
31
  }
@@ -1,7 +1,8 @@
1
1
  import { MetadataTypes } from "@tsed/core";
2
2
  import { BaseContext } from "@tsed/di";
3
- import { TtlFunction } from "cache-manager";
3
+ import type { TtlFunction } from "cache-manager";
4
4
  export interface PlatformCacheOptions extends MetadataTypes {
5
5
  ttl?: number | TtlFunction;
6
6
  key?: string | ((args: any[], ctx?: BaseContext) => string);
7
+ refreshThreshold?: number;
7
8
  }
@@ -1,11 +1,11 @@
1
1
  import type { BaseContext } from "@tsed/di";
2
- import { Cache, CacheOptions, CachingConfig, Store } from "cache-manager";
3
- export * from "./PlatformCachedObject";
2
+ import type { Cache, CacheOptions, CachingConfig, Store } from "cache-manager";
3
+ import type { CacheManager } from "../services/PlatformCache";
4
4
  export interface PlatformCacheSettings extends CacheOptions, CachingConfig {
5
5
  caches?: Cache[];
6
6
  store?: "memory" | "none" | Store | {
7
7
  create(...args: any[]): Store;
8
- };
8
+ } | (() => any);
9
9
  max?: number;
10
10
  keyResolver?: (args: any[], ctx: BaseContext) => string;
11
11
  /**
@@ -21,3 +21,6 @@ declare global {
21
21
  }
22
22
  }
23
23
  }
24
+ export interface OnCreateCacheManager {
25
+ $onCreateCacheManager(cache: CacheManager): void | Promise<void>;
26
+ }
@@ -0,0 +1,36 @@
1
+ import { Configuration, InjectorService } from "@tsed/di";
2
+ import { JsonDeserializerOptions } from "@tsed/json-mapper";
3
+ import type { Cache, CachingConfig, MultiCache, TtlFunction } from "cache-manager";
4
+ import { PlatformCachedObject } from "../interfaces/PlatformCachedObject";
5
+ import { PlatformCacheSettings } from "../interfaces/interfaces";
6
+ import { Logger } from "@tsed/logger";
7
+ export declare type CacheManager = Cache | MultiCache;
8
+ /**
9
+ * @platform
10
+ */
11
+ export declare class PlatformCache {
12
+ #private;
13
+ protected settings: Configuration;
14
+ protected injector: InjectorService;
15
+ protected logger: Logger;
16
+ get cache(): CacheManager | undefined;
17
+ $onInit(): Promise<void>;
18
+ disabled(): boolean;
19
+ defaultKeyResolver(): (args: any[], ctx?: any) => string;
20
+ defaultTtl(): number | TtlFunction;
21
+ calculateTTL(result?: any, currentTtl?: number | TtlFunction): number;
22
+ ttl(key: string): Promise<any>;
23
+ wrap<T>(key: string, fetch: () => Promise<T>, options?: CachingConfig): Promise<T>;
24
+ get<T>(key: string, options?: JsonDeserializerOptions): Promise<T | undefined>;
25
+ set<T>(key: string, value: any, options?: CachingConfig): Promise<T | undefined>;
26
+ getCachedObject(key: string): Promise<PlatformCachedObject | undefined>;
27
+ setCachedObject(key: string, data: any, opts: {
28
+ ttl: number;
29
+ } & Record<string, any>): Promise<void>;
30
+ del(key: string): Promise<void>;
31
+ reset(): Promise<void>;
32
+ keys(...args: any[]): Promise<string[]>;
33
+ getMatchingKeys(patterns: string): Promise<string[]>;
34
+ deleteMatchingKeys(patterns: string): Promise<string[]>;
35
+ protected createCacheManager(settings: PlatformCacheSettings): Promise<MultiCache>;
36
+ }
package/package.json CHANGED
@@ -1,45 +1,63 @@
1
1
  {
2
2
  "name": "@tsed/platform-cache",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-beta.10",
4
4
  "description": "Platform cache module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
7
- "main": "./lib/index.js",
8
- "typings": "./lib/index.d.ts",
7
+ "main": "./lib/cjs/index.js",
8
+ "module": "./lib/esm/index.js",
9
+ "typings": "./lib/types/index.d.ts",
9
10
  "exports": {
10
- "require": "./lib/index.js",
11
- "default": "./lib/index.modern.js"
11
+ "types": "./lib/types/index.d.ts",
12
+ "import": "./lib/esm/index.js",
13
+ "require": "./lib/cjs/index.js",
14
+ "default": "./lib/esm/index.js"
12
15
  },
13
16
  "scripts": {
14
- "build": "microbundle --target node --no-compress --format modern,cjs --tsconfig ./tsconfig.compile.json"
17
+ "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
18
+ "build:cjs": "tsc --build tsconfig.compile.json",
19
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
20
+ "test": "cross-env NODE_ENV=test yarn jest --max-workers=2",
21
+ "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
15
22
  },
16
23
  "dependencies": {
17
- "@types/cache-manager": "^3.4.2",
18
- "cache-manager": "^3.4.1",
19
- "micromatch": "^4.0.2",
20
- "tslib": "2.2.0"
24
+ "@types/cache-manager": "^3.4.3",
25
+ "cache-manager": "^3.6.0",
26
+ "micromatch": "4.0.5",
27
+ "tslib": "2.3.1"
21
28
  },
22
29
  "devDependencies": {
23
- "@tsed/core": "7.0.0-alpha.1",
24
- "@tsed/di": "7.0.0-alpha.1",
25
- "@tsed/exceptions": "7.0.0-alpha.1",
26
- "@tsed/json-mapper": "7.0.0-alpha.1",
27
- "@tsed/schema": "7.0.0-alpha.1",
28
- "@types/cache-manager": "^3.4.2",
30
+ "@tsed/core": "7.0.0-beta.10",
31
+ "@tsed/di": "7.0.0-beta.10",
32
+ "@tsed/json-mapper": "7.0.0-beta.10",
33
+ "@tsed/schema": "7.0.0-beta.10",
29
34
  "@types/micromatch": "^4.0.1"
30
35
  },
31
36
  "peerDependencies": {
32
- "@tsed/core": "^7.0.0-alpha.1",
33
- "@tsed/di": "^7.0.0-alpha.1",
34
- "@tsed/exceptions": "^7.0.0-alpha.1",
35
- "@tsed/json-mapper": "^7.0.0-alpha.1",
36
- "@tsed/schema": "^7.0.0-alpha.1"
37
+ "@tsed/core": "^7.0.0-beta.10",
38
+ "@tsed/di": "^7.0.0-beta.10",
39
+ "@tsed/json-mapper": "^7.0.0-beta.10",
40
+ "@tsed/schema": "^7.0.0-beta.10"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "@tsed/core": {
44
+ "optional": false
45
+ },
46
+ "@tsed/di": {
47
+ "optional": false
48
+ },
49
+ "@tsed/json-mapper": {
50
+ "optional": false
51
+ },
52
+ "@tsed/schema": {
53
+ "optional": false
54
+ }
37
55
  },
38
56
  "repository": "https://github.com/tsedio/tsed",
39
57
  "bugs": {
40
58
  "url": "https://github.com/tsedio/tsed/issues"
41
59
  },
42
- "homepage": "https://github.com/tsedio/tsed/tree/alpha/packages/platform/platform-cache",
60
+ "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-cache",
43
61
  "author": "Romain Lenzotti",
44
62
  "license": "MIT"
45
63
  }
package/readme.md CHANGED
@@ -27,7 +27,7 @@
27
27
 
28
28
  <hr />
29
29
 
30
- A package of Ts.ED framework. See website: https://tsed.io/
30
+ A package of Ts.ED framework. See website: https://tsed.io/docs/cache.html
31
31
 
32
32
  # Installation
33
33
 
@@ -36,18 +36,17 @@ npm install --save @tsed/platform-cache
36
36
  ```
37
37
 
38
38
  ## Contributors
39
+
39
40
  Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html).
40
41
 
41
42
  <a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
42
43
 
43
-
44
44
  ## Backers
45
45
 
46
46
  Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
47
47
 
48
48
  <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
49
49
 
50
-
51
50
  ## Sponsors
52
51
 
53
52
  Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./tsconfig.compile.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types"
10
+ },
11
+ "exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
12
+ }
@@ -1,5 +0,0 @@
1
- /**
2
- * @ignore
3
- */
4
- export declare class PlatformCacheModule {
5
- }
package/lib/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from "./interfaces";
2
- export * from "./services/PlatformCache";
3
- export * from "./decorators/useCache";
4
- export * from "./PlatformCacheModule";