Gathering detailed insights and metrics for trpc-playground
Gathering detailed insights and metrics for trpc-playground
Gathering detailed insights and metrics for trpc-playground
Gathering detailed insights and metrics for trpc-playground
npm install trpc-playground
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
294 Stars
183 Commits
22 Forks
4 Watching
3 Branches
6 Contributors
Updated on 24 Nov 2024
TypeScript (93.73%)
JavaScript (5.31%)
HTML (0.52%)
CSS (0.43%)
Cumulative downloads
Total Downloads
Last day
-48.9%
4,439
Compared to previous day
Last week
39%
32,675
Compared to previous week
Last month
72.2%
91,025
Compared to previous month
Last year
27.2%
617,075
Compared to previous year
2
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.
1npm install trpc-playground
tRPC Playground provides handlers that serve the playground HTML page and handle playground-related requests such as getting types from the router.
Next.js App Router
1// src/app/api/trpc-playground/route.ts 2import { appRouter } from '@/server' 3import { fetchHandler } from 'trpc-playground/handlers/fetch' 4 5const setupHandler = fetchHandler({ 6 router: appRouter, 7 trpcApiEndpoint: '/api/trpc', 8 playgroundEndpoint: '/api/trpc-playground', 9}) 10 11const handler = async (req: Request) => { 12 const playgroundHandler = await setupHandler 13 return await playgroundHandler(req) 14} 15 16export { handler as GET, handler as POST }
Next.js Pages Router
1// pages/api/trpc-playground.ts
2import { NextApiHandler } from 'next'
3import { appRouter } from 'server/routers/_app'
4import { nextHandler } from 'trpc-playground/handlers/next'
5
6const setupHandler = nextHandler({
7 router: appRouter,
8 // tRPC api path, pages/api/trpc/[trpc].ts in this case
9 trpcApiEndpoint: '/api/trpc',
10 playgroundEndpoint: '/api/trpc-playground',
11 // uncomment this if you're using superjson
12 // request: {
13 // superjson: true,
14 // },
15})
16
17const handler: NextApiHandler = async (req, res) => {
18 const playgroundHandler = await setupHandler
19 await playgroundHandler(req, res)
20}
21
22export default handler
1// server.ts 2import * as trpcExpress from '@trpc/server/adapters/express' 3import express from 'express' 4import { expressHandler } from 'trpc-playground/handlers/express' 5import { appRouter } from './router' 6 7const runApp = async () => { 8 const app = express() 9 10 const trpcApiEndpoint = '/api/trpc' 11 const playgroundEndpoint = '/api/trpc-playground' 12 13 app.use( 14 trpcApiEndpoint, 15 trpcExpress.createExpressMiddleware({ 16 router: appRouter, 17 }), 18 ) 19 20 app.use( 21 playgroundEndpoint, 22 await expressHandler({ 23 trpcApiEndpoint, 24 playgroundEndpoint, 25 router: appRouter, 26 // uncomment this if you're using superjson 27 // request: { 28 // superjson: true, 29 // }, 30 }), 31 ) 32 33 app.listen(3000, () => { 34 console.log('listening at http://localhost:3000') 35 }) 36} 37 38runApp()
1// server/api/trpc-playground.ts
2
3import { h3Handler } from 'trpc-playground/handlers/h3'
4import { appRouter } from '~~/server/trpc/routers'
5
6export default defineLazyEventHandler(async () => {
7 const setupHandler = await h3Handler({
8 router: appRouter,
9 trpcApiEndpoint: '/api/trpc',
10 playgroundEndpoint: '/api/trpc-playground',
11 // uncomment this if you're using superjson
12 // request: {
13 // superjson: true,
14 // },
15 })
16
17 return defineEventHandler(setupHandler)
18})
tRPC Playground also supports Fastify, Fetch, h3, Koa, and AWS Lambda. You can import them using this format: trpc-playground/handlers/{framework}
.
For all configuration options, see the API docs.
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:
1await trpc.path.query(input) 2 3// example 4await trpc.getUser.query({ id: 4 })
For mutations:
1await trpc.path.mutate(input) 2 3// example 4await 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:
1const name: string = 'John' 2 3// run queries one at a time 4await trpc.getGreeting.query({ name }) 5await trpc.getFarewell.query({ name }) 6 7// batch queries 8await Promise.all([ 9 trpc.getGreeting.query({ name }), 10 trpc.getFarewell.query({ name }), 11])
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:
1const { sum } = await trpc.addNums.query({ a: 1, b: 2 }) 2await trpc.subtractNums.query({ a: sum, b: -7 })
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.
No vulnerabilities found.
No security vulnerabilities found.