npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@robojs/trpc

v0.2.3

Published

Fully managed tRPC optimized for Robo.js

Downloads

206

Readme


@robojs/trpc

Add a tRPC server and client to your Robo automatically. tRPC is a modern TypeScript-first RPC framework for building web services. This plugin sets up a tRPC router and client in your project, allowing you to easily create and consume APIs.

You must already have @robojs/server installed.

📚 Documentation: Getting started

🚀 Community: Join our Discord server

Installation 💻

To add this plugin to your Robo.js project:

npx robo add @robojs/server @robojs/trpc

New to Robo.js? Start your project with this plugin pre-installed:

npx create-robo <project-name> -p @robojs/server @robojs/trpc

We also recommend you npm install zod for schema validation. If you want to use React Query capabilities, you will also need to wrap your app in a TRPCProvider.

import { TRPCProvider } from '@robojs/trpc'
import { trpc, trpcQueryClient } from '../trpc/client'

function App() {
	return (
		<TRPCProvider trpc={trpc} trpcClient={trpcQueryClient}>
			<Activity />
		</TRPCProvider>
	)
}

The trpc and trpcQueryClient objects can be seeded for you during installation.

Usage 🌐

If you allowed the plugin to seed your project, you can start using tRPC right away. The plugin sets up a /trpc folder in your project with client.ts and server.ts files. A start event is also added to ensure the router is registered.

Server

The server file is where you define your tRPC router. You can add your procedures here for the client to consume.

import { initTRPC } from '@robojs/trpc'
import { z } from 'zod'

const t = initTRPC.create()
export const router = t.router
export const procedure = t.procedure

export const appRouter = router({
	// Query example: returns a greeting message based on the input text
	hello: procedure
		.input(
			z.object({
				text: z.string()
			})
		)
		.query((opts) => {
			const { text } = opts.input

			return {
				message: `Hello ${text}!`
			}
		}),

	// Mutation example: performs an action with an optional details parameter
	performAction: procedure
		.input(
			z.object({
				actionId: z.number(),
				details: z.string().optional()
			})
		)
		.mutation((opts) => {
			const { actionId, details } = opts.input

			return {
				actionId: actionId,
				details: details,
				message: `Action ${actionId} performed with details: ${details || 'None'}.`
			}
		})
})

export type AppRouter = typeof appRouter

As your project grows and you write more procedures, you can split this into multiple files and merge them together into the appRouter.

[!IMPORTANT] Notice how initTRPC is imported from @robojs/trpc.

Client

The client file initializes the tRPC clients and exports them for use in your app. For example, you can use the trpcClient to directly call queries when the user interacts with your app.

import { trpcClient } from '../trpc/client'
import { useState } from 'react'

export const MyComponent = () => {
	const [data, setData] = useState(null)

	const onClick = async () => {
		const data = await trpcClient.hello.query({ text: 'World' })
		setData(data)
	}

	return <button onClick={onClick}>{data?.message}</button>
}

Preferring to use React Query? You can use the trpc object to create hooks for your queries.

import { trpc } from '../trpc/client'

export const MyComponent = () => {
	const { data } = trpc.hello.useQuery({ text: 'World' })

	return <span>{data?.message}</span>
}

Got questions? 🤔

If you have any questions or need help with this plugin, feel free to join our Discord Server. We're a friendly bunch and always happy to help! Plus, our very own AI Robo, Sage, is there to assist you with any questions you may have.

🚀 Community: Join our Discord server