Port to node.js

This commit is contained in:
2026-07-30 00:45:46 +02:00
parent 988025bfac
commit 1249781204
22 changed files with 1461 additions and 1768 deletions

View File

@@ -1,9 +1,9 @@
import { Data, Effect, Option, pipe, Record, Schema } from "effect";
import { Console, Data, Effect, Option, ParseResult, pipe, Record, Schema } from "effect";
import { constant } from "effect/Function";
import * as Api from "./Api";
import * as Body from "./Body";
import * as Cbor from "./Cbor";
import { fetch, FetchError } from "./Fetch";
import * as Api from "./Api.ts";
import * as Body from "./Body.ts";
import * as Cbor from "./Cbor.ts";
import { fetch, FetchError } from "./Fetch.ts";
export class ServerDie extends Data.TaggedError("ServerDie")<{ cause: unknown }> { }
export class UnexpectedStatus extends Data.TaggedError("UnexpectedStatusCode")<{ status: number }> { }
@@ -61,7 +61,7 @@ export const client = <const Record extends { readonly [_: string]: Api.ApiAny }
})) as { readonly [K in keyof Record]: (request: Record[K]["request"]["Type"]) => Effect.Effect<Record[K]["response"]["Type"], Record[K]["error"]["Type"] | FetchError, never> };
};
export const readBody = (body: Body) => pipe(
export const readBody = (body: { bytes(): Promise<Uint8Array<ArrayBuffer>> }) => pipe(
body,
Body.bytes,
Effect.map((bytes) => bytes.byteLength > 0 ? Option.some(bytes) : Option.none()),
@@ -74,7 +74,15 @@ export const readBody = (body: Body) => pipe(
export const decodeBody = <A>(schema: Schema.Schema<A, any>) => {
const decoder = Schema.decodeUnknown(schema);
return (body: Body) => Effect.flatMap(readBody(body), decoder);
return (body: { bytes(): Promise<Uint8Array<ArrayBuffer>> }) => pipe(
readBody(body),
Effect.flatMap(decoder),
Effect.tapErrorTag("ParseError", (error) => pipe(
error,
ParseResult.TreeFormatter.formatError,
Effect.flatMap(Console.error),
)),
);
};
export interface BodyData {