soon-fetch 4.0.0-beta.4 → 4.0.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/README.md +32 -134
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/package.json +11 -8
package/README.md
CHANGED
|
@@ -180,6 +180,10 @@ export const login = soon
|
|
|
180
180
|
login({ username: "admin", password: "123" }).then((res) => {
|
|
181
181
|
localStorage.setItem("token", res.token);
|
|
182
182
|
});
|
|
183
|
+
|
|
184
|
+
//with Params for type safety
|
|
185
|
+
export const getUserById = soon.GET("/user/:id").Params<{ id: number }>().Ok<{ id: number; name: string }>();
|
|
186
|
+
getUserById({ id: 1 }).then((res) => console.log(res));
|
|
183
187
|
```
|
|
184
188
|
|
|
185
189
|
### API
|
|
@@ -270,6 +274,10 @@ const API = createShortApi(
|
|
|
270
274
|
// Usage example
|
|
271
275
|
const getUser = API.GET("/api/users/:id").Ok<{ id: number; name: string }>();
|
|
272
276
|
const userData = await getUser({ id: 1 });
|
|
277
|
+
|
|
278
|
+
// With Params for type safety
|
|
279
|
+
const getUserById = API.GET("/api/users/:id").Params<{ id: number }>().Ok<{ id: number; name: string }>();
|
|
280
|
+
const userData = await getUserById({ id: 1 });
|
|
273
281
|
```
|
|
274
282
|
|
|
275
283
|
#### createShortMethods
|
|
@@ -295,7 +303,7 @@ const methods = createShortMethods(["get", "post"] as const, (method) => {
|
|
|
295
303
|
// Usage: methods.get<{ id: number; name: string }[]>('/api/users')
|
|
296
304
|
```
|
|
297
305
|
|
|
298
|
-
####
|
|
306
|
+
#### parseOptions
|
|
299
307
|
|
|
300
308
|
Parse URL options.
|
|
301
309
|
|
|
@@ -303,17 +311,17 @@ Parse URL options.
|
|
|
303
311
|
|
|
304
312
|
- `urlOptions`: Object containing url, options, baseURL and baseOptions
|
|
305
313
|
|
|
306
|
-
**Returns:**
|
|
314
|
+
**Returns:** Object containing parsed url, options, is_body_json, and abortController
|
|
307
315
|
|
|
308
316
|
**Example:**
|
|
309
317
|
|
|
310
318
|
```typescript
|
|
311
|
-
const
|
|
319
|
+
const parsed = parseOptions({
|
|
312
320
|
url: "/api/users/:id",
|
|
313
321
|
options: { params: { id: "123" } },
|
|
314
322
|
baseURL: "https://api.example.com",
|
|
315
323
|
});
|
|
316
|
-
// Returns:
|
|
324
|
+
// Returns: { url: 'https://api.example.com/api/users/123', options: {...}, is_body_json: false, abortController: AbortController }
|
|
317
325
|
```
|
|
318
326
|
|
|
319
327
|
#### mergeHeaders
|
|
@@ -356,7 +364,7 @@ const mergedSignal = mergeSignals(
|
|
|
356
364
|
);
|
|
357
365
|
```
|
|
358
366
|
|
|
359
|
-
####
|
|
367
|
+
#### parseUrl
|
|
360
368
|
|
|
361
369
|
Merge URL and its related parameters.
|
|
362
370
|
Handle baseURL, path parameters and query parameters to generate complete URL.
|
|
@@ -443,24 +451,6 @@ const key = genRequestKey({
|
|
|
443
451
|
});
|
|
444
452
|
```
|
|
445
453
|
|
|
446
|
-
#### raceAbort
|
|
447
|
-
|
|
448
|
-
Race condition handling function.
|
|
449
|
-
Used to handle request race conditions, terminating previous requests.
|
|
450
|
-
|
|
451
|
-
**Parameters:**
|
|
452
|
-
|
|
453
|
-
- `abortController`: Controller of the current request
|
|
454
|
-
- `controllers`: Array of existing controllers
|
|
455
|
-
|
|
456
|
-
**Example:**
|
|
457
|
-
|
|
458
|
-
```typescript
|
|
459
|
-
const controller = new AbortController();
|
|
460
|
-
const controllers: AbortController[] = [];
|
|
461
|
-
// 注意:raceAbort 函数已不再直接导出,而是通过 createRequestStore 使用
|
|
462
|
-
```
|
|
463
|
-
|
|
464
454
|
#### createRequestStore
|
|
465
455
|
|
|
466
456
|
Create request store instance.
|
|
@@ -568,27 +558,6 @@ const data = await soonFetch<User[]>({
|
|
|
568
558
|
});
|
|
569
559
|
```
|
|
570
560
|
|
|
571
|
-
#### parseWithBase
|
|
572
|
-
|
|
573
|
-
Parse base URL configuration.
|
|
574
|
-
Process baseURL, headers, body and other configuration items to generate final request configuration.
|
|
575
|
-
|
|
576
|
-
**Parameters:**
|
|
577
|
-
|
|
578
|
-
- `urlOptions`: Object containing url, options, baseURL and baseOptions
|
|
579
|
-
|
|
580
|
-
**Returns:** Processed url, options, is_body_json and abortController
|
|
581
|
-
|
|
582
|
-
**Example:**
|
|
583
|
-
|
|
584
|
-
```typescript
|
|
585
|
-
const result = parseWithBase({
|
|
586
|
-
url: "/api/users",
|
|
587
|
-
options: { method: "GET" },
|
|
588
|
-
baseURL: "https://api.example.com",
|
|
589
|
-
});
|
|
590
|
-
```
|
|
591
|
-
|
|
592
561
|
#### toFormData
|
|
593
562
|
|
|
594
563
|
Convert object to FormData.
|
|
@@ -653,28 +622,6 @@ const buffer = await progressReadBody(response.body!, (progress, downloaded, tot
|
|
|
653
622
|
});
|
|
654
623
|
```
|
|
655
624
|
|
|
656
|
-
#### parseOptions
|
|
657
|
-
|
|
658
|
-
Parse and merge request options.
|
|
659
|
-
Used to process request options, including baseURL, headers, and body.
|
|
660
|
-
|
|
661
|
-
**Parameters:**
|
|
662
|
-
|
|
663
|
-
- `urlOptions`: Object containing url, options, baseURL, and baseOptions
|
|
664
|
-
|
|
665
|
-
**Returns:** Object containing parsed url, options, is_body_json, and abortController
|
|
666
|
-
|
|
667
|
-
**Example:**
|
|
668
|
-
|
|
669
|
-
```typescript
|
|
670
|
-
const parsed = parseOptions({
|
|
671
|
-
url: "/api/users",
|
|
672
|
-
options: { method: "GET", query: { page: 1 } },
|
|
673
|
-
baseURL: "https://api.example.com",
|
|
674
|
-
baseOptions: { timeout: 5000 },
|
|
675
|
-
});
|
|
676
|
-
```
|
|
677
|
-
|
|
678
625
|
#### requestWithStore
|
|
679
626
|
|
|
680
627
|
Request wrapper with store support.
|
|
@@ -779,6 +726,10 @@ export const login = soon
|
|
|
779
726
|
login({ username: "admin", password: "123" }).then((res) => {
|
|
780
727
|
localStorage.setItem("token", res.token);
|
|
781
728
|
});
|
|
729
|
+
|
|
730
|
+
//使用 Params 进行类型安全定义
|
|
731
|
+
export const getUserById = soon.GET("/user/:id").Params<{ id: number }>().Ok<{ id: number; name: string }>();
|
|
732
|
+
getUserById({ id: 1 }).then((res) => console.log(res));
|
|
782
733
|
```
|
|
783
734
|
|
|
784
735
|
### 特别功能
|
|
@@ -888,6 +839,10 @@ export default function App() {
|
|
|
888
839
|
login({username:'admin',password:'123'}).then(res=>{
|
|
889
840
|
localStorage.setItem('token', res.token);
|
|
890
841
|
})
|
|
842
|
+
|
|
843
|
+
//使用 Params 进行类型安全定义
|
|
844
|
+
export const getUserById = soon.GET('/user/:id').Params<{ id: number }>().Ok<{ id: number; name: string }>();
|
|
845
|
+
getUserById({ id: 1 }).then(res => console.log(res));
|
|
891
846
|
```
|
|
892
847
|
|
|
893
848
|
### API
|
|
@@ -969,7 +924,7 @@ login({ username: "admin", password: "123" }).then((res) => {
|
|
|
969
924
|
const API = createShortApi(
|
|
970
925
|
async (url, method, params, query, body, options) => {
|
|
971
926
|
// 处理请求逻辑
|
|
972
|
-
const _url =
|
|
927
|
+
const { url: _url } = parseUrl(url, { params, query });
|
|
973
928
|
const response = await fetch(_url, { ...options, method, body });
|
|
974
929
|
return response.json();
|
|
975
930
|
}
|
|
@@ -978,6 +933,10 @@ const API = createShortApi(
|
|
|
978
933
|
// 使用示例
|
|
979
934
|
const getUser = API.GET("/api/users/:id").Ok();
|
|
980
935
|
const userData = await getUser({ id: 1 });
|
|
936
|
+
|
|
937
|
+
// 使用 Params 进行类型安全定义
|
|
938
|
+
const getUserById = API.GET("/api/users/:id").Params<{ id: number }>().Ok<{ id: number; name: string }>();
|
|
939
|
+
const userData = await getUserById({ id: 1 });
|
|
981
940
|
```
|
|
982
941
|
|
|
983
942
|
#### createShortMethods
|
|
@@ -1000,7 +959,7 @@ const methods = createShortMethods(["get", "post"] as const, (method) => {
|
|
|
1000
959
|
// 使用: methods.get('/api/users')
|
|
1001
960
|
```
|
|
1002
961
|
|
|
1003
|
-
####
|
|
962
|
+
#### parseOptions
|
|
1004
963
|
|
|
1005
964
|
解析 URL 选项。
|
|
1006
965
|
|
|
@@ -1008,17 +967,17 @@ const methods = createShortMethods(["get", "post"] as const, (method) => {
|
|
|
1008
967
|
|
|
1009
968
|
- `urlOptions`: 包含 url、options、baseURL 和 baseOptions 的对象
|
|
1010
969
|
|
|
1011
|
-
**返回:**
|
|
970
|
+
**返回:** 包含解析后的 url、options、is_body_json 和 abortController 的对象
|
|
1012
971
|
|
|
1013
972
|
**示例:**
|
|
1014
973
|
|
|
1015
974
|
```typescript
|
|
1016
|
-
const
|
|
975
|
+
const parsed = parseOptions({
|
|
1017
976
|
url: "/api/users/:id",
|
|
1018
977
|
options: { params: { id: "123" } },
|
|
1019
978
|
baseURL: "https://api.example.com",
|
|
1020
979
|
});
|
|
1021
|
-
// 返回:
|
|
980
|
+
// 返回: { url: 'https://api.example.com/api/users/123', options: {...}, is_body_json: false, abortController: AbortController }
|
|
1022
981
|
```
|
|
1023
982
|
|
|
1024
983
|
#### mergeHeaders
|
|
@@ -1061,7 +1020,7 @@ const mergedSignal = mergeSignals(
|
|
|
1061
1020
|
);
|
|
1062
1021
|
```
|
|
1063
1022
|
|
|
1064
|
-
####
|
|
1023
|
+
#### parseUrl
|
|
1065
1024
|
|
|
1066
1025
|
合并 URL 及其相关参数。
|
|
1067
1026
|
处理 baseURL、路径参数和查询参数,生成完整 URL。
|
|
@@ -1076,7 +1035,7 @@ const mergedSignal = mergeSignals(
|
|
|
1076
1035
|
**示例:**
|
|
1077
1036
|
|
|
1078
1037
|
```typescript
|
|
1079
|
-
const url =
|
|
1038
|
+
const url = parseUrl("/api/users/:id", {
|
|
1080
1039
|
params: { id: "123" },
|
|
1081
1040
|
query: { filter: "active" },
|
|
1082
1041
|
baseURL: "https://api.example.com",
|
|
@@ -1148,24 +1107,6 @@ const key = genRequestKey({
|
|
|
1148
1107
|
});
|
|
1149
1108
|
```
|
|
1150
1109
|
|
|
1151
|
-
#### raceAbort
|
|
1152
|
-
|
|
1153
|
-
竞态处理函数。
|
|
1154
|
-
用于处理请求竞态,终止之前的请求。
|
|
1155
|
-
|
|
1156
|
-
**参数:**
|
|
1157
|
-
|
|
1158
|
-
- `abortController`: 当前请求的控制器
|
|
1159
|
-
- `controllers`: 已存在的控制器数组
|
|
1160
|
-
|
|
1161
|
-
**示例:**
|
|
1162
|
-
|
|
1163
|
-
```typescript
|
|
1164
|
-
const controller = new AbortController();
|
|
1165
|
-
const controllers = [];
|
|
1166
|
-
raceAbort(controller, controllers); // 终止之前的请求并添加当前控制器
|
|
1167
|
-
```
|
|
1168
|
-
|
|
1169
1110
|
#### createRequestStore
|
|
1170
1111
|
|
|
1171
1112
|
创建请求存储实例。
|
|
@@ -1273,27 +1214,6 @@ const data = await soonFetch<User[]>({
|
|
|
1273
1214
|
});
|
|
1274
1215
|
```
|
|
1275
1216
|
|
|
1276
|
-
#### parseWithBase
|
|
1277
|
-
|
|
1278
|
-
解析基础 URL 配置。
|
|
1279
|
-
处理 baseURL、headers、body 等配置项,生成最终的请求配置。
|
|
1280
|
-
|
|
1281
|
-
**参数:**
|
|
1282
|
-
|
|
1283
|
-
- `urlOptions`: 包含 url、options、baseURL 和 baseOptions 的对象
|
|
1284
|
-
|
|
1285
|
-
**返回:** 处理后的 url、options、is_body_json 和 abortController
|
|
1286
|
-
|
|
1287
|
-
**示例:**
|
|
1288
|
-
|
|
1289
|
-
```typescript
|
|
1290
|
-
const result = parseWithBase({
|
|
1291
|
-
url: "/api/users",
|
|
1292
|
-
options: { method: "GET" },
|
|
1293
|
-
baseURL: "https://api.example.com",
|
|
1294
|
-
});
|
|
1295
|
-
```
|
|
1296
|
-
|
|
1297
1217
|
#### toFormData
|
|
1298
1218
|
|
|
1299
1219
|
将对象转换为 FormData。
|
|
@@ -1358,28 +1278,6 @@ const buffer = await progressReadBody(response.body, (progress, downloaded, tota
|
|
|
1358
1278
|
});
|
|
1359
1279
|
```
|
|
1360
1280
|
|
|
1361
|
-
#### parseOptions
|
|
1362
|
-
|
|
1363
|
-
解析和合并请求选项。
|
|
1364
|
-
用于处理请求选项,包括 baseURL、headers 和 body。
|
|
1365
|
-
|
|
1366
|
-
**参数:**
|
|
1367
|
-
|
|
1368
|
-
- `urlOptions`: 包含 url、options、baseURL 和 baseOptions 的对象
|
|
1369
|
-
|
|
1370
|
-
**返回:** 包含解析后的 url、options、is_body_json 和 abortController 的对象
|
|
1371
|
-
|
|
1372
|
-
**示例:**
|
|
1373
|
-
|
|
1374
|
-
```typescript
|
|
1375
|
-
const parsed = parseOptions({
|
|
1376
|
-
url: "/api/users",
|
|
1377
|
-
options: { method: "GET", query: { page: 1 } },
|
|
1378
|
-
baseURL: "https://api.example.com",
|
|
1379
|
-
baseOptions: { timeout: 5000 },
|
|
1380
|
-
});
|
|
1381
|
-
```
|
|
1382
|
-
|
|
1383
1281
|
#### requestWithStore
|
|
1384
1282
|
|
|
1385
1283
|
带存储支持的请求包装器。
|
package/dist/index.d.cts
CHANGED
|
@@ -194,14 +194,14 @@ declare function requestWithStore<T>(store: ReturnType<typeof createRequestStore
|
|
|
194
194
|
aborts?: [AbortController] | [];
|
|
195
195
|
staleTime?: number;
|
|
196
196
|
}): Promise<T>;
|
|
197
|
-
declare const soonFetch:
|
|
197
|
+
declare const soonFetch: (config: {
|
|
198
198
|
url: string;
|
|
199
199
|
options?: SoonOptions;
|
|
200
200
|
baseURL?: string;
|
|
201
201
|
baseOptions?: SoonOptions;
|
|
202
202
|
store?: ReturnType<typeof createRequestStore>;
|
|
203
203
|
sortRequestKey?: boolean;
|
|
204
|
-
}) => Promise<
|
|
204
|
+
}) => Promise<Response>;
|
|
205
205
|
declare function createSoon<Options extends SoonOptions>(request: <T>(url: string, options?: Options) => Promise<T>): {
|
|
206
206
|
options: <T>(url: string, options?: Options) => Promise<T>;
|
|
207
207
|
get: <T>(url: string, options?: Options) => Promise<T>;
|
package/dist/index.d.mts
CHANGED
|
@@ -194,14 +194,14 @@ declare function requestWithStore<T>(store: ReturnType<typeof createRequestStore
|
|
|
194
194
|
aborts?: [AbortController] | [];
|
|
195
195
|
staleTime?: number;
|
|
196
196
|
}): Promise<T>;
|
|
197
|
-
declare const soonFetch:
|
|
197
|
+
declare const soonFetch: (config: {
|
|
198
198
|
url: string;
|
|
199
199
|
options?: SoonOptions;
|
|
200
200
|
baseURL?: string;
|
|
201
201
|
baseOptions?: SoonOptions;
|
|
202
202
|
store?: ReturnType<typeof createRequestStore>;
|
|
203
203
|
sortRequestKey?: boolean;
|
|
204
|
-
}) => Promise<
|
|
204
|
+
}) => Promise<Response>;
|
|
205
205
|
declare function createSoon<Options extends SoonOptions>(request: <T>(url: string, options?: Options) => Promise<T>): {
|
|
206
206
|
options: <T>(url: string, options?: Options) => Promise<T>;
|
|
207
207
|
get: <T>(url: string, options?: Options) => Promise<T>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soon-fetch",
|
|
3
|
-
"version": "4.0.0
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "a request lib alternative to axios with timeout, request reusing, race, response cache ...",
|
|
5
5
|
"homepage": "https://github.com/leafio/soon-fetch",
|
|
6
6
|
"type": "module",
|
|
@@ -21,16 +21,19 @@
|
|
|
21
21
|
"request",
|
|
22
22
|
"fetch",
|
|
23
23
|
"lightweight",
|
|
24
|
+
"axios",
|
|
24
25
|
"timeout",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
26
|
+
"request reusing",
|
|
27
|
+
"race",
|
|
28
|
+
"response cache",
|
|
29
|
+
"abort controller"
|
|
27
30
|
],
|
|
28
|
-
"author": "",
|
|
31
|
+
"author": "leaf",
|
|
29
32
|
"license": "MIT",
|
|
30
33
|
"devDependencies": {
|
|
34
|
+
"@types/node": "^20.11.30",
|
|
31
35
|
"tsdown": "^0.21.7",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"vitest": "^2.1.1"
|
|
36
|
+
"typescript": "^5.4.3",
|
|
37
|
+
"vitest": "^1.3.1"
|
|
35
38
|
}
|
|
36
|
-
}
|
|
39
|
+
}
|