Skip to content

Commit

Permalink
Merge branch 'master' of github.com:alex-cory/react-usefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Cory committed Apr 17, 2020
2 parents 5f655a4 + c5ef71e commit 84dd39a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/doFetchArgs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions src/__tests__/useFetch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1066,9 +1066,9 @@ describe('useFetch - BROWSER - errors', (): void => {
const wrapperCustomError = ({ children }: { children?: ReactNode }): ReactElement => {
const options = {
interceptors: {
async response(res: Res<any>): Promise<Res<any>> {
if (!res.ok) throw expectedError
return res
async response<TData = any>({ response }: { response: Res<TData> }): Promise<Res<TData>> {
if (!response.ok) throw expectedError
return response
}
},
cachePolicy: NO_CACHE
Expand Down
2 changes: 1 addition & 1 deletion src/doFetchArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default async function doFetchArgs<TData = any>(
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
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ export type UseFetchObjectReturn<TData> = ReqBase<TData> &
export type UseFetch<TData> = UseFetchArrayReturn<TData> &
UseFetchObjectReturn<TData>

export type Interceptors = {
request?: (options: Options, url: string, path: string, route: string) => Promise<Options> | Options
response?: (response: Res<any>) => Promise<Res<any>>
export type Interceptors<TData = any> = {
request?: ({ options, url, path, route }: { options: Options, url: string, path: string, route: string }) => Promise<Options> | Options
response?: ({ response }: { response: Res<TData> }) => Promise<Res<TData>>
}

// this also holds the response keys. It mimics js Map
Expand Down
4 changes: 2 additions & 2 deletions src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
res.current = response.cached as Res<TData>
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()
Expand Down Expand Up @@ -124,7 +124,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
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

Expand Down

0 comments on commit 84dd39a

Please sign in to comment.