mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
docs(nuxt): add @since
annotations to exported composables (#25086)
This commit is contained in:
parent
b47ff972fa
commit
ce8a2aaf37
@ -1,6 +1,7 @@
|
|||||||
// @ts-expect-error withAsyncContext is internal API
|
// @ts-expect-error withAsyncContext is internal API
|
||||||
import { getCurrentInstance, withAsyncContext as withVueAsyncContext } from 'vue'
|
import { getCurrentInstance, withAsyncContext as withVueAsyncContext } from 'vue'
|
||||||
|
|
||||||
|
/** @since 3.8.0 */
|
||||||
export function withAsyncContext (fn: () => PromiseLike<unknown>) {
|
export function withAsyncContext (fn: () => PromiseLike<unknown>) {
|
||||||
return withVueAsyncContext(() => {
|
return withVueAsyncContext(() => {
|
||||||
const nuxtApp = getCurrentInstance()?.appContext.app.$nuxt
|
const nuxtApp = getCurrentInstance()?.appContext.app.$nuxt
|
||||||
|
@ -121,6 +121,7 @@ const isDefer = (dedupe?: boolean | 'cancel' | 'defer') => dedupe === 'defer' ||
|
|||||||
/**
|
/**
|
||||||
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
* Provides access to data that resolves asynchronously in an SSR-friendly composable.
|
||||||
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
* See {@link https://nuxt.com/docs/api/composables/use-async-data}
|
||||||
|
* @since 3.0.0
|
||||||
* @param handler An asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side.
|
* @param handler An asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side.
|
||||||
* @param options customize the behavior of useAsyncData
|
* @param options customize the behavior of useAsyncData
|
||||||
*/
|
*/
|
||||||
@ -378,6 +379,7 @@ export function useAsyncData<
|
|||||||
|
|
||||||
return asyncDataPromise as AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
return asyncDataPromise as AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>
|
||||||
}
|
}
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function useLazyAsyncData<
|
export function useLazyAsyncData<
|
||||||
ResT,
|
ResT,
|
||||||
DataE = Error,
|
DataE = Error,
|
||||||
@ -441,6 +443,7 @@ export function useLazyAsyncData<
|
|||||||
return useAsyncData(key, handler, { ...options, lazy: true }, null)
|
return useAsyncData(key, handler, { ...options, lazy: true }, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.1.0 */
|
||||||
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null> } {
|
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null> } {
|
||||||
const nuxt = useNuxtApp()
|
const nuxt = useNuxtApp()
|
||||||
|
|
||||||
@ -454,6 +457,7 @@ export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export async function refreshNuxtData (keys?: string | string[]): Promise<void> {
|
export async function refreshNuxtData (keys?: string | string[]): Promise<void> {
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
@ -465,6 +469,7 @@ export async function refreshNuxtData (keys?: string | string[]): Promise<void>
|
|||||||
await useNuxtApp().hooks.callHookParallel('app:data:refresh', _keys)
|
await useNuxtApp().hooks.callHookParallel('app:data:refresh', _keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void {
|
export function clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
const _allKeys = Object.keys(nuxtApp.payload.data)
|
const _allKeys = Object.keys(nuxtApp.payload.data)
|
||||||
|
@ -25,6 +25,7 @@ export interface ReloadNuxtAppOptions {
|
|||||||
path?: string
|
path?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.3.0 */
|
||||||
export function reloadNuxtApp (options: ReloadNuxtAppOptions = {}) {
|
export function reloadNuxtApp (options: ReloadNuxtAppOptions = {}) {
|
||||||
if (import.meta.server) { return }
|
if (import.meta.server) { return }
|
||||||
const path = options.path || window.location.pathname
|
const path = options.path || window.location.pathname
|
||||||
|
@ -27,6 +27,7 @@ async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
/*@__NO_SIDE_EFFECTS__*/
|
/*@__NO_SIDE_EFFECTS__*/
|
||||||
export const defineNuxtComponent: typeof defineComponent =
|
export const defineNuxtComponent: typeof defineComponent =
|
||||||
function defineNuxtComponent (...args: any[]): any {
|
function defineNuxtComponent (...args: any[]): any {
|
||||||
|
@ -29,6 +29,7 @@ const CookieDefaults = {
|
|||||||
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
|
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
|
||||||
} satisfies CookieOptions<any>
|
} satisfies CookieOptions<any>
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function useCookie<T = string | null | undefined> (name: string, _opts?: CookieOptions<T> & { readonly?: false }): CookieRef<T>
|
export function useCookie<T = string | null | undefined> (name: string, _opts?: CookieOptions<T> & { readonly?: false }): CookieRef<T>
|
||||||
export function useCookie<T = string | null | undefined> (name: string, _opts: CookieOptions<T> & { readonly: true }): Readonly<CookieRef<T>>
|
export function useCookie<T = string | null | undefined> (name: string, _opts: CookieOptions<T> & { readonly: true }): Readonly<CookieRef<T>>
|
||||||
export function useCookie<T = string | null | undefined> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
|
export function useCookie<T = string | null | undefined> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
|
||||||
|
@ -6,10 +6,12 @@ import { useRouter } from './router'
|
|||||||
|
|
||||||
export const NUXT_ERROR_SIGNATURE = '__nuxt_error'
|
export const NUXT_ERROR_SIGNATURE = '__nuxt_error'
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const useError = () => toRef(useNuxtApp().payload, 'error')
|
export const useError = () => toRef(useNuxtApp().payload, 'error')
|
||||||
|
|
||||||
export interface NuxtError<DataT = unknown> extends H3Error<DataT> {}
|
export interface NuxtError<DataT = unknown> extends H3Error<DataT> {}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const showError = <DataT = unknown>(
|
export const showError = <DataT = unknown>(
|
||||||
error: string | Error | Partial<NuxtError<DataT>>
|
error: string | Error | Partial<NuxtError<DataT>>
|
||||||
) => {
|
) => {
|
||||||
@ -31,6 +33,7 @@ export const showError = <DataT = unknown>(
|
|||||||
return nuxtError
|
return nuxtError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const clearError = async (options: { redirect?: string } = {}) => {
|
export const clearError = async (options: { redirect?: string } = {}) => {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
const error = useError()
|
const error = useError()
|
||||||
@ -44,12 +47,14 @@ export const clearError = async (options: { redirect?: string } = {}) => {
|
|||||||
error.value = null
|
error.value = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const isNuxtError = <DataT = unknown>(
|
export const isNuxtError = <DataT = unknown>(
|
||||||
error?: string | object
|
error?: string | object
|
||||||
): error is NuxtError<DataT> => (
|
): error is NuxtError<DataT> => (
|
||||||
!!error && typeof error === 'object' && NUXT_ERROR_SIGNATURE in error
|
!!error && typeof error === 'object' && NUXT_ERROR_SIGNATURE in error
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const createError = <DataT = unknown>(
|
export const createError = <DataT = unknown>(
|
||||||
error: string | Partial<NuxtError<DataT>>
|
error: string | Partial<NuxtError<DataT>>
|
||||||
) => {
|
) => {
|
||||||
|
@ -42,6 +42,7 @@ export interface UseFetchOptions<
|
|||||||
/**
|
/**
|
||||||
* Fetch data from an API endpoint with an SSR-friendly composable.
|
* Fetch data from an API endpoint with an SSR-friendly composable.
|
||||||
* See {@link https://nuxt.com/docs/api/composables/use-fetch}
|
* See {@link https://nuxt.com/docs/api/composables/use-fetch}
|
||||||
|
* @since 3.0.0
|
||||||
* @param request The URL to fetch
|
* @param request The URL to fetch
|
||||||
* @param opts extends $fetch options and useAsyncData options
|
* @param opts extends $fetch options and useAsyncData options
|
||||||
*/
|
*/
|
||||||
@ -184,6 +185,7 @@ export function useFetch<
|
|||||||
return asyncData
|
return asyncData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function useLazyFetch<
|
export function useLazyFetch<
|
||||||
ResT = void,
|
ResT = void,
|
||||||
ErrorT = FetchError,
|
ErrorT = FetchError,
|
||||||
|
@ -6,6 +6,7 @@ import type { NuxtPayload } from '../nuxt'
|
|||||||
* @param key a unique key to identify the data in the Nuxt payload
|
* @param key a unique key to identify the data in the Nuxt payload
|
||||||
* @param get a function that returns the value to set the initial data
|
* @param get a function that returns the value to set the initial data
|
||||||
* @param set a function that will receive the data on the client-side
|
* @param set a function that will receive the data on the client-side
|
||||||
|
* @since 3.0.0
|
||||||
*/
|
*/
|
||||||
export const useHydration = <K extends keyof NuxtPayload, T = NuxtPayload[K]> (key: K, get: () => T, set: (value: T) => void) => {
|
export const useHydration = <K extends keyof NuxtPayload, T = NuxtPayload[K]> (key: K, get: () => T, set: (value: T) => void) => {
|
||||||
const nuxt = useNuxtApp()
|
const nuxt = useNuxtApp()
|
||||||
|
@ -112,6 +112,7 @@ function createLoadingIndicator (opts: Partial<LoadingIndicatorOpts> = {}) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* composable to handle the loading state of the page
|
* composable to handle the loading state of the page
|
||||||
|
* @since 3.9.0
|
||||||
*/
|
*/
|
||||||
export function useLoadingIndicator (opts: Partial<LoadingIndicatorOpts> = {}): Omit<LoadingIndicator, '_cleanup'> {
|
export function useLoadingIndicator (opts: Partial<LoadingIndicatorOpts> = {}): Omit<LoadingIndicator, '_cleanup'> {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
|
@ -33,6 +33,7 @@ function fetchManifest () {
|
|||||||
return manifest
|
return manifest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.7.4 */
|
||||||
export function getAppManifest (): Promise<NuxtAppManifest> {
|
export function getAppManifest (): Promise<NuxtAppManifest> {
|
||||||
if (!isAppManifestEnabled) {
|
if (!isAppManifestEnabled) {
|
||||||
throw new Error('[nuxt] app manifest should be enabled with `experimental.appManifest`')
|
throw new Error('[nuxt] app manifest should be enabled with `experimental.appManifest`')
|
||||||
@ -40,6 +41,7 @@ export function getAppManifest (): Promise<NuxtAppManifest> {
|
|||||||
return manifest || fetchManifest()
|
return manifest || fetchManifest()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.7.4 */
|
||||||
export async function getRouteRules (url: string) {
|
export async function getRouteRules (url: string) {
|
||||||
await getAppManifest()
|
await getAppManifest()
|
||||||
return defu({} as Record<string, any>, ...matcher.matchAll(url).reverse())
|
return defu({} as Record<string, any>, ...matcher.matchAll(url).reverse())
|
||||||
|
@ -5,6 +5,7 @@ import { useNuxtApp } from '../nuxt'
|
|||||||
* @param key a unique key ensuring the function can be properly de-duplicated across requests
|
* @param key a unique key ensuring the function can be properly de-duplicated across requests
|
||||||
* @param fn a function to call
|
* @param fn a function to call
|
||||||
* @see https://nuxt.com/docs/api/utils/call-once
|
* @see https://nuxt.com/docs/api/utils/call-once
|
||||||
|
* @since 3.9.0
|
||||||
*/
|
*/
|
||||||
export function callOnce (key?: string, fn?: (() => any | Promise<any>)): Promise<void>
|
export function callOnce (key?: string, fn?: (() => any | Promise<any>)): Promise<void>
|
||||||
export function callOnce (fn?: (() => any | Promise<any>)): Promise<void>
|
export function callOnce (fn?: (() => any | Promise<any>)): Promise<void>
|
||||||
|
@ -15,6 +15,7 @@ interface LoadPayloadOptions {
|
|||||||
hash?: string
|
hash?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function loadPayload (url: string, opts: LoadPayloadOptions = {}): Record<string, any> | Promise<Record<string, any>> | null {
|
export function loadPayload (url: string, opts: LoadPayloadOptions = {}): Record<string, any> | Promise<Record<string, any>> | null {
|
||||||
if (import.meta.server || !payloadExtraction) { return null }
|
if (import.meta.server || !payloadExtraction) { return null }
|
||||||
const payloadURL = _getPayloadURL(url, opts)
|
const payloadURL = _getPayloadURL(url, opts)
|
||||||
@ -37,7 +38,7 @@ export function loadPayload (url: string, opts: LoadPayloadOptions = {}): Record
|
|||||||
})
|
})
|
||||||
return cache[payloadURL]
|
return cache[payloadURL]
|
||||||
}
|
}
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function preloadPayload (url: string, opts: LoadPayloadOptions = {}) {
|
export function preloadPayload (url: string, opts: LoadPayloadOptions = {}) {
|
||||||
const payloadURL = _getPayloadURL(url, opts)
|
const payloadURL = _getPayloadURL(url, opts)
|
||||||
useHead({
|
useHead({
|
||||||
@ -75,7 +76,7 @@ async function _importPayload (payloadURL: string) {
|
|||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
/** @since 3.0.0 */
|
||||||
export async function isPrerendered (url = useRoute().path) {
|
export async function isPrerendered (url = useRoute().path) {
|
||||||
// Note: Alternative for server is checking x-nitro-prerender header
|
// Note: Alternative for server is checking x-nitro-prerender header
|
||||||
if (!appManifest) { return !!useNuxtApp().payload.prerenderedAt }
|
if (!appManifest) { return !!useNuxtApp().payload.prerenderedAt }
|
||||||
@ -89,6 +90,7 @@ export async function isPrerendered (url = useRoute().path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let payloadCache: any = null
|
let payloadCache: any = null
|
||||||
|
/** @since 3.4.0 */
|
||||||
export async function getNuxtClientPayload () {
|
export async function getNuxtClientPayload () {
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
return
|
return
|
||||||
@ -121,6 +123,7 @@ export async function parsePayload (payload: string) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an experimental function for configuring passing rich data from server -> client.
|
* This is an experimental function for configuring passing rich data from server -> client.
|
||||||
|
* @since 3.4.0
|
||||||
*/
|
*/
|
||||||
export function definePayloadReducer (
|
export function definePayloadReducer (
|
||||||
name: string,
|
name: string,
|
||||||
@ -135,6 +138,7 @@ export function definePayloadReducer (
|
|||||||
* This is an experimental function for configuring passing rich data from server -> client.
|
* This is an experimental function for configuring passing rich data from server -> client.
|
||||||
*
|
*
|
||||||
* This function _must_ be called in a Nuxt plugin that is `unshift`ed to the beginning of the Nuxt plugins array.
|
* This function _must_ be called in a Nuxt plugin that is `unshift`ed to the beginning of the Nuxt plugins array.
|
||||||
|
* @since 3.4.0
|
||||||
*/
|
*/
|
||||||
export function definePayloadReviver (
|
export function definePayloadReviver (
|
||||||
name: string,
|
name: string,
|
||||||
|
@ -7,6 +7,7 @@ import { useRouter } from './router'
|
|||||||
/**
|
/**
|
||||||
* Preload a component or components that have been globally registered.
|
* Preload a component or components that have been globally registered.
|
||||||
* @param components Pascal-cased name or names of components to prefetch
|
* @param components Pascal-cased name or names of components to prefetch
|
||||||
|
* @since 3.0.0
|
||||||
*/
|
*/
|
||||||
export const preloadComponents = async (components: string | string[]) => {
|
export const preloadComponents = async (components: string | string[]) => {
|
||||||
if (import.meta.server) { return }
|
if (import.meta.server) { return }
|
||||||
@ -19,6 +20,7 @@ export const preloadComponents = async (components: string | string[]) => {
|
|||||||
/**
|
/**
|
||||||
* Prefetch a component or components that have been globally registered.
|
* Prefetch a component or components that have been globally registered.
|
||||||
* @param components Pascal-cased name or names of components to prefetch
|
* @param components Pascal-cased name or names of components to prefetch
|
||||||
|
* @since 3.0.0
|
||||||
*/
|
*/
|
||||||
export const prefetchComponents = (components: string | string[]) => {
|
export const prefetchComponents = (components: string | string[]) => {
|
||||||
// TODO
|
// TODO
|
||||||
@ -33,6 +35,7 @@ function _loadAsyncComponent (component: Component) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export async function preloadRouteComponents (to: RouteLocationRaw, router: Router & { _routePreloaded?: Set<string>; _preloadPromises?: Array<Promise<any>> } = useRouter()): Promise<void> {
|
export async function preloadRouteComponents (to: RouteLocationRaw, router: Router & { _routePreloaded?: Set<string>; _preloadPromises?: Array<Promise<any>> } = useRouter()): Promise<void> {
|
||||||
if (import.meta.server) { return }
|
if (import.meta.server) { return }
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { useNuxtApp } from '../nuxt'
|
import { useNuxtApp } from '../nuxt'
|
||||||
import { requestIdleCallback } from '../compat/idle-callback'
|
import { requestIdleCallback } from '../compat/idle-callback'
|
||||||
|
|
||||||
|
/** @since 3.1.0 */
|
||||||
export const onNuxtReady = (callback: () => any) => {
|
export const onNuxtReady = (callback: () => any) => {
|
||||||
if (import.meta.server) { return }
|
if (import.meta.server) { return }
|
||||||
|
|
||||||
|
@ -12,10 +12,12 @@ import { PageRouteSymbol } from '../components/injections'
|
|||||||
import type { NuxtError } from './error'
|
import type { NuxtError } from './error'
|
||||||
import { createError, showError } from './error'
|
import { createError, showError } from './error'
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const useRouter: typeof _useRouter = () => {
|
export const useRouter: typeof _useRouter = () => {
|
||||||
return useNuxtApp()?.$router as Router
|
return useNuxtApp()?.$router as Router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const useRoute: typeof _useRoute = () => {
|
export const useRoute: typeof _useRoute = () => {
|
||||||
if (import.meta.dev && isProcessingMiddleware()) {
|
if (import.meta.dev && isProcessingMiddleware()) {
|
||||||
console.warn('[nuxt] Calling `useRoute` within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.')
|
console.warn('[nuxt] Calling `useRoute` within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.')
|
||||||
@ -26,6 +28,7 @@ export const useRoute: typeof _useRoute = () => {
|
|||||||
return useNuxtApp()._route
|
return useNuxtApp()._route
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const onBeforeRouteLeave = (guard: NavigationGuard) => {
|
export const onBeforeRouteLeave = (guard: NavigationGuard) => {
|
||||||
const unsubscribe = useRouter().beforeEach((to, from, next) => {
|
const unsubscribe = useRouter().beforeEach((to, from, next) => {
|
||||||
if (to === from) { return }
|
if (to === from) { return }
|
||||||
@ -34,6 +37,7 @@ export const onBeforeRouteLeave = (guard: NavigationGuard) => {
|
|||||||
onScopeDispose(unsubscribe)
|
onScopeDispose(unsubscribe)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const onBeforeRouteUpdate = (guard: NavigationGuard) => {
|
export const onBeforeRouteUpdate = (guard: NavigationGuard) => {
|
||||||
const unsubscribe = useRouter().beforeEach(guard)
|
const unsubscribe = useRouter().beforeEach(guard)
|
||||||
onScopeDispose(unsubscribe)
|
onScopeDispose(unsubscribe)
|
||||||
@ -43,6 +47,7 @@ export interface RouteMiddleware {
|
|||||||
(to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard>
|
(to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
/*@__NO_SIDE_EFFECTS__*/
|
/*@__NO_SIDE_EFFECTS__*/
|
||||||
export function defineNuxtRouteMiddleware (middleware: RouteMiddleware) {
|
export function defineNuxtRouteMiddleware (middleware: RouteMiddleware) {
|
||||||
return middleware
|
return middleware
|
||||||
@ -57,6 +62,7 @@ interface AddRouteMiddleware {
|
|||||||
(middleware: RouteMiddleware): void
|
(middleware: RouteMiddleware): void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const addRouteMiddleware: AddRouteMiddleware = (name: string | RouteMiddleware, middleware?: RouteMiddleware, options: AddRouteMiddlewareOptions = {}) => {
|
export const addRouteMiddleware: AddRouteMiddleware = (name: string | RouteMiddleware, middleware?: RouteMiddleware, options: AddRouteMiddlewareOptions = {}) => {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
const global = options.global || typeof name !== 'string'
|
const global = options.global || typeof name !== 'string'
|
||||||
@ -72,6 +78,7 @@ export const addRouteMiddleware: AddRouteMiddleware = (name: string | RouteMiddl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
const isProcessingMiddleware = () => {
|
const isProcessingMiddleware = () => {
|
||||||
try {
|
try {
|
||||||
if (useNuxtApp()._processingMiddleware) {
|
if (useNuxtApp()._processingMiddleware) {
|
||||||
@ -109,6 +116,7 @@ export interface NavigateToOptions {
|
|||||||
open?: OpenOptions
|
open?: OpenOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: NavigateToOptions): Promise<void | NavigationFailure | false> | false | void | RouteLocationRaw => {
|
export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: NavigateToOptions): Promise<void | NavigationFailure | false> | false | void | RouteLocationRaw => {
|
||||||
if (!to) {
|
if (!to) {
|
||||||
to = '/'
|
to = '/'
|
||||||
@ -206,7 +214,10 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
|
|||||||
return options?.replace ? router.replace(to) : router.push(to)
|
return options?.replace ? router.replace(to) : router.push(to)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This will abort navigation within a Nuxt route middleware handler. */
|
/**
|
||||||
|
* This will abort navigation within a Nuxt route middleware handler.
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
export const abortNavigation = (err?: string | Partial<NuxtError>) => {
|
export const abortNavigation = (err?: string | Partial<NuxtError>) => {
|
||||||
if (import.meta.dev && !isProcessingMiddleware()) {
|
if (import.meta.dev && !isProcessingMiddleware()) {
|
||||||
throw new Error('abortNavigation() is only usable inside a route middleware handler.')
|
throw new Error('abortNavigation() is only usable inside a route middleware handler.')
|
||||||
@ -223,6 +234,7 @@ export const abortNavigation = (err?: string | Partial<NuxtError>) => {
|
|||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export const setPageLayout = (layout: unknown extends PageMeta['layout'] ? string : PageMeta['layout']) => {
|
export const setPageLayout = (layout: unknown extends PageMeta['layout'] ? string : PageMeta['layout']) => {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
|
@ -4,10 +4,12 @@ import type { NuxtApp } from '../nuxt'
|
|||||||
import { useNuxtApp } from '../nuxt'
|
import { useNuxtApp } from '../nuxt'
|
||||||
import { toArray } from '../utils'
|
import { toArray } from '../utils'
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
|
||||||
return nuxtApp.ssrContext?.event as H3Event
|
return nuxtApp.ssrContext?.event as H3Event
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
|
export function useRequestHeaders<K extends string = string> (include: K[]): { [key in Lowercase<K>]?: string }
|
||||||
export function useRequestHeaders (): Readonly<Record<string, string>>
|
export function useRequestHeaders (): Readonly<Record<string, string>>
|
||||||
export function useRequestHeaders (include?: any[]) {
|
export function useRequestHeaders (include?: any[]) {
|
||||||
@ -26,12 +28,14 @@ export function useRequestHeaders (include?: any[]) {
|
|||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.9.0 */
|
||||||
export function useRequestHeader(header: string) {
|
export function useRequestHeader(header: string) {
|
||||||
if (import.meta.client) { return undefined }
|
if (import.meta.client) { return undefined }
|
||||||
const event = useRequestEvent()
|
const event = useRequestEvent()
|
||||||
return event ? getRequestHeader(event, header) : undefined
|
return event ? getRequestHeader(event, header) : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.2.0 */
|
||||||
export function useRequestFetch (): typeof global.$fetch {
|
export function useRequestFetch (): typeof global.$fetch {
|
||||||
if (import.meta.client) {
|
if (import.meta.client) {
|
||||||
return globalThis.$fetch
|
return globalThis.$fetch
|
||||||
@ -39,6 +43,7 @@ export function useRequestFetch (): typeof global.$fetch {
|
|||||||
return useRequestEvent()?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
return useRequestEvent()?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.0.0 */
|
||||||
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
|
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
|
||||||
/** @deprecated Pass `event` as first option. */
|
/** @deprecated Pass `event` as first option. */
|
||||||
export function setResponseStatus (code: number, message?: string): void
|
export function setResponseStatus (code: number, message?: string): void
|
||||||
@ -50,6 +55,7 @@ export function setResponseStatus (arg1: H3Event | number | undefined, arg2?: nu
|
|||||||
return _setResponseStatus(useRequestEvent(), arg1, arg2 as string | undefined)
|
return _setResponseStatus(useRequestEvent(), arg1, arg2 as string | undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @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 }
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import { toArray } from '../utils'
|
|||||||
const useStateKeyPrefix = '$s'
|
const useStateKeyPrefix = '$s'
|
||||||
/**
|
/**
|
||||||
* Create a global reactive ref that will be hydrated but not shared across ssr requests
|
* Create a global reactive ref that will be hydrated but not shared across ssr requests
|
||||||
|
* @since 3.0.0
|
||||||
* @param key a unique key ensuring that data fetching can be properly de-duplicated across requests
|
* @param key a unique key ensuring that data fetching can be properly de-duplicated across requests
|
||||||
* @param init a function that provides initial value for the state when it's not initiated
|
* @param init a function that provides initial value for the state when it's not initiated
|
||||||
*/
|
*/
|
||||||
@ -37,6 +38,7 @@ export function useState <T> (...args: any): Ref<T> {
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @since 3.6.0 */
|
||||||
export function clearNuxtState (
|
export function clearNuxtState (
|
||||||
keys?: string | string[] | ((key: string) => boolean)
|
keys?: string | string[] | ((key: string) => boolean)
|
||||||
): void {
|
): void {
|
||||||
|
@ -3,6 +3,7 @@ import { joinURL } from 'ufo'
|
|||||||
import { useRuntimeConfig } from '../nuxt'
|
import { useRuntimeConfig } from '../nuxt'
|
||||||
import { useRequestEvent } from './ssr'
|
import { useRequestEvent } from './ssr'
|
||||||
|
|
||||||
|
/** @since 3.5.0 */
|
||||||
export function useRequestURL () {
|
export function useRequestURL () {
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
const url = getRequestURL(useRequestEvent())
|
const url = getRequestURL(useRequestEvent())
|
||||||
|
Loading…
Reference in New Issue
Block a user