@koine/browser 1.0.94 → 1.0.95
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.
- package/createStorage.d.ts +8 -7
- package/createStorage.js +51 -44
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/navigateToHash.js +2 -1
- package/navigateToParams.js +2 -1
- package/navigateToUrl.d.ts +7 -0
- package/navigateToUrl.js +12 -0
- package/node/createStorage.js +50 -43
- package/node/index.js +1 -0
- package/node/navigateToHash.js +3 -1
- package/node/navigateToParams.js +3 -1
- package/node/navigateToUrl.js +16 -0
- package/node/storage.js +12 -0
- package/node/storageClient.js +104 -0
- package/package.json +3 -3
- package/storage.d.ts +18 -0
- package/storage.js +9 -0
- package/storageClient.d.ts +10 -0
- package/storageClient.js +100 -0
package/createStorage.d.ts
CHANGED
|
@@ -3,26 +3,27 @@ export declare type CreateStorageConfig = Record<string, any>;
|
|
|
3
3
|
* Utility to create a storage instance to interact with `localStorage` using
|
|
4
4
|
* encrypted (encoded) key/values.
|
|
5
5
|
*/
|
|
6
|
-
export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T
|
|
6
|
+
export declare const createStorage: <T extends CreateStorageConfig>(config: Partial<T>, useSessionStorage?: boolean) => {
|
|
7
7
|
/**
|
|
8
8
|
* Get all storage value (it uses `localStorage.get()`).
|
|
9
9
|
*
|
|
10
10
|
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
11
|
-
*
|
|
11
|
+
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
12
|
+
* returned, otherwise `null`.
|
|
12
13
|
*/
|
|
13
|
-
get<TKey extends keyof T>(key: TKey): T[TKey] | null;
|
|
14
|
+
get<TKey extends keyof T>(key: TKey, defaultValue?: T[TKey] | null | undefined): T[TKey] | null;
|
|
14
15
|
/**
|
|
15
16
|
* Get all storage values (it uses `localStorage.get()`).
|
|
16
17
|
*
|
|
17
18
|
* `undefined` and `null` values are not returned.
|
|
18
19
|
*/
|
|
19
|
-
getAll(): T;
|
|
20
|
+
getAll(defaultValues?: Partial<T> | undefined): T;
|
|
20
21
|
/**
|
|
21
22
|
* Set a storage value (it uses `localStorage.set()`).
|
|
22
23
|
*
|
|
23
24
|
* Non-string values are stringified with `JSON.stringify()`
|
|
24
25
|
*/
|
|
25
|
-
set<TKey_1 extends keyof T
|
|
26
|
+
set<TKey_1 extends Extract<keyof T, string>>(key: TKey_1, value: T[TKey_1]): void;
|
|
26
27
|
/**
|
|
27
28
|
* Set all given storage values (it uses `localStorage.set()`).
|
|
28
29
|
*
|
|
@@ -33,11 +34,11 @@ export declare const createStorage: <T extends CreateStorageConfig>(config: Part
|
|
|
33
34
|
/**
|
|
34
35
|
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
35
36
|
*/
|
|
36
|
-
has<TKey_2 extends keyof T
|
|
37
|
+
has<TKey_2 extends Extract<keyof T, string>>(key: TKey_2): boolean;
|
|
37
38
|
/**
|
|
38
39
|
* Remove a storage value (it uses `localStorage.remove()`).
|
|
39
40
|
*/
|
|
40
|
-
remove<TKey_3 extends keyof T
|
|
41
|
+
remove<TKey_3 extends Extract<keyof T, string>>(key: TKey_3): void;
|
|
41
42
|
/**
|
|
42
43
|
* Clear all storage values (it uses `localStorage.remove()`).
|
|
43
44
|
*/
|
package/createStorage.js
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
import { __assign } from "tslib";
|
|
2
|
-
import { decode, encode, isBrowser, isNullOrUndefined
|
|
2
|
+
import { decode, encode, isBrowser, isNullOrUndefined } from "@koine/utils";
|
|
3
3
|
import { on } from "@koine/dom";
|
|
4
|
+
import { storage } from "./storage";
|
|
4
5
|
/**
|
|
5
6
|
* Utility to create a storage instance to interact with `localStorage` using
|
|
6
7
|
* encrypted (encoded) key/values.
|
|
7
8
|
*/
|
|
8
|
-
export var createStorage = function (config) {
|
|
9
|
-
var
|
|
10
|
-
/**
|
|
11
|
-
* Super minifiable localStorage wrapper with SSR safety
|
|
12
|
-
*/
|
|
13
|
-
var ls = function (method, key, value) {
|
|
14
|
-
return isBrowser
|
|
15
|
-
? localStorage[methodsMap[method]](key, value)
|
|
16
|
-
: function () {
|
|
17
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
18
|
-
console.warn("[@koine/utils] createStorage: localStorage does not exists in this environment.");
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
};
|
|
9
|
+
export var createStorage = function (config, useSessionStorage) {
|
|
10
|
+
var client = useSessionStorage ? storage.s : storage.l;
|
|
22
11
|
var keys = Object.keys(config).reduce(function (map, key) {
|
|
23
12
|
var _a;
|
|
24
13
|
return (__assign(__assign({}, map), (_a = {}, _a[key] = encode(key), _a)));
|
|
@@ -28,33 +17,34 @@ export var createStorage = function (config) {
|
|
|
28
17
|
* Get all storage value (it uses `localStorage.get()`).
|
|
29
18
|
*
|
|
30
19
|
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
31
|
-
*
|
|
20
|
+
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
21
|
+
* returned, otherwise `null`.
|
|
32
22
|
*/
|
|
33
|
-
get: function (key) {
|
|
34
|
-
|
|
35
|
-
if (stored) {
|
|
36
|
-
stored = decode(stored);
|
|
37
|
-
try {
|
|
38
|
-
return JSON.parse(stored);
|
|
39
|
-
}
|
|
40
|
-
catch (_e) {
|
|
41
|
-
return stored;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return null;
|
|
23
|
+
get: function (key, defaultValue) {
|
|
24
|
+
return client.get(keys[key], decode, defaultValue);
|
|
45
25
|
},
|
|
46
26
|
/**
|
|
47
27
|
* Get all storage values (it uses `localStorage.get()`).
|
|
48
28
|
*
|
|
49
29
|
* `undefined` and `null` values are not returned.
|
|
50
30
|
*/
|
|
51
|
-
getAll: function () {
|
|
31
|
+
getAll: function (defaultValues) {
|
|
32
|
+
if (!isBrowser) {
|
|
33
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
34
|
+
console.log("[@koine/utils:createStorage] attempt to use 'getAll' outside of browser.");
|
|
35
|
+
}
|
|
36
|
+
return {};
|
|
37
|
+
}
|
|
52
38
|
var all = {};
|
|
53
39
|
for (var key in keys) {
|
|
54
|
-
var value =
|
|
40
|
+
var value = client.get(key);
|
|
41
|
+
var defaultValue = defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues[key];
|
|
55
42
|
if (!isNullOrUndefined(value)) {
|
|
56
43
|
all[key] = value;
|
|
57
44
|
}
|
|
45
|
+
else if (defaultValue) {
|
|
46
|
+
all[key] = defaultValue;
|
|
47
|
+
}
|
|
58
48
|
}
|
|
59
49
|
return all;
|
|
60
50
|
},
|
|
@@ -64,7 +54,7 @@ export var createStorage = function (config) {
|
|
|
64
54
|
* Non-string values are stringified with `JSON.stringify()`
|
|
65
55
|
*/
|
|
66
56
|
set: function (key, value) {
|
|
67
|
-
|
|
57
|
+
client.set(key, value, encode);
|
|
68
58
|
},
|
|
69
59
|
/**
|
|
70
60
|
* Set all given storage values (it uses `localStorage.set()`).
|
|
@@ -73,13 +63,20 @@ export var createStorage = function (config) {
|
|
|
73
63
|
* and `null` values are removed from the storage
|
|
74
64
|
*/
|
|
75
65
|
setMany: function (newValues) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.set(key, value);
|
|
66
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
67
|
+
if (!isBrowser) {
|
|
68
|
+
console.log("[@koine/utils:createStorage] attempt to use 'setMany' outside of browser.");
|
|
80
69
|
}
|
|
81
|
-
|
|
82
|
-
|
|
70
|
+
}
|
|
71
|
+
if (isBrowser) {
|
|
72
|
+
for (var key in newValues) {
|
|
73
|
+
var value = newValues[key];
|
|
74
|
+
if (!isNullOrUndefined(value)) {
|
|
75
|
+
client.set(key, value);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
client.remove(key);
|
|
79
|
+
}
|
|
83
80
|
}
|
|
84
81
|
}
|
|
85
82
|
},
|
|
@@ -87,21 +84,27 @@ export var createStorage = function (config) {
|
|
|
87
84
|
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
88
85
|
*/
|
|
89
86
|
has: function (key) {
|
|
90
|
-
|
|
91
|
-
return !!stored;
|
|
87
|
+
return client.has(keys[key]);
|
|
92
88
|
},
|
|
93
89
|
/**
|
|
94
90
|
* Remove a storage value (it uses `localStorage.remove()`).
|
|
95
91
|
*/
|
|
96
92
|
remove: function (key) {
|
|
97
|
-
|
|
93
|
+
client.remove(keys[key]);
|
|
98
94
|
},
|
|
99
95
|
/**
|
|
100
96
|
* Clear all storage values (it uses `localStorage.remove()`).
|
|
101
97
|
*/
|
|
102
98
|
clear: function () {
|
|
103
|
-
|
|
104
|
-
|
|
99
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
100
|
+
if (!isBrowser) {
|
|
101
|
+
console.log("[@koine/utils:createStorage] attempt to use 'clear' outside of browser.");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (isBrowser) {
|
|
105
|
+
for (var key in keys) {
|
|
106
|
+
client.remove(keys[key]);
|
|
107
|
+
}
|
|
105
108
|
}
|
|
106
109
|
},
|
|
107
110
|
/**
|
|
@@ -111,6 +114,12 @@ export var createStorage = function (config) {
|
|
|
111
114
|
* Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
|
|
112
115
|
*/
|
|
113
116
|
watch: function (keyToWatch, onRemoved, onAdded) {
|
|
117
|
+
if (!isBrowser) {
|
|
118
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
119
|
+
console.log("[@koine/utils:createStorage] attempt to use 'watch' outside of browser.");
|
|
120
|
+
}
|
|
121
|
+
return function () { return void 0; };
|
|
122
|
+
}
|
|
114
123
|
var handler = function (event) {
|
|
115
124
|
var key = event.key, oldValue = event.oldValue, newValue = event.newValue;
|
|
116
125
|
if (key === keys[keyToWatch]) {
|
|
@@ -122,8 +131,6 @@ export var createStorage = function (config) {
|
|
|
122
131
|
}
|
|
123
132
|
}
|
|
124
133
|
};
|
|
125
|
-
if (!isBrowser)
|
|
126
|
-
return function () { return void 0; };
|
|
127
134
|
var listener = on(window, "storage", handler);
|
|
128
135
|
return listener;
|
|
129
136
|
},
|
package/index.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export * from "./navigateToHashParams";
|
|
|
8
8
|
export * from "./navigateToMergedHashParams";
|
|
9
9
|
export * from "./navigateToMergedParams";
|
|
10
10
|
export * from "./navigateToParams";
|
|
11
|
+
export * from "./navigateToUrl";
|
|
11
12
|
export * from "./navigateWithoutUrlParam";
|
|
12
13
|
export * from "./redirectTo";
|
package/index.js
CHANGED
|
@@ -8,5 +8,6 @@ export * from "./navigateToHashParams";
|
|
|
8
8
|
export * from "./navigateToMergedHashParams";
|
|
9
9
|
export * from "./navigateToMergedParams";
|
|
10
10
|
export * from "./navigateToParams";
|
|
11
|
+
export * from "./navigateToUrl";
|
|
11
12
|
export * from "./navigateWithoutUrlParam";
|
|
12
13
|
export * from "./redirectTo";
|
package/navigateToHash.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import navigateToUrl from "./navigateToUrl";
|
|
1
2
|
/**
|
|
2
3
|
* It updates the browser's location hash by replacing the history state.
|
|
3
4
|
* The non-silent standard way would simply be `location.hash = "#new-hash"`
|
|
@@ -7,6 +8,6 @@
|
|
|
7
8
|
export function navigateToHash(hash) {
|
|
8
9
|
if (hash === void 0) { hash = ""; }
|
|
9
10
|
var pathname = location.pathname, search = location.search;
|
|
10
|
-
|
|
11
|
+
navigateToUrl(pathname + (search ? "?" + search : "") + "#" + hash, true);
|
|
11
12
|
}
|
|
12
13
|
export default navigateToHash;
|
package/navigateToParams.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isBrowser, buildUrlQueryString, } from "@koine/utils";
|
|
2
|
+
import navigateToUrl from "./navigateToUrl";
|
|
2
3
|
/**
|
|
3
4
|
* Change current URL query parameters, it uses `history`.
|
|
4
5
|
*
|
|
@@ -10,7 +11,7 @@ export function navigateToParams(params, replace) {
|
|
|
10
11
|
if (params === void 0) { params = {}; }
|
|
11
12
|
var queryString = typeof params === "string" ? params : buildUrlQueryString(params);
|
|
12
13
|
if (isBrowser) {
|
|
13
|
-
|
|
14
|
+
navigateToUrl(location.pathname + queryString, replace);
|
|
14
15
|
}
|
|
15
16
|
return queryString;
|
|
16
17
|
}
|
package/navigateToUrl.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* It updates the browser's location URL with global `history` object
|
|
3
|
+
*
|
|
4
|
+
* @category location
|
|
5
|
+
*/
|
|
6
|
+
export function navigateToUrl(url, replace) {
|
|
7
|
+
if (url === void 0) { url = ""; }
|
|
8
|
+
if (url) {
|
|
9
|
+
history[replace ? "replaceState" : "pushState"](null, "", url);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export default navigateToUrl;
|
package/node/createStorage.js
CHANGED
|
@@ -4,24 +4,13 @@ exports.createStorage = void 0;
|
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var utils_1 = require("@koine/utils");
|
|
6
6
|
var dom_1 = require("@koine/dom");
|
|
7
|
+
var storage_1 = require("./storage");
|
|
7
8
|
/**
|
|
8
9
|
* Utility to create a storage instance to interact with `localStorage` using
|
|
9
10
|
* encrypted (encoded) key/values.
|
|
10
11
|
*/
|
|
11
|
-
var createStorage = function (config) {
|
|
12
|
-
var
|
|
13
|
-
/**
|
|
14
|
-
* Super minifiable localStorage wrapper with SSR safety
|
|
15
|
-
*/
|
|
16
|
-
var ls = function (method, key, value) {
|
|
17
|
-
return utils_1.isBrowser
|
|
18
|
-
? localStorage[methodsMap[method]](key, value)
|
|
19
|
-
: function () {
|
|
20
|
-
if (process.env["NODE_ENV"] !== "production") {
|
|
21
|
-
console.warn("[@koine/utils] createStorage: localStorage does not exists in this environment.");
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
};
|
|
12
|
+
var createStorage = function (config, useSessionStorage) {
|
|
13
|
+
var client = useSessionStorage ? storage_1.storage.s : storage_1.storage.l;
|
|
25
14
|
var keys = Object.keys(config).reduce(function (map, key) {
|
|
26
15
|
var _a;
|
|
27
16
|
return (tslib_1.__assign(tslib_1.__assign({}, map), (_a = {}, _a[key] = (0, utils_1.encode)(key), _a)));
|
|
@@ -31,33 +20,34 @@ var createStorage = function (config) {
|
|
|
31
20
|
* Get all storage value (it uses `localStorage.get()`).
|
|
32
21
|
*
|
|
33
22
|
* Unparseable values with `JSON.parse()` return their value as it is.
|
|
34
|
-
*
|
|
23
|
+
* On ssr or if the given `key` argument is not found `defaultValue` is
|
|
24
|
+
* returned, otherwise `null`.
|
|
35
25
|
*/
|
|
36
|
-
get: function (key) {
|
|
37
|
-
|
|
38
|
-
if (stored) {
|
|
39
|
-
stored = (0, utils_1.decode)(stored);
|
|
40
|
-
try {
|
|
41
|
-
return JSON.parse(stored);
|
|
42
|
-
}
|
|
43
|
-
catch (_e) {
|
|
44
|
-
return stored;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return null;
|
|
26
|
+
get: function (key, defaultValue) {
|
|
27
|
+
return client.get(keys[key], utils_1.decode, defaultValue);
|
|
48
28
|
},
|
|
49
29
|
/**
|
|
50
30
|
* Get all storage values (it uses `localStorage.get()`).
|
|
51
31
|
*
|
|
52
32
|
* `undefined` and `null` values are not returned.
|
|
53
33
|
*/
|
|
54
|
-
getAll: function () {
|
|
34
|
+
getAll: function (defaultValues) {
|
|
35
|
+
if (!utils_1.isBrowser) {
|
|
36
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
37
|
+
console.log("[@koine/utils:createStorage] attempt to use 'getAll' outside of browser.");
|
|
38
|
+
}
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
55
41
|
var all = {};
|
|
56
42
|
for (var key in keys) {
|
|
57
|
-
var value =
|
|
43
|
+
var value = client.get(key);
|
|
44
|
+
var defaultValue = defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues[key];
|
|
58
45
|
if (!(0, utils_1.isNullOrUndefined)(value)) {
|
|
59
46
|
all[key] = value;
|
|
60
47
|
}
|
|
48
|
+
else if (defaultValue) {
|
|
49
|
+
all[key] = defaultValue;
|
|
50
|
+
}
|
|
61
51
|
}
|
|
62
52
|
return all;
|
|
63
53
|
},
|
|
@@ -67,7 +57,7 @@ var createStorage = function (config) {
|
|
|
67
57
|
* Non-string values are stringified with `JSON.stringify()`
|
|
68
58
|
*/
|
|
69
59
|
set: function (key, value) {
|
|
70
|
-
|
|
60
|
+
client.set(key, value, utils_1.encode);
|
|
71
61
|
},
|
|
72
62
|
/**
|
|
73
63
|
* Set all given storage values (it uses `localStorage.set()`).
|
|
@@ -76,13 +66,20 @@ var createStorage = function (config) {
|
|
|
76
66
|
* and `null` values are removed from the storage
|
|
77
67
|
*/
|
|
78
68
|
setMany: function (newValues) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this.set(key, value);
|
|
69
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
70
|
+
if (!utils_1.isBrowser) {
|
|
71
|
+
console.log("[@koine/utils:createStorage] attempt to use 'setMany' outside of browser.");
|
|
83
72
|
}
|
|
84
|
-
|
|
85
|
-
|
|
73
|
+
}
|
|
74
|
+
if (utils_1.isBrowser) {
|
|
75
|
+
for (var key in newValues) {
|
|
76
|
+
var value = newValues[key];
|
|
77
|
+
if (!(0, utils_1.isNullOrUndefined)(value)) {
|
|
78
|
+
client.set(key, value);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
client.remove(key);
|
|
82
|
+
}
|
|
86
83
|
}
|
|
87
84
|
}
|
|
88
85
|
},
|
|
@@ -90,21 +87,27 @@ var createStorage = function (config) {
|
|
|
90
87
|
* Check if a storage value is _truthy_ (it uses `localStorage.get()`).
|
|
91
88
|
*/
|
|
92
89
|
has: function (key) {
|
|
93
|
-
|
|
94
|
-
return !!stored;
|
|
90
|
+
return client.has(keys[key]);
|
|
95
91
|
},
|
|
96
92
|
/**
|
|
97
93
|
* Remove a storage value (it uses `localStorage.remove()`).
|
|
98
94
|
*/
|
|
99
95
|
remove: function (key) {
|
|
100
|
-
|
|
96
|
+
client.remove(keys[key]);
|
|
101
97
|
},
|
|
102
98
|
/**
|
|
103
99
|
* Clear all storage values (it uses `localStorage.remove()`).
|
|
104
100
|
*/
|
|
105
101
|
clear: function () {
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
103
|
+
if (!utils_1.isBrowser) {
|
|
104
|
+
console.log("[@koine/utils:createStorage] attempt to use 'clear' outside of browser.");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (utils_1.isBrowser) {
|
|
108
|
+
for (var key in keys) {
|
|
109
|
+
client.remove(keys[key]);
|
|
110
|
+
}
|
|
108
111
|
}
|
|
109
112
|
},
|
|
110
113
|
/**
|
|
@@ -114,6 +117,12 @@ var createStorage = function (config) {
|
|
|
114
117
|
* Inspiration from [Multi Tab Logout in React — Redux](https://medium.com/front-end-weekly/multi-tab-logout-in-react-redux-4715f071c7fa)
|
|
115
118
|
*/
|
|
116
119
|
watch: function (keyToWatch, onRemoved, onAdded) {
|
|
120
|
+
if (!utils_1.isBrowser) {
|
|
121
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
122
|
+
console.log("[@koine/utils:createStorage] attempt to use 'watch' outside of browser.");
|
|
123
|
+
}
|
|
124
|
+
return function () { return void 0; };
|
|
125
|
+
}
|
|
117
126
|
var handler = function (event) {
|
|
118
127
|
var key = event.key, oldValue = event.oldValue, newValue = event.newValue;
|
|
119
128
|
if (key === keys[keyToWatch]) {
|
|
@@ -125,8 +134,6 @@ var createStorage = function (config) {
|
|
|
125
134
|
}
|
|
126
135
|
}
|
|
127
136
|
};
|
|
128
|
-
if (!utils_1.isBrowser)
|
|
129
|
-
return function () { return void 0; };
|
|
130
137
|
var listener = (0, dom_1.on)(window, "storage", handler);
|
|
131
138
|
return listener;
|
|
132
139
|
},
|
package/node/index.js
CHANGED
|
@@ -11,5 +11,6 @@ tslib_1.__exportStar(require("./navigateToHashParams"), exports);
|
|
|
11
11
|
tslib_1.__exportStar(require("./navigateToMergedHashParams"), exports);
|
|
12
12
|
tslib_1.__exportStar(require("./navigateToMergedParams"), exports);
|
|
13
13
|
tslib_1.__exportStar(require("./navigateToParams"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./navigateToUrl"), exports);
|
|
14
15
|
tslib_1.__exportStar(require("./navigateWithoutUrlParam"), exports);
|
|
15
16
|
tslib_1.__exportStar(require("./redirectTo"), exports);
|
package/node/navigateToHash.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.navigateToHash = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var navigateToUrl_1 = tslib_1.__importDefault(require("./navigateToUrl"));
|
|
4
6
|
/**
|
|
5
7
|
* It updates the browser's location hash by replacing the history state.
|
|
6
8
|
* The non-silent standard way would simply be `location.hash = "#new-hash"`
|
|
@@ -10,7 +12,7 @@ exports.navigateToHash = void 0;
|
|
|
10
12
|
function navigateToHash(hash) {
|
|
11
13
|
if (hash === void 0) { hash = ""; }
|
|
12
14
|
var pathname = location.pathname, search = location.search;
|
|
13
|
-
|
|
15
|
+
(0, navigateToUrl_1.default)(pathname + (search ? "?" + search : "") + "#" + hash, true);
|
|
14
16
|
}
|
|
15
17
|
exports.navigateToHash = navigateToHash;
|
|
16
18
|
exports.default = navigateToHash;
|
package/node/navigateToParams.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.navigateToParams = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
4
5
|
var utils_1 = require("@koine/utils");
|
|
6
|
+
var navigateToUrl_1 = tslib_1.__importDefault(require("./navigateToUrl"));
|
|
5
7
|
/**
|
|
6
8
|
* Change current URL query parameters, it uses `history`.
|
|
7
9
|
*
|
|
@@ -13,7 +15,7 @@ function navigateToParams(params, replace) {
|
|
|
13
15
|
if (params === void 0) { params = {}; }
|
|
14
16
|
var queryString = typeof params === "string" ? params : (0, utils_1.buildUrlQueryString)(params);
|
|
15
17
|
if (utils_1.isBrowser) {
|
|
16
|
-
|
|
18
|
+
(0, navigateToUrl_1.default)(location.pathname + queryString, replace);
|
|
17
19
|
}
|
|
18
20
|
return queryString;
|
|
19
21
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.navigateToUrl = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* It updates the browser's location URL with global `history` object
|
|
6
|
+
*
|
|
7
|
+
* @category location
|
|
8
|
+
*/
|
|
9
|
+
function navigateToUrl(url, replace) {
|
|
10
|
+
if (url === void 0) { url = ""; }
|
|
11
|
+
if (url) {
|
|
12
|
+
history[replace ? "replaceState" : "pushState"](null, "", url);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.navigateToUrl = navigateToUrl;
|
|
16
|
+
exports.default = navigateToUrl;
|
package/node/storage.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.storage = void 0;
|
|
4
|
+
var storageClient_1 = require("./storageClient");
|
|
5
|
+
/**
|
|
6
|
+
* Storage, for `localStorage` and `sessionStorage`
|
|
7
|
+
*/
|
|
8
|
+
exports.storage = {
|
|
9
|
+
l: (0, storageClient_1.storageClient)(),
|
|
10
|
+
s: (0, storageClient_1.storageClient)(true),
|
|
11
|
+
};
|
|
12
|
+
exports.default = exports.storage;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.storageClient = void 0;
|
|
4
|
+
var utils_1 = require("@koine/utils");
|
|
5
|
+
var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
|
|
6
|
+
/**
|
|
7
|
+
* Super minifiable `local/session Storage` client creator with SSR safety
|
|
8
|
+
*/
|
|
9
|
+
var storageClient = function (useSessionStorage) {
|
|
10
|
+
var nativeMethod = function (method, key, value) {
|
|
11
|
+
return utils_1.isBrowser
|
|
12
|
+
? window[useSessionStorage ? "sessionStorage" : "localStorage"][methodsMap[method]](key, value)
|
|
13
|
+
: function () {
|
|
14
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
15
|
+
console.warn("[@koine/utils:storage]:ls localStorage does not exists outside of browser.");
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
var get = function (key, transform, defaultValue) {
|
|
20
|
+
var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
|
|
21
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
22
|
+
if (!utils_1.isBrowser) {
|
|
23
|
+
console.log("[@koine/utils:storage] called 'get' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (utils_1.isBrowser) {
|
|
27
|
+
var stored = nativeMethod("g", key);
|
|
28
|
+
if (stored) {
|
|
29
|
+
stored = transform ? transform(stored) : stored;
|
|
30
|
+
try {
|
|
31
|
+
var parsed = JSON.parse(stored);
|
|
32
|
+
if (parsed)
|
|
33
|
+
value = parsed;
|
|
34
|
+
}
|
|
35
|
+
catch (_e) {
|
|
36
|
+
value = stored;
|
|
37
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
38
|
+
console.warn("[@koine/utils:storage]: 'get' failed to parse stored value as JSON. Plain '".concat(stored, "' value is returned."));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
};
|
|
45
|
+
var set = function (key, value, transform) {
|
|
46
|
+
if (transform === void 0) { transform = function (value) { return value; }; }
|
|
47
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
48
|
+
if (!utils_1.isBrowser) {
|
|
49
|
+
console.log("[@koine/utils:storage] called 'set' outside of browser does not work.");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (utils_1.isBrowser) {
|
|
53
|
+
try {
|
|
54
|
+
var transformedValue = (0, utils_1.isString)(value)
|
|
55
|
+
? transform(value)
|
|
56
|
+
: transform(JSON.stringify(value));
|
|
57
|
+
nativeMethod("s", key, transformedValue);
|
|
58
|
+
}
|
|
59
|
+
catch (_e) {
|
|
60
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
61
|
+
console.warn("[@koine/utils:createStorage]: 'set' error.", _e);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var remove = function (key) {
|
|
67
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
68
|
+
if (!utils_1.isBrowser) {
|
|
69
|
+
console.log("[@koine/utils:storage] called 'remove' outside of browser does not work.");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (utils_1.isBrowser) {
|
|
73
|
+
try {
|
|
74
|
+
nativeMethod("r", key);
|
|
75
|
+
}
|
|
76
|
+
catch (_e) {
|
|
77
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
78
|
+
console.warn("[@koine/utils:createStorage]: 'remove' error.", _e);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var has = function (key, defaultValue) {
|
|
84
|
+
var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : false;
|
|
85
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
86
|
+
if (!utils_1.isBrowser) {
|
|
87
|
+
console.log("[@koine/utils:storage] called 'has' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (utils_1.isBrowser) {
|
|
91
|
+
var stored = nativeMethod("g", key);
|
|
92
|
+
value = stored !== null && stored !== void 0 ? stored : false;
|
|
93
|
+
}
|
|
94
|
+
return value;
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
get: get,
|
|
98
|
+
set: set,
|
|
99
|
+
remove: remove,
|
|
100
|
+
has: has,
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
exports.storageClient = storageClient;
|
|
104
|
+
exports.default = exports.storageClient;
|
package/package.json
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
"main": "./node/index.js",
|
|
5
5
|
"typings": "./index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@koine/utils": "1.0.
|
|
7
|
+
"@koine/utils": "1.0.95",
|
|
8
8
|
"ts-debounce": "^4.0.0",
|
|
9
|
-
"@koine/dom": "1.0.
|
|
9
|
+
"@koine/dom": "1.0.95",
|
|
10
10
|
"date-fns-tz": "^1.3.7",
|
|
11
11
|
"tslib": "^2.4.0"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {},
|
|
14
|
-
"version": "1.0.
|
|
14
|
+
"version": "1.0.95",
|
|
15
15
|
"module": "./index.js",
|
|
16
16
|
"types": "./index.d.ts"
|
|
17
17
|
}
|
package/storage.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage, for `localStorage` and `sessionStorage`
|
|
3
|
+
*/
|
|
4
|
+
export declare const storage: {
|
|
5
|
+
l: {
|
|
6
|
+
get: <TValue>(key: string, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
|
7
|
+
set: <TValue_1>(key: string, value?: TValue_1 | undefined, transform?: (value: any) => string) => void;
|
|
8
|
+
remove: (key: string) => void;
|
|
9
|
+
has: (key: string, defaultValue?: boolean | undefined) => boolean;
|
|
10
|
+
};
|
|
11
|
+
s: {
|
|
12
|
+
get: <TValue>(key: string, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
|
13
|
+
set: <TValue_1>(key: string, value?: TValue_1 | undefined, transform?: (value: any) => string) => void;
|
|
14
|
+
remove: (key: string) => void;
|
|
15
|
+
has: (key: string, defaultValue?: boolean | undefined) => boolean;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export default storage;
|
package/storage.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Super minifiable `local/session Storage` client creator with SSR safety
|
|
3
|
+
*/
|
|
4
|
+
export declare const storageClient: (useSessionStorage?: boolean) => {
|
|
5
|
+
get: <TValue>(key: string, transform?: ((value: string) => TValue) | undefined, defaultValue?: TValue | null | undefined) => NonNullable<TValue> | null;
|
|
6
|
+
set: <TValue_1>(key: string, value?: TValue_1 | undefined, transform?: (value: any) => string) => void;
|
|
7
|
+
remove: (key: string) => void;
|
|
8
|
+
has: (key: string, defaultValue?: boolean) => boolean;
|
|
9
|
+
};
|
|
10
|
+
export default storageClient;
|
package/storageClient.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { isBrowser, isString } from "@koine/utils";
|
|
2
|
+
var methodsMap = { g: "getItem", s: "setItem", r: "removeItem" };
|
|
3
|
+
/**
|
|
4
|
+
* Super minifiable `local/session Storage` client creator with SSR safety
|
|
5
|
+
*/
|
|
6
|
+
export var storageClient = function (useSessionStorage) {
|
|
7
|
+
var nativeMethod = function (method, key, value) {
|
|
8
|
+
return isBrowser
|
|
9
|
+
? window[useSessionStorage ? "sessionStorage" : "localStorage"][methodsMap[method]](key, value)
|
|
10
|
+
: function () {
|
|
11
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
12
|
+
console.warn("[@koine/utils:storage]:ls localStorage does not exists outside of browser.");
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
var get = function (key, transform, defaultValue) {
|
|
17
|
+
var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
|
|
18
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
19
|
+
if (!isBrowser) {
|
|
20
|
+
console.log("[@koine/utils:storage] called 'get' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (isBrowser) {
|
|
24
|
+
var stored = nativeMethod("g", key);
|
|
25
|
+
if (stored) {
|
|
26
|
+
stored = transform ? transform(stored) : stored;
|
|
27
|
+
try {
|
|
28
|
+
var parsed = JSON.parse(stored);
|
|
29
|
+
if (parsed)
|
|
30
|
+
value = parsed;
|
|
31
|
+
}
|
|
32
|
+
catch (_e) {
|
|
33
|
+
value = stored;
|
|
34
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
35
|
+
console.warn("[@koine/utils:storage]: 'get' failed to parse stored value as JSON. Plain '".concat(stored, "' value is returned."));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
};
|
|
42
|
+
var set = function (key, value, transform) {
|
|
43
|
+
if (transform === void 0) { transform = function (value) { return value; }; }
|
|
44
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
45
|
+
if (!isBrowser) {
|
|
46
|
+
console.log("[@koine/utils:storage] called 'set' outside of browser does not work.");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (isBrowser) {
|
|
50
|
+
try {
|
|
51
|
+
var transformedValue = isString(value)
|
|
52
|
+
? transform(value)
|
|
53
|
+
: transform(JSON.stringify(value));
|
|
54
|
+
nativeMethod("s", key, transformedValue);
|
|
55
|
+
}
|
|
56
|
+
catch (_e) {
|
|
57
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
58
|
+
console.warn("[@koine/utils:createStorage]: 'set' error.", _e);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var remove = function (key) {
|
|
64
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
65
|
+
if (!isBrowser) {
|
|
66
|
+
console.log("[@koine/utils:storage] called 'remove' outside of browser does not work.");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (isBrowser) {
|
|
70
|
+
try {
|
|
71
|
+
nativeMethod("r", key);
|
|
72
|
+
}
|
|
73
|
+
catch (_e) {
|
|
74
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
75
|
+
console.warn("[@koine/utils:createStorage]: 'remove' error.", _e);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var has = function (key, defaultValue) {
|
|
81
|
+
var value = defaultValue !== null && defaultValue !== void 0 ? defaultValue : false;
|
|
82
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
83
|
+
if (!isBrowser) {
|
|
84
|
+
console.log("[@koine/utils:storage] called 'has' outside of browser with default value '".concat(JSON.stringify(defaultValue), "'."));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (isBrowser) {
|
|
88
|
+
var stored = nativeMethod("g", key);
|
|
89
|
+
value = stored !== null && stored !== void 0 ? stored : false;
|
|
90
|
+
}
|
|
91
|
+
return value;
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
get: get,
|
|
95
|
+
set: set,
|
|
96
|
+
remove: remove,
|
|
97
|
+
has: has,
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
export default storageClient;
|