ag-common 0.0.135 → 0.0.136

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.
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Sidebar = void 0;
7
7
  const react_1 = __importDefault(require("react"));
8
8
  const styled_components_1 = __importDefault(require("styled-components"));
9
- const cookie_1 = require("../../helpers/cookie");
9
+ const use_1 = require("../../helpers/cookie/use");
10
10
  const common_1 = require("../../styles/common");
11
11
  const Chevron_1 = require("../Chevron");
12
12
  const Base = styled_components_1.default.div `
@@ -92,7 +92,7 @@ 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 [open, setOpen] = (0, cookie_1.useCookieBoolean)({
95
+ const [open, setOpen] = (0, use_1.useCookieBoolean)({
96
96
  name: key,
97
97
  defaultValue: false,
98
98
  cookieDocument: cookieDocument,
@@ -20,10 +20,9 @@ const string_1 = require("../../../common/helpers/string");
20
20
  let callOpenApiCache;
21
21
  function getCacheKey({ cacheKey, overrideAuth, }) {
22
22
  const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
23
- (0, cookie_1.getCookieWrapper)({
23
+ (0, cookie_1.getCookieString)({
24
24
  name: 'id_token',
25
25
  defaultValue: '',
26
- parse: (s) => s,
27
26
  });
28
27
  let cacheKeyRet;
29
28
  if (cacheKey) {
@@ -26,10 +26,9 @@ 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)({
29
+ const isAuthed = !!(0, cookie_1.getCookieString)({
30
30
  name: 'id_token',
31
31
  defaultValue: '',
32
- parse: (s) => s,
33
32
  });
34
33
  if (isAuthed) {
35
34
  const updated = yield refreshToken();
@@ -0,0 +1,6 @@
1
+ export declare const expireDate = "Thu, 01 Jan 1970 00:00:00 UTC";
2
+ export declare const maxCookieLen = 4000;
3
+ export declare type Dispatch<A> = (value: A) => void;
4
+ export declare type SetStateAction<S> = S | ((prevState: S) => S);
5
+ export declare type ReturnType<T> = [T, Dispatch<SetStateAction<T>>];
6
+ export declare type TParse<T> = (s: string | null | undefined) => T;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.maxCookieLen = exports.expireDate = void 0;
4
+ exports.expireDate = 'Thu, 01 Jan 1970 00:00:00 UTC';
5
+ exports.maxCookieLen = 4000;
@@ -0,0 +1,25 @@
1
+ export declare function getCookieRaw({ name, cookieDocument, }: {
2
+ name: string;
3
+ cookieDocument?: string;
4
+ }): string | undefined;
5
+ /**
6
+ * read chunks. json parse+unb64
7
+ * parse defaults to JSON.parse. must change if want if type is not an object!
8
+ * @param param0
9
+ * @returns
10
+ */
11
+ export declare function getCookieRawWrapper<T>({ name, cookieDocument, defaultValue, parse: parseRaw, }: {
12
+ defaultValue: T;
13
+ name: string;
14
+ cookieDocument?: string;
15
+ /**
16
+ * required for objects. defaults to JSON.parse
17
+ * parse defaults to JSON.parse. must change if want if type is not an object!
18
+ */
19
+ parse?: (s: string) => T;
20
+ }): T;
21
+ export declare const getCookieString: (p: {
22
+ defaultValue: string;
23
+ name: string;
24
+ cookieDocument?: string;
25
+ }) => string;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCookieString = exports.getCookieRawWrapper = exports.getCookieRaw = void 0;
4
+ const log_1 = require("../../../common/helpers/log");
5
+ const string_1 = require("../../../common/helpers/string");
6
+ const set_1 = require("./set");
7
+ function getCookieRaw({ name, cookieDocument, }) {
8
+ const nameeq = `${name}=`;
9
+ const ca1 = cookieDocument || (typeof window !== 'undefined' && document.cookie);
10
+ if (!ca1 || !(ca1 === null || ca1 === void 0 ? void 0 : ca1.trim())) {
11
+ return undefined;
12
+ }
13
+ const ca = ca1.split(';').map((t) => t.trim());
14
+ const c = ca.find((c2) => c2.startsWith(nameeq));
15
+ if (c) {
16
+ const raw = c.substr(nameeq.length, c.length);
17
+ return raw;
18
+ }
19
+ return undefined;
20
+ }
21
+ exports.getCookieRaw = getCookieRaw;
22
+ /**
23
+ * read chunks. json parse+unb64
24
+ * parse defaults to JSON.parse. must change if want if type is not an object!
25
+ * @param param0
26
+ * @returns
27
+ */
28
+ function getCookieRawWrapper({ name, cookieDocument, defaultValue, parse: parseRaw, }) {
29
+ const parse = (s) => {
30
+ if (!s) {
31
+ return defaultValue;
32
+ }
33
+ if (parseRaw) {
34
+ return parseRaw(s);
35
+ }
36
+ return JSON.parse(s);
37
+ };
38
+ let raw = '';
39
+ let currentCount = 0;
40
+ // eslint-disable-next-line no-constant-condition
41
+ while (true) {
42
+ const newv = getCookieRaw({
43
+ name: name + currentCount,
44
+ cookieDocument,
45
+ });
46
+ if (!newv) {
47
+ break;
48
+ }
49
+ raw += newv;
50
+ currentCount += 1;
51
+ }
52
+ try {
53
+ return parse((0, string_1.fromBase64)(raw));
54
+ }
55
+ catch (e) {
56
+ (0, log_1.warn)('cookie error:', e);
57
+ (0, set_1.wipeCookies)(name);
58
+ return defaultValue;
59
+ }
60
+ }
61
+ exports.getCookieRawWrapper = getCookieRawWrapper;
62
+ const getCookieString = (p) => getCookieRawWrapper(Object.assign(Object.assign({}, p), { parse: (s) => s }));
63
+ exports.getCookieString = getCookieString;
@@ -0,0 +1,4 @@
1
+ export * from './const';
2
+ export * from './get';
3
+ export * from './set';
4
+ export * from './use';
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./const"), exports);
14
+ __exportStar(require("./get"), exports);
15
+ __exportStar(require("./set"), exports);
16
+ __exportStar(require("./use"), exports);
@@ -0,0 +1,15 @@
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 setCookieRawWrapper<T>(p: {
8
+ value: T;
9
+ name: string;
10
+ cookieDocument?: string;
11
+ /**
12
+ * required for objects. defaults to JSON.stringify
13
+ */
14
+ stringify?: (s: T) => string;
15
+ }): void;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setCookieRawWrapper = exports.wipeCookies = void 0;
4
+ const log_1 = require("../../../common/helpers/log");
5
+ const string_1 = require("../../../common/helpers/string");
6
+ const const_1 = require("./const");
7
+ const get_1 = require("./get");
8
+ /**
9
+ * expiryDays <0 will delete
10
+ * @param param0
11
+ * @returns
12
+ */
13
+ function setCookieRaw({ name, value, expiryDays = 1, }) {
14
+ if (typeof window === undefined) {
15
+ return;
16
+ }
17
+ const d = new Date();
18
+ d.setTime(d.getTime() + expiryDays * 24 * 60 * 60 * 1000);
19
+ const expires = `expires=${!value || expiryDays < 0 ? const_1.expireDate : d.toUTCString()}`;
20
+ document.cookie = `${name}=${!value ? '' : value};${expires};path=/`;
21
+ }
22
+ function wipeCookies(name) {
23
+ if (typeof window === 'undefined') {
24
+ (0, log_1.warn)('cant wipe cookies on server');
25
+ return;
26
+ }
27
+ let currentCount = 0;
28
+ // eslint-disable-next-line no-constant-condition
29
+ while (true) {
30
+ if ((0, get_1.getCookieRaw)({
31
+ name: name + currentCount,
32
+ cookieDocument: '',
33
+ })) {
34
+ setCookieRaw({ name: name + currentCount, value: '', expiryDays: -1 });
35
+ currentCount += 1;
36
+ }
37
+ else {
38
+ return;
39
+ }
40
+ }
41
+ }
42
+ exports.wipeCookies = wipeCookies;
43
+ /**
44
+ * json+b64 incoming. chunk. write chunks
45
+ * @param p
46
+ * @returns
47
+ */
48
+ function setCookieRawWrapper(p) {
49
+ const stringify = (s) => {
50
+ if (p.stringify) {
51
+ return p.stringify(s);
52
+ }
53
+ return JSON.stringify(s);
54
+ };
55
+ wipeCookies(p.name);
56
+ if (!p.value) {
57
+ return;
58
+ }
59
+ const str = (0, string_1.toBase64)(stringify(p.value));
60
+ const chunks = (0, string_1.chunkString)(str, const_1.maxCookieLen);
61
+ for (const index1 in chunks) {
62
+ const chunk = chunks[index1];
63
+ setCookieRaw(Object.assign(Object.assign({}, p), { name: p.name + index1, value: chunk }));
64
+ }
65
+ }
66
+ exports.setCookieRawWrapper = setCookieRawWrapper;
@@ -0,0 +1,29 @@
1
+ import { TParse, ReturnType } from './const';
2
+ export declare function useCookie<T>(p: {
3
+ defaultValue: T;
4
+ name: string;
5
+ cookieDocument?: string;
6
+ /**
7
+ * required for objects. defaults to JSON.parse
8
+ */
9
+ parse?: TParse<T>;
10
+ /**
11
+ * required for objects. defaults to JSON.stringify
12
+ */
13
+ stringify?: (v: T) => string;
14
+ }): ReturnType<T>;
15
+ export declare const useCookieString: (p: {
16
+ defaultValue: string;
17
+ name: string;
18
+ cookieDocument?: string;
19
+ }) => ReturnType<string>;
20
+ export declare const useCookieNumber: (p: {
21
+ defaultValue: number | undefined;
22
+ name: string;
23
+ cookieDocument?: string;
24
+ }) => ReturnType<number | undefined>;
25
+ export declare const useCookieBoolean: (p: {
26
+ defaultValue: boolean;
27
+ name: string;
28
+ cookieDocument?: string;
29
+ }) => ReturnType<boolean>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCookieBoolean = exports.useCookieNumber = exports.useCookieString = exports.useCookie = void 0;
4
+ const react_1 = require("react");
5
+ const get_1 = require("./get");
6
+ const set_1 = require("./set");
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ function useCookie(p) {
9
+ const parse = (s) => {
10
+ if (!s) {
11
+ return p.defaultValue;
12
+ }
13
+ if (p.parse) {
14
+ return p.parse(s);
15
+ }
16
+ return JSON.parse(s);
17
+ };
18
+ const stringify = (s) => {
19
+ if (p.stringify) {
20
+ return p.stringify(s);
21
+ }
22
+ return JSON.stringify(s);
23
+ };
24
+ const [cookie, setCookie] = (0, react_1.useState)((0, get_1.getCookieRawWrapper)(Object.assign(Object.assign({}, p), { parse })) || p.defaultValue);
25
+ const setState = (valueRaw) => {
26
+ const value = (valueRaw instanceof Function ? valueRaw(cookie) : valueRaw);
27
+ (0, set_1.setCookieRawWrapper)(Object.assign(Object.assign({}, p), { stringify, value }));
28
+ setCookie(value);
29
+ };
30
+ return [cookie, setState];
31
+ }
32
+ exports.useCookie = useCookie;
33
+ const useCookieString = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s || '', stringify: (s) => s }));
34
+ exports.useCookieString = useCookieString;
35
+ const useCookieNumber = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => (!s ? undefined : Number.parseFloat(s)), stringify: (s) => (!s ? '' : s.toString()) }));
36
+ exports.useCookieNumber = useCookieNumber;
37
+ const useCookieBoolean = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s === 'true', stringify: (s) => s.toString() }));
38
+ exports.useCookieBoolean = useCookieBoolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.135",
3
+ "version": "0.0.136",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
@@ -1,62 +0,0 @@
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;
10
- cookieDocument?: string;
11
- /**
12
- * required for objects. defaults to JSON.stringify
13
- */
14
- stringify?: (s: T) => string;
15
- }): void;
16
- /**
17
- * read chunks. json parse+unb64
18
- * @param param0
19
- * @returns
20
- */
21
- export declare function getCookieWrapper<T>({ name, cookieDocument, defaultValue, parse: parseRaw, }: {
22
- defaultValue: T;
23
- name: string;
24
- cookieDocument?: string;
25
- /**
26
- * required for objects. defaults to JSON.parse
27
- */
28
- parse?: (s: string) => T;
29
- }): T;
30
- declare type Dispatch<A> = (value: A) => void;
31
- declare type SetStateAction<S> = S | ((prevState: S) => S);
32
- declare type ReturnType<T> = [T, Dispatch<SetStateAction<T>>];
33
- declare type TParse<T> = (s: string | null | undefined) => T;
34
- export declare function useCookie<T>(p: {
35
- defaultValue: T;
36
- name: string;
37
- cookieDocument?: string;
38
- /**
39
- * required for objects. defaults to JSON.parse
40
- */
41
- parse?: TParse<T>;
42
- /**
43
- * required for objects. defaults to JSON.stringify
44
- */
45
- stringify?: (v: T) => string;
46
- }): ReturnType<T>;
47
- export declare const useCookieString: (p: {
48
- defaultValue: string;
49
- name: string;
50
- cookieDocument?: string;
51
- }) => ReturnType<string>;
52
- export declare const useCookieNumber: (p: {
53
- defaultValue: number | undefined;
54
- name: string;
55
- cookieDocument?: string;
56
- }) => ReturnType<number | undefined>;
57
- export declare const useCookieBoolean: (p: {
58
- defaultValue: boolean;
59
- name: string;
60
- cookieDocument?: string;
61
- }) => ReturnType<boolean>;
62
- export {};
@@ -1,155 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useCookieBoolean = exports.useCookieNumber = exports.useCookieString = exports.useCookie = exports.getCookieWrapper = exports.setCookieWrapper = exports.wipeCookies = void 0;
4
- /* eslint-disable guard-for-in */
5
- /* eslint-disable no-restricted-syntax */
6
- const react_1 = require("react");
7
- const log_1 = require("../../common/helpers/log");
8
- const string_1 = require("../../common/helpers/string");
9
- const expireDate = 'Thu, 01 Jan 1970 00:00:00 UTC';
10
- const maxCookieLen = 4000;
11
- /**
12
- * expiryDays <0 will delete
13
- * @param param0
14
- * @returns
15
- */
16
- function setCookieRaw({ name, value, expiryDays = 1, }) {
17
- if (typeof window === undefined) {
18
- return;
19
- }
20
- const d = new Date();
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=/`;
24
- }
25
- function getCookieRaw({ name, cookieDocument, }) {
26
- const nameeq = `${name}=`;
27
- const ca1 = cookieDocument || (typeof window !== 'undefined' && document.cookie);
28
- if (!ca1 || !(ca1 === null || ca1 === void 0 ? void 0 : ca1.trim())) {
29
- return undefined;
30
- }
31
- const ca = ca1.split(';').map((t) => t.trim());
32
- for (const c of ca) {
33
- if (c.indexOf(nameeq) === 0) {
34
- const raw = c.substr(nameeq.length, c.length);
35
- return raw;
36
- }
37
- }
38
- return undefined;
39
- }
40
- function wipeCookies(name) {
41
- if (typeof window === 'undefined') {
42
- (0, log_1.warn)('cant wipe cookies on server');
43
- return;
44
- }
45
- let currentCount = 0;
46
- // eslint-disable-next-line no-constant-condition
47
- while (true) {
48
- if (getCookieRaw({
49
- name: name + currentCount,
50
- cookieDocument: '',
51
- })) {
52
- setCookieRaw({ name: name + currentCount, value: '', expiryDays: -1 });
53
- currentCount += 1;
54
- }
55
- else {
56
- return;
57
- }
58
- }
59
- }
60
- exports.wipeCookies = wipeCookies;
61
- /**
62
- * json+b64 incoming. chunk. write chunks
63
- * @param p
64
- * @returns
65
- */
66
- function setCookieWrapper(p) {
67
- const stringify = (s) => {
68
- if (p.stringify) {
69
- return p.stringify(s);
70
- }
71
- return JSON.stringify(s);
72
- };
73
- wipeCookies(p.name);
74
- if (!p.value) {
75
- return;
76
- }
77
- const str = (0, string_1.toBase64)(stringify(p.value));
78
- const chunks = (0, string_1.chunkString)(str, maxCookieLen);
79
- for (const index1 in chunks) {
80
- const chunk = chunks[index1];
81
- setCookieRaw(Object.assign(Object.assign({}, p), { name: p.name + index1, value: chunk }));
82
- }
83
- }
84
- exports.setCookieWrapper = setCookieWrapper;
85
- /**
86
- * read chunks. json parse+unb64
87
- * @param param0
88
- * @returns
89
- */
90
- function getCookieWrapper({ name, cookieDocument, defaultValue, parse: parseRaw, }) {
91
- const parse = (s) => {
92
- if (!s) {
93
- return defaultValue;
94
- }
95
- if (parseRaw) {
96
- return parseRaw(s);
97
- }
98
- return JSON.parse(s);
99
- };
100
- let raw = '';
101
- let currentCount = 0;
102
- // eslint-disable-next-line no-constant-condition
103
- while (true) {
104
- const newv = getCookieRaw({
105
- name: name + currentCount,
106
- cookieDocument,
107
- });
108
- if (!newv) {
109
- break;
110
- }
111
- raw += newv;
112
- currentCount += 1;
113
- }
114
- try {
115
- return parse((0, string_1.fromBase64)(raw));
116
- }
117
- catch (e) {
118
- (0, log_1.warn)('cookie error:', e);
119
- wipeCookies(name);
120
- return defaultValue;
121
- }
122
- }
123
- exports.getCookieWrapper = getCookieWrapper;
124
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
125
- function useCookie(p) {
126
- const parse = (s) => {
127
- if (!s) {
128
- return p.defaultValue;
129
- }
130
- if (p.parse) {
131
- return p.parse(s);
132
- }
133
- return JSON.parse(s);
134
- };
135
- const stringify = (s) => {
136
- if (p.stringify) {
137
- return p.stringify(s);
138
- }
139
- return JSON.stringify(s);
140
- };
141
- const [cookie, setCookie] = (0, react_1.useState)(getCookieWrapper(Object.assign(Object.assign({}, p), { parse })) || p.defaultValue);
142
- const setState = (valueRaw) => {
143
- const value = (valueRaw instanceof Function ? valueRaw(cookie) : valueRaw);
144
- setCookieWrapper(Object.assign(Object.assign({}, p), { stringify, value }));
145
- setCookie(value);
146
- };
147
- return [cookie, setState];
148
- }
149
- exports.useCookie = useCookie;
150
- const useCookieString = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s || '', stringify: (s) => s }));
151
- exports.useCookieString = useCookieString;
152
- const useCookieNumber = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => (!s ? undefined : Number.parseFloat(s)), stringify: (s) => (!s ? '' : s.toString()) }));
153
- exports.useCookieNumber = useCookieNumber;
154
- const useCookieBoolean = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s === 'true', stringify: (s) => s.toString() }));
155
- exports.useCookieBoolean = useCookieBoolean;