ag-common 0.0.231 → 0.0.232
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,4 +1,4 @@
|
|
|
1
|
-
import { ICallOpenApi } from './types';
|
|
1
|
+
import { ICallOpenApi, OverrideAuth } from './types';
|
|
2
2
|
import { CacheItems } from '../routes';
|
|
3
3
|
import { AxiosWrapperLite } from '../jwt';
|
|
4
4
|
export declare type TCallOpenApiCached<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
@@ -19,6 +19,10 @@ export declare type TCallOpenApiCached<T, TDefaultApi> = ICallOpenApi<T, TDefaul
|
|
|
19
19
|
*/
|
|
20
20
|
onlyCached?: boolean;
|
|
21
21
|
};
|
|
22
|
+
export declare const setOpenApiCacheRaw: <T>(p: {
|
|
23
|
+
cacheKey: string;
|
|
24
|
+
overrideAuth?: OverrideAuth | undefined;
|
|
25
|
+
}, data: T) => Promise<void>;
|
|
22
26
|
/**
|
|
23
27
|
* sync call to callOpenApiCache.
|
|
24
28
|
* @param p
|
|
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.callOpenApiCached = exports.callOpenApiCachedRaw = void 0;
|
|
15
|
+
exports.callOpenApiCached = exports.callOpenApiCachedRaw = exports.setOpenApiCacheRaw = void 0;
|
|
16
16
|
const direct_1 = require("./direct");
|
|
17
17
|
const cookie_1 = require("../cookie");
|
|
18
18
|
const string_1 = require("../../../common/helpers/string");
|
|
@@ -31,6 +31,13 @@ function getCacheKey({ cacheKey, overrideAuth, }) {
|
|
|
31
31
|
}
|
|
32
32
|
return cacheKeyRet;
|
|
33
33
|
}
|
|
34
|
+
const setOpenApiCacheRaw = (p, data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
const userPrefixedCacheKey = getCacheKey(p);
|
|
36
|
+
if (callOpenApiCache && userPrefixedCacheKey) {
|
|
37
|
+
callOpenApiCache.set(userPrefixedCacheKey, data);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
exports.setOpenApiCacheRaw = setOpenApiCacheRaw;
|
|
34
41
|
/**
|
|
35
42
|
* sync call to callOpenApiCache.
|
|
36
43
|
* @param p
|
|
@@ -68,10 +75,7 @@ const callOpenApiCached = (p) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
68
75
|
if (resp.error) {
|
|
69
76
|
return { error: resp.error, data: undefined };
|
|
70
77
|
}
|
|
71
|
-
|
|
72
|
-
if (callOpenApiCache && userPrefixedCacheKey) {
|
|
73
|
-
callOpenApiCache.set(userPrefixedCacheKey, resp.data);
|
|
74
|
-
}
|
|
78
|
+
yield (0, exports.setOpenApiCacheRaw)(p, resp.data);
|
|
75
79
|
return resp;
|
|
76
80
|
});
|
|
77
81
|
exports.callOpenApiCached = callOpenApiCached;
|
|
@@ -6,8 +6,12 @@ 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;
|
|
9
13
|
};
|
|
10
|
-
declare type
|
|
14
|
+
declare type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
11
15
|
cacheKey: string;
|
|
12
16
|
/**
|
|
13
17
|
* will shortcut and return the appropriate axioswrapper data if cachekey is found
|
|
@@ -23,5 +27,5 @@ declare type TUseCallOpenApi1<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
|
23
27
|
* @param p
|
|
24
28
|
* @returns
|
|
25
29
|
*/
|
|
26
|
-
export declare const useCallOpenApi: <T, TDefaultApi>(pIn:
|
|
30
|
+
export declare const useCallOpenApi: <T, TDefaultApi>(pIn: TUseCallOpenApiInt<T, TDefaultApi>) => TUseCallOpenApi<T>;
|
|
27
31
|
export {};
|
|
@@ -22,7 +22,7 @@ noSsr = false) => {
|
|
|
22
22
|
const cachedData = noSsr
|
|
23
23
|
? undefined
|
|
24
24
|
: (_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
|
|
25
|
+
return Object.assign(Object.assign({ data: undefined, url: '', datetime: 0, loadcount: 0, loading: false }, (cachedData && { data: cachedData })), { loaded: !!cachedData });
|
|
26
26
|
};
|
|
27
27
|
/**
|
|
28
28
|
* hooks+cached call to callOpenApi
|
|
@@ -30,31 +30,36 @@ noSsr = false) => {
|
|
|
30
30
|
* @returns
|
|
31
31
|
*/
|
|
32
32
|
const useCallOpenApi = (pIn) => {
|
|
33
|
-
const [
|
|
34
|
-
const [data, setData] = (0, react_1.useState)(defaultState(p));
|
|
33
|
+
const [data, setData] = (0, react_1.useState)([pIn, defaultState(pIn)]);
|
|
35
34
|
(0, react_1.useEffect)(() => {
|
|
36
|
-
if (JSON.stringify(
|
|
37
|
-
|
|
38
|
-
setData(defaultState(pIn, true));
|
|
35
|
+
if (JSON.stringify(data[0]) !== JSON.stringify(pIn)) {
|
|
36
|
+
setData([pIn, defaultState(pIn, true)]);
|
|
39
37
|
}
|
|
40
|
-
}, [
|
|
38
|
+
}, [data, pIn]);
|
|
41
39
|
(0, react_1.useEffect)(() => {
|
|
42
40
|
function run() {
|
|
43
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
const resp = yield (0, direct_1.callOpenApi)(
|
|
45
|
-
setData((d) =>
|
|
42
|
+
const resp = yield (0, direct_1.callOpenApi)(data[0]);
|
|
43
|
+
setData((d) => [
|
|
44
|
+
d[0],
|
|
45
|
+
Object.assign(Object.assign({}, resp), { loaded: true, loading: false, loadcount: d[1].loadcount + 1, url: '', datetime: new Date().getTime() }),
|
|
46
|
+
]);
|
|
46
47
|
});
|
|
47
48
|
}
|
|
48
|
-
const { error, loaded, loading, loadcount } = data;
|
|
49
|
-
const ng =
|
|
49
|
+
const { error, loaded, loading, loadcount } = data[1];
|
|
50
|
+
const ng = data[0].disabled || loaded || loading || (error && loadcount > 2);
|
|
50
51
|
if (ng) {
|
|
51
52
|
return;
|
|
52
53
|
}
|
|
53
54
|
setData((d) => (Object.assign(Object.assign({}, d), { loading: true })));
|
|
54
55
|
void run();
|
|
55
|
-
}, [data,
|
|
56
|
-
|
|
57
|
-
setData(
|
|
58
|
-
|
|
56
|
+
}, [data, setData]);
|
|
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) => {
|
|
58
|
+
setData([
|
|
59
|
+
data[0],
|
|
60
|
+
Object.assign(Object.assign({}, data[1]), { data: d(data[1].data), datetime: new Date().getTime() }),
|
|
61
|
+
]);
|
|
62
|
+
}, invalidateCacheKey: () => (0, cached_1.setOpenApiCacheRaw)(data[0], undefined) });
|
|
63
|
+
return ret;
|
|
59
64
|
};
|
|
60
65
|
exports.useCallOpenApi = useCallOpenApi;
|