From 3a6c5b659b91ef5ed344036b1ee908d043b93cec Mon Sep 17 00:00:00 2001 From: wobsoriano Date: Sat, 5 Nov 2022 19:50:39 -0700 Subject: [PATCH] update function names --- playground/server/trpc/{context.ts => trpc.ts} | 0 src/module.ts | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) rename playground/server/trpc/{context.ts => trpc.ts} (100%) diff --git a/playground/server/trpc/context.ts b/playground/server/trpc/trpc.ts similarity index 100% rename from playground/server/trpc/context.ts rename to playground/server/trpc/trpc.ts diff --git a/src/module.ts b/src/module.ts index af297f8..e88e0cc 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,3 +1,4 @@ +import fs from 'fs' import { defineNuxtModule, addTemplate, addServerHandler } from '@nuxt/kit' import fg from 'fast-glob' import { resolve } from 'pathe' @@ -34,7 +35,7 @@ export default defineNuxtModule({ ] const extGlob = '*.{ts,js}' - async function scanServerFunctions () { + async function scanServerRouters () { files.length = 0 files.push(...new Set( (await Promise.all( @@ -50,7 +51,7 @@ export default defineNuxtModule({ } const abs = resolve(nuxt.options.rootDir, path) if (files.includes(abs) || dirs.some(dir => abs.startsWith(dir))) { - await scanServerFunctions() + await scanServerRouters() await nuxt.callHook('builder:generateApp') } }) @@ -66,11 +67,8 @@ export default defineNuxtModule({ getContents () { return dedent` import { initTRPC } from '@trpc/server' - import superjson from 'superjson' - const t = initTRPC.context().create({ - transformer: superjson, - }) + const t = initTRPC.context().create() export const router = t.router export const defineRouter = router @@ -103,9 +101,12 @@ export default defineNuxtModule({ write: true, getContents () { const routeFiles = files.map(i => i.replace(/\.ts$/, '')) + const otherConfigPath = resolve(nuxt.options.rootDir, 'server/trpc/trpc.ts') + const hasConfig = fs.existsSync(otherConfigPath) return dedent` import { createNuxtApiHandler } from 'trpc-nuxt/handler' import { router } from '${resolve(nuxt.options.buildDir, 'trpc/init.mjs')}' + ${hasConfig ? `import * as handlerConfig from '${otherConfigPath.replace('.ts', '')}'` : ''} ${routeFiles.map(i => `import { default as ${getRouteName(i)}Route } from '${i}'`).join('\n')} export const appRouter = router({ @@ -115,7 +116,8 @@ export default defineNuxtModule({ export type AppRouter = typeof appRouter export default createNuxtApiHandler({ - router: appRouter + router: appRouter, + ${hasConfig ? '...handlerConfig' : ''} }) ` } @@ -136,6 +138,6 @@ export default defineNuxtModule({ options.references.push({ path: resolve(nuxt.options.buildDir, 'trpc/types.d.ts') }) }) - await scanServerFunctions() + await scanServerRouters() } })