mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-28 10:50:35 +01:00
update readme
This commit is contained in:
6
README.md
Normal file
6
README.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# tRPC-Nuxt
|
||||||
|
|
||||||
|
[](https://bundlephobia.com/result?p=trpc-nuxt)
|
||||||
|
[](https://www.npmjs.com/package/trpc-nuxt)
|
||||||
|
|
||||||
|
End-to-end typesafe APIs with [tRPC.io](https://trpc.io/) in Nuxt applications.
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { data, error } = await useTrpcQuery('getUser', {
|
const { data, error } = await useTrpcQuery('getUser', {
|
||||||
name: 'john',
|
name: 'johns',
|
||||||
})
|
})
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
@@ -10,5 +10,5 @@ watchEffect(() => {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>hello</div>
|
<div>{{ data.name }}</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -13,4 +13,20 @@ export const router = trpc
|
|||||||
resolve: () => 'world',
|
resolve: () => 'world',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// optional
|
||||||
|
export const createContext = () => {
|
||||||
|
// ...
|
||||||
|
return {
|
||||||
|
/** context data */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// optional
|
||||||
|
export const responseMeta = () => {
|
||||||
|
// ...
|
||||||
|
return {
|
||||||
|
// { headers: ... }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export type Router = typeof router
|
export type Router = typeof router
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { dirname, join, resolve } from 'pathe'
|
import { dirname, join } from 'pathe'
|
||||||
import { addServerHandler, defineNuxtModule } from '@nuxt/kit'
|
import { addServerHandler, defineNuxtModule } from '@nuxt/kit'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
|
|
||||||
@@ -31,9 +31,6 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const srcDir = nuxt.options.srcDir
|
|
||||||
const optionsPath = resolve(srcDir, 'server/fn/index.ts')
|
|
||||||
|
|
||||||
await fs.ensureDir(dirname(clientPath))
|
await fs.ensureDir(dirname(clientPath))
|
||||||
|
|
||||||
await fs.writeFile(clientPath, `
|
await fs.writeFile(clientPath, `
|
||||||
@@ -61,24 +58,12 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
await fs.writeFile(handlerPath, `
|
await fs.writeFile(handlerPath, `
|
||||||
import { createTRPCHandler } from 'trpc-nuxt/api'
|
import { createTRPCHandler } from 'trpc-nuxt/api'
|
||||||
import * as functions from '~/server/fn'
|
import * as functions from '~/server/fn'
|
||||||
|
|
||||||
export default createTRPCHandler({
|
export default createTRPCHandler({
|
||||||
router: functions.router
|
router: functions.router,
|
||||||
|
createContext: functions.createContext ?? undefined,
|
||||||
|
responseMeta: functions.responseMeta ?? undefined,
|
||||||
})
|
})
|
||||||
`)
|
`)
|
||||||
|
|
||||||
if (!fs.existsSync(optionsPath)) {
|
|
||||||
await fs.writeFile(optionsPath, `
|
|
||||||
// Generated by trpc-nuxt
|
|
||||||
import * as trpc from '@trpc/server'
|
|
||||||
|
|
||||||
export const router = trpc
|
|
||||||
.router()
|
|
||||||
.query('hello', {
|
|
||||||
resolve: () => 'world',
|
|
||||||
});
|
|
||||||
|
|
||||||
export type Router = typeof router
|
|
||||||
`.trimStart())
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user