Skip to content

Commit c5ef71e

Browse files
authored
Scalable interceptors (#243)
* v1.0.3 * v1.0.4 * making args for interceptors be objects for scalability
1 parent 2850f53 commit c5ef71e

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

src/__tests__/doFetchArgs.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('doFetchArgs: general usages', (): void => {
9696
cachePolicy: defaults.cachePolicy
9797
})
9898
const interceptors = {
99-
request(options: any) {
99+
async request({ options }: { options: any }) {
100100
options.headers.Authorization = 'Bearer test'
101101
return options
102102
}

src/__tests__/useFetch.test.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ describe('useFetch - BROWSER - interceptors', (): void => {
557557
const wrapper = ({ children }: { children?: ReactNode }): ReactElement => {
558558
const options: Options = {
559559
interceptors: {
560-
request: async (opts, url, path, route) => {
560+
request: async ({ options: opts, url, path, route }) => {
561561
if (path === '/path') {
562562
opts.data = 'path'
563563
}
@@ -569,7 +569,7 @@ describe('useFetch - BROWSER - interceptors', (): void => {
569569
}
570570
return opts
571571
},
572-
async response(res) {
572+
async response({ response: res }) {
573573
if (res.data) res.data = toCamel(res.data)
574574
return res
575575
}
@@ -645,11 +645,11 @@ describe('useFetch - BROWSER - interceptors', (): void => {
645645
const { result, waitForNextUpdate } = renderHook(
646646
() => useFetch('url', {
647647
interceptors: {
648-
async request(options) {
648+
async request({ options }) {
649649
requestCalled++
650650
return options
651651
},
652-
async response(response) {
652+
async response({ response }) {
653653
responseCalled++
654654
return response
655655
}
@@ -1066,9 +1066,9 @@ describe('useFetch - BROWSER - errors', (): void => {
10661066
const wrapperCustomError = ({ children }: { children?: ReactNode }): ReactElement => {
10671067
const options = {
10681068
interceptors: {
1069-
async response(res: Res<any>): Promise<Res<any>> {
1070-
if (!res.ok) throw expectedError
1071-
return res
1069+
async response<TData = any>({ response }: { response: Res<TData> }): Promise<Res<TData>> {
1070+
if (!response.ok) throw expectedError
1071+
return response
10721072
}
10731073
},
10741074
cachePolicy: NO_CACHE

src/doFetchArgs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default async function doFetchArgs<TData = any>(
8181
if (body !== null) opts.body = body
8282

8383
if (requestInterceptor) {
84-
const interceptor = await requestInterceptor(opts, initialURL, path, route)
84+
const interceptor = await requestInterceptor({ options: opts, url: initialURL, path, route })
8585
return interceptor as any
8686
}
8787
return opts

src/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ export type UseFetchObjectReturn<TData> = ReqBase<TData> &
159159
export type UseFetch<TData> = UseFetchArrayReturn<TData> &
160160
UseFetchObjectReturn<TData>
161161

162-
export type Interceptors = {
163-
request?: (options: Options, url: string, path: string, route: string) => Promise<Options> | Options
164-
response?: (response: Res<any>) => Promise<Res<any>>
162+
export type Interceptors<TData = any> = {
163+
request?: ({ options, url, path, route }: { options: Options, url: string, path: string, route: string }) => Promise<Options> | Options
164+
response?: ({ response }: { response: Res<TData> }) => Promise<Res<TData>>
165165
}
166166

167167
// this also holds the response keys. It mimics js Map

src/useFetch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
9292
res.current = response.cached as Res<TData>
9393
const theData = await tryGetData(response.cached, defaults.data, responseType)
9494
res.current.data = theData
95-
res.current = interceptors.response ? await interceptors.response(res.current) : res.current
95+
res.current = interceptors.response ? await interceptors.response({ response: res.current }) : res.current
9696
invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`')
9797
data.current = res.current.data as TData
9898
if (!suspense && mounted.current) forceUpdate()
@@ -124,7 +124,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
124124
newData = await tryGetData(newRes, defaults.data, responseType)
125125
res.current.data = onNewData(data.current, newData)
126126

127-
res.current = interceptors.response ? await interceptors.response(res.current) : res.current
127+
res.current = interceptors.response ? await interceptors.response({ response: res.current }) : res.current
128128
invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`')
129129
data.current = res.current.data as TData
130130

0 commit comments

Comments
 (0)