-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Ability to serialize arbitrary classes to send to client #8743
Comments
This came up before in various variations, and we're hesitant to add something like this as it complicates the (de)serialization mechanism quite a bit. Also, what about /// +page.svelte
export let data; // is the jsonified thing
$: user = unjsonify<User>(data); |
Good points. As a user I would expect the feature to occur for actions, and perhaps error data as well, although my gut feeling is that case would be extremely niche (and tbh I've never even tested if |
+1 on this, would be nice to have some transformation hook where we can add custom logic for transforming variables eg: async transform(value: unknown) {
if (value instanceof Foo) { ... }
return value
} I have some classes being added by SurrealDB for |
It's also stupid because this messes with typing, and svelte thinks that a value is still of a certain type when transformed. |
Closed via #13149 |
Describe the problem
My problem is that my database client returns data that includes classes, and I can't return this from my
+page.server.ts
file and use it in my+page.{ts,svelte}
. Examples of these classes includePrisma.Decimal
, model instances from Type/MikroORM, objects with extra methods returned by an extended Prisma client (see Prisma docs) or anything else that might be a class inside a deeply nested object.Describe the proposed solution
Having read through #6008 (and the pull request), my understanding is that
devalue
creates JS code that recreates the JS object is it passed, and that getting the generated code to reference constructors for these classes would be difficult (that said, I have an idea for this that I'd like to propose, not certain if it'd work, see below).Instead, what I really am looking for is the ability to hand some arbitrary data to Svelte and have it transform it to data that is compatible with
devalue
. i.e. I register a function (server-side) that identifies my class, then provide a function that takes an instance of this class and returns an object that is already compatible withdevalue
. This could then be incorporated into the type generation system (similar to param matchers) so thatPageLoad
functions have corrected types.I don't think my explanation was very clear, so here's an example:
Idea for deserializing classes
Rich's initial explanation of using devalue in #6008 says the deserialization process is (roughly):Could devalue be made to set
window.__data
to a function that can be passed functions to deserialize the data? This would avoid the issue of ensuring constructors are available todevalue
's generated code. The deserialization functions could also be lazy loaded if needed.This isn't really useful in the above example (a database model likely has server-side only functions attached to it, in which case I only want the POJO), but for custom scalars such as
Prisma.Decimal
, this would be quite helpful.Alternatives considered
My current solution is to write a function that 'jsonifies' my complex objects in a type-safe-ish way (
Range
is fromedgedb
):I can then wrap database queries in this (or add it with
.then(jsonify)
). This doesn't allow for deserializing classes.I've also been looking at potentially using
superjson
, but I lose the ability to modify thePageData
type without touching anything else - I have to modify the types that I am reporting tosuperjson
.Importance
nice to have
Additional Information
No response
The text was updated successfully, but these errors were encountered: