@kovalenko/http-request-cache 0.1.11 → 1.2.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/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +74 -1
- package/{esm2015/kovalenko-http-request-cache.js → esm2020/kovalenko-http-request-cache.mjs} +4 -4
- package/{esm2015/lib/default-storage.js → esm2020/lib/default-storage.mjs} +15 -15
- package/esm2020/lib/http-cache-options.mjs +2 -0
- package/{esm2015/lib/http-cache-storage.js → esm2020/lib/http-cache-storage.mjs} +2 -2
- package/esm2020/lib/http-request-cache.decorator.mjs +59 -0
- package/esm2020/lib/request-times.mjs +15 -0
- package/{esm2015/public-api.js → esm2020/public-api.mjs} +7 -7
- package/fesm2015/kovalenko-http-request-cache.mjs +99 -0
- package/fesm2015/kovalenko-http-request-cache.mjs.map +1 -0
- package/fesm2020/kovalenko-http-request-cache.mjs +98 -0
- package/fesm2020/kovalenko-http-request-cache.mjs.map +1 -0
- package/{kovalenko-http-request-cache.d.ts → index.d.ts} +5 -4
- package/lib/default-storage.d.ts +8 -8
- package/lib/http-cache-options.d.ts +9 -7
- package/lib/http-cache-storage.d.ts +6 -6
- package/lib/http-request-cache.decorator.d.ts +5 -5
- package/lib/request-times.d.ts +8 -0
- package/package.json +25 -13
- package/public-api.d.ts +4 -4
- package/bundles/kovalenko-http-request-cache.umd.js +0 -384
- package/bundles/kovalenko-http-request-cache.umd.js.map +0 -1
- package/bundles/kovalenko-http-request-cache.umd.min.js +0 -16
- package/bundles/kovalenko-http-request-cache.umd.min.js.map +0 -1
- package/esm2015/lib/http-cache-options.js +0 -2
- package/esm2015/lib/http-request-cache.decorator.js +0 -33
- package/fesm2015/kovalenko-http-request-cache.js +0 -58
- package/fesm2015/kovalenko-http-request-cache.js.map +0 -1
- package/kovalenko-http-request-cache.metadata.json +0 -1
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { NEVER } from 'rxjs';
|
|
2
|
-
import { startWith, switchMap, shareReplay } from 'rxjs/operators';
|
|
3
|
-
|
|
4
|
-
class DefaultStorage {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.storage = new Map();
|
|
7
|
-
}
|
|
8
|
-
getItem(key) {
|
|
9
|
-
return this.storage.get(key);
|
|
10
|
-
}
|
|
11
|
-
setItem(key, item) {
|
|
12
|
-
this.storage.set(key, item);
|
|
13
|
-
}
|
|
14
|
-
deleteItem(key) {
|
|
15
|
-
this.storage.delete(key);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const HttpRequestCache = (optionsHandler) => {
|
|
20
|
-
return (target, methodName, descriptor) => {
|
|
21
|
-
if (!((descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) instanceof Function)) {
|
|
22
|
-
throw Error(`'@HttpRequestCache' can be applied only to the class method which returns an Observable`);
|
|
23
|
-
}
|
|
24
|
-
const cacheKeyPrefix = `${target.constructor.name}_${methodName}`;
|
|
25
|
-
const originalMethod = descriptor.value;
|
|
26
|
-
descriptor.value = function (...args) {
|
|
27
|
-
var _a, _b, _c;
|
|
28
|
-
const options = optionsHandler === null || optionsHandler === void 0 ? void 0 : optionsHandler.call(this, this, ...args);
|
|
29
|
-
if (!(options === null || options === void 0 ? void 0 : options.storage) && !target._____storage_____) {
|
|
30
|
-
target._____storage_____ = new DefaultStorage();
|
|
31
|
-
}
|
|
32
|
-
const storage = (_a = options === null || options === void 0 ? void 0 : options.storage) !== null && _a !== void 0 ? _a : target._____storage_____;
|
|
33
|
-
const refreshOn = (_b = options === null || options === void 0 ? void 0 : options.refreshOn) !== null && _b !== void 0 ? _b : NEVER;
|
|
34
|
-
const key = `${cacheKeyPrefix}_${JSON.stringify(args)}`;
|
|
35
|
-
let observable = storage.getItem(key);
|
|
36
|
-
if (!observable) {
|
|
37
|
-
observable = refreshOn.pipe(startWith(true), switchMap(() => originalMethod.apply(this, [...args])), shareReplay({
|
|
38
|
-
bufferSize: 1,
|
|
39
|
-
refCount: (_c = options === null || options === void 0 ? void 0 : options.refCount) !== null && _c !== void 0 ? _c : false,
|
|
40
|
-
}));
|
|
41
|
-
storage.setItem(key, observable);
|
|
42
|
-
}
|
|
43
|
-
return observable;
|
|
44
|
-
};
|
|
45
|
-
return descriptor;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
/*
|
|
50
|
-
* Public API Surface of http-request-cache
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Generated bundle index. Do not edit.
|
|
55
|
-
*/
|
|
56
|
-
|
|
57
|
-
export { DefaultStorage, HttpRequestCache };
|
|
58
|
-
//# sourceMappingURL=kovalenko-http-request-cache.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"kovalenko-http-request-cache.js","sources":["../../../projects/http-request-cache/src/lib/default-storage.ts","../../../projects/http-request-cache/src/lib/http-request-cache.decorator.ts","../../../projects/http-request-cache/src/public-api.ts","../../../projects/http-request-cache/src/kovalenko-http-request-cache.ts"],"sourcesContent":["import {HttpCacheStorage} from './http-cache-storage';\r\nimport {Observable} from 'rxjs';\r\n\r\nexport class DefaultStorage implements HttpCacheStorage {\r\n private storage = new Map<string, Observable<any>>();\r\n\r\n getItem(key: string): Observable<any> | undefined {\r\n return this.storage.get(key);\r\n }\r\n\r\n setItem(key: string, item: Observable<any>): void {\r\n this.storage.set(key, item);\r\n }\r\n\r\n deleteItem(key: string): void {\r\n this.storage.delete(key);\r\n }\r\n}\r\n","import {HttpCacheOptions} from './http-cache-options';\r\nimport {NEVER, Observable} from 'rxjs';\r\nimport {shareReplay, startWith, switchMap} from 'rxjs/operators';\r\nimport {DefaultStorage} from './default-storage';\r\n\r\ntype HttpRequestCacheMethod = (...args: any[]) => Observable<any>;\r\n\r\nexport const HttpRequestCache = <T extends Record<string, any>>(optionsHandler?: (obj: T, ...args: any[]) => HttpCacheOptions) => {\r\n return (target: T, methodName: string, descriptor: TypedPropertyDescriptor<HttpRequestCacheMethod>): TypedPropertyDescriptor<HttpRequestCacheMethod> => {\r\n if (!(descriptor?.value instanceof Function)) {\r\n throw Error(`'@HttpRequestCache' can be applied only to the class method which returns an Observable`);\r\n }\r\n\r\n const cacheKeyPrefix = `${target.constructor.name}_${methodName}`;\r\n const originalMethod = descriptor.value;\r\n\r\n descriptor.value = function(...args: any[]): Observable<any> {\r\n const options = optionsHandler?.call(this as T, this as T, ...args);\r\n\r\n if (!options?.storage && !(target as any)._____storage_____) {\r\n (target as any)._____storage_____ = new DefaultStorage();\r\n }\r\n\r\n const storage = options?.storage ?? (target as any)._____storage_____;\r\n const refreshOn = options?.refreshOn ?? NEVER as Observable<unknown>;\r\n\r\n const key = `${cacheKeyPrefix}_${JSON.stringify(args)}`;\r\n\r\n let observable = storage.getItem(key);\r\n\r\n if (!observable) {\r\n observable = refreshOn.pipe(\r\n startWith(true),\r\n switchMap(() => originalMethod.apply(this, [...args])),\r\n shareReplay({\r\n bufferSize: 1,\r\n refCount: options?.refCount ?? false,\r\n }),\r\n );\r\n storage.setItem(key, observable);\r\n }\r\n\r\n return observable;\r\n };\r\n\r\n return descriptor;\r\n }\r\n}\r\n\r\n","/*\n * Public API Surface of http-request-cache\n */\n\nexport * from './lib/http-request-cache.decorator';\nexport * from './lib/http-cache-storage';\nexport * from './lib/http-cache-options';\nexport * from './lib/default-storage';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAGa,cAAc;IAA3B;QACU,YAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;KAatD;IAXC,OAAO,CAAC,GAAW;QACjB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;KAC9B;IAED,OAAO,CAAC,GAAW,EAAE,IAAqB;QACxC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;KAC7B;IAED,UAAU,CAAC,GAAW;QACpB,IAAI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Ac,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,EACtD,WAAW,CAAC;oBACV,UAAU,EAAE,CAAC;oBACb,QAAQ,QAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,KAAK;iBACrC,CAAC,CACH,CAAC;gBACF,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;aAClC;YAED,OAAO,UAAU,CAAC;SACnB,CAAC;QAEF,OAAO,UAAU,CAAC;KACnB,CAAA;AACH;;AC/CA;;;;ACAA;;;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"__symbolic":"module","version":4,"metadata":{"HttpRequestCache":{"__symbolic":"error","message":"Lambda not supported","line":7,"character":32,"module":"./lib/http-request-cache.decorator"},"HttpCacheStorage":{"__symbolic":"interface"},"HttpCacheOptions":{"__symbolic":"interface"},"DefaultStorage":{"__symbolic":"class","members":{"getItem":[{"__symbolic":"method"}],"setItem":[{"__symbolic":"method"}],"deleteItem":[{"__symbolic":"method"}]}}},"origins":{"HttpRequestCache":"./lib/http-request-cache.decorator","HttpCacheStorage":"./lib/http-cache-storage","HttpCacheOptions":"./lib/http-cache-options","DefaultStorage":"./lib/default-storage"},"importAs":"@kovalenko/http-request-cache"}
|