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

fix(nuxt): respect custom timeout in useFetch #24364

Merged
merged 11 commits into from Nov 20, 2023
1 change: 1 addition & 0 deletions docs/3.api/2.composables/use-fetch.md
Expand Up @@ -80,6 +80,7 @@ const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
- `body`: Request body - automatically stringified (if an object is passed).
- `headers`: Request headers.
- `baseURL`: Base URL for the request.
- `timeout`: Milliseconds to automatically abort request

::callout
All fetch options can be given a `computed` or `ref` value. These will be watched and new requests made automatically with any new values if they are updated.
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt/src/app/composables/fetch.ts
Expand Up @@ -140,6 +140,10 @@ export function useFetch<
controller?.abort?.()
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController

if(toValue(opts.timeout)){
luc122c marked this conversation as resolved.
Show resolved Hide resolved
danielroe marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(() => controller.abort(), toValue(opts.timeout));
danielroe marked this conversation as resolved.
Show resolved Hide resolved
}

let _$fetch = opts.$fetch || globalThis.$fetch

// Use fetch with request context and headers for server direct API calls
Expand Down
7 changes: 7 additions & 0 deletions test/nuxt/composables.test.ts
Expand Up @@ -266,6 +266,13 @@ describe('useFetch', () => {
await useFetch('/api/test', { params: { id: ref('3') } }, '')
expect.soft(getPayloadEntries()).toBe(baseCount + 3)
})

it('should timeout', async () => {
const { pending, status, error } = await useFetch('https://httpbin.org/delay/10', { timeout: 1 })
await pending.value
luc122c marked this conversation as resolved.
Show resolved Hide resolved
expect(status.value).toBe('error')
expect(error.value).toMatchInlineSnapshot('[Error: [GET] "https://httpbin.org/delay/10": <no response> The operation was aborted.]')
})
})

describe('errors', () => {
Expand Down