Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: drop the default fetcher #1304

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 to retrieve the remote data source
- `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']
})
})