Skip to content

tRPC v9 feedback before marking it as 9.0.0 #810

Closed

Description

@KATT

I've just merged the #732 & #718 to main and released a new V9 draft.

Are there any other structural, potentially breaking changes that you would want to get into the next major?

  • It should be production-ready so feel free to update your deps to 9.0.0-alpha.4, play around with it, and give me feedback.
  • I have tried slowing down on the major version bumps, so I would ideally not do more breaking changes for a while.
  • Planning to release 9.0.0 early next week.
  • There might come a 10.x as well later in a month or so in order to add support for SvelteKit - might require some major changes as their API handlers are a bit different.

If you're happy you can just give a 👍 on this issue.

Summary of changes so far

Activity

  1. added
    🙋‍♂️ help wanted
    🙏 review please
    on Aug 20, 2021
  2. added this to the v9 milestone on Aug 20, 2021
  3. simonedelmann commented on Aug 20, 2021

    @simonedelmann
    Contributor

    Great work, @KATT ! I am looking forward to update my app, when I find some time.

    You asked about other things which could be changed for the next major and I think we could use the opportunity to improve the transformer syntax. (my favorite topic 😉 ) I am currently on vacation so I here is just a small idea. Feel free to implement, improve or forget... whatever you like.

    For transformers currently we have the simple syntax, which is fine, and the combined syntax, which I still feel as a compromise to enable multiple transformers. (My fault, I made that PR back then.... 🙈)

    const transformer = {
      serialize: (value) => value,
      deserialize: (value) => value,
    }
    const combinedTransformer = {
      input: transformer,
      output: transformer,
    }

    I was thinking about making transformers more generic and stackable. What if we have a stack of transformers, which is applied depending on some flags.

    Currently: User -> Input Transformer -> Server -> Output Transformer -> User
    
    Suggestion: User -> Transformer A -> Transformer C -> Server -> Transformer C -> Transformer B -> User
    

    A transformer stack could look like that:

    const transformers = [
      {
        transform: (val) => someFancyOptimization(val),
        // if `resolve` is not defined, don't do anything for "deserializing"
        apply: OUTPUT,
      },
      {
        transform: (val) => escapeHtmlCharsForSecurityReasons(val),
        apply: INPUT,
      },
      {
        transform: (val) => superjson.serialize(val),
        resolve: (val) => superjson.deserialize(val),
        apply: ALWAYS,
      }
    ]
    

    I have to admit, I don't see a good use case yet which cannot be solved with the current syntax, but I am sure there is one... ;) Looking forward to hear what you think.

  4. mmkal commented on Aug 20, 2021

    @mmkal
    Contributor

    I am also on vacation for the next couple of weeks, so can't contribute code but can leave a couple of thoughts I've had here. I don't think any of them would need to be breaking though, necessarily.

    Middleware - it'd be really cool to be able to "refine" the context. Example for a router with type Context = { userId: string | null }:

    .middleware(async ({ ctx, next }) => {
      const {userId} = ctx
      if (!userId) throw new ...
    
      return next({ ctx: { ...ctx, userId })
    })
    .query(...) // userId is non-nullable after this

    Mentioned in #271 but an output: z.object({ ... }) prop just like input, would mean we could generate full openapi specs purely from trpc definitions.


    Just putting them out there, no reason I'm aware of they'd be breaking, but if you like the ideas and think they would require a breaking change then might be worth looking at.

    Only other things I can think of is if #506 could be a technical breaking change for people constricting requests manually (i.e. without @trpc/client). Or if #755 could lead to a breaking change somehow.

    Either way, I'm more than happy with v9 as it is and very much looking forward to it being stable!

  5. KATT commented on Aug 21, 2021

    @KATT
    MemberAuthor

    I was thinking about making transformers more generic and stackable. What if we have a stack of transformers, which is applied depending on some flags. — @simonedelmann

    I really don't see the use-case of this and it's still possible to do your own implementation like this using the existing solution.

    However, re transformers, one thing I have thought about is that it could be nice in some cases to optionally enable/disable them on a request-basis. Has anyone else been looking for that? Especially useful when once we'll look closer into #755.

    Middleware - it'd be really cool to be able to "refine" the context. Example for a router with type Context = { userId: string | null }: — @mmkal

    Yes, I agree. I know I play around with this before but reverted it because I got stuck in a generics rabbit hole of generics. I'd sign off on that feature and would be really nice for stuff like authz indeed. Happy to jump on a call or do a deep dive on how the router actually does it TypeScript-magic if someone would like to

    Mentioned in #271 but an output: z.object({ ... }) prop just like input, would mean we could generate full openapi specs purely from trpc definitions. — @mmkal

    Yep, but should be possible to do later with backwards compat.

    Also, there are other considerations with Open API:

    • Resources are usually /{resource}/{id}?param1=x&param2=y-style
    • JSON-RPC based response shape might not be the ideal response shape
    • An output schema usually have a $ref-schema which would be possible to do automatically / first feature people would request is to make different paths request the same input type
    • Actually using zod or any other validation on output would slow down API outputs.

    It might be better suited as a specific adapter where you define and map the OpenAPI-schema for a router, possibly in combination with something like output

    Only other things I can think of is if #506 could be a technical breaking change for people constricting requests manually (i.e. without @trpc/client). Or if #755 could lead to a breaking change somehow. — @mmkal

  6. KATT commented on Aug 25, 2021

    @KATT
    MemberAuthor

    Release notes

    • next(): method in server-side middlewares - see docs
    • responseMeta(): Better support for custom HTTP headers & status code - see caching docs
    • Server-side errors in @trpc/next are now serialized

    Upgrade guide for the 99% usecase

    1. npx npm-check-updates --filter /@trpc/ -u && yarn
    2. Simply update any .middleware() and add return next() at the end. See middleware docs

    Breaking changes

    • Middlewares will now have to call a next() function that you have to call.
    • Reduce TRPCErrors - METHOD_NOT_FOUND removed & PATH_NOT_FOUND renamed to NOT_FOUND (Reduce TRPCError-codes #733)
    • Align input and output serialization (Batching transformers inconsistency input/output #746)
    • Remove deprecated httpErrors-helper
    • Remove support for ?input to be an array - now assuming a Record<number, unknown>-style dict (changed with backward compat in fix: batching with zod .optional() input #669)
    • Remove support for POST calls being { input: x } (originally from flatten post requests' input #671)
    • Refactored some internals and removed some unnecessary exports on adapters - minor breaking changes if someone relied on something they shouldn't have :)
    • Stricter ErrorFormatter - data has to be a Record<string, unknown>

    Minor changes

    • Expose httpStatus as part of error object as the default error shape
    • httpStatus will be used to determine the actual HTTP Status code set
    • Expose TRPCClientError.data as a shortcut to error.shape.data as that's the only one I find myself using
    • TRPCError we infer in @trpc/react is now TRPCErrorLike and is serialized for SSR in @trpc/next
    • add new method responseMeta() that is triggered before a request ends - both as part of @trpc/next & @trpc/server
      • on @trpc/server receives type, paths, errors, and the unserialized data[] that is going out
      • on @trpc/next it client errors

    Thanks to @mmkal, @peterp, @anthonyshort, & @simonedelmann for your feedback!

  7. github-actions commented on Oct 4, 2022

    @github-actions
    Contributor

    This issue has been locked because it had no new activity for 14 days. 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 Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

💬 discussion🙋‍♂️ help wanted🙏 review please

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions