From c5ef71ea51482f88bdbadb5d509ed1598dcba94c Mon Sep 17 00:00:00 2001 From: Alex Cory Date: Thu, 16 Apr 2020 19:08:45 -0700 Subject: [PATCH] Scalable interceptors (#243) * v1.0.3 * v1.0.4 * making args for interceptors be objects for scalability --- src/__tests__/doFetchArgs.test.tsx | 2 +- src/__tests__/useFetch.test.tsx | 14 +++++++------- src/doFetchArgs.ts | 2 +- src/types.ts | 6 +++--- src/useFetch.ts | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/__tests__/doFetchArgs.test.tsx b/src/__tests__/doFetchArgs.test.tsx index 01b356bb..55d13dc8 100644 --- a/src/__tests__/doFetchArgs.test.tsx +++ b/src/__tests__/doFetchArgs.test.tsx @@ -96,7 +96,7 @@ describe('doFetchArgs: general usages', (): void => { cachePolicy: defaults.cachePolicy }) const interceptors = { - request(options: any) { + async request({ options }: { options: any }) { options.headers.Authorization = 'Bearer test' return options } diff --git a/src/__tests__/useFetch.test.tsx b/src/__tests__/useFetch.test.tsx index 4038ec8f..af0e0393 100644 --- a/src/__tests__/useFetch.test.tsx +++ b/src/__tests__/useFetch.test.tsx @@ -557,7 +557,7 @@ describe('useFetch - BROWSER - interceptors', (): void => { const wrapper = ({ children }: { children?: ReactNode }): ReactElement => { const options: Options = { interceptors: { - request: async (opts, url, path, route) => { + request: async ({ options: opts, url, path, route }) => { if (path === '/path') { opts.data = 'path' } @@ -569,7 +569,7 @@ describe('useFetch - BROWSER - interceptors', (): void => { } return opts }, - async response(res) { + async response({ response: res }) { if (res.data) res.data = toCamel(res.data) return res } @@ -645,11 +645,11 @@ describe('useFetch - BROWSER - interceptors', (): void => { const { result, waitForNextUpdate } = renderHook( () => useFetch('url', { interceptors: { - async request(options) { + async request({ options }) { requestCalled++ return options }, - async response(response) { + async response({ response }) { responseCalled++ return response } @@ -1066,9 +1066,9 @@ describe('useFetch - BROWSER - errors', (): void => { const wrapperCustomError = ({ children }: { children?: ReactNode }): ReactElement => { const options = { interceptors: { - async response(res: Res): Promise> { - if (!res.ok) throw expectedError - return res + async response({ response }: { response: Res }): Promise> { + if (!response.ok) throw expectedError + return response } }, cachePolicy: NO_CACHE diff --git a/src/doFetchArgs.ts b/src/doFetchArgs.ts index 3489b434..6ff55854 100644 --- a/src/doFetchArgs.ts +++ b/src/doFetchArgs.ts @@ -81,7 +81,7 @@ export default async function doFetchArgs( if (body !== null) opts.body = body if (requestInterceptor) { - const interceptor = await requestInterceptor(opts, initialURL, path, route) + const interceptor = await requestInterceptor({ options: opts, url: initialURL, path, route }) return interceptor as any } return opts diff --git a/src/types.ts b/src/types.ts index 029c7548..d7b0f49c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -159,9 +159,9 @@ export type UseFetchObjectReturn = ReqBase & export type UseFetch = UseFetchArrayReturn & UseFetchObjectReturn -export type Interceptors = { - request?: (options: Options, url: string, path: string, route: string) => Promise | Options - response?: (response: Res) => Promise> +export type Interceptors = { + request?: ({ options, url, path, route }: { options: Options, url: string, path: string, route: string }) => Promise | Options + response?: ({ response }: { response: Res }) => Promise> } // this also holds the response keys. It mimics js Map diff --git a/src/useFetch.ts b/src/useFetch.ts index 3bb2fb60..849a29c3 100644 --- a/src/useFetch.ts +++ b/src/useFetch.ts @@ -92,7 +92,7 @@ function useFetch(...args: UseFetchArgs): UseFetch { res.current = response.cached as Res const theData = await tryGetData(response.cached, defaults.data, responseType) res.current.data = theData - res.current = interceptors.response ? await interceptors.response(res.current) : res.current + res.current = interceptors.response ? await interceptors.response({ response: res.current }) : res.current invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`') data.current = res.current.data as TData if (!suspense && mounted.current) forceUpdate() @@ -124,7 +124,7 @@ function useFetch(...args: UseFetchArgs): UseFetch { newData = await tryGetData(newRes, defaults.data, responseType) res.current.data = onNewData(data.current, newData) - res.current = interceptors.response ? await interceptors.response(res.current) : res.current + res.current = interceptors.response ? await interceptors.response({ response: res.current }) : res.current invariant('data' in res.current, 'You must have `data` field on the Response returned from your `interceptors.response`') data.current = res.current.data as TData