@effect/schema
solves all the issues related to serialization (or encoding).
Encoding a schema allows to convert it to its original plain object representation. This is exactly what we need to send the data to the client.
We therefore use Schema.encode
to encode the schema inside main
:
const main = Effect.gen(function* () {
const api = yield* Api;
const posts = yield* api.getPosts;
return yield* Schema.encode(Post.Array)(posts);
}).pipe(Effect.scoped);
After encoding, main
now returns the full posts type as an object instead of the Post
class:
Effect<
readonly {
readonly userId: number;
readonly id: number;
readonly title: string;
readonly body: string;
}[],
ParseError | HttpClientError,
Api
>
This solves the issue with serialization, and the app is now working correctly!