From e4e38894210d954858105cdaa33c010115f3de12 Mon Sep 17 00:00:00 2001 From: Bastien Rossi <38658276+bastienrossi@users.noreply.github.com> Date: Wed, 25 Oct 2023 02:49:36 +0200 Subject: [PATCH] refactor(nuxt): use `useRequestEvent()` internally (#23916) --- packages/nuxt/src/app/composables/ssr.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/app/composables/ssr.ts b/packages/nuxt/src/app/composables/ssr.ts index 69fc78dd5a..6e5dc7cc08 100644 --- a/packages/nuxt/src/app/composables/ssr.ts +++ b/packages/nuxt/src/app/composables/ssr.ts @@ -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 (include: K[]): { [key in Lowercase]?: string } export function useRequestHeaders (): Readonly> 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