ag-common 0.0.112 → 0.0.113

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.
@@ -1,8 +1,15 @@
1
1
  import { AxiosWrapper } from './jwt';
2
2
  import { ICallOpenApi } from './types';
3
+ import { CacheItems } from './routes';
3
4
  declare type AxiosWrapperWrap<T> = AxiosWrapper<T | undefined> & {
4
5
  loaded: boolean;
5
6
  loadcount: number;
6
7
  };
7
- export declare const useCallOpenApi: <T, TDefaultApi>(p: ICallOpenApi<T, TDefaultApi>) => AxiosWrapperWrap<T>;
8
+ export declare const useCallOpenApi: <T, TDefaultApi>(p: ICallOpenApi<T, TDefaultApi> & {
9
+ cacheKey: string;
10
+ /**
11
+ * will shortcut and return the appropriate axioswrapper data if cachekey is found
12
+ */
13
+ ssrCacheItems?: CacheItems | undefined;
14
+ }) => AxiosWrapperWrap<T>;
8
15
  export {};
@@ -12,36 +12,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useCallOpenApi = void 0;
13
13
  const react_1 = require("react");
14
14
  const callOpenApi_1 = require("./callOpenApi");
15
- const defaultV = (def) => ({
16
- loading: false,
17
- data: def,
18
- datetime: new Date().getTime(),
19
- reFetch: () => __awaiter(void 0, void 0, void 0, function* () { }),
20
- error: undefined,
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- url: undefined,
23
- //
24
- loaded: false,
25
- loadcount: 0,
26
- });
27
15
  const useCallOpenApi = (p) => {
28
- const [data, setData] = (0, react_1.useState)(defaultV(undefined));
16
+ var _a, _b;
17
+ const ssrCached = (_a = p.ssrCacheItems) === null || _a === void 0 ? void 0 : _a.find((s) => s.cacheKey === p.cacheKey);
18
+ const defv = {
19
+ data: undefined,
20
+ url: '',
21
+ datetime: 0,
22
+ loadcount: 0,
23
+ loading: false,
24
+ loaded: false,
25
+ reFetch: () => __awaiter(void 0, void 0, void 0, function* () { }),
26
+ };
27
+ const [data, setData] = (0, react_1.useState)(Object.assign(Object.assign({}, defv), { data: ssrCached ? (_b = ssrCached.prefillData) === null || _b === void 0 ? void 0 : _b.data : undefined, loaded: !!ssrCached }));
29
28
  (0, react_1.useEffect)(() => {
30
29
  function run() {
31
30
  return __awaiter(this, void 0, void 0, function* () {
32
31
  const res = yield (0, callOpenApi_1.callOpenApi)(p);
32
+ console.log('set res=', res);
33
33
  setData((d) => (Object.assign(Object.assign({}, res), { loaded: true, loading: false, loadcount: d.loadcount + 1 })));
34
34
  });
35
35
  }
36
36
  const { error, loaded, loading, loadcount } = data;
37
- if (p.disabled || loaded || loading || (error && loadcount < 2)) {
37
+ const ng = p.disabled || loaded || loading || (error && loadcount > 2);
38
+ if (ng) {
38
39
  return;
39
40
  }
40
41
  setData((d) => (Object.assign(Object.assign({}, d), { loading: true })));
41
42
  void run();
42
43
  }, [data, p, setData]);
43
44
  return Object.assign(Object.assign({}, data), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () {
44
- setData(defaultV(undefined));
45
+ setData(defv);
45
46
  }) });
46
47
  };
47
48
  exports.useCallOpenApi = useCallOpenApi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.112",
3
+ "version": "0.0.113",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",