mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-02-02 21:27:53 +01:00
update readme
This commit is contained in:
65
README.md
65
README.md
@@ -34,6 +34,13 @@ export const router = trpc
|
|||||||
.query('hello', {
|
.query('hello', {
|
||||||
resolve: () => 'world',
|
resolve: () => 'world',
|
||||||
})
|
})
|
||||||
|
.query('bye', {
|
||||||
|
resolve() {
|
||||||
|
return {
|
||||||
|
text: 'goodbye',
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
// optional
|
// optional
|
||||||
export const createContext = () => {
|
export const createContext = () => {
|
||||||
@@ -58,10 +65,60 @@ Use the client like so:
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const client = useClient()
|
const client = useClient()
|
||||||
|
|
||||||
const bilbo = await client.query('getUser', 'id_bilbo');
|
const greeting = await client.query('hello');
|
||||||
// => { id: 'id_bilbo', name: 'Bilbo' };
|
console.log(greeting); // => 👈 world
|
||||||
|
|
||||||
const frodo = await client.mutation('createUser', { name: 'Frodo' });
|
const farewell = await client.query('bye');
|
||||||
// => { id: 'id_frodo', name: 'Frodo' };
|
console.log(farewell); // => 👈 goodbye
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Recipes
|
||||||
|
|
||||||
|
## Input validation
|
||||||
|
|
||||||
|
With [Zod](https://github.com/colinhacks/zod)
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// ~/trpc/index.ts
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
export const router = trpc
|
||||||
|
.router()
|
||||||
|
.mutation('createUser', {
|
||||||
|
// validate input with Zod
|
||||||
|
input: z.object({
|
||||||
|
name: z.string().min(5)
|
||||||
|
}),
|
||||||
|
async resolve(req) {
|
||||||
|
// use your ORM of choice
|
||||||
|
return await UserModel.create({
|
||||||
|
data: req.input,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
With [Yup](https://github.com/jquense/yup)
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// ~/trpc/index.ts
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
export const router = trpc
|
||||||
|
.router()
|
||||||
|
.mutation('createUser', {
|
||||||
|
// validate input with Zod
|
||||||
|
input: yup.object({
|
||||||
|
name: yup.string().required(),
|
||||||
|
}),
|
||||||
|
async resolve(req) {
|
||||||
|
// use your ORM of choice
|
||||||
|
return await UserModel.create({
|
||||||
|
data: req.input,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
Learn more about tRPC.io [here](https://trpc.io/docs).
|
||||||
|
|||||||
Reference in New Issue
Block a user