Nebulous Plasma Muffin

    @trpc/server
    TypeScript icon, indicating that this package has built-in type declarations

    10.9.1 • Public • Published

    tRPC

    tRPC

    End-to-end typesafe APIs made easy

    Demo

    @trpc/server

    Create tRPC routers and connect them to a server.

    Documentation

    Full documentation for @trpc/server can be found here

    Installation

    # npm
    npm install @trpc/server
    
    # Yarn
    yarn add @trpc/server
    
    # pnpm
    pnpm add @trpc/server

    We also recommend installing zod to validate procedure inputs.

    Basic Example

    import { inferAsyncReturnType, initTRPC } from '@trpc/server';
    import {
      CreateHTTPContextOptions,
      createHTTPServer,
    } from '@trpc/server/adapters/standalone';
    import { z } from 'zod';
    
    // Initialize a context for the server
    function createContext(opts: CreateHTTPContextOptions) {
      return {};
    }
    
    // Get the context type
    type Context = inferAsyncReturnType<typeof createContext>;
    
    // Initialize tRPC
    const t = initTRPC.context<Context>().create();
    
    // Create main router
    const appRouter = t.router({
      // Greeting procedure
      greeting: t.procedure
        .input(
          z.object({
            name: z.string(),
          }),
        )
        .query(({ input }) => `Hello, ${input.name}!`),
    });
    
    // Export the app router type to be imported on the client side
    export type AppRouter = typeof appRouter;
    
    // Create HTTP server
    const { listen } = createHTTPServer({
      router: appRouter,
      createContext,
    });
    
    // Listen on port 2022
    listen(2022);

    Keywords

    none

    Install

    npm i @trpc/server

    Homepage

    trpc.io

    DownloadsWeekly Downloads

    137,447

    Version

    10.9.1

    License

    MIT

    Unpacked Size

    697 kB

    Total Files

    329

    Last publish

    Collaborators

    • sachinraja
    • trpc-bot
    • katt