From e9224c3c5e9ef0c5cdbfb0372e3ccb0ffc897da0 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Mon, 16 May 2022 08:22:54 -0700 Subject: [PATCH] import handler from template --- package.json | 20 ++++----- playground/server/api/trpc/[query].ts | 7 --- playground/server/fn/index.ts | 9 +--- pnpm-lock.yaml | 17 ++++--- src/module.ts | 65 ++++++++++++++++++++++----- src/runtime/{handler.ts => api.ts} | 5 ++- 6 files changed, 78 insertions(+), 45 deletions(-) delete mode 100644 playground/server/api/trpc/[query].ts rename src/runtime/{handler.ts => api.ts} (95%) diff --git a/package.json b/package.json index f76be65..2a2fb70 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "trpc-nuxt", "type": "module", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "main": "./dist/module.cjs", "types": "./dist/types.d.ts", @@ -10,9 +10,9 @@ "import": "./dist/module.mjs", "require": "./dist/module.cjs" }, - "./handler": { - "import": "./dist/runtime/handler.mjs", - "types": "./dist/runtime/handler.d.ts" + "./api": { + "import": "./dist/runtime/api.mjs", + "types": "./dist/runtime/api.d.ts" } }, "files": [ @@ -20,11 +20,10 @@ "*.d.ts" ], "scripts": { - "build": "nuxt-module-build", + "prepack": "nuxt-module-build", "dev": "nuxi dev playground", "dev:build": "nuxi build playground", - "play": "pnpm run build && nuxi dev playground", - "prepare": "nuxi prepare playground", + "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground", "lint": "eslint .", "lint:fix": "eslint . --fix" }, @@ -32,15 +31,16 @@ "@nuxt/kit": "^3.0.0-rc.3", "@trpc/server": "^9.23.2", "fs-extra": "^10.1.0", - "ufo": "^0.8.4", - "zod": "^3.16.0" + "pathe": "^0.3.0", + "ufo": "^0.8.4" }, "devDependencies": { "@antfu/eslint-config": "^0.23.1", "@nuxt/module-builder": "latest", "@types/fs-extra": "^9.0.13", "eslint": "latest", - "nuxt": "^3.0.0-rc.3" + "nuxt": "^3.0.0-rc.3", + "zod": "^3.16.0" }, "eslintConfig": { "extends": "@antfu" diff --git a/playground/server/api/trpc/[query].ts b/playground/server/api/trpc/[query].ts deleted file mode 100644 index 0b8f4e9..0000000 --- a/playground/server/api/trpc/[query].ts +++ /dev/null @@ -1,7 +0,0 @@ -// generated by trpc-nuxt - import { createTRPCHandler } from 'trpc-nuxt/handler' - import * as functions from '../../fn' - export default createTRPCHandler({ - router: functions.router - }) - \ No newline at end of file diff --git a/playground/server/fn/index.ts b/playground/server/fn/index.ts index 811abf5..3e80091 100644 --- a/playground/server/fn/index.ts +++ b/playground/server/fn/index.ts @@ -1,16 +1,11 @@ +// generated by trpc-nuxt import * as trpc from '@trpc/server' -import { z } from 'zod' export const router = trpc .router() - .query('getUser', { - input: z.object({ name: z.string().min(5) }), - async resolve(req) { - return { id: 1, name: req.input.name } - }, - }) .query('hello', { resolve: () => 'world', }) export type Router = typeof router + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c253116..f2d13aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,23 +12,22 @@ importers: eslint: latest fs-extra: ^10.1.0 nuxt: ^3.0.0-rc.3 + pathe: ^0.3.0 ufo: ^0.8.4 zod: ^3.16.0 dependencies: '@nuxt/kit': 3.0.0-rc.3 - '@trpc/server': 9.23.2 + '@trpc/server': 9.23.3 fs-extra: 10.1.0 + pathe: 0.3.0 ufo: 0.8.4 - zod: 3.16.0 devDependencies: '@antfu/eslint-config': 0.23.1_eslint@8.15.0 '@nuxt/module-builder': 0.1.7 '@types/fs-extra': 9.0.13 eslint: 8.15.0 nuxt: 3.0.0-rc.3 - - playground: - specifiers: {} + zod: 3.16.0 packages: @@ -872,8 +871,8 @@ packages: estree-walker: 2.0.2 picomatch: 2.3.1 - /@trpc/server/9.23.2: - resolution: {integrity: sha512-uFvn2pFV+wJWLtvH3sqwF+1xfMp1m3Irq4S9MkNtUIFER9k3o6RN6FU2yUA+nefeCDkGsptPE7R4RbxojtpNXw==} + /@trpc/server/9.23.3: + resolution: {integrity: sha512-J8TWw/Y5TFp6QGfdry224TgfnkqXETpxAwYZS+dwf3uYCQG49sdl9RUgrf5V2IxVnQA5OXQtjDHJUSzHM7okwQ==} dev: false /@trysound/sax/0.2.0: @@ -6513,7 +6512,7 @@ packages: /wide-align/1.1.5: resolution: {integrity: sha512-eDMORYaPNZ4sQIuuYPDHdQvf4gyCF9rEEV/yPxGfwPkRodwEgiMUUXTx/dex+Me0wxx53S+NgUHaP7y3MGlDmg==} dependencies: - string-width: 4.2.3 + string-width: 1.0.2 dev: true /word-wrap/1.2.3: @@ -6607,4 +6606,4 @@ packages: /zod/3.16.0: resolution: {integrity: sha512-szrIkryADbTM+xBt2a1KoS2CJQXec4f9xG78bj5MJeEH/XqmmHpnO+fG3IE115AKBJak+2HrbxLZkc9mhdbDKA==} - dev: false + dev: true diff --git a/src/module.ts b/src/module.ts index 27ff226..e0df8b3 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,5 +1,6 @@ +import { fileURLToPath } from 'url' import { dirname, resolve } from 'path' -import { defineNuxtModule } from '@nuxt/kit' +import { addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit' import fs from 'fs-extra' export interface ModuleOptions {} @@ -11,17 +12,61 @@ export default defineNuxtModule({ }, defaults: {}, async setup(_options, nuxt) { - const root = nuxt.options.rootDir - const apiPath = resolve(root, 'server/api/trpc/[query].ts') + const srcDir = nuxt.options.srcDir + // const root = nuxt.options.rootDir + // const clientPath = join(nuxt.options.buildDir, 'trpc-client.ts') + // const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) + // const serverPath = resolve(root, 'server/fn') + const buildDir = fileURLToPath(new URL(nuxt.options.buildDir, import.meta.url)) + // const rootDir = fileURLToPath(new URL(root, import.meta.url)) - await fs.ensureDir(dirname(apiPath)) - await fs.writeFile(apiPath, ` + // nuxt.hook('config', (options) => { + // options.build.transpile.push('trpc-nuxt/client') + // }) + + // nuxt.hook('autoImports:extend', (imports) => { + // imports.push({ + // from: clientPath, + // name: 'useServerFn', + // as: 'useServerFn', + // }) + // }) + + const routePath = resolve(srcDir, 'server/fn/index.ts') + await fs.ensureDir(dirname(routePath)) + if (!fs.existsSync(routePath)) { + await fs.writeFile(routePath, ` // generated by trpc-nuxt - import { createTRPCHandler } from 'trpc-nuxt/handler' - import * as functions from '../../fn' - export default createTRPCHandler({ - router: functions.router - }) + import * as trpc from '@trpc/server' + + export const router = trpc + .router() + .query('hello', { + resolve: () => 'world', + }); + + export type Router = typeof router `.trimStart()) + } + + const handlerPath = resolve(srcDir, 'server/fn') + addTemplate({ + filename: 'trpc-handler.mjs', + write: true, + getContents() { + return ` + import { createTRPCHandler } from 'trpc-nuxt/api' + import * as functions from "${handlerPath}" + export default createTRPCHandler({ + router: functions.router + }) + `.trimStart() + }, + }) + + addServerHandler({ + route: '/api/trpc/*', + handler: resolve(buildDir, 'trpc-handler.mjs'), + }) }, }) diff --git a/src/runtime/handler.ts b/src/runtime/api.ts similarity index 95% rename from src/runtime/handler.ts rename to src/runtime/api.ts index 115b2dc..dd4faa6 100644 --- a/src/runtime/handler.ts +++ b/src/runtime/api.ts @@ -33,11 +33,12 @@ export function createTRPCHandler({ createContext?: CreateContextFn responseMeta?: ResponseMetaFn }) { + const url = '/trpc' + return async (event) => { const { req, res, - context, } = event const $url = createURL(req.url) @@ -50,7 +51,7 @@ export function createTRPCHandler({ body: isMethod(event, 'GET') ? null : await useBody(event), query: $url.searchParams, }, - path: context.params.query, + path: $url.pathname.substring(url.length + 5), createContext: async () => createContext?.(req), responseMeta, })