ag-common 0.0.138 → 0.0.142

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 +1 @@
1
- export declare function shuffle(array: unknown[], seed: number): unknown[];
1
+ export declare function shuffle<T>(array: T[], seed: number): T[];
@@ -34,3 +34,10 @@ export declare function containsInsensitive(str: string, ...args: string[]): boo
34
34
  */
35
35
  export declare const safeStringify: (obj: unknown, indent?: number) => string;
36
36
  export declare const chunkString: (str: string, length: number) => string[];
37
+ /**
38
+ * object to string - can be used for querystring a=b&c=d etc
39
+ * @param raw eg a=b&c=d
40
+ * @param splitKeyValue eg =
41
+ * @param splitKeys eg &
42
+ */
43
+ export declare function stringToObject(raw: string, splitKeyValue: string, splitKeys: string): Record<string, string>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.chunkString = exports.safeStringify = exports.containsInsensitive = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = exports.fromBase64 = exports.toBase64 = void 0;
3
+ exports.stringToObject = exports.chunkString = exports.safeStringify = exports.containsInsensitive = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = exports.fromBase64 = exports.toBase64 = void 0;
4
4
  const toBase64 = (str) => Buffer.from(str).toString('base64');
5
5
  exports.toBase64 = toBase64;
6
6
  const fromBase64 = (str) => Buffer.from(decodeURIComponent(str), 'base64').toString();
@@ -39,6 +39,9 @@ function trimSide(str, fromStart = true, ...params) {
39
39
  }
40
40
  exports.trimSide = trimSide;
41
41
  function trim(str, ...params) {
42
+ if (!str) {
43
+ return '';
44
+ }
42
45
  str = trimSide(str, true, ...params);
43
46
  str = trimSide(str, false, ...params);
44
47
  return str;
@@ -141,3 +144,21 @@ const safeStringify = (obj, indent = 2) => {
141
144
  exports.safeStringify = safeStringify;
142
145
  const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
143
146
  exports.chunkString = chunkString;
147
+ /**
148
+ * object to string - can be used for querystring a=b&c=d etc
149
+ * @param raw eg a=b&c=d
150
+ * @param splitKeyValue eg =
151
+ * @param splitKeys eg &
152
+ */
153
+ function stringToObject(raw, splitKeyValue, splitKeys) {
154
+ const ret = {};
155
+ if (!stringToObject) {
156
+ return ret;
157
+ }
158
+ raw.split(splitKeys).forEach((set) => {
159
+ const [k, v] = set.split(splitKeyValue);
160
+ ret[k] = v;
161
+ });
162
+ return ret;
163
+ }
164
+ exports.stringToObject = stringToObject;
@@ -1,9 +1,11 @@
1
1
  import { AxiosWrapper } from '../jwt';
2
2
  import { ICallOpenApi } from './types';
3
3
  import { CacheItems } from '../routes';
4
- declare type AxiosWrapperWrap<T> = AxiosWrapper<T | undefined> & {
4
+ export declare type TUseCallOpenApiDispatch<A> = (value: A) => A;
5
+ export declare type TUseCallOpenApi<T> = AxiosWrapper<T> & {
5
6
  loaded: boolean;
6
7
  loadcount: number;
8
+ setData: (d: TUseCallOpenApiDispatch<T | undefined>) => void;
7
9
  };
8
10
  /**
9
11
  * hooks+cached call to callOpenApi
@@ -20,5 +22,4 @@ export declare const useCallOpenApi: <T, TDefaultApi>(p: ICallOpenApi<T, TDefaul
20
22
  * default ttl in seconds for cache - default 120s
21
23
  */
22
24
  cacheTtl?: number | undefined;
23
- }) => AxiosWrapperWrap<T>;
24
- export {};
25
+ }) => TUseCallOpenApi<T>;
@@ -28,6 +28,7 @@ const useCallOpenApi = (p) => {
28
28
  loading: false,
29
29
  loaded: false,
30
30
  reFetch: () => __awaiter(void 0, void 0, void 0, function* () { }),
31
+ setData: () => { },
31
32
  };
32
33
  const cachedData = (_a = (0, cached_1.callOpenApiCachedRaw)(Object.assign(Object.assign({}, p), { onlyCached: true }))) === null || _a === void 0 ? void 0 : _a.data;
33
34
  const [data, setData] = (0, react_1.useState)(Object.assign(Object.assign(Object.assign({}, defv), (cachedData && { data: cachedData })), { loaded: !!cachedData }));
@@ -35,7 +36,7 @@ const useCallOpenApi = (p) => {
35
36
  function run() {
36
37
  return __awaiter(this, void 0, void 0, function* () {
37
38
  const resp = yield (0, direct_1.callOpenApi)(p);
38
- setData((d) => (Object.assign(Object.assign({}, resp), { loaded: true, loading: false, loadcount: d.loadcount + 1, reFetch: () => __awaiter(this, void 0, void 0, function* () { }), url: '', datetime: new Date().getTime() })));
39
+ setData((d) => (Object.assign(Object.assign({}, resp), { loaded: true, loading: false, loadcount: d.loadcount + 1, reFetch: () => __awaiter(this, void 0, void 0, function* () { }), url: '', datetime: new Date().getTime(), setData: () => { } })));
39
40
  });
40
41
  }
41
42
  const { error, loaded, loading, loadcount } = data;
@@ -48,6 +49,8 @@ const useCallOpenApi = (p) => {
48
49
  }, [data, p, setData]);
49
50
  return Object.assign(Object.assign({}, data), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () {
50
51
  setData(defv);
51
- }) });
52
+ }), setData: (d) => {
53
+ setData(Object.assign(Object.assign({}, data), { data: d(data.data), datetime: new Date().getTime() }));
54
+ } });
52
55
  };
53
56
  exports.useCallOpenApi = useCallOpenApi;
@@ -2,11 +2,38 @@ import { TLang } from '../../common/helpers/i18n';
2
2
  import { ICognitoAuth } from './cognito';
3
3
  import { AxiosWrapperLite } from './jwt';
4
4
  export interface LocationSubset {
5
+ /**
6
+ * slash only path eg /aaa/bbb
7
+ */
5
8
  pathname: string;
9
+ /**
10
+ * eg #aaa
11
+ */
6
12
  hash: string;
13
+ /**
14
+ * up to first slash eg http://aaa.com:1111
15
+ */
7
16
  origin: string;
17
+ /**
18
+ * parse querystring keyvalues
19
+ */
8
20
  query: Record<string, string>;
21
+ /**
22
+ * protocol less up to first slash eg aaa.com:1111
23
+ */
9
24
  host: string;
25
+ /**
26
+ * eg http: or https:
27
+ */
28
+ protocol: string;
29
+ /**
30
+ * full url
31
+ */
32
+ href: string;
33
+ /**
34
+ * url from first slash eg /aaa/bbb?q=a#111
35
+ */
36
+ path: string;
10
37
  }
11
38
  export declare type CacheItems = CacheItem<any>[];
12
39
  export interface CacheItem<T> {
@@ -35,3 +62,15 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
35
62
  */
36
63
  cookieDocument: string | undefined;
37
64
  }
65
+ /**
66
+ * get parsed url. will use window if possible
67
+ * @param param0
68
+ * @returns
69
+ */
70
+ export declare const getClientOrServerReqHref: ({ href }: {
71
+ href?: string | undefined;
72
+ }) => LocationSubset;
73
+ export declare const getClientOrServerReq: ({ host, pathname, }: {
74
+ pathname: string;
75
+ host: string;
76
+ }) => LocationSubset;
@@ -1,2 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getClientOrServerReq = exports.getClientOrServerReqHref = void 0;
4
+ const url_1 = require("url");
5
+ const string_1 = require("../../common/helpers/string");
6
+ const calculateServerHref = ({ host, pathname, }) => {
7
+ if (!host) {
8
+ return undefined;
9
+ }
10
+ let href = '';
11
+ if (host.includes('localhost')) {
12
+ href += 'http://';
13
+ }
14
+ else {
15
+ href += 'https://';
16
+ }
17
+ href += host + pathname;
18
+ return href;
19
+ };
20
+ /**
21
+ * get parsed url. will use window if possible
22
+ * @param param0
23
+ * @returns
24
+ */
25
+ const getClientOrServerReqHref = ({ href }) => {
26
+ if (typeof window !== 'undefined') {
27
+ href = window.location.href;
28
+ }
29
+ if (!href) {
30
+ throw new Error('no href');
31
+ }
32
+ const parsed = (0, url_1.parse)(href);
33
+ const ret = {
34
+ hash: parsed.hash || '',
35
+ host: parsed.host || '',
36
+ origin: `${parsed.protocol}//${parsed.host}`,
37
+ href: `${parsed.protocol}//${parsed.host}${parsed.path}${parsed.hash || ''}`,
38
+ path: `${parsed.path}${parsed.hash || ''}`,
39
+ pathname: parsed.pathname || '',
40
+ protocol: parsed.protocol || '',
41
+ query: (0, string_1.stringToObject)(parsed.query || '', '=', '&'),
42
+ };
43
+ return ret;
44
+ };
45
+ exports.getClientOrServerReqHref = getClientOrServerReqHref;
46
+ const getClientOrServerReq = ({ host, pathname, }) => {
47
+ const href = calculateServerHref({
48
+ host,
49
+ pathname,
50
+ });
51
+ return (0, exports.getClientOrServerReqHref)({ href });
52
+ };
53
+ exports.getClientOrServerReq = getClientOrServerReq;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.138",
3
+ "version": "0.0.142",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",