Skip to content

Add output response parser - #1699

Merged
jlalmes merged 12 commits into
mainfrom
feature/output-parser-oas
Apr 1, 2022
Merged

jlalmes merged 12 commits into
mainfrom
feature/output-parser-oas

Conversation

@jlalmes

@jlalmes jlalmes commented Mar 25, 2022 •

Copy link
Copy Markdown
Contributor

🚧 Work in progress

Added output validation to router queries & mutations

I have a goal to try and auto-generate OpenAPI docs for a TRPC router (cc. #755). The feature in this PR will enable us to generate JSON schemas for request and response payloads. This output parser is opt-in only and should not introduce any breaking changes.

API

const appRouter = new trpc.router<Context>()
  .query({
    input: z.string(),
    output: z.object({
      input: z.string(),
    }),
    resolve: ({ input }) => {
      return { input };
    }
  });
  • Added a new optional output property.
  • When output is provided, the resolve function will expect a return type inferred from the output parser.
  • The response data will be validated against the output parser, unless NODE_ENV is production.
  • If the response data fails validation a TRPCError INTERNAL_SERVER_ERROR error will be thrown.

Todo

  • Get some feedback (cc. @KATT)
  • Write some comprehensive output parser tests.
  • Update documentation.

@vercel

vercel Bot commented Mar 25, 2022 •

Copy link
Copy Markdown

@KATT KATT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not able to look it through until tomorrow at the earliest but it is great! I would love for optional output validation

@KATT

KATT commented Mar 25, 2022 •

Copy link
Copy Markdown
Member

I think if there is an output validation, it should always run, we can add the optionality in prod as a feature later!

I would use this myself to guarantee that I don't leak anything sensitive.

@KATT

KATT commented Mar 25, 2022

Copy link
Copy Markdown
Member

Also setup github sponsors so I can send you 💷💷!

@sachinraja

sachinraja commented Mar 25, 2022 •

Copy link
Copy Markdown
Member

I have a goal to try and auto-generate OpenAPI docs for a TRPC router

Let me know if there's any way I can help! I have some experience in this area with zod-to-ts and tRPC Playground.

@KATT KATT left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks really sound to me. GOOD JOB!! 🙏 🙏

Some suggestions:

  • Make the output parser stuff into its own test file
  • Don't allow bad outputs regardless of the environment
  • Some smaller comments inline

Comment thread packages/server/test/inference.test.ts Resolved
Comment thread packages/server/test/inference.test.ts Resolved
@KATT

KATT commented Mar 26, 2022

Copy link
Copy Markdown
Member

You might've accidentally removed the #1182 feature BTW ☺

@jlalmes

jlalmes commented Mar 26, 2022

Copy link
Copy Markdown
Contributor Author

#1182

Rolled it back - will make sure that any transforms in the output zod schemas are also handled!

@KATT

KATT commented Mar 26, 2022

Copy link
Copy Markdown
Member

#1182

Rolled it back - will make sure that any transforms in the output zod schemas are also handled!

Seems like an edge case to have transformers in the output but why not!

@KATT KATT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will merge this tomorrow and do a release.

I removed the TRPC_SKIP_OUTPUT_VALIDATION because I don't want magic runtime variables - this should either be explicitly implemented per procedure or on the app router similarily to transformers/error formatters.

@jlalmes
jlalmes merged commit 40f7a7b into main Apr 1, 2022
@jlalmes
jlalmes deleted the feature/output-parser-oas branch April 1, 2022 10:55
@KATT KATT mentioned this pull request Apr 4, 2022
@github-actions

github-actions Bot commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

This pull request has been locked because it had no new activity for 30 days. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 6, 2022

This branch was successfully deployed

3 active deployments
Preview – next-prisma-starter — 2f237a3b Deployed Apr 1, 2022 by vercel[bot]
Preview – www — 2f237a3b Deployed Apr 1, 2022 by vercel[bot]
Preview – todomvc — 2f237a3b Deployed Apr 1, 2022 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants