import handler from template

This commit is contained in:
Robert Soriano
2022-05-16 08:22:54 -07:00
parent b0eab95b47
commit e9224c3c5e
6 changed files with 78 additions and 45 deletions

View File

@@ -1,7 +1,7 @@
{ {
"name": "trpc-nuxt", "name": "trpc-nuxt",
"type": "module", "type": "module",
"version": "0.0.2", "version": "0.0.3",
"license": "MIT", "license": "MIT",
"main": "./dist/module.cjs", "main": "./dist/module.cjs",
"types": "./dist/types.d.ts", "types": "./dist/types.d.ts",
@@ -10,9 +10,9 @@
"import": "./dist/module.mjs", "import": "./dist/module.mjs",
"require": "./dist/module.cjs" "require": "./dist/module.cjs"
}, },
"./handler": { "./api": {
"import": "./dist/runtime/handler.mjs", "import": "./dist/runtime/api.mjs",
"types": "./dist/runtime/handler.d.ts" "types": "./dist/runtime/api.d.ts"
} }
}, },
"files": [ "files": [
@@ -20,11 +20,10 @@
"*.d.ts" "*.d.ts"
], ],
"scripts": { "scripts": {
"build": "nuxt-module-build", "prepack": "nuxt-module-build",
"dev": "nuxi dev playground", "dev": "nuxi dev playground",
"dev:build": "nuxi build playground", "dev:build": "nuxi build playground",
"play": "pnpm run build && nuxi dev playground", "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"prepare": "nuxi prepare playground",
"lint": "eslint .", "lint": "eslint .",
"lint:fix": "eslint . --fix" "lint:fix": "eslint . --fix"
}, },
@@ -32,15 +31,16 @@
"@nuxt/kit": "^3.0.0-rc.3", "@nuxt/kit": "^3.0.0-rc.3",
"@trpc/server": "^9.23.2", "@trpc/server": "^9.23.2",
"fs-extra": "^10.1.0", "fs-extra": "^10.1.0",
"ufo": "^0.8.4", "pathe": "^0.3.0",
"zod": "^3.16.0" "ufo": "^0.8.4"
}, },
"devDependencies": { "devDependencies": {
"@antfu/eslint-config": "^0.23.1", "@antfu/eslint-config": "^0.23.1",
"@nuxt/module-builder": "latest", "@nuxt/module-builder": "latest",
"@types/fs-extra": "^9.0.13", "@types/fs-extra": "^9.0.13",
"eslint": "latest", "eslint": "latest",
"nuxt": "^3.0.0-rc.3" "nuxt": "^3.0.0-rc.3",
"zod": "^3.16.0"
}, },
"eslintConfig": { "eslintConfig": {
"extends": "@antfu" "extends": "@antfu"

View File

@@ -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
})

View File

@@ -1,16 +1,11 @@
// generated by trpc-nuxt
import * as trpc from '@trpc/server' import * as trpc from '@trpc/server'
import { z } from 'zod'
export const router = trpc export const router = trpc
.router() .router()
.query('getUser', {
input: z.object({ name: z.string().min(5) }),
async resolve(req) {
return { id: 1, name: req.input.name }
},
})
.query('hello', { .query('hello', {
resolve: () => 'world', resolve: () => 'world',
}) })
export type Router = typeof router export type Router = typeof router

17
pnpm-lock.yaml generated
View File

@@ -12,23 +12,22 @@ importers:
eslint: latest eslint: latest
fs-extra: ^10.1.0 fs-extra: ^10.1.0
nuxt: ^3.0.0-rc.3 nuxt: ^3.0.0-rc.3
pathe: ^0.3.0
ufo: ^0.8.4 ufo: ^0.8.4
zod: ^3.16.0 zod: ^3.16.0
dependencies: dependencies:
'@nuxt/kit': 3.0.0-rc.3 '@nuxt/kit': 3.0.0-rc.3
'@trpc/server': 9.23.2 '@trpc/server': 9.23.3
fs-extra: 10.1.0 fs-extra: 10.1.0
pathe: 0.3.0
ufo: 0.8.4 ufo: 0.8.4
zod: 3.16.0
devDependencies: devDependencies:
'@antfu/eslint-config': 0.23.1_eslint@8.15.0 '@antfu/eslint-config': 0.23.1_eslint@8.15.0
'@nuxt/module-builder': 0.1.7 '@nuxt/module-builder': 0.1.7
'@types/fs-extra': 9.0.13 '@types/fs-extra': 9.0.13
eslint: 8.15.0 eslint: 8.15.0
nuxt: 3.0.0-rc.3 nuxt: 3.0.0-rc.3
zod: 3.16.0
playground:
specifiers: {}
packages: packages:
@@ -872,8 +871,8 @@ packages:
estree-walker: 2.0.2 estree-walker: 2.0.2
picomatch: 2.3.1 picomatch: 2.3.1
/@trpc/server/9.23.2: /@trpc/server/9.23.3:
resolution: {integrity: sha512-uFvn2pFV+wJWLtvH3sqwF+1xfMp1m3Irq4S9MkNtUIFER9k3o6RN6FU2yUA+nefeCDkGsptPE7R4RbxojtpNXw==} resolution: {integrity: sha512-J8TWw/Y5TFp6QGfdry224TgfnkqXETpxAwYZS+dwf3uYCQG49sdl9RUgrf5V2IxVnQA5OXQtjDHJUSzHM7okwQ==}
dev: false dev: false
/@trysound/sax/0.2.0: /@trysound/sax/0.2.0:
@@ -6513,7 +6512,7 @@ packages:
/wide-align/1.1.5: /wide-align/1.1.5:
resolution: {integrity: sha512-eDMORYaPNZ4sQIuuYPDHdQvf4gyCF9rEEV/yPxGfwPkRodwEgiMUUXTx/dex+Me0wxx53S+NgUHaP7y3MGlDmg==} resolution: {integrity: sha512-eDMORYaPNZ4sQIuuYPDHdQvf4gyCF9rEEV/yPxGfwPkRodwEgiMUUXTx/dex+Me0wxx53S+NgUHaP7y3MGlDmg==}
dependencies: dependencies:
string-width: 4.2.3 string-width: 1.0.2
dev: true dev: true
/word-wrap/1.2.3: /word-wrap/1.2.3:
@@ -6607,4 +6606,4 @@ packages:
/zod/3.16.0: /zod/3.16.0:
resolution: {integrity: sha512-szrIkryADbTM+xBt2a1KoS2CJQXec4f9xG78bj5MJeEH/XqmmHpnO+fG3IE115AKBJak+2HrbxLZkc9mhdbDKA==} resolution: {integrity: sha512-szrIkryADbTM+xBt2a1KoS2CJQXec4f9xG78bj5MJeEH/XqmmHpnO+fG3IE115AKBJak+2HrbxLZkc9mhdbDKA==}
dev: false dev: true

View File

@@ -1,5 +1,6 @@
import { fileURLToPath } from 'url'
import { dirname, resolve } from 'path' import { dirname, resolve } from 'path'
import { defineNuxtModule } from '@nuxt/kit' import { addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
import fs from 'fs-extra' import fs from 'fs-extra'
export interface ModuleOptions {} export interface ModuleOptions {}
@@ -11,17 +12,61 @@ export default defineNuxtModule<ModuleOptions>({
}, },
defaults: {}, defaults: {},
async setup(_options, nuxt) { async setup(_options, nuxt) {
const root = nuxt.options.rootDir const srcDir = nuxt.options.srcDir
const apiPath = resolve(root, 'server/api/trpc/[query].ts') // 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)) // nuxt.hook('config', (options) => {
await fs.writeFile(apiPath, ` // 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 // generated by trpc-nuxt
import { createTRPCHandler } from 'trpc-nuxt/handler' import * as trpc from '@trpc/server'
import * as functions from '../../fn'
export default createTRPCHandler({ export const router = trpc
router: functions.router .router()
}) .query('hello', {
resolve: () => 'world',
});
export type Router = typeof router
`.trimStart()) `.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'),
})
}, },
}) })

View File

@@ -33,11 +33,12 @@ export function createTRPCHandler<Router extends AnyRouter>({
createContext?: CreateContextFn<Router> createContext?: CreateContextFn<Router>
responseMeta?: ResponseMetaFn<Router> responseMeta?: ResponseMetaFn<Router>
}) { }) {
const url = '/trpc'
return async (event) => { return async (event) => {
const { const {
req, req,
res, res,
context,
} = event } = event
const $url = createURL(req.url) const $url = createURL(req.url)
@@ -50,7 +51,7 @@ export function createTRPCHandler<Router extends AnyRouter>({
body: isMethod(event, 'GET') ? null : await useBody(event), body: isMethod(event, 'GET') ? null : await useBody(event),
query: $url.searchParams, query: $url.searchParams,
}, },
path: context.params.query, path: $url.pathname.substring(url.length + 5),
createContext: async () => createContext?.(req), createContext: async () => createContext?.(req),
responseMeta, responseMeta,
}) })