ag-common 0.0.413 → 0.0.415

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,11 +1,12 @@
1
+ /// <reference types="react" />
1
2
  import { AxiosWrapper } from '../jwt';
2
3
  import { CacheItems } from '../routes';
3
4
  import { ICallOpenApi } from './types';
4
- export type TUseCallOpenApiDispatch<A> = (value: A) => A;
5
5
  export type TUseCallOpenApi<T> = AxiosWrapper<T> & {
6
6
  loaded: boolean;
7
7
  loadcount: number;
8
- setData: (d: TUseCallOpenApiDispatch<T | undefined>) => void;
8
+ /** internally mutate state, but do not refetch. will automatically bump datetime */
9
+ setData: React.Dispatch<React.SetStateAction<Omit<TUseCallOpenApi<T>, 'reFetch' | 'setData'>>>;
9
10
  };
10
11
  type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
11
12
  cacheKey: string;
@@ -23,5 +24,5 @@ type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
23
24
  * @param p
24
25
  * @returns
25
26
  */
26
- export declare const useCallOpenApi: <T, TDefaultApi>(pIn: TUseCallOpenApiInt<T, TDefaultApi>) => TUseCallOpenApi<T>;
27
+ export declare const useCallOpenApi: <T, TDefaultApi>(inConfig: TUseCallOpenApiInt<T, TDefaultApi>) => TUseCallOpenApi<T>;
27
28
  export {};
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useCallOpenApi = void 0;
13
13
  const react_1 = require("react");
14
+ const useGranularHook_1 = require("../useGranularHook");
14
15
  const cached_1 = require("./cached");
15
16
  const direct_1 = require("./direct");
16
17
  const defaultState = (p,
@@ -22,42 +23,55 @@ noSsr = false) => {
22
23
  const cachedData = noSsr
23
24
  ? undefined
24
25
  : (_a = (0, cached_1.callOpenApiCachedRaw)(Object.assign(Object.assign({}, p), { onlyCached: true }))) === null || _a === void 0 ? void 0 : _a.data;
25
- return Object.assign(Object.assign({ data: undefined, url: '', datetime: 0, loadcount: 0, loading: false }, (cachedData && { data: cachedData })), { loaded: !!cachedData });
26
+ return Object.assign(Object.assign({ data: undefined, datetime: 0, loadcount: 0, loading: false }, (cachedData && { data: cachedData })), { loaded: !!cachedData });
26
27
  };
27
28
  /**
28
29
  * hooks+cached call to callOpenApi
29
30
  * @param p
30
31
  * @returns
31
32
  */
32
- const useCallOpenApi = (pIn) => {
33
- const [data, setData] = (0, react_1.useState)([pIn, defaultState(pIn)]);
34
- (0, react_1.useEffect)(() => {
35
- if (JSON.stringify(data[0]) !== JSON.stringify(pIn)) {
36
- setData([pIn, defaultState(pIn, true)]);
33
+ const useCallOpenApi = (inConfig) => {
34
+ /** response from hook */
35
+ const [resp, setResp] = (0, react_1.useState)(defaultState(inConfig));
36
+ /** config about hook*/
37
+ const [config, setConfig] = (0, react_1.useState)(inConfig);
38
+ (0, useGranularHook_1.useGranularEffect)(() => {
39
+ if (JSON.stringify(config) !== JSON.stringify(inConfig)) {
40
+ setConfig(inConfig);
41
+ setResp(defaultState(inConfig, true));
37
42
  }
38
- }, [data, pIn]);
39
- const run = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
40
- const resp = yield (0, direct_1.callOpenApi)(data[0]);
41
- setData((d) => [
42
- d[0],
43
- Object.assign(Object.assign({}, resp), { loaded: true, loading: false, loadcount: d[1].loadcount + 1, url: '', datetime: new Date().getTime() }),
44
- ]);
45
- }), [data]);
43
+ }, [inConfig], [resp, setResp, config, setConfig]);
44
+ const reFetch = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
45
+ const newdate = new Date().getTime();
46
+ const newresp = yield (0, direct_1.callOpenApi)(config);
47
+ setResp((d) => {
48
+ let newState = Object.assign(Object.assign({}, d), { loaded: true, loading: false });
49
+ if (newdate > d.datetime) {
50
+ newState = Object.assign(Object.assign({}, newresp), { loaded: true, loading: false, loadcount: d.loadcount + 1, datetime: newdate });
51
+ }
52
+ if (JSON.stringify(d) !== JSON.stringify(newState)) {
53
+ return newState;
54
+ }
55
+ else {
56
+ return d;
57
+ }
58
+ });
59
+ }), [config]);
46
60
  (0, react_1.useEffect)(() => {
47
- const { error, loaded, loading, loadcount } = data[1];
48
- const ng = data[0].disabled || loaded || loading || (error && loadcount > 2);
61
+ const { error, loaded, loading, loadcount } = resp;
62
+ const ng = config.disabled || loaded || loading || (error && loadcount > 2);
49
63
  if (ng) {
50
64
  return;
51
65
  }
52
- setData((d) => [d[0], Object.assign(Object.assign({}, d[1]), { loading: true })]);
53
- void run();
54
- }, [data, run, setData]);
55
- const ret = Object.assign(Object.assign({}, data[1]), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () { return run; }), setData: (d) => {
56
- setData([
57
- data[0],
58
- Object.assign(Object.assign({}, data[1]), { data: d(data[1].data), datetime: new Date().getTime() }),
59
- ]);
60
- } });
61
- return ret;
66
+ setResp((d) => (Object.assign(Object.assign({}, d), { loading: true })));
67
+ void reFetch();
68
+ }, [config.disabled, reFetch, resp]);
69
+ return Object.assign(Object.assign({}, resp), { reFetch, setData: (p) => __awaiter(void 0, void 0, void 0, function* () {
70
+ //wipe cache, or might revert
71
+ yield (0, cached_1.setOpenApiCacheRaw)(config, undefined);
72
+ setResp(p);
73
+ //ensure datetime is changed, or might get overwritten
74
+ setResp((x) => (Object.assign(Object.assign({}, x), { datetime: new Date().getTime() })));
75
+ }) });
62
76
  };
63
77
  exports.useCallOpenApi = useCallOpenApi;
@@ -54,7 +54,6 @@ export interface AxiosWrapper<T> {
54
54
  error?: AxiosError<unknown, any>;
55
55
  loading: boolean;
56
56
  reFetch: () => Promise<any>;
57
- url: string;
58
57
  datetime: number;
59
58
  }
60
59
  export interface AxiosWrapperLite<T> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.413",
3
+ "version": "0.0.415",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
@@ -19,19 +19,19 @@
19
19
  "aws-sdk": "2.x",
20
20
  "axios": "1.x",
21
21
  "constructs": "10.x",
22
+ "eslint-import-resolver-typescript": ">=3",
23
+ "eslint-plugin-simple-import-sort": ">=10",
22
24
  "jsonwebtoken": "9.x",
23
25
  "jwks-rsa": "3.x",
24
26
  "node-cache": "5.x",
25
- "openapi-request-validator": "12.1.0",
26
- "react": "18.2.0",
27
- "react-dom": "18.2.0",
28
- "typescript": "4.x",
29
- "eslint-import-resolver-typescript": "3.x",
30
- "eslint-plugin-simple-import-sort": ">=8"
27
+ "openapi-request-validator": "12.x",
28
+ "react": "18.x",
29
+ "react-dom": "18.x",
30
+ "typescript": "4.x"
31
31
  },
32
32
  "peerDependencies": {
33
- "@emotion/react": ">11",
34
- "@emotion/styled": ">11"
33
+ "@emotion/react": ">=11",
34
+ "@emotion/styled": ">=11"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@babel/core": "7.20.12",