Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@trpc/server

Package Overview
Dependencies
Maintainers
3
Versions
1072
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trpc/server

TRPC Server

  • 2.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
720K
increased by10.17%
Maintainers
3
Weekly downloads
 
Created

What is @trpc/server?

@trpc/server is a TypeScript-first framework for building end-to-end typesafe APIs. It allows you to create APIs where the client and server share the same type definitions, ensuring type safety across the entire stack.

What are @trpc/server's main functionalities?

Creating a Router

This code demonstrates how to create a basic router with a single procedure using @trpc/server. The `greeting` procedure returns a simple 'Hello, world!' message.

const { initTRPC } = require('@trpc/server');
const t = initTRPC.create();
const appRouter = t.router({
  greeting: t.procedure.query(() => 'Hello, world!'),
});
module.exports = { appRouter };

Creating Procedures

This code shows how to create a procedure that takes input and performs an operation. The `add` procedure takes two numbers as input and returns their sum.

const { initTRPC } = require('@trpc/server');
const t = initTRPC.create();
const appRouter = t.router({
  add: t.procedure.input((z) => z.object({ a: z.number(), b: z.number() })).query(({ input }) => input.a + input.b),
});
module.exports = { appRouter };

Middleware

This code demonstrates how to use middleware in @trpc/server. The `isAuthed` middleware checks if the user is authenticated before allowing access to the `secretData` procedure.

const { initTRPC } = require('@trpc/server');
const t = initTRPC.create();
const isAuthed = t.middleware(({ ctx, next }) => {
  if (!ctx.user) {
    throw new Error('Not authenticated');
  }
  return next();
});
const appRouter = t.router({
  secretData: t.procedure.use(isAuthed).query(() => 'Secret data'),
});
module.exports = { appRouter };

Other packages similar to @trpc/server

FAQs

Package last updated on 21 Feb 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc