@nickyzj2023/utils 1.0.59 → 1.0.60
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/dist/index.d.mts +31 -28
- package/package.json +34 -34
package/dist/index.d.mts
CHANGED
|
@@ -153,53 +153,56 @@ type Primitive = number | string | boolean | symbol | bigint | undefined | null;
|
|
|
153
153
|
declare const isPrimitive: (value: any) => value is Primitive;
|
|
154
154
|
//#endregion
|
|
155
155
|
//#region src/network/fetcher.d.ts
|
|
156
|
-
type
|
|
157
|
-
/**
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
type RequestInit = globalThis.RequestInit & {
|
|
157
|
+
/**
|
|
158
|
+
* searchParams 查询参数对象
|
|
159
|
+
*/
|
|
160
160
|
params?: Record<string, any>;
|
|
161
|
+
/**
|
|
162
|
+
* 响应解析器,默认的解析方法为 response.json()
|
|
163
|
+
*/
|
|
161
164
|
parser?: (response: Response) => Promise<any>;
|
|
162
165
|
};
|
|
163
166
|
/**
|
|
164
|
-
* 基于 Fetch API
|
|
167
|
+
* 基于 Fetch API 的请求实例
|
|
165
168
|
* @param baseURL 接口前缀
|
|
166
|
-
* @param baseOptions
|
|
169
|
+
* @param baseOptions 应用于整个实例的请求体,后续请求都会带上
|
|
167
170
|
*
|
|
168
171
|
* @remarks
|
|
169
172
|
* 特性:
|
|
170
|
-
* -
|
|
171
|
-
* -
|
|
172
|
-
* -
|
|
173
|
-
* -
|
|
174
|
-
* -
|
|
175
|
-
* - 支持 proxy 选项
|
|
173
|
+
* - 支持在创建实例、发出请求时合并相同的请求体(后者覆盖前者)
|
|
174
|
+
* - 支持在 GET 的 params 请求体中传递对象
|
|
175
|
+
* - 支持在 POST、PUT 的 body 请求体中传递对象
|
|
176
|
+
* - 可选 to() 函数转换请求结果为 [Error, Response]
|
|
177
|
+
* - 可选 withCache() 函数缓存请求结果
|
|
176
178
|
*
|
|
177
179
|
* @example
|
|
180
|
+
* // 直接发请求
|
|
181
|
+
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world", {
|
|
182
|
+
* params: {
|
|
183
|
+
* page: 2,
|
|
184
|
+
* pageSize: 10,
|
|
185
|
+
* }
|
|
186
|
+
* });
|
|
178
187
|
*
|
|
179
|
-
* //
|
|
180
|
-
* const
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* const res = await api.get<Blog>("/blogs/hello-world", { headers: {...}, params: { page: 1 } }); // 与实例相同的 headers 会覆盖上去,params 会转成 ?page=1 跟到 url 后面
|
|
185
|
-
*
|
|
186
|
-
* // 用法3:使用代理
|
|
187
|
-
* const api = fetcher("https://api.example.com", {
|
|
188
|
-
* proxy: "http://127.0.0.1:7890"
|
|
188
|
+
* // 创建实例,发请求
|
|
189
|
+
* const api = fetcher("https://nickyzj.run:3030", {
|
|
190
|
+
* headers: {
|
|
191
|
+
* Authorization: "Bearer token"
|
|
192
|
+
* }
|
|
189
193
|
* });
|
|
194
|
+
* const res = await api.get<Blog>("/blogs/hello-world");
|
|
190
195
|
*
|
|
191
|
-
* //
|
|
196
|
+
* // 安全返回请求结果,不抛异常
|
|
192
197
|
* const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
|
|
193
198
|
* if (error) {
|
|
194
|
-
*
|
|
195
|
-
* return;
|
|
199
|
+
* // ...
|
|
196
200
|
* }
|
|
197
|
-
*
|
|
201
|
+
* // ...
|
|
198
202
|
*
|
|
199
203
|
* // 缓存请求结果
|
|
200
204
|
* const getBlogs = withCache(api.get);
|
|
201
205
|
* await getBlogs("/blogs");
|
|
202
|
-
* await sleep();
|
|
203
206
|
* await getBlogs("/blogs"); // 不发请求,使用缓存
|
|
204
207
|
*/
|
|
205
208
|
declare const fetcher: (baseURL?: string, baseOptions?: RequestInit) => {
|
|
@@ -485,4 +488,4 @@ declare const sleep: (time?: number) => Promise<unknown>;
|
|
|
485
488
|
*/
|
|
486
489
|
declare const throttle: <T extends (...args: any[]) => any>(fn: T, delay?: number) => (this: any, ...args: Parameters<T>) => void;
|
|
487
490
|
//#endregion
|
|
488
|
-
export { CamelToSnake, Capitalize, Decapitalize, DeepMapKeys, DeepMapValues, Falsy,
|
|
491
|
+
export { CamelToSnake, Capitalize, Decapitalize, DeepMapKeys, DeepMapValues, Falsy, ImageCompressionOptions, LogOptions, Primitive, RequestInit, SetTtl, SnakeToCamel, camelToSnake, capitalize, compactStr, debounce, decapitalize, fetcher, getRealURL, imageUrlToBase64, isFalsy, isNil, isObject, isPrimitive, log, loopUntil, mapKeys, mapValues, mergeObjects, qs, randomInt, sleep, snakeToCamel, throttle, to, withCache };
|
package/package.json
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
2
|
+
"name": "@nickyzj2023/utils",
|
|
3
|
+
"version": "1.0.60",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.mjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.mts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsdown",
|
|
19
|
+
"docs": "typedoc src/index.ts --plugin typedoc-material-theme"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/Nickyzj628/utils.git"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@biomejs/biome": "^2.4.11",
|
|
27
|
+
"@types/node": "^25.6.0",
|
|
28
|
+
"tsdown": "^0.21.7",
|
|
29
|
+
"typedoc": "^0.28.19",
|
|
30
|
+
"typedoc-material-theme": "^1.4.1"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"typescript": "^5.9.3"
|
|
34
|
+
}
|
|
35
|
+
}
|