feat(composables): start new useResponseHeader and useResponseHeaders composables

This commit is contained in:
Jelmer de Maat 2024-05-09 16:21:04 +02:00
parent 76925a4477
commit cff7f5987a
2 changed files with 16 additions and 2 deletions

View File

@ -24,7 +24,7 @@ export { useFetch, useLazyFetch } from './fetch'
export type { FetchResult, UseFetchOptions } from './fetch' export type { FetchResult, UseFetchOptions } from './fetch'
export { useCookie, refreshCookie } from './cookie' export { useCookie, refreshCookie } from './cookie'
export type { CookieOptions, CookieRef } from './cookie' export type { CookieOptions, CookieRef } from './cookie'
export { onPrehydrate, prerenderRoutes, useRequestHeaders, useRequestEvent, useRequestFetch, setResponseStatus } from './ssr' export { onPrehydrate, prerenderRoutes, useRequestHeaders, useRequestEvent, useRequestFetch, setResponseStatus, useResponseHeader, useResponseHeaders } from './ssr'
export { onNuxtReady } from './ready' export { onNuxtReady } from './ready'
export { abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, onBeforeRouteLeave, onBeforeRouteUpdate, setPageLayout, navigateTo, useRoute, useRouter } from './router' export { abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, onBeforeRouteLeave, onBeforeRouteUpdate, setPageLayout, navigateTo, useRoute, useRouter } from './router'
export type { AddRouteMiddlewareOptions, RouteMiddleware } from './router' export type { AddRouteMiddlewareOptions, RouteMiddleware } from './router'

View File

@ -1,5 +1,5 @@
import type { H3Event } from 'h3' import type { H3Event } from 'h3'
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders } from 'h3' import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, setResponseHeader, setResponseHeaders } from 'h3'
import { getCurrentInstance } from 'vue' import { getCurrentInstance } from 'vue'
import { useServerHead } from '@unhead/vue' import { useServerHead } from '@unhead/vue'
@ -61,6 +61,20 @@ export function setResponseStatus (arg1: H3Event | number | undefined, arg2?: nu
} }
} }
/** @since 3.11.3 */
export function useResponseHeader (header: string, value: string) {
if (import.meta.client) { return undefined }
const event = useRequestEvent()
return event ? setResponseHeader(event, header, value) : undefined
}
/** @since 3.11.3 */
export function useResponseHeaders (headers: Record<string, string>) {
if (import.meta.client) { return undefined }
const event = useRequestEvent()
return event ? setResponseHeaders(event, headers) : undefined
}
/** @since 3.8.0 */ /** @since 3.8.0 */
export function prerenderRoutes (path: string | string[]) { export function prerenderRoutes (path: string | string[]) {
if (!import.meta.server || !import.meta.prerender) { return } if (!import.meta.server || !import.meta.prerender) { return }