JSPM

@trpc/server

11.0.0-rc.824+b21849468
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1167876
  • Score
    100M100P100Q250071F
  • License MIT

The tRPC server library

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@trpc/server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    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@next
    
    # Yarn
    yarn add @trpc/server@next
    
    # pnpm
    pnpm add @trpc/server@next
    
    # Bun
    bun add @trpc/server@next

    We also recommend installing zod to validate procedure inputs.

    Basic Example

    import { 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 = Awaited<ReturnType<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);