mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 20:19:33 +01:00
c2001cdbacc083043ebe6de2e6278a13a6d6bb46
tRPC-Nuxt
End-to-end typesafe APIs with tRPC.io in Nuxt applications.
Install
npm i trpc-nuxt -D
// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'
export default defineNuxtConfig({
modules: ['trpc-nuxt'],
})
Usage
Create your tRPC routes and context under server/fn/index.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:
<script setup lang="ts">
const { data } = await useTrpcQuery('hello')
</script>
Languages
TypeScript
88.1%
Vue
11.9%