2022-05-19 10:54:08 -07:00
2022-05-19 10:26:13 -07:00
2022-05-19 10:54:08 -07:00
2022-05-19 09:37:37 -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-19 09:26:22 -07:00
2022-05-19 09:26:22 -07:00
2022-05-17 14:25:03 -07:00
2022-05-19 10:50:50 -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'],
  trpc: {
    baseURL: 'http://localhost:3000', // defaults to http://localhost:3000
    trpcURL: '/api/trpc', // defaults to /api/trpc
  },
  typescript: {
    strict: true // set this to true to make input/output types work
  }
})

Usage

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

// ~/server/trpc/index.ts
import type { inferAsyncReturnType } from '@trpc/server'
import * as trpc from '@trpc/server'
import { z } from 'zod' //  yup/superstruct/zod/myzod/custom

export const router = trpc.router()
  // queries and mutations...
  .query('getUsers', {
    async resolve(req) {
      // use your ORM of choice
      return await UserModel.all()
    },
  })
  .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,
      })
    },
  })

Use the client like so:

const client = useClient() // auto-imported

const users = await client.query('getUsers')

const addUser = async () => {
  const newUser = await client.mutation('createUser', {
    name: 'wagmi'
  })
}

useAsyncQuery

A thin wrapper around useAsyncData and client.query().

The first argument is a [path, input]-tuple - if the input is optional, you can omit the, input-part.

You'll notice that you get autocompletion on the path and automatic typesafety on the input.

const {
  data,
  pending,
  error,
  refresh
} = await useAsyncQuery(['getUser', { id: 69 }], {
  // pass useAsyncData options here
  server: true
})

Options

trpc-nuxt accepts the following options exposed under ~/server/trpc.index.ts:

import * as trpc from '@trpc/server'
import type { inferAsyncReturnType } from '@trpc/server'
import type { CompatibilityEvent } from 'h3'
import type { OnErrorPayload } from 'trpc-nuxt/api'

export const router = trpc.router<inferAsyncReturnType<typeof createContext>>()

// Optional
// https://trpc.io/docs/context
export const createContext = (event: CompatibilityEvent) => {
  // ...
  return {
    /** context data */
  }
}

// Optional
// https://trpc.io/docs/caching#using-responsemeta--to-cache-responses
export const responseMeta = () => {
  // ...
  return {
    // { headers: ... }
  }
}

// Optional
// https://trpc.io/docs/error-handling#handling-errors
export const onError = (payload: OnErrorPayload<typeof router>) => {
  // Do whatever here like send to bug reporting and stuff
}

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%