ag-common 0.0.103 → 0.0.108
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,3 +1,5 @@
|
|
|
1
|
+
export declare const toBase64: (str: string) => string;
|
|
2
|
+
export declare const fromBase64: (str: string) => string;
|
|
1
3
|
export declare const csvJSON: (csv: string) => {}[];
|
|
2
4
|
export declare function trimSide(str: string, fromStart?: boolean, ...params: string[]): string;
|
|
3
5
|
export declare function trim(str: string, ...params: string[]): string;
|
|
@@ -1,6 +1,10 @@
|
|
|
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 = void 0;
|
|
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;
|
|
4
|
+
const toBase64 = (str) => Buffer.from(str).toString('base64');
|
|
5
|
+
exports.toBase64 = toBase64;
|
|
6
|
+
const fromBase64 = (str) => Buffer.from(decodeURIComponent(str), 'base64').toString();
|
|
7
|
+
exports.fromBase64 = fromBase64;
|
|
4
8
|
const csvJSON = (csv) => {
|
|
5
9
|
const lines = csv.split('\n');
|
|
6
10
|
const result = [];
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
export declare const toBase64: (str: string) => string;
|
|
2
|
-
export declare const fromBase64: (str: string) => string;
|
|
3
|
-
export declare const setNodeCookieDocument: (s: string) => void;
|
|
4
1
|
export declare function setCookieWrapper<T>(cname: string, raw: T, exdays?: number): void;
|
|
5
2
|
export declare function wipeCookies(cname: string): void;
|
|
6
|
-
export declare function getCookieWrapper<T>({ cname, defaultValue, }: {
|
|
3
|
+
export declare function getCookieWrapper<T>({ cname, cookieDocument, defaultValue, }: {
|
|
7
4
|
cname: string;
|
|
5
|
+
cookieDocument?: string;
|
|
8
6
|
defaultValue?: string;
|
|
9
7
|
}): T;
|
|
10
|
-
export declare function useCookie<T>({ key, defaultValue, }: {
|
|
8
|
+
export declare function useCookie<T>({ key, defaultValue, cookieDocument, }: {
|
|
11
9
|
key: string;
|
|
12
10
|
defaultValue?: string;
|
|
11
|
+
cookieDocument?: string;
|
|
13
12
|
}): [T, (v: T) => any];
|
|
@@ -1,43 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useCookie = exports.getCookieWrapper = exports.wipeCookies = exports.setCookieWrapper =
|
|
3
|
+
exports.useCookie = exports.getCookieWrapper = exports.wipeCookies = exports.setCookieWrapper = 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
|
|
7
|
+
const string_1 = require("../../common/helpers/string");
|
|
8
8
|
const expireDate = 'Thu, 01 Jan 1970 00:00:00 UTC';
|
|
9
9
|
const maxCookieLen = 4000;
|
|
10
10
|
const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
|
|
11
|
-
const toBase64 = (str) => Buffer.from(str).toString('base64');
|
|
12
|
-
exports.toBase64 = toBase64;
|
|
13
|
-
const fromBase64 = (str) => Buffer.from(decodeURIComponent(str), 'base64').toString();
|
|
14
|
-
exports.fromBase64 = fromBase64;
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
-
if (!process.nodeLocalCookie === undefined) {
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
process.nodeLocalCookie = '';
|
|
19
|
-
}
|
|
20
|
-
const setNodeCookieDocument = (s) => {
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
process.nodeLocalCookie = s;
|
|
23
|
-
};
|
|
24
|
-
exports.setNodeCookieDocument = setNodeCookieDocument;
|
|
25
11
|
function setCookie(cname, raw, exdays = 1) {
|
|
26
|
-
const d = new Date();
|
|
27
|
-
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
|
|
28
|
-
const expires = `expires=${!raw || exdays < 0 ? expireDate : d.toUTCString()}`;
|
|
29
12
|
if (typeof window === undefined) {
|
|
30
|
-
(0, log_1.warn)('cant set cookie with no window object');
|
|
31
13
|
return;
|
|
32
14
|
}
|
|
15
|
+
const d = new Date();
|
|
16
|
+
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
|
|
17
|
+
const expires = `expires=${!raw || exdays < 0 ? expireDate : d.toUTCString()}`;
|
|
33
18
|
document.cookie = `${cname}=${!raw ? '' : raw};${expires};path=/`;
|
|
34
19
|
}
|
|
35
|
-
function getCookie({ cname }) {
|
|
20
|
+
function getCookie({ cname, cookieDocument, }) {
|
|
36
21
|
const name = `${cname}=`;
|
|
37
|
-
const ca1 =
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
-
process.nodeLocalCookie ||
|
|
40
|
-
(typeof window !== 'undefined' && document.cookie);
|
|
22
|
+
const ca1 = cookieDocument || (typeof window !== 'undefined' && document.cookie);
|
|
41
23
|
if (!ca1 || !(ca1 === null || ca1 === void 0 ? void 0 : ca1.trim())) {
|
|
42
24
|
return undefined;
|
|
43
25
|
}
|
|
@@ -58,7 +40,7 @@ function setCookieWrapper(cname, raw, exdays = 1) {
|
|
|
58
40
|
if (!raw) {
|
|
59
41
|
return;
|
|
60
42
|
}
|
|
61
|
-
const str = (0,
|
|
43
|
+
const str = (0, string_1.toBase64)(JSON.stringify(raw));
|
|
62
44
|
let chunks = [str];
|
|
63
45
|
if (str.length > maxCookieLen) {
|
|
64
46
|
chunks = chunkString(str, maxCookieLen);
|
|
@@ -83,13 +65,14 @@ function wipeCookies(cname) {
|
|
|
83
65
|
}
|
|
84
66
|
}
|
|
85
67
|
exports.wipeCookies = wipeCookies;
|
|
86
|
-
function getCookieWrapper({ cname, defaultValue, }) {
|
|
68
|
+
function getCookieWrapper({ cname, cookieDocument, defaultValue, }) {
|
|
87
69
|
let raw = '';
|
|
88
70
|
let currentCount = 0;
|
|
89
71
|
// eslint-disable-next-line no-constant-condition
|
|
90
72
|
while (true) {
|
|
91
73
|
const newv = getCookie({
|
|
92
74
|
cname: cname + currentCount,
|
|
75
|
+
cookieDocument,
|
|
93
76
|
});
|
|
94
77
|
if (!newv) {
|
|
95
78
|
if (currentCount === 0) {
|
|
@@ -101,7 +84,7 @@ function getCookieWrapper({ cname, defaultValue, }) {
|
|
|
101
84
|
return undefined;
|
|
102
85
|
}
|
|
103
86
|
try {
|
|
104
|
-
return JSON.parse((0,
|
|
87
|
+
return JSON.parse((0, string_1.fromBase64)(raw));
|
|
105
88
|
}
|
|
106
89
|
catch (e) {
|
|
107
90
|
wipeCookies(cname);
|
|
@@ -114,8 +97,8 @@ function getCookieWrapper({ cname, defaultValue, }) {
|
|
|
114
97
|
}
|
|
115
98
|
exports.getCookieWrapper = getCookieWrapper;
|
|
116
99
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
|
-
function useCookie({ key, defaultValue, }) {
|
|
118
|
-
const [cookie, setC] = (0, react_1.useState)(getCookieWrapper({ cname: key, defaultValue }));
|
|
100
|
+
function useCookie({ key, defaultValue, cookieDocument, }) {
|
|
101
|
+
const [cookie, setC] = (0, react_1.useState)(getCookieWrapper({ cname: key, defaultValue, cookieDocument }));
|
|
119
102
|
return [
|
|
120
103
|
cookie,
|
|
121
104
|
(v) => {
|
|
@@ -5,16 +5,8 @@ const react_1 = require("react");
|
|
|
5
5
|
const log_1 = require("../../common/helpers/log");
|
|
6
6
|
const object_1 = require("../../common/helpers/object");
|
|
7
7
|
const getTimeSeconds = () => Math.ceil(new Date().getTime() / 1000);
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
if (!process.nodeLocalStorage) {
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
-
process.nodeLocalStorage = {};
|
|
13
|
-
}
|
|
14
8
|
const clearLocalStorageItem = (key) => {
|
|
15
9
|
if (typeof window === 'undefined') {
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
-
delete process.nodeLocalStorage[key];
|
|
18
10
|
return;
|
|
19
11
|
}
|
|
20
12
|
try {
|
|
@@ -28,8 +20,6 @@ exports.clearLocalStorageItem = clearLocalStorageItem;
|
|
|
28
20
|
const clearAllLocalStorage = (except) => {
|
|
29
21
|
try {
|
|
30
22
|
if (typeof window === 'undefined') {
|
|
31
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
-
process.nodeLocalStorage = {};
|
|
33
23
|
return;
|
|
34
24
|
}
|
|
35
25
|
for (let i = 0; i < localStorage.length; i += 1) {
|
|
@@ -48,8 +38,6 @@ exports.clearAllLocalStorage = clearAllLocalStorage;
|
|
|
48
38
|
const setLocalStorageItem = (key, value, ttl) => {
|
|
49
39
|
try {
|
|
50
40
|
if (typeof window === 'undefined') {
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
-
process.nodeLocalStorage[key] = value;
|
|
53
41
|
return;
|
|
54
42
|
}
|
|
55
43
|
const set = {
|
|
@@ -66,9 +54,7 @@ const setLocalStorageItem = (key, value, ttl) => {
|
|
|
66
54
|
exports.setLocalStorageItem = setLocalStorageItem;
|
|
67
55
|
const getLocalStorageItem = (key, initialValue, ttl) => {
|
|
68
56
|
if (typeof window === 'undefined') {
|
|
69
|
-
|
|
70
|
-
const value = process.nodeLocalStorage[key] || initialValue;
|
|
71
|
-
return value;
|
|
57
|
+
return initialValue;
|
|
72
58
|
}
|
|
73
59
|
const itemraw = window.localStorage.getItem(key);
|
|
74
60
|
const item = (0, object_1.tryJsonParse)(itemraw, undefined);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ag-common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.108",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"author": "Andrei Gec <@andreigec> (https://gec.dev/)",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"start": "tsc --watch"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"aws-sdk": "2.
|
|
16
|
+
"aws-sdk": "2.1064.0",
|
|
17
17
|
"axios": "0.25.0",
|
|
18
18
|
"jsonwebtoken": "8.5.1",
|
|
19
19
|
"jwks-rsa": "2.0.5",
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/jsonwebtoken": "8.5.8",
|
|
31
|
-
"@types/node": "17.0.
|
|
31
|
+
"@types/node": "17.0.12",
|
|
32
32
|
"@types/react": "17.0.38",
|
|
33
33
|
"@types/react-dom": "17.0.11",
|
|
34
34
|
"@types/styled-components": "5.1.21",
|
|
35
|
-
"@typescript-eslint/eslint-plugin": "5.10.
|
|
36
|
-
"@typescript-eslint/parser": "5.10.
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "5.10.1",
|
|
36
|
+
"@typescript-eslint/parser": "5.10.1",
|
|
37
37
|
"eslint": "8.7.0",
|
|
38
38
|
"eslint-config-airbnb-typescript": "16.1.0",
|
|
39
39
|
"eslint-config-prettier": "8.3.0",
|