mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 13:48:13 +00:00
refactor(nuxt): use useRequestEvent()
internally (#23916)
This commit is contained in:
parent
8e44395d7a
commit
e4e3889421
@ -3,26 +3,25 @@ import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader
|
|||||||
import type { NuxtApp } from '../nuxt'
|
import type { NuxtApp } from '../nuxt'
|
||||||
import { useNuxtApp } from '../nuxt'
|
import { useNuxtApp } from '../nuxt'
|
||||||
|
|
||||||
|
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
||||||
|
return nuxtApp.ssrContext?.event as H3Event
|
||||||
|
}
|
||||||
|
|
||||||
export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
|
export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
|
||||||
export function useRequestHeaders (): Readonly<Record<string, string>>
|
export function useRequestHeaders (): Readonly<Record<string, string>>
|
||||||
export function useRequestHeaders (include?: any[]) {
|
export function useRequestHeaders (include?: any[]) {
|
||||||
if (import.meta.client) { return {} }
|
if (import.meta.client) { return {} }
|
||||||
const event = useNuxtApp().ssrContext?.event
|
const event = useRequestEvent()
|
||||||
const headers = event ? getRequestHeaders(event) : {}
|
const headers = event ? getRequestHeaders(event) : {}
|
||||||
if (!include) { return headers }
|
if (!include) { return headers }
|
||||||
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
|
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
|
||||||
return nuxtApp.ssrContext?.event as H3Event
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useRequestFetch (): typeof global.$fetch {
|
export function useRequestFetch (): typeof global.$fetch {
|
||||||
if (import.meta.client) {
|
if (import.meta.client) {
|
||||||
return globalThis.$fetch
|
return globalThis.$fetch
|
||||||
}
|
}
|
||||||
const event = useNuxtApp().ssrContext?.event as H3Event
|
return useRequestEvent()?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
||||||
return event?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
|
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
|
||||||
|
Loading…
Reference in New Issue
Block a user