Repository navigation
Description
I had a new idea yesterday that would be amazing if implemented.
Problem
When using tRPC, it's easy to accidentally leak sensitive information since you don't have output validation like in GraphQL.
It's also hard to track if you're doing accidental breaking changes to your API for clients that are running right now or can't be upgraded easily (e.g. React Native)
Implementation idea
Outputs could have a trpc.snapshot() similar to Jest snapshots. We would need to have a CLI that runs in your development, analyzes the typescript & updates the snapshots for you.
This would remove the whole burden of writing output schema (could reset with trpc -u ) whilst still guaranteeing an output that can be reviewed in PRs easily.
tRPC CLI commands
-w, --watch Start watcher
-u, --update-snapshots Update all snapshotsExample
Input
const procedure = trpc.procedure
.output(
trpc.snapshot()
)
.resolve(() => ({ foo: 'bar' })Output
It could generate a zod parser or something.
const procedure = trpc.procedure
.output(
trpc.snapshot(
/* <✨ Auto-generated ✨ > */
z.object({ foo: z.string() })
/* </✨ > */
)
)
.resolve(() => ({ foo: 'bar' })Rough outline of how it'd work
- Convert the typescript in your app to an AST (abstract syntax tree)
- Find the
snapshot()calls- If empty, convert the the inferred output into a validator (like zod)
- If non-empty
- If running with
--update-snapshots:- Clear value
- Go to 2.1
- Else ignore
- If running with
Potentially a trpc.config.ts would be good to specify watch folder, etc
Notes
- Ideally, it would be a performant parser, see https://github.com/moltar/typescript-runtime-type-benchmarks
- Did some quick googles for similar libraries:
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields✅ Done
Activity
[-]Idea: `trpc.snapshot()`[/-][+]feat: `trpc.snapshot()`[/+][-]feat: `trpc.snapshot()`[/-][+][T-76] feat: `trpc.snapshot()`[/+][-][T-76] feat: `trpc.snapshot()`[/-][+]feat: `trpc.snapshot()`[/+]github-actions commentedon Jan 12, 2024
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.