Skip to content

feat: trpc.snapshot() #3435

Closed
Closed

Description

@KATT

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 snapshots

Example

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

  1. Convert the typescript in your app to an AST (abstract syntax tree)
  2. Find the snapshot() calls
    1. If empty, convert the the inferred output into a validator (like zod)
    2. If non-empty
      1. If running with --update-snapshots:
        1. Clear value
        2. Go to 2.1
      2. Else ignore

Potentially a trpc.config.ts would be good to specify watch folder, etc

Notes

T-76

Activity

  1. transferred this issue fromtrpc/v10-playgroundon Dec 19, 2022
  2. moved this from 🤔 Maybe / Nice to have to 🚧 In Progress in Roadmap and prioritieson Dec 19, 2022
  3. changed the title [-]Idea: `trpc.snapshot()`[/-] [+]feat: `trpc.snapshot()`[/+] on Dec 19, 2022
  4. changed the title [-]feat: `trpc.snapshot()`[/-] [+][T-76] feat: `trpc.snapshot()`[/+] on May 25, 2023
  5. changed the title [-][T-76] feat: `trpc.snapshot()`[/-] [+]feat: `trpc.snapshot()`[/+] on May 25, 2023
  6. moved this from 🚧 In Progress to ✅ Done in Roadmap and prioritieson Jan 11, 2024
  7. github-actions commented on Jan 12, 2024

    @github-actions
    Contributor

    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.

  8. locked as resolved and limited conversation to collaborators on Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions