Skip to content

Commit 3280f44

Browse files
committed
refactor: useRequest supports passing custom plugins #204
1 parent 35b07a4 commit 3280f44

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

src/__tests__/index.test.tsx

+119-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
RECONNECT_LISTENER,
1111
VISIBLE_LISTENER,
1212
} from '../core/utils/listener';
13-
import { useRequest } from '../index';
13+
import { definePlugin, useRequest } from '../index';
1414
import { mount, waitForAll, waitForTime } from './utils';
1515
import { failedRequest, request } from './utils/request';
1616
declare let jsdom: any;
@@ -4511,4 +4511,122 @@ describe('useRequest', () => {
45114511
await waitForTime(500);
45124512
expect(wrapper.loading).toBe(false);
45134513
});
4514+
4515+
test('plugins should be use', async () => {
4516+
enum PluginStatus {
4517+
default = -1,
4518+
onBefore,
4519+
onQuery,
4520+
onSuccess,
4521+
onError,
4522+
onAfter,
4523+
onCancel,
4524+
onMutate,
4525+
}
4526+
4527+
let pluginStatus = PluginStatus.default;
4528+
4529+
let cancelStatus = false;
4530+
let mutateStatus = false;
4531+
let errorStatus = false;
4532+
let isError = false;
4533+
4534+
const testNormalPlugin = definePlugin(() => {
4535+
return {
4536+
onBefore(params) {
4537+
if (!isError) {
4538+
expect(params[0]).toBe('testData');
4539+
}
4540+
expect(pluginStatus).toBe(PluginStatus.default);
4541+
pluginStatus = PluginStatus.onBefore;
4542+
},
4543+
onQuery(service) {
4544+
expect(pluginStatus).toBe(PluginStatus.onBefore);
4545+
pluginStatus = PluginStatus.onQuery;
4546+
return () => service();
4547+
},
4548+
onSuccess(data, params) {
4549+
expect(params[0]).toBe('testData');
4550+
expect(data).toBe('testData');
4551+
expect(pluginStatus).toBe(PluginStatus.onQuery);
4552+
pluginStatus = PluginStatus.onSuccess;
4553+
},
4554+
onAfter(params, data, error) {
4555+
if (!isError) {
4556+
expect(params[0]).toBe('testData');
4557+
expect(data).toBe('testData');
4558+
expect(pluginStatus).toBe(PluginStatus.onSuccess);
4559+
} else {
4560+
expect(data).toBe(undefined);
4561+
expect(error.message).toBe('fail');
4562+
}
4563+
pluginStatus = PluginStatus.default;
4564+
},
4565+
onCancel() {
4566+
expect(cancelStatus).toBe(false);
4567+
cancelStatus = true;
4568+
pluginStatus = PluginStatus.default;
4569+
},
4570+
onMutate(data) {
4571+
expect(mutateStatus).toBe(false);
4572+
expect(data).toBe('mutateData');
4573+
mutateStatus = true;
4574+
pluginStatus = PluginStatus.default;
4575+
},
4576+
onError(error, params) {
4577+
expect(params[0]).toBe('error');
4578+
expect(error.message).toBe('fail');
4579+
expect(errorStatus).toBe(false);
4580+
errorStatus = true;
4581+
pluginStatus = PluginStatus.default;
4582+
},
4583+
};
4584+
});
4585+
const wrapper = mount(
4586+
defineComponent({
4587+
template: '<div/>',
4588+
setup() {
4589+
const { data, runAsync, cancel, mutate } = useRequest(
4590+
params => (isError ? failedRequest() : request(params)),
4591+
{
4592+
defaultParams: ['testData'],
4593+
errorRetryCount: 0,
4594+
},
4595+
[testNormalPlugin],
4596+
);
4597+
return {
4598+
data,
4599+
runAsync,
4600+
cancel,
4601+
mutate,
4602+
};
4603+
},
4604+
}),
4605+
);
4606+
expect(wrapper.data).toBeUndefined();
4607+
await waitForAll();
4608+
expect(wrapper.data).toBe('testData');
4609+
4610+
// test onCancel hooks
4611+
wrapper.runAsync('testData');
4612+
await waitForTime(500);
4613+
expect(cancelStatus).toBe(false);
4614+
wrapper.cancel();
4615+
expect(cancelStatus).toBe(true);
4616+
4617+
// test onMutate
4618+
expect(mutateStatus).toBe(false);
4619+
wrapper.mutate('mutateData');
4620+
expect(mutateStatus).toBe(true);
4621+
4622+
// test onError hooks
4623+
isError = true;
4624+
expect(errorStatus).toBe(false);
4625+
wrapper.runAsync('error').catch(() => {
4626+
return;
4627+
});
4628+
await waitForAll();
4629+
expect(errorStatus).toBe(true);
4630+
isError = false;
4631+
});
45144632
});

src/useRequest.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ import useReadyPlugin from './core/plugins/useReadyPlugin';
77
import useRefreshDepsPlugin from './core/plugins/useRefreshDepsPlugin';
88
import useRefreshOnWindowFocus from './core/plugins/useRefreshOnWindowFocus';
99
import useThrottlePlugin from './core/plugins/useThrottlePlugin';
10-
import type { Options, QueryResult, Service } from './core/types';
10+
import type {
11+
Options,
12+
PluginImplementType,
13+
QueryResult,
14+
Service,
15+
} from './core/types';
1116
import useQuery from './core/useQuery';
1217

1318
function useRequest<R, P extends unknown[] = any>(
1419
service: Service<R, P>,
1520
options?: Options<R, P>,
21+
plugins?: PluginImplementType<R, P>[],
1622
): QueryResult<R, P> {
1723
return useQuery<R, P>(service, options, [
24+
...(plugins || []),
1825
useLoadingDelayPlugin,
1926
useErrorRetryPlugin,
2027
useDebouncePlugin,

0 commit comments

Comments
 (0)