NuxtN
Nuxtβ€’6mo agoβ€’
29 replies
BenG

Nuxt module for oRPC

I am trying to create a nuxt module which would register the catch all event handler, but I am having hard time to register the routers in it. Anyone has idea how to achieve this?

export function registerRouter(moduleName: string, routerSlice: any, schemas?: Record<string, any>) {
  console.log(`πŸ“‹ Registering router for module: ${moduleName}`)

  routerRegistry.set(moduleName, routerSlice)

  if (schemas) {
    schemaRegistry.set(moduleName, schemas)
    console.log(`πŸ“‹ Registered ${Object.keys(schemas).length} schemas for module: ${moduleName}`)
  }
}
export function getRouter(): any {
  const combinedRouter: any = {}

  for (const [moduleName, routerSlice] of routerRegistry.entries()) {
    combinedRouter[moduleName] = routerSlice
  }

  console.log(`πŸ”§ Built combined router with modules: ${Array.from(routerRegistry.keys()).join(', ')}`)
  return combinedRouter
}


And the handler:
export default defineEventHandler(async (event) => {
  console.log('πŸš€ RPC Handler - Handling oRPC request:', event.node.req.method, event.node.req.url)
  const router = getRouter()
  console.log('πŸ”§ Available router modules:', Object.keys(router))

On the project side, I am registering it in a server pluginI can see that:
πŸ“‹ Registering router for module: todos
πŸ“‹ Registered 6 schemas for module: todos
πŸ”§ Built combined router with modules: todos

But on actual requests I see:
πŸš€ RPC Handler - Handling oRPC request: POST /rpc/todos/list
πŸ”§ Built combined router with modules:
πŸ”§ Available router modules: []

So somewhere I lost the registered items, I have tried with useRuntimConfig, same happens. What clears it between the server plugin and the request?
Was this page helpful?