mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-23 16:30:41 +01:00
import handler from template
This commit is contained in:
@@ -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'),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
Reference in New Issue
Block a user