mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
initial commit
This commit is contained in:
51
.gitignore
vendored
Normal file
51
.gitignore
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
# Dependencies
|
||||
node_modules
|
||||
|
||||
# Logs
|
||||
*.log*
|
||||
|
||||
# Temp directories
|
||||
.temp
|
||||
.tmp
|
||||
.cache
|
||||
|
||||
# Yarn
|
||||
**/.yarn/cache
|
||||
**/.yarn/*state*
|
||||
|
||||
# Generated dirs
|
||||
dist
|
||||
|
||||
# Nuxt
|
||||
.nuxt
|
||||
.output
|
||||
.vercel_build_output
|
||||
.build-*
|
||||
.env
|
||||
.netlify
|
||||
|
||||
# Env
|
||||
.env
|
||||
|
||||
# Testing
|
||||
reports
|
||||
coverage
|
||||
*.lcov
|
||||
.nyc_output
|
||||
|
||||
# VSCode
|
||||
.vscode
|
||||
|
||||
# Intellij idea
|
||||
*.iml
|
||||
.idea
|
||||
|
||||
# OSX
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
2
.npmrc
Normal file
2
.npmrc
Normal file
@@ -0,0 +1,2 @@
|
||||
shamefully-hoist=true
|
||||
ignore-workspace-root-check=true
|
||||
6
README.md
Normal file
6
README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Nuxt Module
|
||||
|
||||
## Development
|
||||
|
||||
- Run `npm run dev:prepare` to generate type stubs.
|
||||
- Use `npm run dev` to start [playground](./playground) in development mode.
|
||||
1
handler.d.ts
vendored
Normal file
1
handler.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export * from './dist/runtime/handler'
|
||||
48
package.json
Normal file
48
package.json
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "trpc-nuxt",
|
||||
"type": "module",
|
||||
"version": "0.0.2",
|
||||
"license": "MIT",
|
||||
"main": "./dist/module.cjs",
|
||||
"types": "./dist/types.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/module.mjs",
|
||||
"require": "./dist/module.cjs"
|
||||
},
|
||||
"./handler": {
|
||||
"import": "./dist/runtime/handler.mjs",
|
||||
"types": "./dist/runtime/handler.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"*.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "nuxt-module-build",
|
||||
"dev": "nuxi dev playground",
|
||||
"dev:build": "nuxi build playground",
|
||||
"play": "pnpm run build && nuxi dev playground",
|
||||
"prepare": "nuxi prepare playground",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix"
|
||||
},
|
||||
"dependencies": {
|
||||
"@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"
|
||||
},
|
||||
"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"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "@antfu"
|
||||
}
|
||||
}
|
||||
8
playground/app.vue
Normal file
8
playground/app.vue
Normal file
@@ -0,0 +1,8 @@
|
||||
<script setup>
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
Nuxt module playground!
|
||||
</div>
|
||||
</template>
|
||||
11
playground/nuxt.config.ts
Normal file
11
playground/nuxt.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineNuxtConfig } from 'nuxt'
|
||||
import MyModule from '..'
|
||||
|
||||
export default defineNuxtConfig({
|
||||
modules: [
|
||||
MyModule,
|
||||
],
|
||||
myModule: {
|
||||
addPlugin: true,
|
||||
},
|
||||
})
|
||||
4
playground/package.json
Normal file
4
playground/package.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "playground",
|
||||
"private": true
|
||||
}
|
||||
7
playground/server/api/trpc/[query].ts
Normal file
7
playground/server/api/trpc/[query].ts
Normal file
@@ -0,0 +1,7 @@
|
||||
// generated by trpc-nuxt
|
||||
import { createTRPCHandler } from 'trpc-nuxt/handler'
|
||||
import * as functions from '../../fn'
|
||||
export default createTRPCHandler({
|
||||
router: functions.router
|
||||
})
|
||||
|
||||
16
playground/server/fn/index.ts
Normal file
16
playground/server/fn/index.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
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
|
||||
6610
pnpm-lock.yaml
generated
Normal file
6610
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
2
pnpm-workspace.yaml
Normal file
2
pnpm-workspace.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
- playground
|
||||
27
src/module.ts
Normal file
27
src/module.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { dirname, resolve } from 'path'
|
||||
import { defineNuxtModule } from '@nuxt/kit'
|
||||
import fs from 'fs-extra'
|
||||
|
||||
export interface ModuleOptions {}
|
||||
|
||||
export default defineNuxtModule<ModuleOptions>({
|
||||
meta: {
|
||||
name: 'trpc-nuxt',
|
||||
configKey: 'trpc',
|
||||
},
|
||||
defaults: {},
|
||||
async setup(_options, nuxt) {
|
||||
const root = nuxt.options.rootDir
|
||||
const apiPath = resolve(root, 'server/api/trpc/[query].ts')
|
||||
|
||||
await fs.ensureDir(dirname(apiPath))
|
||||
await fs.writeFile(apiPath, `
|
||||
// generated by trpc-nuxt
|
||||
import { createTRPCHandler } from 'trpc-nuxt/handler'
|
||||
import * as functions from '../../fn'
|
||||
export default createTRPCHandler({
|
||||
router: functions.router
|
||||
})
|
||||
`.trimStart())
|
||||
},
|
||||
})
|
||||
68
src/runtime/handler.ts
Normal file
68
src/runtime/handler.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { resolveHTTPResponse } from '@trpc/server'
|
||||
import type {
|
||||
AnyRouter,
|
||||
ProcedureType,
|
||||
ResponseMeta,
|
||||
TRPCError,
|
||||
inferRouterContext,
|
||||
inferRouterError,
|
||||
} from '@trpc/server'
|
||||
import { createURL } from 'ufo'
|
||||
import type { IncomingMessage } from 'h3'
|
||||
import type { TRPCResponse } from '@trpc/server/dist/declarations/src/rpc'
|
||||
import { isMethod, useBody } from 'h3'
|
||||
|
||||
type MaybePromise<T> = T | Promise<T>
|
||||
|
||||
type CreateContextFn<TRouter extends AnyRouter> = (req: IncomingMessage) => MaybePromise<inferRouterContext<TRouter>>
|
||||
|
||||
type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
|
||||
data: TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
||||
ctx?: inferRouterContext<TRouter>
|
||||
paths?: string[]
|
||||
type: ProcedureType | 'unknown'
|
||||
errors: TRPCError[]
|
||||
}) => ResponseMeta
|
||||
|
||||
export function createTRPCHandler<Router extends AnyRouter>({
|
||||
router,
|
||||
createContext,
|
||||
responseMeta,
|
||||
}: {
|
||||
router: Router
|
||||
createContext?: CreateContextFn<Router>
|
||||
responseMeta?: ResponseMetaFn<Router>
|
||||
}) {
|
||||
return async (event) => {
|
||||
const {
|
||||
req,
|
||||
res,
|
||||
context,
|
||||
} = event
|
||||
|
||||
const $url = createURL(req.url)
|
||||
|
||||
const httpResponse = await resolveHTTPResponse({
|
||||
router,
|
||||
req: {
|
||||
method: req.method,
|
||||
headers: req.headers,
|
||||
body: isMethod(event, 'GET') ? null : await useBody(event),
|
||||
query: $url.searchParams,
|
||||
},
|
||||
path: context.params.query,
|
||||
createContext: async () => createContext?.(req),
|
||||
responseMeta,
|
||||
})
|
||||
|
||||
const { status, headers, body } = httpResponse
|
||||
|
||||
res.statusCode = status
|
||||
|
||||
Object.keys(headers).forEach((key) => {
|
||||
res.setHeader(key, headers[key])
|
||||
})
|
||||
|
||||
return body
|
||||
}
|
||||
}
|
||||
3
tsconfig.json
Normal file
3
tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "./playground/.nuxt/tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user