mmkal / trpc-cli

Turn a tRPC router into a type-safe, fully-functional, documented CLI
Other
49 stars 0 forks source link

Does not work in ESM #7

Closed kran6a closed 4 weeks ago

kran6a commented 1 month ago

Reproduction

import {initTRPC} from '@trpc/server';
import {z} from 'zod';
import {trpcCli} from 'trpc-cli';

const t = initTRPC.create();

export const router = t.router({
    add: t.procedure
        .input(z.object({left: z.number(), right: z.number()}))
        .query(({input}) => input.left + input.right),
});

const cli = trpcCli({router});
await cli.run();

If you add console.log({parsedArgv}) on trpc-cli index.ts you can see why it does not work:

[
  [
    "add",
    "Invalid input type ZodObject, only zod inputs are supported"
  ]
]

Apparently import {z} from "zod" and require("zod") are not importing the same classes thus instanceof does not work as intended.

mmkal commented 1 month ago

Interesting, sounds like zod has the dual-package hazard. Probably can be worked around by making a helper function with a guard return type, which checks the constructor name or something.

mmkal commented 1 month ago

@kran6a I just published a prerelease version which should help with this. You can install with npm install trpc-cli@0.3.1-0.

I was able to repro the issue and that the new version fixes. https://github.com/mmkal/trpc-cli/pull/8. Could you try it out?

kran6a commented 4 weeks ago

It works! Thanks for the quick fix