JSPM

  • Created
  • Published
  • Downloads 166603
  • Score
    100M100P100Q158393F
  • License Apache-2.0

Strongly-typed CLI commands, using tRPC

Package Exports

  • trpc-cli
  • trpc-cli/dist/index.js

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-cli) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

trpc-cli

Turn any trpc router into a fully-functional, documented CLI.

Installation

npm install trpc-cli @trpc/server zod

Usage

// router.js
import * as trpcServer from '@trpc/server'
import {trpcCli} from 'trpc-cli'
import {z} from 'zod'

const trpc = trpcServer.initTRPC.create()

const appRouter = trpc.router({
  sum: trpc.procedure
    .input(
      z.object({
        left: z.number(),
        right: z.number(),
      }),
    )
    .mutation(({input}) => input.left + input.right),
  divide: trpc.procedure
    .input(
      z.object({
        left: z.number(),
        right: z.number().refine(n => n !== 0),
      }),
    )
    .query(({input}) => input.left / input.right),
})

const cli = trpcCli({router: appRouter})

cli.run()

Then run node router.js --help and you will see formatted help text for the sum and divide commands.

Commands:
  sum           
  divide        

Flags:
      --full-errors        Throw unedited raw errors rather than summarising to make more human-readable.
  -h, --help               Show help

Running node router.js sum --help and node router.js divide --help will show help text for the corresponding procedures:

sum

Usage:
  sum [flags...]

Flags:
  -h, --help                  Show help
      --left <number>         
      --right <number>

Features

Procedures can define meta value with description, usage and help props. Zod's describe method allows adding descriptions to individual flags.

const appRouter = trpc.router({
  divide: trpc.procedure
    .input(
      z.object({
        left: z.number().describe('The numerator of the division operator'),
        right: z.number().describe('The denominator of the division operator'),
      }),
    )
    .mutation(({input}) => input.left / input.right),
})

Limitations

  • Only zod types are supported right now

Implementation