@tais00/helpers 0.7.0
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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/base-request.d.ts +16 -0
- package/dist/gzip.d.ts +8 -0
- package/dist/id.d.ts +15 -0
- package/dist/index.cjs +284 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +207 -0
- package/dist/localstorage.d.ts +34 -0
- package/dist/responsive/index.d.ts +16 -0
- package/dist/vars.d.ts +6 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present, Lexmin0412.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Rslib project
|
|
2
|
+
|
|
3
|
+
## Setup
|
|
4
|
+
|
|
5
|
+
Install the dependencies:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Get started
|
|
12
|
+
|
|
13
|
+
Build the library:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Build the library in watch mode:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm dev
|
|
23
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class BaseRequest {
|
|
2
|
+
constructor(options: {
|
|
3
|
+
baseURL: string;
|
|
4
|
+
headers?: Record<string, string>;
|
|
5
|
+
});
|
|
6
|
+
options: {
|
|
7
|
+
baseURL: string;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
baseRequest: (url: string, options: RequestInit) => Promise<Response>;
|
|
11
|
+
jsonRequest: (url: string, options: RequestInit) => Promise<unknown>;
|
|
12
|
+
get: (url: string, params?: Record<string, string>, headers?: Record<string, string>) => Promise<unknown>;
|
|
13
|
+
post: (url: string, params?: Record<string, unknown>, headers?: Record<string, string>) => Promise<unknown>;
|
|
14
|
+
put: (url: string, params?: Record<string, unknown>, headers?: Record<string, string>) => Promise<unknown>;
|
|
15
|
+
delete: (url: string, params?: Record<string, unknown>, headers?: Record<string, string>) => Promise<Response>;
|
|
16
|
+
}
|
package/dist/gzip.d.ts
ADDED
package/dist/id.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 是否为临时 ID
|
|
3
|
+
*/
|
|
4
|
+
export declare const isTempId: (id: string | undefined) => boolean;
|
|
5
|
+
/**
|
|
6
|
+
* 生成符合 RFC 4122 标准的 UUID v4
|
|
7
|
+
* 支持浏览器和 Node.js 环境,不依赖第三方库
|
|
8
|
+
*/
|
|
9
|
+
export declare const generateUuidV4: () => string;
|
|
10
|
+
/**
|
|
11
|
+
* 验证 UUID 格式是否正确
|
|
12
|
+
* @param uuid 要验证的 UUID 字符串
|
|
13
|
+
* @returns 是否为有效的 UUID 格式
|
|
14
|
+
*/
|
|
15
|
+
export declare const isValidUuid: (uuid: string) => boolean;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
+
__webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
});
|
|
9
|
+
return getter;
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
14
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: definition[key]
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
})();
|
|
20
|
+
(()=>{
|
|
21
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
22
|
+
})();
|
|
23
|
+
(()=>{
|
|
24
|
+
__webpack_require__.r = (exports1)=>{
|
|
25
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
26
|
+
value: 'Module'
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
29
|
+
value: true
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
})();
|
|
33
|
+
var __webpack_exports__ = {};
|
|
34
|
+
__webpack_require__.r(__webpack_exports__);
|
|
35
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
36
|
+
LocalStorageStore: ()=>LocalStorageStore,
|
|
37
|
+
generateUuidV4: ()=>generateUuidV4,
|
|
38
|
+
DEFAULT_RESPONSIVE_CONFIG: ()=>DEFAULT_RESPONSIVE_CONFIG,
|
|
39
|
+
initResponsiveConfig: ()=>initResponsiveConfig,
|
|
40
|
+
isValidUuid: ()=>isValidUuid,
|
|
41
|
+
isTempId: ()=>isTempId,
|
|
42
|
+
LocalStorageKeys: ()=>LocalStorageKeys,
|
|
43
|
+
BaseRequest: ()=>BaseRequest,
|
|
44
|
+
genLocalStorageKey: ()=>genLocalStorageKey,
|
|
45
|
+
unParseGzipString: ()=>unParseGzipString,
|
|
46
|
+
useIsMobile: ()=>useIsMobile,
|
|
47
|
+
DIFY_INFO: ()=>DIFY_INFO
|
|
48
|
+
});
|
|
49
|
+
function _define_property(obj, key, value) {
|
|
50
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
51
|
+
value: value,
|
|
52
|
+
enumerable: true,
|
|
53
|
+
configurable: true,
|
|
54
|
+
writable: true
|
|
55
|
+
});
|
|
56
|
+
else obj[key] = value;
|
|
57
|
+
return obj;
|
|
58
|
+
}
|
|
59
|
+
class BaseRequest {
|
|
60
|
+
constructor(options){
|
|
61
|
+
_define_property(this, "options", void 0);
|
|
62
|
+
_define_property(this, "baseRequest", async (url, options)=>{
|
|
63
|
+
const result = await fetch(`${this.options.baseURL}${url}`, {
|
|
64
|
+
...options,
|
|
65
|
+
headers: {
|
|
66
|
+
...this.options.headers,
|
|
67
|
+
...options.headers
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return result;
|
|
71
|
+
});
|
|
72
|
+
_define_property(this, "jsonRequest", async (url, options)=>{
|
|
73
|
+
const result = await this.baseRequest(url, {
|
|
74
|
+
...options,
|
|
75
|
+
headers: {
|
|
76
|
+
...options.headers,
|
|
77
|
+
'Content-Type': 'application/json'
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return result.json();
|
|
81
|
+
});
|
|
82
|
+
_define_property(this, "get", async (url, params = {}, headers = {})=>{
|
|
83
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
84
|
+
const queryString = filteredParams && Object.keys(filteredParams).length > 0 ? `?${new URLSearchParams(filteredParams).toString()}` : '';
|
|
85
|
+
const result = await this.jsonRequest(`${url}${queryString}`, {
|
|
86
|
+
method: 'GET',
|
|
87
|
+
headers
|
|
88
|
+
});
|
|
89
|
+
return result;
|
|
90
|
+
});
|
|
91
|
+
_define_property(this, "post", async (url, params = {}, headers = {})=>{
|
|
92
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
93
|
+
const result = await this.jsonRequest(url, {
|
|
94
|
+
method: 'POST',
|
|
95
|
+
body: JSON.stringify(filteredParams),
|
|
96
|
+
headers
|
|
97
|
+
});
|
|
98
|
+
return result;
|
|
99
|
+
});
|
|
100
|
+
_define_property(this, "put", async (url, params = {}, headers = {})=>{
|
|
101
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
102
|
+
const result = await this.jsonRequest(url, {
|
|
103
|
+
method: 'PUT',
|
|
104
|
+
body: JSON.stringify(filteredParams),
|
|
105
|
+
headers
|
|
106
|
+
});
|
|
107
|
+
return result;
|
|
108
|
+
});
|
|
109
|
+
_define_property(this, "delete", async (url, params = {}, headers = {})=>{
|
|
110
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
111
|
+
const result = await this.baseRequest(url, {
|
|
112
|
+
method: 'DELETE',
|
|
113
|
+
body: JSON.stringify(filteredParams),
|
|
114
|
+
headers: {
|
|
115
|
+
...headers,
|
|
116
|
+
'Content-Type': 'application/json'
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return result;
|
|
120
|
+
});
|
|
121
|
+
this.options = options;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const isTempId = (id)=>{
|
|
125
|
+
if (!id) return false;
|
|
126
|
+
return id.startsWith('temp');
|
|
127
|
+
};
|
|
128
|
+
const generateUuidV4 = ()=>{
|
|
129
|
+
let randomBytes;
|
|
130
|
+
randomBytes = 'undefined' != typeof crypto ? crypto.getRandomValues ? (size)=>{
|
|
131
|
+
const array = new Uint8Array(size);
|
|
132
|
+
crypto.getRandomValues(array);
|
|
133
|
+
return array;
|
|
134
|
+
} : 'randomBytes' in crypto ? crypto.randomBytes : (size)=>{
|
|
135
|
+
const array = new Uint8Array(size);
|
|
136
|
+
for(let i = 0; i < size; i++)array[i] = Math.floor(256 * Math.random());
|
|
137
|
+
return array;
|
|
138
|
+
} : (size)=>{
|
|
139
|
+
const array = new Uint8Array(size);
|
|
140
|
+
for(let i = 0; i < size; i++)array[i] = Math.floor(256 * Math.random());
|
|
141
|
+
return array;
|
|
142
|
+
};
|
|
143
|
+
const bytes = randomBytes(16);
|
|
144
|
+
bytes[6] = 0x0f & bytes[6] | 0x40;
|
|
145
|
+
bytes[8] = 0x3f & bytes[8] | 0x80;
|
|
146
|
+
const hex = Array.from(bytes, (byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
147
|
+
return [
|
|
148
|
+
hex.slice(0, 8),
|
|
149
|
+
hex.slice(8, 12),
|
|
150
|
+
hex.slice(12, 16),
|
|
151
|
+
hex.slice(16, 20),
|
|
152
|
+
hex.slice(20, 32)
|
|
153
|
+
].join('-');
|
|
154
|
+
};
|
|
155
|
+
const isValidUuid = (uuid)=>{
|
|
156
|
+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
157
|
+
return uuidRegex.test(uuid);
|
|
158
|
+
};
|
|
159
|
+
const external_pako_namespaceObject = require("pako");
|
|
160
|
+
var external_pako_default = /*#__PURE__*/ __webpack_require__.n(external_pako_namespaceObject);
|
|
161
|
+
const unParseGzipString = (encodedStr)=>{
|
|
162
|
+
try {
|
|
163
|
+
const binaryString = atob(encodedStr);
|
|
164
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
165
|
+
for(let i = 0; i < binaryString.length; i++)bytes[i] = binaryString.charCodeAt(i);
|
|
166
|
+
const decompressedData = external_pako_default().inflate(bytes, {
|
|
167
|
+
to: 'string'
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
error: false,
|
|
171
|
+
data: decompressedData
|
|
172
|
+
};
|
|
173
|
+
} catch (error) {
|
|
174
|
+
console.error("\u89E3\u538B\u7F29\u8FC7\u7A0B\u4E2D\u51FA\u73B0\u9519\u8BEF:", error);
|
|
175
|
+
return {
|
|
176
|
+
error: error,
|
|
177
|
+
data: ''
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
function localstorage_define_property(obj, key, value) {
|
|
182
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
183
|
+
value: value,
|
|
184
|
+
enumerable: true,
|
|
185
|
+
configurable: true,
|
|
186
|
+
writable: true
|
|
187
|
+
});
|
|
188
|
+
else obj[key] = value;
|
|
189
|
+
return obj;
|
|
190
|
+
}
|
|
191
|
+
const KEY_PREFIX = '__DC__';
|
|
192
|
+
const LocalStorageKeyList = [
|
|
193
|
+
'USER_ID',
|
|
194
|
+
'ENABLE_SETTING',
|
|
195
|
+
'THEME',
|
|
196
|
+
'THEME_MODE',
|
|
197
|
+
'RUNNING_MODE',
|
|
198
|
+
'ENABLE_SETTING'
|
|
199
|
+
];
|
|
200
|
+
const LocalStorageKeys = LocalStorageKeyList.reduce((acc, key)=>{
|
|
201
|
+
acc[key] = key;
|
|
202
|
+
return acc;
|
|
203
|
+
}, {});
|
|
204
|
+
const genLocalStorageKey = (key)=>`${KEY_PREFIX}${key}`;
|
|
205
|
+
class LocalStorageStoreBuilder {
|
|
206
|
+
constructor(){
|
|
207
|
+
localstorage_define_property(this, "validateKey", (key)=>{
|
|
208
|
+
if (!key) throw new Error('key is required');
|
|
209
|
+
if (!LocalStorageKeyList.some((item)=>item === key)) throw new Error(`key is not valid, must be one of: ${LocalStorageKeyList.join(',')}`);
|
|
210
|
+
return true;
|
|
211
|
+
});
|
|
212
|
+
localstorage_define_property(this, "get", (key)=>{
|
|
213
|
+
this.validateKey(key);
|
|
214
|
+
const storageKey = genLocalStorageKey(key);
|
|
215
|
+
const rawValue = localStorage.getItem(storageKey);
|
|
216
|
+
let value;
|
|
217
|
+
try {
|
|
218
|
+
value = JSON.parse(rawValue);
|
|
219
|
+
} catch (_error) {
|
|
220
|
+
value = rawValue;
|
|
221
|
+
}
|
|
222
|
+
return value;
|
|
223
|
+
});
|
|
224
|
+
localstorage_define_property(this, "set", (key, value)=>{
|
|
225
|
+
this.validateKey(key);
|
|
226
|
+
if ('object' == typeof value && null !== value) value = JSON.stringify(value);
|
|
227
|
+
const storageKey = genLocalStorageKey(key);
|
|
228
|
+
localStorage.setItem(storageKey, value);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const LocalStorageStore = new LocalStorageStoreBuilder();
|
|
233
|
+
const external_ahooks_namespaceObject = require("ahooks");
|
|
234
|
+
const DEFAULT_RESPONSIVE_CONFIG = {
|
|
235
|
+
sm: 0,
|
|
236
|
+
md: 768,
|
|
237
|
+
lg: 1024,
|
|
238
|
+
xl: 1280,
|
|
239
|
+
'2xl': 1536
|
|
240
|
+
};
|
|
241
|
+
const initResponsiveConfig = (config = DEFAULT_RESPONSIVE_CONFIG)=>(0, external_ahooks_namespaceObject.configResponsive)(config);
|
|
242
|
+
const useIsMobile = ()=>{
|
|
243
|
+
const responsive = (0, external_ahooks_namespaceObject.useResponsive)();
|
|
244
|
+
const { sm, md } = responsive || {};
|
|
245
|
+
return !!sm && !md;
|
|
246
|
+
};
|
|
247
|
+
const DIFY_VERSION_KEY = 'DIFY_CHAT__DIFY_VERSION';
|
|
248
|
+
const DIFY_INFO = {
|
|
249
|
+
get version () {
|
|
250
|
+
return localStorage.getItem(DIFY_VERSION_KEY) || '';
|
|
251
|
+
},
|
|
252
|
+
set version (version){
|
|
253
|
+
localStorage.setItem(DIFY_VERSION_KEY, version);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
exports.BaseRequest = __webpack_exports__.BaseRequest;
|
|
257
|
+
exports.DEFAULT_RESPONSIVE_CONFIG = __webpack_exports__.DEFAULT_RESPONSIVE_CONFIG;
|
|
258
|
+
exports.DIFY_INFO = __webpack_exports__.DIFY_INFO;
|
|
259
|
+
exports.LocalStorageKeys = __webpack_exports__.LocalStorageKeys;
|
|
260
|
+
exports.LocalStorageStore = __webpack_exports__.LocalStorageStore;
|
|
261
|
+
exports.genLocalStorageKey = __webpack_exports__.genLocalStorageKey;
|
|
262
|
+
exports.generateUuidV4 = __webpack_exports__.generateUuidV4;
|
|
263
|
+
exports.initResponsiveConfig = __webpack_exports__.initResponsiveConfig;
|
|
264
|
+
exports.isTempId = __webpack_exports__.isTempId;
|
|
265
|
+
exports.isValidUuid = __webpack_exports__.isValidUuid;
|
|
266
|
+
exports.unParseGzipString = __webpack_exports__.unParseGzipString;
|
|
267
|
+
exports.useIsMobile = __webpack_exports__.useIsMobile;
|
|
268
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
269
|
+
"BaseRequest",
|
|
270
|
+
"DEFAULT_RESPONSIVE_CONFIG",
|
|
271
|
+
"DIFY_INFO",
|
|
272
|
+
"LocalStorageKeys",
|
|
273
|
+
"LocalStorageStore",
|
|
274
|
+
"genLocalStorageKey",
|
|
275
|
+
"generateUuidV4",
|
|
276
|
+
"initResponsiveConfig",
|
|
277
|
+
"isTempId",
|
|
278
|
+
"isValidUuid",
|
|
279
|
+
"unParseGzipString",
|
|
280
|
+
"useIsMobile"
|
|
281
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
282
|
+
Object.defineProperty(exports, '__esModule', {
|
|
283
|
+
value: true
|
|
284
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import pako from "pako";
|
|
2
|
+
import { configResponsive, useResponsive } from "ahooks";
|
|
3
|
+
function _define_property(obj, key, value) {
|
|
4
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
5
|
+
value: value,
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true
|
|
9
|
+
});
|
|
10
|
+
else obj[key] = value;
|
|
11
|
+
return obj;
|
|
12
|
+
}
|
|
13
|
+
class BaseRequest {
|
|
14
|
+
constructor(options){
|
|
15
|
+
_define_property(this, "options", void 0);
|
|
16
|
+
_define_property(this, "baseRequest", async (url, options)=>{
|
|
17
|
+
const result = await fetch(`${this.options.baseURL}${url}`, {
|
|
18
|
+
...options,
|
|
19
|
+
headers: {
|
|
20
|
+
...this.options.headers,
|
|
21
|
+
...options.headers
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
});
|
|
26
|
+
_define_property(this, "jsonRequest", async (url, options)=>{
|
|
27
|
+
const result = await this.baseRequest(url, {
|
|
28
|
+
...options,
|
|
29
|
+
headers: {
|
|
30
|
+
...options.headers,
|
|
31
|
+
'Content-Type': 'application/json'
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return result.json();
|
|
35
|
+
});
|
|
36
|
+
_define_property(this, "get", async (url, params = {}, headers = {})=>{
|
|
37
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
38
|
+
const queryString = filteredParams && Object.keys(filteredParams).length > 0 ? `?${new URLSearchParams(filteredParams).toString()}` : '';
|
|
39
|
+
const result = await this.jsonRequest(`${url}${queryString}`, {
|
|
40
|
+
method: 'GET',
|
|
41
|
+
headers
|
|
42
|
+
});
|
|
43
|
+
return result;
|
|
44
|
+
});
|
|
45
|
+
_define_property(this, "post", async (url, params = {}, headers = {})=>{
|
|
46
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
47
|
+
const result = await this.jsonRequest(url, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
body: JSON.stringify(filteredParams),
|
|
50
|
+
headers
|
|
51
|
+
});
|
|
52
|
+
return result;
|
|
53
|
+
});
|
|
54
|
+
_define_property(this, "put", async (url, params = {}, headers = {})=>{
|
|
55
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
56
|
+
const result = await this.jsonRequest(url, {
|
|
57
|
+
method: 'PUT',
|
|
58
|
+
body: JSON.stringify(filteredParams),
|
|
59
|
+
headers
|
|
60
|
+
});
|
|
61
|
+
return result;
|
|
62
|
+
});
|
|
63
|
+
_define_property(this, "delete", async (url, params = {}, headers = {})=>{
|
|
64
|
+
const filteredParams = Object.fromEntries(Object.entries(params).filter(([_, value])=>void 0 !== value));
|
|
65
|
+
const result = await this.baseRequest(url, {
|
|
66
|
+
method: 'DELETE',
|
|
67
|
+
body: JSON.stringify(filteredParams),
|
|
68
|
+
headers: {
|
|
69
|
+
...headers,
|
|
70
|
+
'Content-Type': 'application/json'
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return result;
|
|
74
|
+
});
|
|
75
|
+
this.options = options;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const isTempId = (id)=>{
|
|
79
|
+
if (!id) return false;
|
|
80
|
+
return id.startsWith('temp');
|
|
81
|
+
};
|
|
82
|
+
const generateUuidV4 = ()=>{
|
|
83
|
+
let randomBytes;
|
|
84
|
+
randomBytes = 'undefined' != typeof crypto ? crypto.getRandomValues ? (size)=>{
|
|
85
|
+
const array = new Uint8Array(size);
|
|
86
|
+
crypto.getRandomValues(array);
|
|
87
|
+
return array;
|
|
88
|
+
} : 'randomBytes' in crypto ? crypto.randomBytes : (size)=>{
|
|
89
|
+
const array = new Uint8Array(size);
|
|
90
|
+
for(let i = 0; i < size; i++)array[i] = Math.floor(256 * Math.random());
|
|
91
|
+
return array;
|
|
92
|
+
} : (size)=>{
|
|
93
|
+
const array = new Uint8Array(size);
|
|
94
|
+
for(let i = 0; i < size; i++)array[i] = Math.floor(256 * Math.random());
|
|
95
|
+
return array;
|
|
96
|
+
};
|
|
97
|
+
const bytes = randomBytes(16);
|
|
98
|
+
bytes[6] = 0x0f & bytes[6] | 0x40;
|
|
99
|
+
bytes[8] = 0x3f & bytes[8] | 0x80;
|
|
100
|
+
const hex = Array.from(bytes, (byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
101
|
+
return [
|
|
102
|
+
hex.slice(0, 8),
|
|
103
|
+
hex.slice(8, 12),
|
|
104
|
+
hex.slice(12, 16),
|
|
105
|
+
hex.slice(16, 20),
|
|
106
|
+
hex.slice(20, 32)
|
|
107
|
+
].join('-');
|
|
108
|
+
};
|
|
109
|
+
const isValidUuid = (uuid)=>{
|
|
110
|
+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
111
|
+
return uuidRegex.test(uuid);
|
|
112
|
+
};
|
|
113
|
+
const unParseGzipString = (encodedStr)=>{
|
|
114
|
+
try {
|
|
115
|
+
const binaryString = atob(encodedStr);
|
|
116
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
117
|
+
for(let i = 0; i < binaryString.length; i++)bytes[i] = binaryString.charCodeAt(i);
|
|
118
|
+
const decompressedData = pako.inflate(bytes, {
|
|
119
|
+
to: 'string'
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
error: false,
|
|
123
|
+
data: decompressedData
|
|
124
|
+
};
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error("\u89E3\u538B\u7F29\u8FC7\u7A0B\u4E2D\u51FA\u73B0\u9519\u8BEF:", error);
|
|
127
|
+
return {
|
|
128
|
+
error: error,
|
|
129
|
+
data: ''
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
function localstorage_define_property(obj, key, value) {
|
|
134
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
135
|
+
value: value,
|
|
136
|
+
enumerable: true,
|
|
137
|
+
configurable: true,
|
|
138
|
+
writable: true
|
|
139
|
+
});
|
|
140
|
+
else obj[key] = value;
|
|
141
|
+
return obj;
|
|
142
|
+
}
|
|
143
|
+
const KEY_PREFIX = '__DC__';
|
|
144
|
+
const LocalStorageKeyList = [
|
|
145
|
+
'USER_ID',
|
|
146
|
+
'ENABLE_SETTING',
|
|
147
|
+
'THEME',
|
|
148
|
+
'THEME_MODE',
|
|
149
|
+
'RUNNING_MODE',
|
|
150
|
+
'ENABLE_SETTING'
|
|
151
|
+
];
|
|
152
|
+
const LocalStorageKeys = LocalStorageKeyList.reduce((acc, key)=>{
|
|
153
|
+
acc[key] = key;
|
|
154
|
+
return acc;
|
|
155
|
+
}, {});
|
|
156
|
+
const genLocalStorageKey = (key)=>`${KEY_PREFIX}${key}`;
|
|
157
|
+
class LocalStorageStoreBuilder {
|
|
158
|
+
constructor(){
|
|
159
|
+
localstorage_define_property(this, "validateKey", (key)=>{
|
|
160
|
+
if (!key) throw new Error('key is required');
|
|
161
|
+
if (!LocalStorageKeyList.some((item)=>item === key)) throw new Error(`key is not valid, must be one of: ${LocalStorageKeyList.join(',')}`);
|
|
162
|
+
return true;
|
|
163
|
+
});
|
|
164
|
+
localstorage_define_property(this, "get", (key)=>{
|
|
165
|
+
this.validateKey(key);
|
|
166
|
+
const storageKey = genLocalStorageKey(key);
|
|
167
|
+
const rawValue = localStorage.getItem(storageKey);
|
|
168
|
+
let value;
|
|
169
|
+
try {
|
|
170
|
+
value = JSON.parse(rawValue);
|
|
171
|
+
} catch (_error) {
|
|
172
|
+
value = rawValue;
|
|
173
|
+
}
|
|
174
|
+
return value;
|
|
175
|
+
});
|
|
176
|
+
localstorage_define_property(this, "set", (key, value)=>{
|
|
177
|
+
this.validateKey(key);
|
|
178
|
+
if ('object' == typeof value && null !== value) value = JSON.stringify(value);
|
|
179
|
+
const storageKey = genLocalStorageKey(key);
|
|
180
|
+
localStorage.setItem(storageKey, value);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const LocalStorageStore = new LocalStorageStoreBuilder();
|
|
185
|
+
const DEFAULT_RESPONSIVE_CONFIG = {
|
|
186
|
+
sm: 0,
|
|
187
|
+
md: 768,
|
|
188
|
+
lg: 1024,
|
|
189
|
+
xl: 1280,
|
|
190
|
+
'2xl': 1536
|
|
191
|
+
};
|
|
192
|
+
const initResponsiveConfig = (config = DEFAULT_RESPONSIVE_CONFIG)=>configResponsive(config);
|
|
193
|
+
const useIsMobile = ()=>{
|
|
194
|
+
const responsive = useResponsive();
|
|
195
|
+
const { sm, md } = responsive || {};
|
|
196
|
+
return !!sm && !md;
|
|
197
|
+
};
|
|
198
|
+
const DIFY_VERSION_KEY = 'DIFY_CHAT__DIFY_VERSION';
|
|
199
|
+
const DIFY_INFO = {
|
|
200
|
+
get version () {
|
|
201
|
+
return localStorage.getItem(DIFY_VERSION_KEY) || '';
|
|
202
|
+
},
|
|
203
|
+
set version (version){
|
|
204
|
+
localStorage.setItem(DIFY_VERSION_KEY, version);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
export { BaseRequest, DEFAULT_RESPONSIVE_CONFIG, DIFY_INFO, LocalStorageKeys, LocalStorageStore, genLocalStorageKey, generateUuidV4, initResponsiveConfig, isTempId, isValidUuid, unParseGzipString, useIsMobile };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
declare const LocalStorageKeyList: readonly ["USER_ID", "ENABLE_SETTING", "THEME", "THEME_MODE", "RUNNING_MODE", "ENABLE_SETTING"];
|
|
2
|
+
export declare const LocalStorageKeys: {
|
|
3
|
+
USER_ID: "USER_ID";
|
|
4
|
+
ENABLE_SETTING: "ENABLE_SETTING";
|
|
5
|
+
THEME: "THEME";
|
|
6
|
+
THEME_MODE: "THEME_MODE";
|
|
7
|
+
RUNNING_MODE: "RUNNING_MODE";
|
|
8
|
+
};
|
|
9
|
+
type ILocalStorageKey = (typeof LocalStorageKeyList)[number];
|
|
10
|
+
/**
|
|
11
|
+
* 生成 localStorage key
|
|
12
|
+
*/
|
|
13
|
+
export declare const genLocalStorageKey: (key: ILocalStorageKey) => string;
|
|
14
|
+
/**
|
|
15
|
+
* LocalStorage 操作封装
|
|
16
|
+
*/
|
|
17
|
+
declare class LocalStorageStoreBuilder {
|
|
18
|
+
validateKey: (key: string) => boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 获取 localStorage 值
|
|
21
|
+
*/
|
|
22
|
+
get: (key: ILocalStorageKey) => any;
|
|
23
|
+
/**
|
|
24
|
+
* 设置 localStorage 值
|
|
25
|
+
* @param key 必须是 LocalStorageKeys 中的 key
|
|
26
|
+
* @param value 必须是 string 类型
|
|
27
|
+
*/
|
|
28
|
+
set: (key: ILocalStorageKey, value: string) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* LocalStorage 操作实例
|
|
32
|
+
*/
|
|
33
|
+
export declare const LocalStorageStore: LocalStorageStoreBuilder;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 与 tailwind 保持一致的的响应式配置
|
|
3
|
+
*/
|
|
4
|
+
export type ITailwindCompatibleResponsiveConfig = Record<'sm' | 'md' | 'lg' | 'xl' | '2xl', number>;
|
|
5
|
+
/**
|
|
6
|
+
* 默认的响应式配置
|
|
7
|
+
*/
|
|
8
|
+
export declare const DEFAULT_RESPONSIVE_CONFIG: ITailwindCompatibleResponsiveConfig;
|
|
9
|
+
/**
|
|
10
|
+
* 初始化响应式配置
|
|
11
|
+
*/
|
|
12
|
+
export declare const initResponsiveConfig: (config?: ITailwindCompatibleResponsiveConfig) => void;
|
|
13
|
+
/**
|
|
14
|
+
* 是否是移动端
|
|
15
|
+
*/
|
|
16
|
+
export declare const useIsMobile: () => boolean;
|
package/dist/vars.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tais00/helpers",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Dify Chat 的辅助工具包",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "lexmin0412",
|
|
7
|
+
"email": "zhangle_dev@outlook.com",
|
|
8
|
+
"url": "http://github.com/lexmin0412"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/lexmin0412/dify-chat.git",
|
|
13
|
+
"directory": "packages/helpers"
|
|
14
|
+
},
|
|
15
|
+
"source": "./src/index.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./dist/index.cjs",
|
|
21
|
+
"module": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"source": "./src/index.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"require": "./dist/index.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"registry": "https://registry.npmjs.org"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"ahooks": "^3.8.4",
|
|
37
|
+
"pako": "^2.1.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@biomejs/biome": "^1.9.4",
|
|
41
|
+
"@rslib/core": "^0.12.4",
|
|
42
|
+
"@types/node": "^22.14.1",
|
|
43
|
+
"@types/pako": "^2.0.3",
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"vitest": "^3.2.3"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "rslib build",
|
|
49
|
+
"check": "biome check --write",
|
|
50
|
+
"dev": "rslib build --watch",
|
|
51
|
+
"format": "biome format --write",
|
|
52
|
+
"test": "vitest run"
|
|
53
|
+
}
|
|
54
|
+
}
|