fix(nuxt): mark useRequestHeaders keys as optional (#20286)

This commit is contained in:
Daniel Roe 2023-04-15 15:43:19 +01:00 committed by GitHub
parent ac2e466175
commit 8aa86559c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -4,8 +4,8 @@ import { setResponseStatus as _setResponseStatus } from 'h3'
import type { NuxtApp } from '../nuxt'
import { useNuxtApp } from '../nuxt'
export function useRequestHeaders<K extends string = string> (include: K[]): Record<Lowercase<K>, string | undefined>
export function useRequestHeaders (): Readonly<Record<string, string | undefined>>
export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
export function useRequestHeaders (): Readonly<Record<string, string>>
export function useRequestHeaders (include?: any[]) {
if (process.client) { return {} }
const headers = useNuxtApp().ssrContext?.event.node.req.headers ?? {}

View File

@ -260,6 +260,17 @@ describe('composables', () => {
expectTypeOf(useLazyFetch('/api/hey', { default: () => 'bar', transform: v => v.foo }).data).toEqualTypeOf<Ref<string | null>>()
})
it('uses types compatible between useRequestHeaders and useFetch', () => {
useFetch('/api/hey', {
headers: useRequestHeaders()
})
useFetch('/api/hey', {
headers: useRequestHeaders(['test'])
})
const { test } = useRequestHeaders(['test'])
expectTypeOf(test).toEqualTypeOf<string | undefined>()
})
it('correctly types returns with key signatures', () => {
interface TestType {
id: string