Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
trpc-playground
Advanced tools
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.
npm 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.
// 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
// 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 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.
For trpc.query(path, inputArgs)
or trpc.useQuery([path, inputArgs])
:
await query(path, inputArgs)
// example
await query('getUser', { id: 4 })
For trpc.mutation(path, inputArgs)
or trpc.useMutation([path, inputArgs])
:
await mutation(path, inputArgs)
// example
await mutation('createUser', { 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'
await query('getGreeting', { name })
await query('getFarewell', { 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.
If request.batching
is false
in your config (it is true
by default), the queries will be run one at a time so you can use the return value of one query and pass it to the next:
const { sum } = await query('addNums', { a: 1, b: 2 })
await query('subtractNums', { 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.
FAQs
playground for running tRPC queries in the browser
We found that trpc-playground demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.