Skip to content

feat: automatically serialize JSON to equivalent type - #3261

Merged
KATT merged 19 commits into
mainfrom
issues/1694-take-two
Nov 25, 2022
Merged

KATT merged 19 commits into
mainfrom
issues/1694-take-two

Conversation

@KATT

@KATT KATT commented Nov 25, 2022 •

Copy link
Copy Markdown
Member

Closes #1694

🎯 Changes

This PR adds support to automatically serialize the output as the JSON-representation of the same object.

  • It's currently using type-fest as a dependency, which should probably be changed Using what Remix does
  • This PR has the assumption that any @trpc/client or @trpc/react-query calls will happen over the HTTP-boundary and that the response will be JSON. This is a fair assumption right now but if/when we add for instance an "SSR-link" you'll get unexpected results
  • See the test for an example:
    • const appRouter = t.router({
      happy: t.procedure.query(() => ({
      map: new Map<string, string>([['foo', 'bar']]),
      set: new Set<string>(['foo', 'bar']),
      date: new Date(0),
      fn: () => {
      return 'hello';
      },
      arrayWithUndefined: [1, undefined, 2],
      })),
      });
    • const result = await ctx.proxy.happy.query();
      expectTypeOf(result.date).toBeString();
      result.set;
      // ^?
      result.map;
      // ^?
      expectTypeOf(result).toMatchTypeOf<{
      date: string;
      map: {};
      set: {};
      }>();

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.
  • Remove type-fest somehow

@vercel

vercel Bot commented Nov 25, 2022 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
next-prisma-starter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Nov 25, 2022 at 5:38PM (UTC)
www ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Nov 25, 2022 at 5:38PM (UTC)

@KATT KATT changed the title Issues/1694 take two feat: automatically serialize JSON Nov 25, 2022
@KATT KATT changed the title feat: automatically serialize JSON feat: automatically serialize JSON to equivalent type Nov 25, 2022

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

good for now, jump to definition works

Comment on lines +41 to +43
type SerializeObject<T extends object> = {
[k in keyof T]: T[k] extends NonJsonPrimitive ? never : Serialize<T[k]>;
};

@sachinraja sachinraja Nov 25, 2022 •

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.

right now any keys of type NonJsonPrimitive show up as never because when I tried applying the filter keys strategy, TS broke

@brhx

brhx commented Nov 26, 2022

Copy link
Copy Markdown

This is an amazing quality of life improvement! Thank you ❤️🥳

KATT added a commit that referenced this pull request Nov 26, 2022
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 26, 2022

This branch was successfully deployed

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

Labels

@trpc/client @trpc/react-query @trpc/server 🕸 www Stuff to do with the website (`/www` folder)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants