Skip to content

Commit

Permalink
feat: use-request middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonYong committed Oct 4, 2023
1 parent c480b1d commit 164095f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/hooks/src/useRequest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export interface UseRequestPluginReturn<TData, TParams extends unknown[]> {
onMutate?: (data: TData) => void
}

export type RequestHook<TData = any, TParams extends any[] = any[]> = (
service: UseRequestService<TData, TParams>,
options: UseRequestOptions<TData, TParams, any>,
plugins: UseRequestPlugin<TData, TParams>[],
) => useRequestResult<TData, TParams>



export type UseRequestMiddleware<TData, TParams extends any[]> = (
useRequestNext: RequestHook<TData, TParams>
) => RequestHook<TData, TParams>

export interface UseRequestBasicOptions<TData, TParams extends unknown[]> {
/**
* Init data.
Expand Down Expand Up @@ -229,6 +241,11 @@ export interface UseRequestBasicOptions<TData, TParams extends unknown[]> {
* If not set, the simple exponential backoff algorithm will be used by default, taking `1000 * 2 ** retryCount`, that is, waiting for 2s for the first retry, and 4s for the second retry. By analogy, if it is greater than 30s, take 30s.
*/
retryInterval?: number

/**
* Middleware
*/
use?: UseRequestMiddleware<TData, TParams>[]
}

export type UseRequestOptions<TData, TParams extends unknown[], TPlugin> = {
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks/src/useRequest/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import useThrottlePlugin from './plugins/useThrottlePlugin'
import useRequestImplement from './useRequestImplement'

import { UseRequestOptions, UseRequestPlugin, UseRequestService } from './types'
import { withArgs } from './utils/resolve-args'

function useRequest<
TData,
Expand Down Expand Up @@ -42,7 +43,7 @@ function useRequest<
useRetryPlugin
]?.filter(Boolean)

return useRequestImplement<TData, TParams>(service, options, [
return withArgs<TData, TParams>(useRequestImplement, options?.use)(service, options, [
...(plugins || []),
...BuiltInPlugins
] as UseRequestPlugin<TData, TParams>[])
Expand Down
16 changes: 16 additions & 0 deletions packages/hooks/src/useRequest/utils/resolve-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RequestHook, UseRequestMiddleware, UseRequestOptions, UseRequestPlugin, UseRequestService, useRequestResult } from '../types'

export const withArgs = <TData, TParams extends any[]>(hook: RequestHook<TData, TParams>, use?: UseRequestMiddleware<TData, TParams>[]) => {
return function useRequestArgs(
service: UseRequestService<TData, TParams>,
options: UseRequestOptions<TData, TParams, any> = {},
plugins: UseRequestPlugin<TData, TParams>[] = [],
): useRequestResult<TData, TParams> {
let next = hook
const middleware = use || []
for (let i = middleware.length; i--;) {
next = middleware[i](next)
}
return next(service, options, plugins)
}
}

0 comments on commit 164095f

Please sign in to comment.