Skip to content

feat: if no transformer is defined - serialize types as JSON would #1694

Closed

Description

@KATT

Goal

Automatically make a safe data transfer object to corresponds to what JSON.parse(JSON.stringify(obj)) would do.

These 2 are likely two separate PRs/implementations:

  • For outputs it's pretty straight-forward
  • For inputs we somehow need to disable complex types that are not serializable? Or do some other wild stuff.

Let's start with outputs.

Example ## Example

Convert this...

interface Post {
  author: string
  title: string
  datePublished: Date
}

... into this:

// to:
type Post = {
  author: string
  title: string
  datePublished: string // <-- automatic transform according to `JSON.parse()`
}

Using only TypeScript generics!

Bounty 🤑

$200

Overview of how to do it

  • Should be done on the @trpc/client / @trpc/react-level.
  • router.createCaller() should not be affected
  • We have a DefaultDataTransformer that can be used to check if a router or procedure is using a transformer (see play with adding a JSON DTO #3254 for some initial hacks)
  • Might be some tests that have the types assertions (i.e. using undefined instead of null - update those). If the tests are in interop/*, do whatever hack you need to do to make them work

References

Both MIT, so we can vendor one of these:

Activity

  1. added
    🙏 review please
    on Mar 25, 2022
  2. ecwyne commented on Mar 27, 2022

    @ecwyne

    This is a very good idea. This has gotten me in the past

  3. self-assigned this
    on Apr 12, 2022
  4. moved this from 🤔 Maybe / Nice to have to ❗️ Todo in Roadmap and prioritieson Apr 20, 2022
  5. moved this from ❗️ Todo to 🤔 Maybe / Nice to have in Roadmap and prioritieson Jun 19, 2022
  6. Pruxis commented on Nov 2, 2022

    @Pruxis
    Contributor

    @KATT still looking for contributors for this? I think this could be a huge step forward for type-safety.

  7. KATT commented on Nov 24, 2022

    @KATT
    MemberAuthor

    Feel free to have a stab at it! :)

  8. changed the title [-]Idea: if no transformer is defined - use a DTO[/-] [+]feat: if no transformer is defined - use a DTO[/+] on Nov 24, 2022
  9. removed
    🙏 review please
    on Nov 24, 2022
  10. changed the title [-]feat: if no transformer is defined - use a DTO[/-] [+]feat: if no transformer is defined - serialize types as JSON would[/+] on Nov 24, 2022
  11. Repository owner moved this from 🤔 Maybe / Nice to have to ✅ Done in Roadmap and prioritieson Nov 25, 2022
  12. locked as resolved and limited conversation to collaborators on Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions