• Stars
    star
    289
  • Rank 143,419 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

playground for running tRPC queries in the browser

tRPC Playground

Playground for running tRPC queries in the browser. Backed by CodeMirror and the TypeScript language server to provide you with the same fully-typed experience.

trpc-playground-preview-example.mp4

Installation

npm install trpc-playground

Handlers

tRPC Playground provides handlers that serve the playground HTML page and handle playground-related requests such as getting types from the router.

Next.js

Example

// pages/api/trpc-playground.ts
import { NextApiHandler } from 'next'
import { appRouter } from 'server/routers/_app'
import { nextHandler } from 'trpc-playground/handlers/next'

const setupHandler = nextHandler({
  router: appRouter,
  // tRPC api path, pages/api/trpc/[trpc].ts in this case
  trpcApiEndpoint: '/api/trpc',
  playgroundEndpoint: '/api/trpc-playground',
  // uncomment this if you're using superjson
  // request: {
  //   superjson: true,
  // },
})

const handler: NextApiHandler = async (req, res) => {
  const playgroundHandler = await setupHandler
  await playgroundHandler(req, res)
}

export default handler
Express

Example

// server.ts
import * as trpcExpress from '@trpc/server/adapters/express'
import express from 'express'
import { expressHandler } from 'trpc-playground/handlers/express'
import { appRouter } from './router'

const runApp = async () => {
  const app = express()

  const trpcApiEndpoint = '/api/trpc'
  const playgroundEndpoint = '/api/trpc-playground'

  app.use(
    trpcApiEndpoint,
    trpcExpress.createExpressMiddleware({
      router: appRouter,
    }),
  )

  app.use(
    playgroundEndpoint,
    await expressHandler({
      trpcApiEndpoint,
      playgroundEndpoint,
      router: appRouter,
      // uncomment this if you're using superjson
      // request: {
      //   superjson: true,
      // },
    }),
  )

  app.listen(3000, () => {
    console.log('listening at http://localhost:3000')
  })
}

runApp()
trpc-nuxt

trpc-nuxt

// server/api/trpc-playground.ts

import { h3Handler } from 'trpc-playground/handlers/h3'
import { appRouter } from '~~/server/trpc/routers'

export default defineLazyEventHandler(async () => {
  const setupHandler = await h3Handler({
    router: appRouter,
    trpcApiEndpoint: '/api/trpc',
    playgroundEndpoint: '/api/trpc-playground',
    // uncomment this if you're using superjson
    // request: {
    //   superjson: true,
    // },
  })

  return defineEventHandler(setupHandler)
})

tRPC Playground also supports Fastify, Fetch, h3, Koa, and AWS Lambda. You can import them using this format: trpc-playground/handlers/{framework}.

Settings

For all configuration options, see the API docs.

Writing Queries

In the playground, writing queries is meant to mimic the experience of writing queries in a tRPC client as closely as possible. You can even write TS and your code will be transformed to JS before it is run.

tRPC Playground queries follow the syntax of the proxy vanilla client:

await trpc.path.query(input)

// example
await trpc.getUser.query({ id: 4 })

For mutations:

await trpc.path.mutate(input)

// example
await trpc.createUser.mutate({ name: 'Bob' })

When using the Run all queries button in the center of the editor, you can write any code and it will just work:

const name: string = 'John'

// run queries one at a time
await trpc.getGreeting.query({ name })
await trpc.getFarewell.query({ name })

// batch queries
await Promise.all([
  trpc.getGreeting.query({ name }),
  trpc.getFarewell.query({ name }),
])

Queries can be run individually by pressing on the button to the left of them or by pressing Alt + Q when your cursor in the editor is on the query. Note that any variables you pass to the query will not be defined when running queries individually.

You can use the return value of one query and pass it to the next:

const { sum } = await trpc.addNums.query({ a: 1, b: 2 })
await trpc.subtractNums.query({ a: sum, b: -7 })

Types

tRPC Playground resolves the types for your queries based on the input schema in your router. The default resolver is zod-to-ts, which should work out of the box for the most part. However, there are a few special cases that it may not handle correctly such as z.lazy() and z.nativeEnum(), so read those docs for more information on how to handle these cases if you have any issues with them.

More Repositories

1

zod-to-ts

generate TypeScript types from your Zod schema
TypeScript
342
star
2

trpc-v10-migrate-codemod

codemod to migrate your tRPC codebase from v9 to v10
TypeScript
69
star
3

trpc-bun

tRPC + Bun
TypeScript
64
star
4

trpc-pokemon

public tRPC Pokemon API
TypeScript
53
star
5

ts-lib-starter

Boilerplate for your next TypeScript library. Build with speed.
TypeScript
50
star
6

rodemirror

React component for CodeMirror 6
TypeScript
49
star
7

uttp

write your request handlers once, run anywhere
TypeScript
36
star
8

onetyped

one schema to rule them all
TypeScript
25
star
9

trpc-ultra

tRPC + Ultra
TypeScript
21
star
10

simple-icons-py

python wrapper for simple-icons
Python
18
star
11

discordbotdash

A python package for testing/managing your discord.py bot in browser.
HTML
10
star
12

rehype-tree-sitter-highlight

highlight your code using tree-sitter-highlight
TypeScript
9
star
13

yarn-plugin-postinstall-dev

Run a postinstallDev script after installation
TypeScript
8
star
14

pdfc

PDF compiler for your source code
TypeScript
7
star
15

trpc-rakkas

tRPC in Rakkas
TypeScript
6
star
16

shiki-renderer-pdf

PDF renderer for Shiki
TypeScript
6
star
17

trpc-nextjs-middleware

tRPC + Next.js Middleware
TypeScript
5
star
18

eslint-config

my ESLint config
TypeScript
4
star
19

si-components

simple-icons components for various frontend libraries
TypeScript
2
star
20

codemirror-lang-elixir

CodeMirror 6 mode for Elixir
JavaScript
2
star
21

svg-rewriter

add attributes to a svg file from a url
TypeScript
2
star
22

trpc-snapshot

TypeScript
1
star
23

rxjs-rollup-bundle

HTML
1
star
24

type-benchmark

A TypeScript type benchmark testing the performance of type alias vs. interfaces.
TypeScript
1
star