Allow customization of prop serialization/hydration by supplying custom ser/des functions

Open
#3,573 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
deno, typescript

Research direction

Start by tracing how Fresh serializes and hydrates props for island components; the issue does not name files or tests, so locate those entry points in the repository. Then assess where custom handlers could be registered and applied, using the Temporal polyfill example as context. Done means island props can round-trip supported custom types through serialization and hydration.

Written by the indexing model from the issue text.

Description

feat

Example:

type CustomPropTypeHandler<T, S extends JsonSerializableValue> {
    is(x: unknown): x is T
    serialize(x: T): S
    hydrate(serialized: S): T
}

// prop type I want to make serializable
class Foo {
    constructor(public readonly bar: string) {}
}

// handler
const fooPropTypeHandler: CustomPropTypeHandler<Foo, string> = {
    is: (x) => x instanceof Foo,
    serialize: (x) => x.bar,
    hydrate: (bar) => new Foo(bar),
}

// middleware
app.use((ctx) => {
    ctx.customPropTypeHandlers.push(fooPropTypeHandler)
})

// island component
export function FancyDiv(foo: Foo) {
    <div>{foo.bar}</div>
}

// server-side component/page
<FancyDiv foo={new Foo('hello world')} />

This probably wouldn't make much sense for mutable data, but it'd allow passing around richer data types. It could even work for stringified functions, with the caveat that there'd be footguns, which is less of an issue if it's a userland solution rather than officially supported.

My current, significantly less footgun-prone use case is that I want to add ser/des support for polyfilled Temporal classes.

Dominant language
TypeScript
Stars
13.8k
Forks
757
Avg merge
3m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from freshframework/fresh

All issues in freshframework/fresh

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.