Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(kit): useNitro() utility #7557

Merged
merged 8 commits into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/kit/src/index.ts
Expand Up @@ -18,7 +18,7 @@ export * from './layout'
export * from './pages'
export * from './plugin'
export * from './resolve'
export * from './server'
export * from './nitro'
export * from './template'
export * from './logger'

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/module/container.ts
Expand Up @@ -3,7 +3,7 @@ import type { Nuxt, ModuleContainer } from '@nuxt/schema'
import { chainFn } from '../internal/task'
import { addTemplate } from '../template'
import { addLayout } from '../layout'
import { addServerMiddleware } from '../server'
import { addServerMiddleware } from '../nitro'
import { isNuxt2 } from '../compatibility'
import { addPluginTemplate } from '../plugin'
import { useNuxt } from '../context'
Expand Down
25 changes: 24 additions & 1 deletion packages/kit/src/server.ts β†’ packages/kit/src/nitro.ts
@@ -1,5 +1,5 @@
import type { Middleware } from 'h3'
import type { NitroEventHandler, NitroDevEventHandler } from 'nitropack'
import type { NitroEventHandler, NitroDevEventHandler, Nitro } from 'nitropack'
import { useNuxt } from './context'

export interface LegacyServerMiddleware {
Expand Down Expand Up @@ -46,3 +46,26 @@ export function addServerHandler (handler: NitroEventHandler) {
export function addDevServerHandler (handler: NitroDevEventHandler) {
useNuxt().options.devServerHandlers.push(handler)
}

/**
* Access to nitro instance
*
* **Note:** You can use useNitro() You can call `useNitro()` only after `ready` hook.
*
* **Note:** Moditifcations to nitro instance configuration are not reflected.
*
* @example
*
* ```ts
* nuxt.hook('ready', () => {
* console.log(useNitro())
* })
* ```
*/
export function useNitro (): Nitro {
const nuxt = useNuxt()
if (!nuxt._nitro) {
throw new Error('Nitro is not initialized yet. You can call `useNitro()` only after `ready` hook.')
}
return nuxt._nitro
}
pi0 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion packages/nuxt/src/core/nitro.ts
Expand Up @@ -134,7 +134,8 @@ export async function initNitro (nuxt: Nuxt) {
// Init nitro
const nitro = await createNitro(nitroConfig)

// Expose nitro to modules
// Expose nitro to modules and kit
nuxt._nitro = nitro
await nuxt.callHook('nitro:init', nitro)

// Connect vfs storages
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/types/nuxt.ts
@@ -1,12 +1,14 @@
import type { Hookable } from 'hookable'
import type { Ignore } from 'ignore'
import type { Nitro } from 'nitropack'
pi0 marked this conversation as resolved.
Show resolved Hide resolved
import type { NuxtHooks, NuxtLayout, NuxtMiddleware } from './hooks'
import type { NuxtOptions } from './config'

export interface Nuxt {
// Private fields.
_version: string
_ignore?: Ignore
_nitro?: Nitro

/** The resolved Nuxt configuration. */
options: NuxtOptions
Expand Down
10 changes: 9 additions & 1 deletion playground/nuxt.config.ts
@@ -1,3 +1,11 @@
export default defineNuxtConfig({
import { useNitro } from '@nuxt/kit'

export default defineNuxtConfig({
modules: [
(_, nuxt) => {
nuxt.hook('ready', () => {
console.log(useNitro())
})
}
]
})