aprog 1.8.3 → 1.8.5

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/index.js CHANGED
@@ -11,7 +11,7 @@ export { router, navigateTo, navi } from './src/router.js';
11
11
  export { encrypt, decrypt } from './src/shifrator.js';
12
12
  export { gemini } from './src/OpenAI.js';
13
13
  export { mathInput, autoPPI } from './src/math.js';
14
- export { cookie, storage } from './src/storage.js';
14
+ export { cookie, storage, cacheKey } from './src/storage.js';
15
15
  export { paginate, parseData } from './src/paginate.js';
16
16
  export { saveItem, loadItem, setSecretKey } from './src/secretStorage.js';
17
17
  export { pushHTMLElementPrototype, pushAfterHTMLElementPrototype, pushBeforeHTMLElementPrototype, dataHTMLElementPrototype } from './src/html.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aprog",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
4
4
  "description": "Допоміжна бібліотека js",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/storage.js CHANGED
@@ -1,10 +1,13 @@
1
+ import {GUID} from "./aprog.js";
2
+
1
3
  export const cookie = {
2
4
  /**
3
- * Отримати значення cookie по імені
5
+ * Отримати значення cookie за ім'ям
4
6
  * @param {string} name
7
+ * @param defaulting
5
8
  * @returns {string|null}
6
9
  */
7
- get(name) {
10
+ get(name, defaulting = null) {
8
11
  const cookies = document.cookie.split('; ');
9
12
  for (let cookie of cookies) {
10
13
  const index = cookie.indexOf('=');
@@ -17,7 +20,19 @@ export const cookie = {
17
20
  return decodeURIComponent(value);
18
21
  }
19
22
  }
20
- return null;
23
+ return defaulting;
24
+ },
25
+
26
+ /**
27
+ * Отримати всі cookie
28
+ * @return {{}}
29
+ */
30
+ getAll() {
31
+ return document.cookie.split('; ').reduce((acc, cookie) => {
32
+ const [key, ...val] = cookie.split('=');
33
+ acc[key] = decodeURIComponent(val.join('='));
34
+ return acc;
35
+ }, {});
21
36
  },
22
37
 
23
38
  /**
@@ -59,3 +74,23 @@ export const storage = {
59
74
  localStorage.removeItem(key);
60
75
  }
61
76
  };
77
+
78
+ /**
79
+ * --- Ключ для кешу на бекенді ---
80
+ * @param {string} name - вводиться як абревіатура у верхньому регістрі
81
+ * @param {boolean} create
82
+ * @return {string}
83
+ */
84
+ export const cacheKey = (name, create = true) => {
85
+ let key = cookie.get(`_key_${name}`);
86
+
87
+ if (!create) return key;
88
+
89
+ if (!key) {
90
+ key = GUID();
91
+ cookie.set(`_key_${name}`, key, 30);
92
+ } else {
93
+ console.warn("load_by_cache");
94
+ }
95
+ return key;
96
+ }