2023-01-14 01:13:48 +00:00
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
import type { H3Event } from 'h3'
|
2023-03-19 23:20:04 +00:00
|
|
|
import { setResponseStatus as _setResponseStatus } from 'h3'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { NuxtApp } from '../nuxt'
|
|
|
|
import { useNuxtApp } from '../nuxt'
|
2021-12-20 16:27:36 +00:00
|
|
|
|
2022-11-09 09:04:19 +00:00
|
|
|
export function useRequestHeaders<K extends string = string> (include: K[]): Record<Lowercase<K>, string | undefined>
|
2022-07-06 19:00:08 +00:00
|
|
|
export function useRequestHeaders (): Readonly<Record<string, string | undefined>>
|
2022-08-12 17:47:58 +00:00
|
|
|
export function useRequestHeaders (include?: any[]) {
|
2021-12-20 16:27:36 +00:00
|
|
|
if (process.client) { return {} }
|
2022-11-15 14:33:43 +00:00
|
|
|
const headers = useNuxtApp().ssrContext?.event.node.req.headers ?? {}
|
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
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
|
|
|
return nuxtApp.ssrContext?.event as H3Event
|
2022-04-07 11:28:04 +00:00
|
|
|
}
|
2022-08-02 16:01:59 +00:00
|
|
|
|
2023-02-09 06:02:07 +00:00
|
|
|
export function useRequestFetch (): typeof global.$fetch {
|
|
|
|
if (process.client) {
|
|
|
|
return globalThis.$fetch
|
|
|
|
}
|
|
|
|
const event = useNuxtApp().ssrContext?.event as H3Event
|
|
|
|
return event?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
|
|
|
}
|
|
|
|
|
2022-08-02 16:01:59 +00:00
|
|
|
export function setResponseStatus (code: number, message?: string) {
|
2023-03-19 23:20:04 +00:00
|
|
|
if (process.client) { return }
|
|
|
|
_setResponseStatus(useRequestEvent(), code, message)
|
2022-08-02 16:01:59 +00:00
|
|
|
}
|