2022-05-18 10:29:13 -07:00
2022-05-18 10:29:13 -07:00
2022-05-18 10:29:13 -07:00
2022-05-18 10:29:13 -07:00
2022-05-16 01:55:10 -07:00
2022-05-17 08:57:07 -07:00
2022-05-17 14:25:03 -07:00
2022-05-17 18:28:54 -07:00
2022-05-17 18:04:30 -07:00
2022-05-17 18:04:30 -07:00
2022-05-17 14:25:03 -07:00
2022-05-18 09:23:27 -07:00
2022-05-17 18:29:07 -07:00

tRPC-Nuxt

Version

End-to-end typesafe APIs with tRPC.io in Nuxt applications.

Install

npm i trpc-nuxt
// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  modules: ['trpc-nuxt'],
})

Usage

Expose your tRPC routes and context under ~/server/trpc/index.ts:

// ~/server/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 = (event: CompatibilityEvent) => {
  // ...
  return {
    /** context data */
  }
}

// Optional
export const responseMeta = () => {
  // ...
  return {
    // { headers: ... }
  }
}

Use the client like so:

<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

Learn more about tRPC.io here.

License

MIT

Description
No description provided
Readme MIT 3.1 MiB
Languages
TypeScript 88.1%
Vue 11.9%