Skip to content

Commit

Permalink
refactor: rename allowFetchErrorReturnData to returnDataOnFetchError
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Jul 31, 2023
1 parent ab1a156 commit 790f171
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/core/useFetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ const { data } = useFetch(url, {
console.log(data.value) // { title: 'Hunter x Hunter' }
```

You can also disallow `onFetchError` to modifies the response data by passing `false` to `allowFetchErrorReturnData` option.
You can also disallow `onFetchError` to modifies the response data by passing `false` to `returnDataOnFetchError` option.
```ts
const { data } = useFetch(url, {
allowFetchErrorReturnData: false,
returnDataOnFetchError: false,
onFetchError(ctx) {
// ctx.data can be null when 5xx response
if (ctx.data === null)
Expand Down
12 changes: 6 additions & 6 deletions packages/core/useFetch/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { until } from '@vueuse/shared'
import { nextTick, ref } from 'vue-demi'
import type { SpyInstance } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { retry } from '../../.test'
import { nextTick, ref } from 'vue-demi'
import { createFetch, useFetch } from '.'
import { retry } from '../../.test'
import '../../.test/mockServer'

const jsonMessage = { hello: 'world' }
Expand Down Expand Up @@ -566,9 +566,9 @@ describe('useFetch', () => {
})
})

it('should not return data in onFetchError when allowFetchErrorReturnData is false', async () => {
it('should not return data in onFetchError when returnDataOnFetchError is false', async () => {
const { data, error, statusCode } = useFetch('https://example.com?status=400&json', {
allowFetchErrorReturnData: false,
returnDataOnFetchError: false,
onFetchError(ctx) {
ctx.error = 'Internal Server Error'
ctx.data = 'Internal Server Error'
Expand Down Expand Up @@ -599,9 +599,9 @@ describe('useFetch', () => {
})
})

it('should not return data in onFetchError when allowFetchErrorReturnData is false and network error', async () => {
it('should not return data in onFetchError when returnDataOnFetchError is false and network error', async () => {
const { data, error, statusCode } = useFetch('https://example.com?status=500&text=Internal%20Server%20Error', {
allowFetchErrorReturnData: false,
returnDataOnFetchError: false,
onFetchError(ctx) {
ctx.error = 'Internal Server Error'
ctx.data = 'Internal Server Error'
Expand Down
8 changes: 4 additions & 4 deletions packages/core/useFetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export interface UseFetchOptions {
* Allow `onFetchError` hook to return data
* @default true
*/
allowFetchErrorReturnData?: boolean
returnDataOnFetchError?: boolean

/**
* Will run immediately before the fetch request is dispatched
Expand Down Expand Up @@ -217,7 +217,7 @@ export interface CreateFetchOptions {
* to include the new options
*/
function isFetchOptions(obj: object): obj is UseFetchOptions {
return obj && containsProp(obj, 'immediate', 'refetch', 'initialData', 'timeout', 'beforeFetch', 'afterFetch', 'onFetchError', 'fetch', 'allowFetchErrorReturnData')
return obj && containsProp(obj, 'immediate', 'refetch', 'initialData', 'timeout', 'beforeFetch', 'afterFetch', 'onFetchError', 'fetch', 'returnDataOnFetchError')
}

// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
Expand Down Expand Up @@ -320,7 +320,7 @@ export function useFetch<T>(url: MaybeRefOrGetter<string>, ...args: any[]): UseF
const supportsAbort = typeof AbortController === 'function'

let fetchOptions: RequestInit = {}
let options: UseFetchOptions = { immediate: true, refetch: false, timeout: 0, allowFetchErrorReturnData: true }
let options: UseFetchOptions = { immediate: true, refetch: false, timeout: 0, returnDataOnFetchError: true }
interface InternalConfig { method: HttpMethod; type: DataType; payload: unknown; payloadType?: string }
const config: InternalConfig = {
method: 'GET',
Expand Down Expand Up @@ -468,7 +468,7 @@ export function useFetch<T>(url: MaybeRefOrGetter<string>, ...args: any[]): UseF
if (options.onFetchError)
({ error: errorData, data: responseData } = await options.onFetchError({ data: responseData, error: fetchError, response: response.value }))
error.value = errorData
if (options.allowFetchErrorReturnData)
if (options.returnDataOnFetchError)
data.value = responseData

errorEvent.trigger(fetchError)
Expand Down

0 comments on commit 790f171

Please sign in to comment.