fix(nuxt): do not compute useFetch key from headers (reverts #23462) (#24333)

This commit is contained in:
webfansplz 2023-11-16 22:04:48 +08:00 committed by GitHub
parent db74eade59
commit 55e0f38dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 12 deletions

View File

@ -211,12 +211,12 @@ function generateOptionSegments <_ResT, DataT, DefaultT>(opts: UseFetchOptions<_
toValue(opts.method as MaybeRef<string | undefined> | undefined)?.toUpperCase() || 'GET', toValue(opts.method as MaybeRef<string | undefined> | undefined)?.toUpperCase() || 'GET',
toValue(opts.baseURL), toValue(opts.baseURL),
] ]
for (const _obj of [opts.params || opts.query, opts.headers]) { for (const _obj of [opts.params || opts.query]) {
const obj = toValue(_obj) const obj = toValue(_obj)
if (!obj) { continue } if (!obj) { continue }
const unwrapped: Record<string, string> = {} const unwrapped: Record<string, string> = {}
const iterator = Array.isArray(obj) ? obj : obj instanceof Headers ? obj.entries() : Object.entries(obj) const iterator = Array.isArray(obj) ? obj : Object.entries(obj)
for (const [key, value] of iterator) { for (const [key, value] of iterator) {
unwrapped[toValue(key)] = toValue(value) unwrapped[toValue(key)] = toValue(value)
} }

View File

@ -257,19 +257,13 @@ describe('useFetch', () => {
expect.soft(getPayloadEntries()).toBe(baseCount + 2) expect.soft(getPayloadEntries()).toBe(baseCount + 2)
/* @ts-expect-error Overriding auto-key */ /* @ts-expect-error Overriding auto-key */
await useFetch('/api/test', { headers: { id: '3' } }, '') await useFetch('/api/test', { query: { id: '3' } }, '')
/* @ts-expect-error Overriding auto-key */ /* @ts-expect-error Overriding auto-key */
await useFetch('/api/test', { headers: { id: ref('3') } }, '') await useFetch('/api/test', { query: { id: ref('3') } }, '')
const headers = new Headers()
headers.append('id', '3')
/* @ts-expect-error Overriding auto-key */ /* @ts-expect-error Overriding auto-key */
await useFetch('/api/test', { headers }, '') await useFetch('/api/test', { params: { id: '3' } }, '')
/* @ts-expect-error Overriding auto-key */ /* @ts-expect-error Overriding auto-key */
await useFetch('/api/test', { headers: [['id', '3']] }, '') await useFetch('/api/test', { params: { id: ref('3') } }, '')
/* @ts-expect-error Overriding auto-key */
await useFetch('/api/test', { headers: [['id', ref('3')]] }, '')
/* @ts-expect-error Overriding auto-key */
await useFetch('/api/test', { headers: [[computed(() => 'id'), '3']] }, '')
expect.soft(getPayloadEntries()).toBe(baseCount + 3) expect.soft(getPayloadEntries()).toBe(baseCount + 3)
}) })
}) })