mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-22 16:00:37 +01:00
62 lines
1.1 KiB
Markdown
62 lines
1.1 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 `server/fn/index.ts`:
|
|
|
|
```ts
|
|
// ~/server/fn/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',
|
|
})
|
|
|
|
// optional
|
|
export const createContext = () => {
|
|
// ...
|
|
return {
|
|
/** context data */
|
|
}
|
|
}
|
|
|
|
// optional
|
|
export const responseMeta = () => {
|
|
// ...
|
|
return {
|
|
// { headers: ... }
|
|
}
|
|
}
|
|
```
|
|
|
|
Use the client like so:
|
|
|
|
```html
|
|
<script setup lang="ts">
|
|
const { data } = await useTrpcQuery('hello')
|
|
</script>
|
|
```
|