Skip to content

Releases: mmkal/trpc-cli

v0.1.4

22 May 21:46
Compare
Choose a tag to compare
  • fix: @trpc/server v10 peer dep e480f9e

v0.1.3...v0.1.4

v0.1.3

22 May 20:46
Compare
Choose a tag to compare
  • target es2022 for less ugly js, target node >=18 03092ec

v0.1.2...v0.1.3

v0.1.2

22 May 20:36
Compare
Choose a tag to compare
  • docs: add a simpler getting-started example 7b4b06f

v0.1.1...v0.1.2

v0.1.1

22 May 20:19
Compare
Choose a tag to compare

v0.1.0...v0.1.1

v0.1.0

22 May 19:54
Compare
Choose a tag to compare

v0.0.3

22 May 18:37
Compare
Choose a tag to compare

v0.0.2...v0.0.3

v0.0.2

22 May 18:03
Compare
Choose a tag to compare

v0.0.1...v0.0.2

v0.0.1

22 May 03:21
Compare
Choose a tag to compare

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