mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
125 lines
2.2 KiB
Markdown
125 lines
2.2 KiB
Markdown
# tRPC-Nuxt
|
|
|
|
[](https://www.npmjs.com/package/trpc-nuxt)
|
|
|
|
End-to-end typesafe APIs with [tRPC.io](https://trpc.io/) in Nuxt applications.
|
|
|
|
## Install
|
|
|
|
```bash
|
|
npm i trpc-nuxt -D
|
|
```
|
|
|
|
```ts
|
|
// nuxt.config.ts
|
|
import { defineNuxtConfig } from 'nuxt'
|
|
|
|
export default defineNuxtConfig({
|
|
modules: ['trpc-nuxt'],
|
|
})
|
|
```
|
|
|
|
## Usage
|
|
|
|
Create your tRPC [routes](https://trpc.io/docs/router) and [context](https://trpc.io/docs/context) under `~/trpc/index.ts`:
|
|
|
|
```ts
|
|
// ~/trpc/index.ts
|
|
import type { inferAsyncReturnType } from '@trpc/server'
|
|
import * as trpc from '@trpc/server'
|
|
|
|
export const router = trpc
|
|
.router<inferAsyncReturnType<typeof createContext>>()
|
|
// queries and mutations...
|
|
.query('hello', {
|
|
resolve: () => 'world',
|
|
})
|
|
.query('bye', {
|
|
resolve() {
|
|
return {
|
|
text: 'goodbye',
|
|
}
|
|
},
|
|
})
|
|
|
|
// optional
|
|
export const createContext = () => {
|
|
// ...
|
|
return {
|
|
/** context data */
|
|
}
|
|
}
|
|
|
|
// optional
|
|
export const responseMeta = () => {
|
|
// ...
|
|
return {
|
|
// { headers: ... }
|
|
}
|
|
}
|
|
```
|
|
|
|
Use the client like so:
|
|
|
|
```html
|
|
<script setup lang="ts">
|
|
const client = useClient()
|
|
|
|
const greeting = await client.query('hello');
|
|
console.log(greeting); // => 👈 world
|
|
|
|
const farewell = await client.query('bye');
|
|
console.log(farewell); // => 👈 goodbye
|
|
</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).
|