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

@trpc/next

Package Overview
Dependencies
Maintainers
0
Versions
1019
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trpc/next

The tRPC Next.js library

  • 11.0.0-rc.528
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
258K
increased by5.97%
Maintainers
0
Weekly downloads
 
Created

What is @trpc/next?

@trpc/next is a package that allows you to create end-to-end typesafe APIs using tRPC in a Next.js application. It simplifies the process of building and consuming APIs by leveraging TypeScript for type safety and Next.js for server-side rendering and API routes.

What are @trpc/next's main functionalities?

Creating a tRPC Router

This feature allows you to create a tRPC router with various procedures. In this example, a simple 'hello' query is defined that returns 'Hello world'.

import { initTRPC } from '@trpc/server';

const t = initTRPC.create();

const appRouter = t.router({
  hello: t.procedure.query(() => 'Hello world'),
});

export type AppRouter = typeof appRouter;

Integrating tRPC with Next.js API Routes

This feature demonstrates how to integrate a tRPC router with Next.js API routes. The `createNextApiHandler` function is used to create an API handler that can be used in a Next.js API route.

import { createNextApiHandler } from '@trpc/server/adapters/next';
import { appRouter } from 'path/to/your/router';

export default createNextApiHandler({
  router: appRouter,
  createContext: () => null,
});

Using tRPC in Next.js Pages

This feature shows how to use tRPC in a Next.js page. The `trpc.hello.useQuery` hook is used to fetch data from the 'hello' query defined in the tRPC router.

import { trpc } from 'path/to/trpc';

const HomePage = () => {
  const { data, error, isLoading } = trpc.hello.useQuery();

  if (isLoading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return <div>{data}</div>;
};

export default HomePage;

Other packages similar to @trpc/next

FAQs

Package last updated on 22 Sep 2024

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