Skip to content

Commit

Permalink
refactor(nuxt): use useRequestEvent() internally (#23916)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastienrossi authored and manniL committed Dec 11, 2023
1 parent d785512 commit 0960a5f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/nuxt/src/app/composables/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader
import type { NuxtApp } 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 (): Readonly<Record<string, string>>
export function useRequestHeaders (include?: any[]) {
if (import.meta.client) { return {} }
const event = useNuxtApp().ssrContext?.event
const event = useRequestEvent()
const headers = event ? getRequestHeaders(event) : {}
if (!include) { return headers }
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 {
if (import.meta.client) {
return globalThis.$fetch
}
const event = useNuxtApp().ssrContext?.event as H3Event
return event?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
return useRequestEvent()?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
}

export function setResponseStatus (event: H3Event, code?: number, message?: string): void
Expand Down

0 comments on commit 0960a5f

Please sign in to comment.