Skip to content

Commit 924f655

Browse files
authored
Merge pull request #3 from axios-use/feat-callback-req-params
Feat: callback params add request parameter args
2 parents 7376158 + 23a7b2f commit 924f655

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ const [createRequest, { hasPending, cancel }] = useRequest(
135135
method: "DELETE",
136136
}),
137137
{
138-
onCompleted: (data, response) => console.info(data, response),
139-
onError: (err) => console.info(err),
138+
onCompleted: (data, response, paramsArgs) => console.info(data, response, paramsArgs),
139+
onError: (err, paramsArgs) => console.info(err, paramsArgs),
140140
},
141141
);
142142
```
@@ -255,8 +255,8 @@ const [reqState] = useResource(
255255
}),
256256
[],
257257
{
258-
onCompleted: (data, response) => console.info(data, response),
259-
onError: (err) => console.info(err),
258+
onCompleted: (data, response, paramsArgs) => console.info(data, response, paramsArgs),
259+
onError: (err, paramsArgs) => console.info(err, paramsArgs),
260260
},
261261
);
262262
```

src/request.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ export type RequestCallbackFn<T extends Request> = {
7171
* A callback function that's called when your request successfully completes with zero errors.
7272
* This function is passed the request's result `data` and `response`.
7373
*/
74-
onCompleted?: (data: Payload<T, true>, response: Payload<T>) => void;
74+
onCompleted?: (
75+
data: Payload<T, true>,
76+
response: Payload<T>,
77+
args: Parameters<T>,
78+
) => void;
7579
/**
7680
* A callback function that's called when the request encounters one or more errors.
7781
* This function is passed an `RequestError` object that contains either a networkError object or a `AxiosError`, depending on the error(s) that occurred.
7882
*/
79-
onError?: (err: RequestError<Payload<T>, BodyData<T>>) => void;
83+
onError?: (
84+
err: RequestError<Payload<T>, BodyData<T>>,
85+
args: Parameters<T>,
86+
) => void;
8087
};
8188

8289
/**

src/useRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ export function useRequest<T extends Request>(
8383
? options.getResponseItem(res as Payload<T>)
8484
: requestConfig.getResponseItem(res)
8585
) as Payload<T, true>;
86-
onCompleted?.(_data, res as Payload<T>);
86+
onCompleted?.(_data, res as Payload<T>, args);
8787
return [_data, res as Payload<T>] as const;
8888
})
8989
.catch((err: AxiosError<Payload<T>, BodyData<T>>) => {
9090
removeCancelToken(_source.token);
9191

9292
const _error = createRequestError(err);
93-
onError?.(_error);
93+
onError?.(_error, args);
9494

9595
throw _error;
9696
});

tests/useRequest.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ describe("useRequest", () => {
3232

3333
test("any type (without `request`)", async () => {
3434
const [createRequest] = useRequest(getAPIFuncs(true).user.anyTypeList, {
35-
onCompleted(data, response) {
35+
onCompleted(data, response, args) {
3636
expectTypeOf(data).toEqualTypeOf<any>();
3737
expectTypeOf(response).toEqualTypeOf<any>();
38+
expectTypeOf(args).toEqualTypeOf<[]>();
3839
expect(data).toStrictEqual(MOCK_DATA_USER_LIST);
3940
expect(response.data).toStrictEqual(MOCK_DATA_USER_LIST);
41+
expect(args).toStrictEqual([]);
4042
},
4143
});
4244

@@ -52,11 +54,13 @@ describe("useRequest", () => {
5254
const [createRequest] = useRequest(
5355
getAPIFuncs(true).user.anyTypeWithoutGenericityList,
5456
{
55-
onCompleted(data, response) {
57+
onCompleted(data, response, args) {
5658
expectTypeOf(data).toEqualTypeOf<any>();
5759
expectTypeOf(response).toEqualTypeOf<AxiosResponse<any>>();
60+
expectTypeOf(args).toEqualTypeOf<[]>();
5861
expect(data).toStrictEqual(MOCK_DATA_USER_LIST);
5962
expect(response.data).toStrictEqual(MOCK_DATA_USER_LIST);
63+
expect(args).toStrictEqual([]);
6064
},
6165
},
6266
);
@@ -217,11 +221,13 @@ describe("useRequest", () => {
217221
}),
218222
{
219223
instance: _instance,
220-
onCompleted: (d, r) => {
224+
onCompleted: (d, r, args) => {
221225
expect(d).toBeUndefined();
222226
expectTypeOf(d).toMatchTypeOf<undefined>();
223227
expect(r).toStrictEqual(mockItem);
224228
expectTypeOf(r).toMatchTypeOf<MockDataUserItem | undefined>();
229+
expectTypeOf(args).toMatchTypeOf<[string]>();
230+
expect(args).toStrictEqual([TARGET_ID]);
225231
},
226232
},
227233
);
@@ -264,7 +270,7 @@ describe("useRequest", () => {
264270
{
265271
instance: _instance,
266272
getResponseItem: _getResponseItem,
267-
onCompleted: (d, r) => {
273+
onCompleted: (d, r, args) => {
268274
expect(d).toStrictEqual(mockItem);
269275
expectTypeOf(d).toMatchTypeOf<MockDataUserItem | undefined>();
270276
expect(r).toStrictEqual({
@@ -273,6 +279,8 @@ describe("useRequest", () => {
273279
message: "OK",
274280
});
275281
expectTypeOf(r).toMatchTypeOf<_MyRes<MockDataUserItem> | undefined>();
282+
expectTypeOf(args).toMatchTypeOf<[string]>();
283+
expect(args).toStrictEqual([TARGET_ID]);
276284
},
277285
},
278286
);
@@ -313,14 +321,16 @@ describe("useRequest", () => {
313321
{
314322
instance: _instance,
315323
getResponseItem: _getResponseItem,
316-
onCompleted: (d, r) => {
324+
onCompleted: (d, r, args) => {
317325
// custom `data` value
318326
expect(d).toStrictEqual(mockItem?.name);
319327
expectTypeOf(d).toMatchTypeOf<string | undefined>();
320328
expect(r.data).toStrictEqual(mockItem);
321329
expectTypeOf(r).toMatchTypeOf<
322330
AxiosResponse<MockDataUserItem> | undefined
323331
>();
332+
expectTypeOf(args).toMatchTypeOf<[string]>();
333+
expect(args).toStrictEqual([TARGET_ID]);
324334
},
325335
},
326336
);

tests/useResource.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ describe("useResource", () => {
2121
setup() {
2222
const id = ref("1");
2323
const params = computed(() => ({ id: unref(id) }));
24-
const [res] = useResource(getAPIFuncs(true).user.get, [params], {
25-
onCompleted: (d, r) => {
24+
const reqFn = getAPIFuncs(true).user.get;
25+
const [res] = useResource(reqFn, [params], {
26+
onCompleted: (d, r, a) => {
2627
expectTypeOf(d).toEqualTypeOf<MockDataUserItem | undefined>();
2728
expectTypeOf(r).toEqualTypeOf<AxiosResponse<MockDataUserItem>>();
29+
expectTypeOf(a).toEqualTypeOf<Parameters<typeof reqFn>>();
2830

2931
const _item = MOCK_DATA_USER_LIST.find((i) => i.id === unref(id));
3032
expect(d).toStrictEqual(_item);
3133
expect(r.data).toStrictEqual(_item);
34+
expect(a).toStrictEqual([{ id: unref(id) }]);
3235
},
3336
});
3437

@@ -86,9 +89,10 @@ describe("useResource", () => {
8689
defineComponent({
8790
setup() {
8891
const [res] = useResource(getAPIFuncs(true).user.anyTypeList, false, {
89-
onCompleted: (d, r) => {
92+
onCompleted: (d, r, a) => {
9093
expectTypeOf(d).toEqualTypeOf<any>();
9194
expectTypeOf(r).toEqualTypeOf<any>();
95+
expectTypeOf(a).toEqualTypeOf<[]>();
9296
},
9397
});
9498

@@ -107,9 +111,10 @@ describe("useResource", () => {
107111
getAPIFuncs(true).user.anyTypeWithoutGenericityList,
108112
false,
109113
{
110-
onCompleted: (d, r) => {
114+
onCompleted: (d, r, a) => {
111115
expectTypeOf(d).toEqualTypeOf<any>();
112116
expectTypeOf(r).toEqualTypeOf<AxiosResponse<any>>();
117+
expectTypeOf(a).toEqualTypeOf<[]>();
113118
},
114119
},
115120
);

0 commit comments

Comments
 (0)