Skip to content

Commit

Permalink
Drop default fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jul 21, 2021
1 parent 7afe29b commit f620c2d
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const { data, error, isValidating, mutate } = useSWR(key, fetcher, options)
#### Options

- `suspense = false`: enable React Suspense mode [(details)](#suspense-mode)
- `fetcher = window.fetch`: the default fetcher function
- `fetcher`: the function
- `initialData`: initial data to be returned (note: This is per-hook)
- `revalidateOnMount`: enable or disable automatic revalidation when component is mounted (by default revalidation occurs on mount when initialData is not set, use this flag to force behavior)
- `revalidateOnFocus = true`: auto revalidate when window gets focused
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Configuration<
shouldRetryOnError: boolean
suspense?: boolean
initialData?: Data
fetcher: Fn
fetcher?: Fn
cache: Cache
middlewares?: Middleware[]

Expand Down
2 changes: 0 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { slowConnection } from './env'
import { Configuration, RevalidatorOptions, Revalidator } from '../types'
import { UNDEFINED } from './helper'

const fetcher = (url: string) => fetch(url).then(res => res.json())
const noop = () => {}

// error retry
Expand Down Expand Up @@ -56,7 +55,6 @@ const defaultConfig: Configuration = {
loadingTimeout: slowConnection ? 5000 : 3000,

// providers
fetcher,
compare: dequal,
isPaused: () => false,
cache: wrapCache(new Map()),
Expand Down
23 changes: 0 additions & 23 deletions test/use-swr-integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,27 +357,4 @@ describe('useSWR', () => {
expect(fetcher).toBeCalled()
await screen.findByText('hello, SWR')
})

it('should use fetch api as default fetcher', async () => {
const users = [{ name: 'bob' }, { name: 'sue' }]
global['fetch'] = () => Promise.resolve()
const mockFetch = body =>
Promise.resolve({ json: () => Promise.resolve(body) } as any)
const fn = jest
.spyOn(window, 'fetch')
.mockImplementation(() => mockFetch(users))

function Users() {
const { data } = useSWR('http://localhost:3000/api/users')

return <div>hello, {data && data.map(u => u.name).join(' and ')}</div>
}

render(<Users />)
screen.getByText('hello,')
expect(fn).toBeCalled()

await screen.findByText('hello, bob and sue')
delete global['fetch']
})
})

0 comments on commit f620c2d

Please sign in to comment.