feat: allow serialization/deserialization of custom data types (alternative API)#13149
feat: allow serialization/deserialization of custom data types (alternative API)#13149Rich-Harris merged 30 commits intomainfrom
Conversation
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
|
…:sveltejs/kit into serialize-deserialize-non-pojo-universal
This comment was marked as outdated.
This comment was marked as outdated.
|
preview: https://svelte-dev-git-preview-kit-13149-svelte.vercel.app/ this is an automated message |
|
I would prefer a version where you can tree-shake the server code: |
|
We're talking about a few bytes, unless you have a realistic example to the contrary? I don't think the trade-off is a good one |
| * ``` | ||
| * @since 2.11.0 | ||
| */ | ||
| export type Transport = Record<string, Transporter>; |
There was a problem hiding this comment.
How much use will people really make of this specific type, since it's not helping much with type safety? Should we just omit it?
There was a problem hiding this comment.
I still think we should at least offer the defineTransport function for type safety. We can call it something else if we don't like it but it would be nice
There was a problem hiding this comment.
It's helping plenty — it might not be able to provide inference for the decode parameter, but it enforces the correct shape of transport (i.e. you can't omit either encode or decode for a given transporter, and you can't misspell them). And exposing Transporter means you can add the missing type safety yourself:
export const transport: Transport = {
Foo: {
encode: (value) => ...,
decode: (data) => ...
} satisfies Transporter<Foo, [blah]>
};We could always add defineTransport later if we feel like we need to, but I don't think we should break with existing conventions just yet.
There was a problem hiding this comment.
Yes and no - we can add satisfies Transport for the top level, but we can't do much for the individual entries
|
Amazing stuff! |
Alternative to #13125, Closes #9401. Instead of separating the logic between
hooks.server.jsandhooks.client.js, this puts both things inhooks.js. Initially I thought the separate made more sense, but I'm not so sure. In essentially all cases, you'll need a reference to the custom type — for aninstanceofcheck during serialization, and for instantiation during deserialization — so there doesn't really seem to be a benefit to keeping them in separate files.Putting them in one place is easier to understand, and means you can't end up with mismatches. It does mean that the serialization logic ends up in the client, but we're talking about a few bytes for a
value instanceof Foo && [value.blah]function.I'm not too sure about the naming of things. Right now it looks like this:
TODO:
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits