ag-common 0.0.129 → 0.0.130

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.
@@ -33,3 +33,4 @@ export declare function containsInsensitive(str: string, ...args: string[]): boo
33
33
  * @returns
34
34
  */
35
35
  export declare const safeStringify: (obj: any, indent?: number) => string;
36
+ export declare const chunkString: (str: string, length: number) => string[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- 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.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();
@@ -140,3 +140,5 @@ const safeStringify = (obj, indent = 2) => {
140
140
  return retVal;
141
141
  };
142
142
  exports.safeStringify = safeStringify;
143
+ const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
144
+ exports.chunkString = chunkString;
@@ -92,13 +92,11 @@ const ChevronStyled = (0, styled_components_1.default)(Chevron_1.Chevron) `
92
92
  }
93
93
  `;
94
94
  const Sidebar = ({ children, className, key = 'sidebar', cookieDocument, }) => {
95
- const [openRaw, setOpenRaw] = (0, cookie_1.useCookie)({
96
- key,
97
- defaultValue: 'false',
95
+ const [open, setOpen] = (0, cookie_1.useCookieBoolean)({
96
+ name: key,
97
+ defaultValue: false,
98
98
  cookieDocument: cookieDocument,
99
99
  });
100
- const open = openRaw === 'true';
101
- const setOpen = (o) => setOpenRaw(o.toString());
102
100
  return (react_1.default.createElement(Base, { className: className, "data-open": open, onClick: () => !open && setOpen(true), "data-hover": true },
103
101
  react_1.default.createElement(Hamburger, { "data-open": open, onClick: () => setOpen(!open), "data-hover": true },
104
102
  react_1.default.createElement(ChevronStyled, { point: open ? 'right' : 'left', width: "100%" })),
@@ -19,7 +19,12 @@ const direct_1 = require("./direct");
19
19
  const string_1 = require("../../../common/helpers/string");
20
20
  let callOpenApiCache;
21
21
  function getCacheKey({ cacheKey, overrideAuth, }) {
22
- const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) || (0, cookie_1.getCookieWrapper)({ cname: 'id_token' });
22
+ const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
23
+ (0, cookie_1.getCookieWrapper)({
24
+ name: 'id_token',
25
+ defaultValue: '',
26
+ parse: (s) => s,
27
+ });
23
28
  let cacheKeyRet;
24
29
  if (cacheKey) {
25
30
  const pref = !authkeyPrefix ? '' : (0, string_1.toBase64)(authkeyPrefix);
@@ -26,7 +26,11 @@ const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefa
26
26
  config.baseOptions.headers.authorization = `Bearer ${overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token}`;
27
27
  }
28
28
  else {
29
- const isAuthed = !!(0, cookie_1.getCookieWrapper)({ cname: 'id_token' });
29
+ const isAuthed = !!(0, cookie_1.getCookieWrapper)({
30
+ name: 'id_token',
31
+ defaultValue: '',
32
+ parse: (s) => s,
33
+ });
30
34
  if (isAuthed) {
31
35
  const updated = yield refreshToken();
32
36
  if ((_a = updated === null || updated === void 0 ? void 0 : updated.jwt) === null || _a === void 0 ? void 0 : _a.id_token) {
@@ -1,18 +1,56 @@
1
- export declare function setCookieWrapper<T>(cname: string, raw: T, exdays?: number): void;
2
- export declare function wipeCookies(cname: string): void;
3
- export declare function getCookieWrapper<T>({ cname, cookieDocument, defaultValue, }: {
4
- cname: string;
5
- /**
6
- * set for ssr
7
- */
1
+ export declare function wipeCookies(name: string): void;
2
+ /**
3
+ * json+b64 incoming. chunk. write chunks
4
+ * @param p
5
+ * @returns
6
+ */
7
+ export declare function setCookieWrapper<T>(p: {
8
+ value: T;
9
+ name: string;
8
10
  cookieDocument?: string;
9
- defaultValue?: string;
11
+ stringify: (s: T) => string;
12
+ }): void;
13
+ /**
14
+ * read chunks. json parse+unb64
15
+ * @param param0
16
+ * @returns
17
+ */
18
+ export declare function getCookieWrapper<T>({ name, cookieDocument, defaultValue, parse, }: {
19
+ defaultValue: T;
20
+ name: string;
21
+ cookieDocument?: string;
22
+ parse: (s: string) => T;
10
23
  }): T;
11
- export declare function useCookie<T>({ key, defaultValue, cookieDocument, }: {
12
- key: string;
13
- defaultValue?: string;
24
+ declare type Dispatch<A> = (value: A) => void;
25
+ declare type SetStateAction<S> = S | ((prevState: S) => S);
26
+ declare type ReturnType<T> = [T, Dispatch<SetStateAction<T>>];
27
+ declare type TParse<T> = (s: string | null | undefined) => T;
28
+ export declare function useCookie<T>(p: {
29
+ defaultValue: T;
30
+ name: string;
31
+ cookieDocument?: string;
14
32
  /**
15
- * set for ssr
33
+ * required for objects. defaults to JSON.parse
16
34
  */
35
+ parse?: TParse<T>;
36
+ /**
37
+ * required for objects. defaults to JSON.stringify
38
+ */
39
+ stringify?: (v: T) => string;
40
+ }): ReturnType<T>;
41
+ export declare const useCookieString: (p: {
42
+ defaultValue: string;
43
+ name: string;
44
+ cookieDocument?: string;
45
+ }) => ReturnType<string>;
46
+ export declare const useCookieNumber: (p: {
47
+ defaultValue: number | undefined;
48
+ name: string;
49
+ cookieDocument?: string;
50
+ }) => ReturnType<number | undefined>;
51
+ export declare const useCookieBoolean: (p: {
52
+ defaultValue: boolean;
53
+ name: string;
17
54
  cookieDocument?: string;
18
- }): [T, (v: T) => void];
55
+ }) => ReturnType<boolean>;
56
+ export {};
@@ -1,62 +1,55 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useCookie = exports.getCookieWrapper = exports.wipeCookies = exports.setCookieWrapper = void 0;
3
+ exports.useCookieBoolean = exports.useCookieNumber = exports.useCookieString = exports.useCookie = exports.getCookieWrapper = exports.setCookieWrapper = exports.wipeCookies = void 0;
4
4
  /* eslint-disable guard-for-in */
5
5
  /* eslint-disable no-restricted-syntax */
6
6
  const react_1 = require("react");
7
+ const log_1 = require("../../common/helpers/log");
7
8
  const string_1 = require("../../common/helpers/string");
8
9
  const expireDate = 'Thu, 01 Jan 1970 00:00:00 UTC';
9
10
  const maxCookieLen = 4000;
10
- const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
11
- function setCookie(cname, raw, exdays = 1) {
11
+ /**
12
+ * expiryDays <0 will delete
13
+ * @param param0
14
+ * @returns
15
+ */
16
+ function setCookieRaw({ name, value, expiryDays = 1, }) {
12
17
  if (typeof window === undefined) {
13
18
  return;
14
19
  }
15
20
  const d = new Date();
16
- d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
17
- const expires = `expires=${!raw || exdays < 0 ? expireDate : d.toUTCString()}`;
18
- document.cookie = `${cname}=${!raw ? '' : raw};${expires};path=/`;
21
+ d.setTime(d.getTime() + expiryDays * 24 * 60 * 60 * 1000);
22
+ const expires = `expires=${!value || expiryDays < 0 ? expireDate : d.toUTCString()}`;
23
+ document.cookie = `${name}=${!value ? '' : value};${expires};path=/`;
19
24
  }
20
- function getCookie({ cname, cookieDocument, }) {
21
- const name = `${cname}=`;
25
+ function getCookieRaw({ name, cookieDocument, }) {
26
+ const nameeq = `${name}=`;
22
27
  const ca1 = cookieDocument || (typeof window !== 'undefined' && document.cookie);
23
28
  if (!ca1 || !(ca1 === null || ca1 === void 0 ? void 0 : ca1.trim())) {
24
29
  return undefined;
25
30
  }
26
31
  const ca = ca1.split(';').map((t) => t.trim());
27
32
  for (const c of ca) {
28
- if (c.indexOf(name) === 0) {
29
- return c.substr(name.length, c.length);
33
+ if (c.indexOf(nameeq) === 0) {
34
+ const raw = c.substr(nameeq.length, c.length);
35
+ return raw;
30
36
  }
31
37
  }
32
38
  return undefined;
33
39
  }
34
- function setCookieWrapper(cname, raw, exdays = 1) {
35
- let index = 0;
36
- while (getCookie({ cname: cname + index })) {
37
- setCookie(cname + index, null, -1);
38
- index += 1;
39
- }
40
- if (!raw) {
40
+ function wipeCookies(name) {
41
+ if (typeof window === 'undefined') {
42
+ (0, log_1.warn)('cant wipe cookies on server');
41
43
  return;
42
44
  }
43
- const str = (0, string_1.toBase64)(JSON.stringify(raw));
44
- let chunks = [str];
45
- if (str.length > maxCookieLen) {
46
- chunks = chunkString(str, maxCookieLen);
47
- }
48
- for (const index1 in chunks) {
49
- const chunk = chunks[index1];
50
- setCookie(cname + index1, chunk, exdays);
51
- }
52
- }
53
- exports.setCookieWrapper = setCookieWrapper;
54
- function wipeCookies(cname) {
55
45
  let currentCount = 0;
56
46
  // eslint-disable-next-line no-constant-condition
57
47
  while (true) {
58
- if (getCookie({ cname: cname + currentCount })) {
59
- setCookie(cname + currentCount, undefined);
48
+ if (getCookieRaw({
49
+ name: name + currentCount,
50
+ cookieDocument: '',
51
+ })) {
52
+ setCookieRaw({ name: name + currentCount, value: '', expiryDays: -1 });
60
53
  currentCount += 1;
61
54
  }
62
55
  else {
@@ -65,46 +58,83 @@ function wipeCookies(cname) {
65
58
  }
66
59
  }
67
60
  exports.wipeCookies = wipeCookies;
68
- function getCookieWrapper({ cname, cookieDocument, defaultValue, }) {
61
+ /**
62
+ * json+b64 incoming. chunk. write chunks
63
+ * @param p
64
+ * @returns
65
+ */
66
+ function setCookieWrapper(p) {
67
+ wipeCookies(p.name);
68
+ if (!p.value) {
69
+ return;
70
+ }
71
+ const str = (0, string_1.toBase64)(p.stringify(p.value));
72
+ const chunks = (0, string_1.chunkString)(str, maxCookieLen);
73
+ for (const index1 in chunks) {
74
+ const chunk = chunks[index1];
75
+ setCookieRaw(Object.assign(Object.assign({}, p), { name: p.name + index1, value: chunk }));
76
+ }
77
+ }
78
+ exports.setCookieWrapper = setCookieWrapper;
79
+ /**
80
+ * read chunks. json parse+unb64
81
+ * @param param0
82
+ * @returns
83
+ */
84
+ function getCookieWrapper({ name, cookieDocument, defaultValue, parse, }) {
69
85
  let raw = '';
70
86
  let currentCount = 0;
71
87
  // eslint-disable-next-line no-constant-condition
72
88
  while (true) {
73
- const newv = getCookie({
74
- cname: cname + currentCount,
89
+ const newv = getCookieRaw({
90
+ name: name + currentCount,
75
91
  cookieDocument,
76
92
  });
77
93
  if (!newv) {
78
- if (currentCount === 0) {
79
- if (defaultValue) {
80
- setCookie(cname, defaultValue);
81
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
82
- return defaultValue;
83
- }
84
- return undefined;
85
- }
86
- try {
87
- return JSON.parse((0, string_1.fromBase64)(raw));
88
- }
89
- catch (e) {
90
- wipeCookies(cname);
91
- return undefined;
92
- }
94
+ break;
93
95
  }
94
96
  raw += newv;
95
97
  currentCount += 1;
96
98
  }
99
+ try {
100
+ return parse((0, string_1.fromBase64)(raw));
101
+ }
102
+ catch (e) {
103
+ (0, log_1.warn)('cookie error:', e);
104
+ wipeCookies(name);
105
+ return defaultValue;
106
+ }
97
107
  }
98
108
  exports.getCookieWrapper = getCookieWrapper;
99
109
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
- function useCookie({ key, defaultValue, cookieDocument, }) {
101
- const [cookie, setC] = (0, react_1.useState)(getCookieWrapper({ cname: key, defaultValue, cookieDocument }));
102
- return [
103
- cookie,
104
- (v) => {
105
- setCookieWrapper(key, v, 2);
106
- setC(v);
107
- },
108
- ];
110
+ function useCookie(p) {
111
+ const parse = (s) => {
112
+ if (!s) {
113
+ return p.defaultValue;
114
+ }
115
+ if (p.parse) {
116
+ return p.parse(s);
117
+ }
118
+ return JSON.parse(s);
119
+ };
120
+ const stringify = (s) => {
121
+ if (p.stringify) {
122
+ return p.stringify(s);
123
+ }
124
+ return JSON.stringify(s);
125
+ };
126
+ const [cookie, setCookie] = (0, react_1.useState)(getCookieWrapper(Object.assign(Object.assign({}, p), { parse })) || p.defaultValue);
127
+ const setState = (valueRaw) => {
128
+ const value = (valueRaw instanceof Function ? valueRaw(cookie) : valueRaw);
129
+ setCookieWrapper(Object.assign(Object.assign({}, p), { stringify, value }));
130
+ setCookie(value);
131
+ };
132
+ return [cookie, setState];
109
133
  }
110
134
  exports.useCookie = useCookie;
135
+ const useCookieString = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s || '', stringify: (s) => s }));
136
+ exports.useCookieString = useCookieString;
137
+ const useCookieNumber = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => (!s ? undefined : Number.parseFloat(s)), stringify: (s) => (!s ? '' : s.toString()) }));
138
+ exports.useCookieNumber = useCookieNumber;
139
+ const useCookieBoolean = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s === 'true', stringify: (s) => s.toString() }));
140
+ exports.useCookieBoolean = useCookieBoolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.129",
3
+ "version": "0.0.130",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",