From 92847ed924684723092400baa70908141be5ecf1 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Fri, 6 May 2022 15:31:52 +0200 Subject: [PATCH] feat(nuxt, kit): add shortcuts to register custom server handlers (#4850) --- docs/content/3.api/4.advanced/2.kit.md | 4 +++- packages/kit/build.config.ts | 1 + packages/kit/src/server.ts | 29 +++++++++++++++++++++++--- packages/nuxt/src/core/nitro.ts | 5 +++-- packages/schema/src/config/_common.ts | 3 ++- packages/schema/src/config/nitro.ts | 26 +++++++++++++++++++++-- 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/docs/content/3.api/4.advanced/2.kit.md b/docs/content/3.api/4.advanced/2.kit.md index dc723f20c7..ad84419167 100644 --- a/docs/content/3.api/4.advanced/2.kit.md +++ b/docs/content/3.api/4.advanced/2.kit.md @@ -60,7 +60,9 @@ [source code](https://github.com/nuxt/framework/blob/main/packages/kit/src/server.ts) -- `addServerMiddleware(serverMiddleware)` +- ~~`addServerMiddleware(serverMiddleware)`~~ +- `addServerHandler (handler)` +- `addDevServerHandler (handler)` ### Resolving diff --git a/packages/kit/build.config.ts b/packages/kit/build.config.ts index b9b26b89bc..700d94cd5b 100644 --- a/packages/kit/build.config.ts +++ b/packages/kit/build.config.ts @@ -7,6 +7,7 @@ export default defineBuildConfig({ ], externals: [ '@nuxt/schema', + 'nitropack', 'webpack', 'vite', 'h3' diff --git a/packages/kit/src/server.ts b/packages/kit/src/server.ts index ce17bac29a..d2c6a20476 100644 --- a/packages/kit/src/server.ts +++ b/packages/kit/src/server.ts @@ -1,12 +1,35 @@ import type { Middleware } from 'h3' +import type { NitroEventHandler, NitroDevEventHandler } from 'nitropack' import { useNuxt } from './context' -export interface ServerMiddleware { +export interface LegacyServerMiddleware { route?: string, + path?: string, + handle?: Middleware | string handler: Middleware | string } -/** Adds a new server middleware to the end of the server middleware array. */ -export function addServerMiddleware (middleware: ServerMiddleware) { +/** + * Adds a new server middleware to the end of the server middleware array. + * + * @deprecated Use addServerHandler instead + */ +export function addServerMiddleware (middleware: LegacyServerMiddleware) { useNuxt().options.serverMiddleware.push(middleware) } + +/** + * Adds a nitro server handler + * + */ +export function addServerHandler (handler: NitroEventHandler) { + useNuxt().options.serverHandlers.push(handler) +} + +/** + * Adds a nitro server handler for development-only + * + */ +export function addDevServerHandler (handler: NitroDevEventHandler) { + useNuxt().options.devServerHandlers.push(handler) +} diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 84b8187226..4339ca06c1 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -176,9 +176,10 @@ export async function initNitro (nuxt: Nuxt) { } async function resolveHandlers (nuxt: Nuxt) { - const handlers: NitroEventHandler[] = [] - const devHandlers: NitroDevEventHandler[] = [] + const handlers: NitroEventHandler[] = [...nuxt.options.serverHandlers] + const devHandlers: NitroDevEventHandler[] = [...nuxt.options.devServerHandlers] + // Map legacy serverMiddleware to handlers for (let m of nuxt.options.serverMiddleware) { if (typeof m === 'string' || typeof m === 'function' /* legacy middleware */) { m = { handler: m } } const route = m.path || m.route || '/' diff --git a/packages/schema/src/config/_common.ts b/packages/schema/src/config/_common.ts index 26e6655ab4..0a1457326c 100644 --- a/packages/schema/src/config/_common.ts +++ b/packages/schema/src/config/_common.ts @@ -393,8 +393,9 @@ export default { * } * } * ``` + * * @version 2 - * @version 3 + * @deprecated Use `serverHandlers` instead */ serverMiddleware: { $resolve: (val: any) => { diff --git a/packages/schema/src/config/nitro.ts b/packages/schema/src/config/nitro.ts index 488130e825..7a8aeb1f03 100644 --- a/packages/schema/src/config/nitro.ts +++ b/packages/schema/src/config/nitro.ts @@ -2,11 +2,33 @@ export default { /** * Configuration for Nitro * - * @see https://github.com/unjs/nitropack + * @see https://nitro.unjs.io/config/ * * @type {typeof import('nitropack')['NitroConfig']} * @version 2 * @version 3 */ - nitro: {} + nitro: {}, + + /** + * Nitro server handlers + * + * @see https://nitro.unjs.io/guide/routing.html + * + * **Note:** Files from `server/api`, `server/middleware` and `server/routes` will be automatically registred by Nuxt. + * + * @type {typeof import('nitropack')['NitroEventHandler'][]} + * @version 3 + */ + serverHandlers: [], + + /** + * Nitro devevelopment-only server handlers + * + * @see https://nitro.unjs.io/guide/routing.html + * + * @type {typeof import('nitropack')['NitroDevEventHandler'][]} + * @version 3 + */ + devServerHandlers: [] }