ag-common 0.0.108 → 0.0.112

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
1
  export declare const confirm: ({ bottomText, topText, }: {
2
2
  topText?: string | undefined;
3
3
  bottomText: string;
4
- }) => Promise<unknown>;
4
+ }) => Promise<boolean>;
@@ -1,3 +1,3 @@
1
1
  import { AxiosWrapper } from './jwt';
2
2
  import { ICallOpenApi } from './types';
3
- export declare const callOpenApi: <T, TDefaultApi>({ func, apiUrl, overrideAuth, refreshToken, logout, newDefaultApi, }: ICallOpenApi<T, TDefaultApi>) => Promise<AxiosWrapper<T | undefined>>;
3
+ export declare const callOpenApi: <T, TDefaultApi>({ func, apiUrl, overrideAuth, refreshToken, logout, newDefaultApi, }: ICallOpenApi<T, TDefaultApi>) => Promise<AxiosWrapper<T>>;
@@ -17,7 +17,7 @@ const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefa
17
17
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
18
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
19
  let error;
20
- let data;
20
+ let data = undefined;
21
21
  const config = {
22
22
  basePath: apiUrl,
23
23
  baseOptions: { headers: { authorization: '' } },
@@ -79,13 +79,6 @@ const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefa
79
79
  // eslint-disable-next-line no-await-in-loop
80
80
  yield (0, sleep_1.sleep)(2000);
81
81
  }
82
- return {
83
- data,
84
- error,
85
- loading: false,
86
- reFetch: () => __awaiter(void 0, void 0, void 0, function* () { return func(cl); }),
87
- url: func.toString(),
88
- datetime: new Date().getTime(),
89
- };
82
+ return Object.assign(Object.assign({ data }, (error && { error })), { loading: false, reFetch: () => __awaiter(void 0, void 0, void 0, function* () { return func(cl); }), url: func.toString(), datetime: new Date().getTime() });
90
83
  });
91
84
  exports.callOpenApi = callOpenApi;
@@ -2,11 +2,17 @@ export declare function setCookieWrapper<T>(cname: string, raw: T, exdays?: numb
2
2
  export declare function wipeCookies(cname: string): void;
3
3
  export declare function getCookieWrapper<T>({ cname, cookieDocument, defaultValue, }: {
4
4
  cname: string;
5
+ /**
6
+ * set for ssr
7
+ */
5
8
  cookieDocument?: string;
6
9
  defaultValue?: string;
7
10
  }): T;
8
11
  export declare function useCookie<T>({ key, defaultValue, cookieDocument, }: {
9
12
  key: string;
10
13
  defaultValue?: string;
14
+ /**
15
+ * set for ssr
16
+ */
11
17
  cookieDocument?: string;
12
- }): [T, (v: T) => any];
18
+ }): [T, (v: T) => void];
@@ -51,7 +51,7 @@ export interface User {
51
51
  }
52
52
  export interface AxiosWrapper<T> {
53
53
  data: T;
54
- error: AxiosError<unknown, any> | undefined;
54
+ error?: AxiosError<unknown, any>;
55
55
  loading: boolean;
56
56
  reFetch: () => Promise<any>;
57
57
  url: string;
@@ -1,5 +1,6 @@
1
- import { TLang } from '../..';
1
+ import { TLang } from '../../common/helpers/i18n';
2
2
  import { ICognitoAuth } from './cognito';
3
+ import { AxiosWrapper } from './jwt';
3
4
  export interface LocationSubset {
4
5
  pathname: string;
5
6
  hash: string;
@@ -7,14 +8,14 @@ export interface LocationSubset {
7
8
  query: Record<string, string>;
8
9
  host: string;
9
10
  }
10
- export declare type CacheItems = CacheItem[];
11
- export interface CacheItem {
11
+ export declare type CacheItems = CacheItem<any>[];
12
+ export interface CacheItem<T> {
12
13
  cacheKey: string;
13
- prefillData: any;
14
+ prefillData: AxiosWrapper<T>;
14
15
  ttlSeconds: number;
15
16
  }
16
17
  export interface IInitialStateCommon {
17
- openApiCache?: CacheItem[];
18
+ openApiCache?: CacheItem<any>[];
18
19
  headerTitle?: string;
19
20
  seed?: number;
20
21
  }
@@ -29,4 +30,8 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
29
30
  request: TRequest;
30
31
  auth: ICognitoAuth;
31
32
  pushPath: (path: string) => Promise<void>;
33
+ /**
34
+ * set for ssr
35
+ */
36
+ cookieDocument: string | undefined;
32
37
  }
@@ -1,7 +1,6 @@
1
- import { AxiosResponse } from 'axios';
2
1
  import { CacheItems } from './routes';
3
- import { AxiosWrapper, User } from './jwt';
4
- import { OverrideAuth } from './types';
2
+ import { AxiosWrapper } from './jwt';
3
+ import { ICallOpenApi } from './types';
5
4
  export declare const deleteMutexData: (key: string) => void;
6
5
  export declare const setMutexData: ({ key, data, ttlSeconds, }: {
7
6
  key: string;
@@ -14,15 +13,12 @@ export declare const getMutexData: <T>(key: string) => AxiosWrapper<T>;
14
13
  * @param overrideAuth - auth automatically picked up from id_token cookie, can override value here, but not required
15
14
  * @returns
16
15
  */
17
- export declare const useOpenApiStore: <T, TDefaultApi>(p: {
16
+ export declare const useOpenApiStore: <T, TDefaultApi>(p: ICallOpenApi<T, TDefaultApi> & {
18
17
  ttlSeconds?: number | undefined;
19
- func: (f: TDefaultApi) => Promise<AxiosResponse<T, any>>;
20
18
  cacheKey: string;
21
- logout: () => void;
22
- refreshToken: () => Promise<User | undefined>;
23
- disabled?: boolean | undefined;
24
- apiUrl: string;
25
- newDefaultApi: (config: any) => TDefaultApi;
26
- overrideAuth?: OverrideAuth | undefined;
19
+ /**
20
+ * will shortcut and return the appropriate axioswrapper data if cachekey is found
21
+ */
22
+ ssrCacheItems?: CacheItems | undefined;
27
23
  }) => AxiosWrapper<T>;
28
24
  export declare const setOpenApiCache: (items: CacheItems) => void;
@@ -85,6 +85,7 @@ const getTs = () => new Date().getTime();
85
85
  * @returns
86
86
  */
87
87
  const useOpenApiStore = (p) => {
88
+ var _a;
88
89
  const { cacheKey, disabled } = p;
89
90
  if (!cacheKey) {
90
91
  throw new Error('no cache key');
@@ -105,10 +106,6 @@ const useOpenApiStore = (p) => {
105
106
  mLock(Object.assign({ key }, p));
106
107
  }
107
108
  }, [disabled, key, p]);
108
- if (!mutexData.getData(key)) {
109
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
- return { loading: true };
111
- }
112
109
  if (disabled) {
113
110
  return {
114
111
  loading: false,
@@ -120,7 +117,15 @@ const useOpenApiStore = (p) => {
120
117
  url: '',
121
118
  };
122
119
  }
120
+ const ssrCached = (_a = p.ssrCacheItems) === null || _a === void 0 ? void 0 : _a.find((s) => s.cacheKey === cacheKey);
121
+ if (typeof window === 'undefined' && ssrCached) {
122
+ return ssrCached.prefillData;
123
+ }
123
124
  const current = mutexData.getData(key);
125
+ if (!current) {
126
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
+ return { loading: true };
128
+ }
124
129
  return Object.assign(Object.assign({}, current), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () {
125
130
  mutexData.setData({
126
131
  key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.108",
3
+ "version": "0.0.112",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",