🐛 The bug
The following error message appears when a server middleware is called which uses defineNuxtServerMiddleware:
You need to add
@nuxtjs/composition-apito your buildModules in order to use it
🛠️ To reproduce
server-middleware/ping.ts:
import {defineNuxtServerMiddleware} from "@nuxtjs/composition-api";
export default defineNuxtServerMiddleware((req, res) => {
res.statusCode = 200
res.end('Pong!')
})
nuxt.config.ts
export default defineNuxtConfig( {
// Your config
buildModules: [
'@nuxt/typescript-build',
'@nuxtjs/composition-api',
],
serverMiddleware: [
{ path: '/ping', handler: '~/server-middleware/ping.ts' }
],
})
Now visit http://localhost:3000/ping
I found the following related issue which was markes as solved: https://github.com/nuxt-community/composition-api/issues/409
I am using version 0.22.4 which should include the fix which was mentioned in the issue.
@P4sca1 Good catch; it was fixed incorrectly.
However, I'm likely to deprecate defineNuxtServerMiddleware as it makes the package a runtime dependency which it's not intended to be.
So, for the moment, you can create your own version of the composables like this:
import { Module, ServerMiddleware, NuxtConfig } from '@nuxt/types'
export const defineNuxtModule = <T extends Record<string, unknown>>(module: Module<T>) => module
export const defineNuxtServerMiddleware = (serverMiddleware: ServerMiddleware) => serverMiddleware
export const defineNuxtConfig = (config: NuxtConfig) => config
Sounds good to me. Does this mean without using those helpers @nuxtjs/composition-api can be a devDependency?
@P4sca1 Indeed it can. Note that with the next release of the library, you will need to add @vue/composition-api to your runtime dependencies (or choose to transpile it by adding to build.transpile).
This has now been removed (as promised) in the latest v0.23.0.