@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.
Files changed (2) hide show
  1. package/dist/index.d.mts +31 -28
  2. 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 FetchOptions = {
157
- /** 代理服务器配置 */proxy?: string;
158
- };
159
- type RequestInit = globalThis.RequestInit & FetchOptions & {
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
- * - params 里传递对象,自动转换为 queryString
172
- * - body 里传递对象,自动 JSON.stringify
173
- * - 可选择使用 to() 转换请求结果为 [Error, Response]
174
- * - 可选择使用 withCache() 缓存请求结果
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
- * // 用法1:直接发送请求
180
- * const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
181
- *
182
- * // 用法2:创建实例
183
- * const api = fetcher("https://nickyzj.run:3030", { headers: { Authorization: "Bearer token" } });
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
- * console.error(error);
195
- * return;
199
+ * // ...
196
200
  * }
197
- * console.log(data);
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, FetchOptions, 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 };
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
- "name": "@nickyzj2023/utils",
3
- "version": "1.0.59",
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
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/Nickyzj628/utils.git"
20
- },
21
- "devDependencies": {
22
- "@biomejs/biome": "^2.4.4",
23
- "@types/node": "^25.5.0",
24
- "tsdown": "^0.21.2",
25
- "typedoc": "^0.28.17",
26
- "typedoc-material-theme": "^1.4.1"
27
- },
28
- "peerDependencies": {
29
- "typescript": "^5.9.3"
30
- },
31
- "scripts": {
32
- "build": "tsdown",
33
- "docs": "typedoc src/index.ts --plugin typedoc-material-theme"
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
+ }