2022-10-15 18:42:57 +00:00
|
|
|
import type { H3Event } from 'h3'
|
2023-12-16 09:08:56 +00:00
|
|
|
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders } from 'h3'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { NuxtApp } from '../nuxt'
|
|
|
|
import { useNuxtApp } from '../nuxt'
|
2023-12-23 14:22:58 +00:00
|
|
|
import { toArray } from '../utils'
|
2021-12-20 16:27:36 +00:00
|
|
|
|
2023-10-25 00:49:36 +00:00
|
|
|
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
|
|
|
return nuxtApp.ssrContext?.event as H3Event
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:43:19 +00:00
|
|
|
export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
|
|
|
|
export function useRequestHeaders (): Readonly<Record<string, string>>
|
2022-08-12 17:47:58 +00:00
|
|
|
export function useRequestHeaders (include?: any[]) {
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) { return {} }
|
2023-10-25 00:49:36 +00:00
|
|
|
const event = useRequestEvent()
|
2023-08-23 07:30:53 +00:00
|
|
|
const headers = event ? getRequestHeaders(event) : {}
|
2021-12-20 16:27:36 +00:00
|
|
|
if (!include) { return headers }
|
2022-11-09 09:04:19 +00:00
|
|
|
return Object.fromEntries(include.map(key => key.toLowerCase()).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
|
|
|
|
2023-12-16 09:08:56 +00:00
|
|
|
export function useRequestHeader(header: string) {
|
|
|
|
if (import.meta.client) { return undefined }
|
|
|
|
const event = useRequestEvent()
|
|
|
|
return event ? getRequestHeader(event, header) : undefined
|
|
|
|
}
|
|
|
|
|
2023-02-09 06:02:07 +00:00
|
|
|
export function useRequestFetch (): typeof global.$fetch {
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2023-02-09 06:02:07 +00:00
|
|
|
return globalThis.$fetch
|
|
|
|
}
|
2023-10-25 00:49:36 +00:00
|
|
|
return useRequestEvent()?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
2023-02-09 06:02:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:02:26 +00:00
|
|
|
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
|
|
|
|
/** @deprecated Pass `event` as first option. */
|
|
|
|
export function setResponseStatus (code: number, message?: string): void
|
|
|
|
export function setResponseStatus (arg1: H3Event | number | undefined, arg2?: number | string, arg3?: string) {
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) { return }
|
2023-03-31 14:02:26 +00:00
|
|
|
if (arg1 && typeof arg1 !== 'number') {
|
|
|
|
return _setResponseStatus(arg1, arg2 as number | undefined, arg3)
|
|
|
|
}
|
|
|
|
return _setResponseStatus(useRequestEvent(), arg1, arg2 as string | undefined)
|
2022-08-02 16:01:59 +00:00
|
|
|
}
|
2023-09-28 10:54:22 +00:00
|
|
|
|
|
|
|
export function prerenderRoutes (path: string | string[]) {
|
2023-12-14 13:58:25 +00:00
|
|
|
if (!import.meta.server || !import.meta.prerender) { return }
|
2023-09-28 10:54:22 +00:00
|
|
|
|
2023-12-23 14:22:58 +00:00
|
|
|
const paths = toArray(path)
|
2023-09-28 10:54:22 +00:00
|
|
|
appendHeader(useRequestEvent(), 'x-nitro-prerender', paths.map(p => encodeURIComponent(p)).join(', '))
|
|
|
|
}
|