ag-common 0.0.232 → 0.0.233

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.
@@ -22,6 +22,8 @@ export declare type TCallOpenApiCached<T, TDefaultApi> = ICallOpenApi<T, TDefaul
22
22
  export declare const setOpenApiCacheRaw: <T>(p: {
23
23
  cacheKey: string;
24
24
  overrideAuth?: OverrideAuth | undefined;
25
+ ssrCacheItems?: any;
26
+ cacheTtl?: number;
25
27
  }, data: T) => Promise<void>;
26
28
  /**
27
29
  * sync call to callOpenApiCache.
@@ -16,26 +16,39 @@ exports.callOpenApiCached = exports.callOpenApiCachedRaw = exports.setOpenApiCac
16
16
  const direct_1 = require("./direct");
17
17
  const cookie_1 = require("../cookie");
18
18
  const string_1 = require("../../../common/helpers/string");
19
+ const common_1 = require("../../../common");
19
20
  const node_cache_1 = __importDefault(require("node-cache"));
20
21
  let callOpenApiCache;
21
- function getCacheKey({ cacheKey, overrideAuth, }) {
22
- const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
22
+ /**
23
+ * cache differs per user and per SSR provided data
24
+ * @param param0
25
+ * @returns
26
+ */
27
+ function getCacheKey({ cacheKey, overrideAuth, ssrCacheItems, }) {
28
+ const authkeyPrefix1 = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
23
29
  (0, cookie_1.getCookieString)({
24
30
  name: 'id_token',
25
31
  defaultValue: '',
26
32
  });
33
+ const authPref = !authkeyPrefix1 ? '' : (0, common_1.hashCode)((0, string_1.toBase64)(authkeyPrefix1));
34
+ const ssrCachePref = !ssrCacheItems
35
+ ? ''
36
+ : (0, common_1.hashCode)((0, string_1.toBase64)(JSON.stringify(ssrCacheItems)));
27
37
  let cacheKeyRet;
28
38
  if (cacheKey) {
29
- const pref = !authkeyPrefix ? '' : (0, string_1.toBase64)(authkeyPrefix);
30
- cacheKeyRet = cacheKey + '||' + pref;
39
+ cacheKeyRet = `${cacheKey}||${authPref}||${ssrCachePref}`;
31
40
  }
32
41
  return cacheKeyRet;
33
42
  }
34
43
  const setOpenApiCacheRaw = (p, data) => __awaiter(void 0, void 0, void 0, function* () {
35
44
  const userPrefixedCacheKey = getCacheKey(p);
36
- if (callOpenApiCache && userPrefixedCacheKey) {
37
- callOpenApiCache.set(userPrefixedCacheKey, data);
45
+ if (!userPrefixedCacheKey) {
46
+ return;
47
+ }
48
+ if (!callOpenApiCache) {
49
+ callOpenApiCache = new node_cache_1.default({ stdTTL: p.cacheTtl || 120 });
38
50
  }
51
+ callOpenApiCache.set(userPrefixedCacheKey, data);
39
52
  });
40
53
  exports.setOpenApiCacheRaw = setOpenApiCacheRaw;
41
54
  /**
@@ -6,10 +6,6 @@ export declare type TUseCallOpenApi<T> = AxiosWrapper<T> & {
6
6
  loaded: boolean;
7
7
  loadcount: number;
8
8
  setData: (d: TUseCallOpenApiDispatch<T | undefined>) => void;
9
- /**
10
- * call when you want to refetch, but at a later point (ie same hook/cachekey across different components)
11
- */
12
- invalidateCacheKey: () => void;
13
9
  };
14
10
  declare type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
15
11
  cacheKey: string;
@@ -51,7 +51,7 @@ const useCallOpenApi = (pIn) => {
51
51
  if (ng) {
52
52
  return;
53
53
  }
54
- setData((d) => (Object.assign(Object.assign({}, d), { loading: true })));
54
+ setData((d) => [d[0], Object.assign(Object.assign({}, d[1]), { loading: true })]);
55
55
  void run();
56
56
  }, [data, setData]);
57
57
  const ret = Object.assign(Object.assign({}, data[1]), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () { return setData([data[0], defaultState(data[0], true)]); }), setData: (d) => {
@@ -59,7 +59,7 @@ const useCallOpenApi = (pIn) => {
59
59
  data[0],
60
60
  Object.assign(Object.assign({}, data[1]), { data: d(data[1].data), datetime: new Date().getTime() }),
61
61
  ]);
62
- }, invalidateCacheKey: () => (0, cached_1.setOpenApiCacheRaw)(data[0], undefined) });
62
+ } });
63
63
  return ret;
64
64
  };
65
65
  exports.useCallOpenApi = useCallOpenApi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.232",
3
+ "version": "0.0.233",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",