Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output response parser #1699

Merged
merged 12 commits into from
Apr 1, 2022
Merged

Add output response parser #1699

merged 12 commits into from
Apr 1, 2022

Conversation

jlalmes
Copy link
Contributor

@jlalmes jlalmes commented Mar 25, 2022

🚧 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
Copy link

vercel bot commented Mar 25, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

next-prisma-starter – ./examples/next-prisma-starter

🔍 Inspect: https://vercel.com/trpc/next-prisma-starter/CGiMjxC95wNXtWaHpQUFCM9PV4go
✅ Preview: https://next-prisma-starter-git-feature-output-parser-oas-trpc.vercel.app

www – ./www

🔍 Inspect: https://vercel.com/trpc/www/8g6uQx3qyFSMrKkvbJjj95pbrDXN
✅ Preview: https://www-git-feature-output-parser-oas-trpc.vercel.app

Copy link
Member

@KATT KATT left a comment

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
Copy link
Member

KATT commented Mar 25, 2022

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
Copy link
Member

KATT commented Mar 25, 2022

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

@sachinraja
Copy link
Member

sachinraja commented Mar 25, 2022

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.

Copy link
Member

@KATT KATT left a comment

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

packages/server/test/inference.test.ts Outdated Show resolved Hide resolved
packages/server/test/inference.test.ts Show resolved Hide resolved
packages/server/src/internals/procedure.ts Outdated Show resolved Hide resolved
packages/server/src/internals/procedure.ts Outdated Show resolved Hide resolved
packages/server/src/internals/procedure.ts Outdated Show resolved Hide resolved
@KATT
Copy link
Member

KATT commented Mar 26, 2022

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

@jlalmes
Copy link
Contributor Author

jlalmes commented Mar 26, 2022

#1182

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

@KATT
Copy link
Member

KATT commented Mar 26, 2022

#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!

packages/server/src/internals/procedure.ts Outdated Show resolved Hide resolved
packages/server/src/internals/procedure.ts Outdated Show resolved Hide resolved
Copy link
Member

@KATT KATT left a comment

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
Copy link

github-actions bot commented Oct 6, 2022

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
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.

None yet

3 participants