ag-common 0.0.230 → 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.
|
@@ -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,12 @@ 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
|
+
ssrCacheItems?: any;
|
|
26
|
+
cacheTtl?: number;
|
|
27
|
+
}, data: T) => Promise<void>;
|
|
22
28
|
/**
|
|
23
29
|
* sync call to callOpenApiCache.
|
|
24
30
|
* @param p
|
|
@@ -12,25 +12,45 @@ 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");
|
|
19
|
+
const common_1 = require("../../../common");
|
|
19
20
|
const node_cache_1 = __importDefault(require("node-cache"));
|
|
20
21
|
let callOpenApiCache;
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
30
|
-
cacheKeyRet = cacheKey + '||' + pref;
|
|
39
|
+
cacheKeyRet = `${cacheKey}||${authPref}||${ssrCachePref}`;
|
|
31
40
|
}
|
|
32
41
|
return cacheKeyRet;
|
|
33
42
|
}
|
|
43
|
+
const setOpenApiCacheRaw = (p, data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
44
|
+
const userPrefixedCacheKey = getCacheKey(p);
|
|
45
|
+
if (!userPrefixedCacheKey) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (!callOpenApiCache) {
|
|
49
|
+
callOpenApiCache = new node_cache_1.default({ stdTTL: p.cacheTtl || 120 });
|
|
50
|
+
}
|
|
51
|
+
callOpenApiCache.set(userPrefixedCacheKey, data);
|
|
52
|
+
});
|
|
53
|
+
exports.setOpenApiCacheRaw = setOpenApiCacheRaw;
|
|
34
54
|
/**
|
|
35
55
|
* sync call to callOpenApiCache.
|
|
36
56
|
* @param p
|
|
@@ -68,10 +88,7 @@ const callOpenApiCached = (p) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
68
88
|
if (resp.error) {
|
|
69
89
|
return { error: resp.error, data: undefined };
|
|
70
90
|
}
|
|
71
|
-
|
|
72
|
-
if (callOpenApiCache && userPrefixedCacheKey) {
|
|
73
|
-
callOpenApiCache.set(userPrefixedCacheKey, resp.data);
|
|
74
|
-
}
|
|
91
|
+
yield (0, exports.setOpenApiCacheRaw)(p, resp.data);
|
|
75
92
|
return resp;
|
|
76
93
|
});
|
|
77
94
|
exports.callOpenApiCached = callOpenApiCached;
|
|
@@ -7,7 +7,7 @@ export declare type TUseCallOpenApi<T> = AxiosWrapper<T> & {
|
|
|
7
7
|
loadcount: number;
|
|
8
8
|
setData: (d: TUseCallOpenApiDispatch<T | undefined>) => void;
|
|
9
9
|
};
|
|
10
|
-
declare type
|
|
10
|
+
declare type TUseCallOpenApiInt<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
11
11
|
cacheKey: string;
|
|
12
12
|
/**
|
|
13
13
|
* will shortcut and return the appropriate axioswrapper data if cachekey is found
|
|
@@ -23,5 +23,5 @@ declare type TUseCallOpenApi1<T, TDefaultApi> = ICallOpenApi<T, TDefaultApi> & {
|
|
|
23
23
|
* @param p
|
|
24
24
|
* @returns
|
|
25
25
|
*/
|
|
26
|
-
export declare const useCallOpenApi: <T, TDefaultApi>(pIn:
|
|
26
|
+
export declare const useCallOpenApi: <T, TDefaultApi>(pIn: TUseCallOpenApiInt<T, TDefaultApi>) => TUseCallOpenApi<T>;
|
|
27
27
|
export {};
|
|
@@ -13,10 +13,16 @@ exports.useCallOpenApi = void 0;
|
|
|
13
13
|
const cached_1 = require("./cached");
|
|
14
14
|
const direct_1 = require("./direct");
|
|
15
15
|
const react_1 = require("react");
|
|
16
|
-
const defaultState = (p
|
|
16
|
+
const defaultState = (p,
|
|
17
|
+
/**
|
|
18
|
+
* default false
|
|
19
|
+
*/
|
|
20
|
+
noSsr = false) => {
|
|
17
21
|
var _a;
|
|
18
|
-
const cachedData =
|
|
19
|
-
|
|
22
|
+
const cachedData = noSsr
|
|
23
|
+
? undefined
|
|
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 }, (cachedData && { data: cachedData })), { loaded: !!cachedData });
|
|
20
26
|
};
|
|
21
27
|
/**
|
|
22
28
|
* hooks+cached call to callOpenApi
|
|
@@ -24,31 +30,36 @@ const defaultState = (p) => {
|
|
|
24
30
|
* @returns
|
|
25
31
|
*/
|
|
26
32
|
const useCallOpenApi = (pIn) => {
|
|
27
|
-
const [
|
|
28
|
-
const [data, setData] = (0, react_1.useState)(defaultState(p));
|
|
33
|
+
const [data, setData] = (0, react_1.useState)([pIn, defaultState(pIn)]);
|
|
29
34
|
(0, react_1.useEffect)(() => {
|
|
30
|
-
if (JSON.stringify(
|
|
31
|
-
|
|
32
|
-
setData(defaultState(pIn));
|
|
35
|
+
if (JSON.stringify(data[0]) !== JSON.stringify(pIn)) {
|
|
36
|
+
setData([pIn, defaultState(pIn, true)]);
|
|
33
37
|
}
|
|
34
|
-
}, [
|
|
38
|
+
}, [data, pIn]);
|
|
35
39
|
(0, react_1.useEffect)(() => {
|
|
36
40
|
function run() {
|
|
37
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
const resp = yield (0, direct_1.callOpenApi)(
|
|
39
|
-
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
|
+
]);
|
|
40
47
|
});
|
|
41
48
|
}
|
|
42
|
-
const { error, loaded, loading, loadcount } = data;
|
|
43
|
-
const ng =
|
|
49
|
+
const { error, loaded, loading, loadcount } = data[1];
|
|
50
|
+
const ng = data[0].disabled || loaded || loading || (error && loadcount > 2);
|
|
44
51
|
if (ng) {
|
|
45
52
|
return;
|
|
46
53
|
}
|
|
47
|
-
setData((d) =>
|
|
54
|
+
setData((d) => [d[0], Object.assign(Object.assign({}, d[1]), { loading: true })]);
|
|
48
55
|
void run();
|
|
49
|
-
}, [data,
|
|
50
|
-
|
|
51
|
-
setData(
|
|
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
|
+
]);
|
|
52
62
|
} });
|
|
63
|
+
return ret;
|
|
53
64
|
};
|
|
54
65
|
exports.useCallOpenApi = useCallOpenApi;
|