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'
2024-05-07 14:04:21 +00:00
import { getCurrentInstance } from 'vue'
import { useServerHead } from '@unhead/vue'
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
2024-01-19 17:03:30 +00:00
/** @since 3.0.0 */
2024-01-29 11:48:35 +00:00
export function useRequestEvent ( nuxtApp : NuxtApp = useNuxtApp ( ) ) {
return nuxtApp . ssrContext ? . event
2023-10-25 00:49:36 +00:00
}
2024-01-19 17:03:30 +00:00
/** @since 3.0.0 */
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-12-26 20:07:11 +00:00
const _headers = event ? getRequestHeaders ( event ) : { }
if ( ! include || ! event ) { return _headers }
const headers = Object . create ( null )
for ( const _key of include ) {
const key = _key . toLowerCase ( )
const header = _headers [ key ]
if ( header ) {
headers [ key ] = header
}
}
return headers
2021-12-20 16:27:36 +00:00
}
2022-04-07 11:28:04 +00:00
2024-01-19 17:03:30 +00:00
/** @since 3.9.0 */
2024-01-19 22:38:15 +00:00
export function useRequestHeader ( header : string ) {
2023-12-16 09:08:56 +00:00
if ( import . meta . client ) { return undefined }
const event = useRequestEvent ( )
return event ? getRequestHeader ( event , header ) : undefined
}
2024-01-19 17:03:30 +00:00
/** @since 3.2.0 */
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
}
2024-01-19 17:03:30 +00:00
/** @since 3.0.0 */
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 )
}
2024-01-29 11:48:35 +00:00
const event = useRequestEvent ( )
if ( event ) {
return _setResponseStatus ( event , arg1 , arg2 as string | undefined )
}
2022-08-02 16:01:59 +00:00
}
2023-09-28 10:54:22 +00:00
2024-01-19 17:03:30 +00:00
/** @since 3.8.0 */
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 )
2024-01-29 11:48:35 +00:00
appendHeader ( useRequestEvent ( ) ! , 'x-nitro-prerender' , paths . map ( p = > encodeURIComponent ( p ) ) . join ( ', ' ) )
2023-09-28 10:54:22 +00:00
}
2024-05-07 14:04:21 +00:00
const PREHYDRATE_ATTR_KEY = 'data-prehydrate-id'
/ * *
* ` onPrehydrate ` is a composable lifecycle hook that allows you to run a callback on the client immediately before
* Nuxt hydrates the page . This is an advanced feature .
*
* The callback will be stringified and inlined in the HTML so it should not have any external
* dependencies ( such as auto - imports ) or refer to variables defined outside the callback .
*
* The callback will run before Nuxt runtime initializes so it should not rely on the Nuxt or Vue context .
* @since 3.12 . 0
* /
export function onPrehydrate ( callback : ( el : HTMLElement ) = > void ) : void
export function onPrehydrate ( callback : string | ( ( el : HTMLElement ) = > void ) , key? : string ) : undefined | string {
if ( import . meta . client ) { return }
if ( typeof callback !== 'string' ) {
throw new TypeError ( '[nuxt] To transform a callback into a string, `onPrehydrate` must be processed by the Nuxt build pipeline. If it is called in a third-party library, make sure to add the library to `build.transpile`.' )
}
const vm = getCurrentInstance ( )
if ( vm && key ) {
vm . attrs [ PREHYDRATE_ATTR_KEY ] || = ''
key = ':' + key + ':'
if ( ! ( vm . attrs [ PREHYDRATE_ATTR_KEY ] as string ) . includes ( key ) ) {
vm . attrs [ PREHYDRATE_ATTR_KEY ] += key
}
}
const code = vm && key
? ` document.querySelectorAll('[ ${ PREHYDRATE_ATTR_KEY } *= ${ JSON . stringify ( key ) } ]').forEach ` + callback
: ( callback + '()' )
useServerHead ( {
script : [ {
key : vm && key ? key : code ,
tagPosition : 'bodyClose' ,
tagPriority : 'critical' ,
innerHTML : code ,
} ] ,
} )
return vm && key ? vm . attrs [ PREHYDRATE_ATTR_KEY ] as string : undefined
}