mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
a5dad0d2e8
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Daniel Roe <daniel@roe.dev>
41 lines
859 B
TypeScript
41 lines
859 B
TypeScript
import { getContext } from 'unctx'
|
|
import type { Nuxt } from '@nuxt/schema'
|
|
|
|
/** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */
|
|
export const nuxtCtx = getContext<Nuxt>('nuxt')
|
|
|
|
// TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6
|
|
|
|
/**
|
|
* Get access to Nuxt instance.
|
|
*
|
|
* Throws an error if Nuxt instance is unavailable.
|
|
* @example
|
|
* ```js
|
|
* const nuxt = useNuxt()
|
|
* ```
|
|
*/
|
|
export function useNuxt (): Nuxt {
|
|
const instance = nuxtCtx.tryUse()
|
|
if (!instance) {
|
|
throw new Error('Nuxt instance is unavailable!')
|
|
}
|
|
return instance
|
|
}
|
|
|
|
/**
|
|
* Get access to Nuxt instance.
|
|
*
|
|
* Returns null if Nuxt instance is unavailable.
|
|
* @example
|
|
* ```js
|
|
* const nuxt = tryUseNuxt()
|
|
* if (nuxt) {
|
|
* // Do something
|
|
* }
|
|
* ```
|
|
*/
|
|
export function tryUseNuxt (): Nuxt | null {
|
|
return nuxtCtx.tryUse()
|
|
}
|