2021-12-20 16:27:36 +00:00
|
|
|
/* eslint-disable no-redeclare */
|
2022-04-07 11:28:04 +00:00
|
|
|
import type { CompatibilityEvent } from 'h3'
|
2021-12-20 16:27:36 +00:00
|
|
|
import { useNuxtApp } from '#app'
|
2022-04-07 11:28:04 +00:00
|
|
|
import { NuxtApp } from '#app/nuxt'
|
2021-12-20 16:27:36 +00:00
|
|
|
|
|
|
|
export function useRequestHeaders<K extends string = string> (include: K[]): Record<K, string>;
|
|
|
|
export function useRequestHeaders (): Readonly<Record<string, string>>;
|
|
|
|
export function useRequestHeaders (include?) {
|
|
|
|
if (process.client) { return {} }
|
2022-06-08 19:37:50 +00:00
|
|
|
const headers: Record<string, string | string[]> = useNuxtApp().ssrContext?.event.req.headers ?? {}
|
2021-12-20 16:27:36 +00:00
|
|
|
if (!include) { return headers }
|
2021-12-23 22:33:10 +00:00
|
|
|
return Object.fromEntries(include.filter(key => headers[key]).map(key => [key, headers[key]]))
|
2021-12-20 16:27:36 +00:00
|
|
|
}
|
2022-04-07 11:28:04 +00:00
|
|
|
|
|
|
|
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): CompatibilityEvent {
|
|
|
|
return nuxtApp.ssrContext?.event as CompatibilityEvent
|
|
|
|
}
|