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",
"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"

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 { 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

17
pnpm-lock.yaml generated
View File

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

View File

@@ -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<ModuleOptions>({
},
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'),
})
},
})

View File

@@ -33,11 +33,12 @@ export function createTRPCHandler<Router extends AnyRouter>({
createContext?: CreateContextFn<Router>
responseMeta?: ResponseMetaFn<Router>
}) {
const url = '/trpc'
return async (event) => {
const {
req,
res,
context,
} = event
const $url = createURL(req.url)
@@ -50,7 +51,7 @@ export function createTRPCHandler<Router extends AnyRouter>({
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,
})