update readme

This commit is contained in:
Robert Soriano
2022-05-17 15:04:51 -07:00
parent 9fac4ac4d9
commit fa7db27be1
4 changed files with 29 additions and 22 deletions

6
README.md Normal file
View File

@@ -0,0 +1,6 @@
# tRPC-Nuxt
[![Build Size](https://img.shields.io/bundlephobia/minzip/trpc-nuxt?label=bundle%20size&style=flat&colorA=000000&colorB=000000)](https://bundlephobia.com/result?p=trpc-nuxt)
[![Version](https://img.shields.io/npm/v/trpc-nuxt?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/trpc-nuxt)
End-to-end typesafe APIs with [tRPC.io](https://trpc.io/) in Nuxt applications.

View File

@@ -1,6 +1,6 @@
<script setup lang="ts">
const { data, error } = await useTrpcQuery('getUser', {
name: 'john',
name: 'johns',
})
watchEffect(() => {
@@ -10,5 +10,5 @@ watchEffect(() => {
</script>
<template>
<div>hello</div>
<div>{{ data.name }}</div>
</template>

View File

@@ -13,4 +13,20 @@ export const router = trpc
resolve: () => 'world',
})
// optional
export const createContext = () => {
// ...
return {
/** context data */
}
}
// optional
export const responseMeta = () => {
// ...
return {
// { headers: ... }
}
}
export type Router = typeof router

View File

@@ -1,4 +1,4 @@
import { dirname, join, resolve } from 'pathe'
import { dirname, join } from 'pathe'
import { addServerHandler, defineNuxtModule } from '@nuxt/kit'
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.writeFile(clientPath, `
@@ -61,24 +58,12 @@ export default defineNuxtModule<ModuleOptions>({
await fs.writeFile(handlerPath, `
import { createTRPCHandler } from 'trpc-nuxt/api'
import * as functions from '~/server/fn'
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())
}
},
})