blitz-js / superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
MIT License
3.88k stars 83 forks source link

feat: add `dedupe` flag #247

Closed KATT closed 12 months ago

KATT commented 1 year ago

add dedupe flag that will make sure that complex objects only appear once in the output

KATT commented 1 year ago

You decide everything and me / @juliusmarminge will just follow suit. :)

Feel free to just push to my PR or make another stab at it, I gotta go for today now!

KATT commented 1 year ago

FWIW, changing the circular to anything else but null could be seen as a breaking change - so maybe it's better to have both as null for now

Skn0tt commented 12 months ago

Published as part of https://github.com/blitz-js/superjson/releases/tag/v1.13.0

juliusmarminge commented 12 months ago

Published as part of https://github.com/blitz-js/superjson/releases/tag/v1.13.0

Awesome. Unfortunately, for some reason I get an error when running the compiled version yelling that SuperJSON isn't a constructor 🤔

CleanShot 2023-07-17 at 10 47 44@2x
Skn0tt commented 12 months ago

Can reproduce locally, let's see what's causing it 🤔

Skn0tt commented 12 months ago

It looks like it's related to how TypeScript emits the export default class. Seems like that bug has existed before this PR, but nobody tried doing new SuperJSON until now!

juliusmarminge commented 12 months ago

It looks like it's related to how TypeScript emits the export default class

Bundling javascript....

Seems like that bug has existed before this PR, but nobody tried doing new SuperJSON until now!

Seems reasonable. Nothing in this PR should cause difference in behavior in that regard

Skn0tt commented 12 months ago

As a workaround, could you try const SuperJSON = require(".").default? That fixed it for me locally.

juliusmarminge commented 12 months ago

As a workaround, could you try const SuperJSON = require(".").default? That fixed it for me locally.

How do you mean? The relative require('.') won't work outside of the local repo. I tried this without success though:

import type SuperJSON from 'superjson'

// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
export const superjson: SuperJSON = require('superjson').default({
    dedupe: true,
})
CleanShot 2023-07-17 at 11 12 04@2x
Skn0tt commented 12 months ago

Oops, meant to replace the . with superjson. But you guessed right, that's what I was referring to - doesn't seem to work, though. I'll see what I can do to fix the import ...

juliusmarminge commented 12 months ago

Oops, meant to replace the . with superjson. But you guessed right, that's what I was referring to - doesn't seem to work, though. I'll see what I can do to fix the import ...

Made a temporary workaround that works if you use a named import instead of the default one in #250 🤷🏼‍♂️