Files
punktfunk/sdk/src/effect.ts
T
enricobuehler f012ebbcba feat(sdk): Effect v4 + @effect/openapi-generator; typed pf.api & example ladder
Drop Orval for the first-party @effect/openapi-generator (OpenAPI 3.1 ->
Effect Schema + a typed HttpClient client) and bump effect 3.19 ->
4.0.0-beta.98. Port the hand-written surfaces to the v4 API (Result over
Either, Context.Service, Codec, Literals/Union arrays, Stream/Schedule/
Effect renames). Transport (CA-pinning fetch) and the reconnecting SSE
source are kept intact.

Make the SDK approachable for non-Effect users:
- Add pf.api.* on the Promise facade: the generated client surfaced as
  typed, Promise-native methods (await pf.api.listPairedClients()), so REST
  calls are autocompleted and checked instead of stringly-typed
  pf.request(method, path, body) + `as` casts. Zero-drift veneer over
  make(httpClient), backed by the same pinning fetch. pf.request stays as
  the untyped escape hatch.
- Re-tier examples into a 1-4 complexity ladder, rewritten onto pf.api.*
  (the typed payloads caught a wrong `launch` shape in provider-sync);
  the Effect example is labelled advanced. Add examples/ to tsconfig so
  they are typechecked (stops rot).

typecheck + 19 tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-17 12:40:04 +02:00

74 lines
2.6 KiB
TypeScript

// `@punktfunk/host/effect` — the Effect-native surface (RFC §7): the `PunktfunkHost` service
// tag + live layer, the wire schemas, typed errors, and Stream accessors. The package root
// (`@punktfunk/host`) is the Promise facade; this entry is for plugins and Effect programs.
//
// import { Effect, Stream } from "effect";
// import { PunktfunkHost, PunktfunkHostLive, events } from "@punktfunk/host/effect";
//
// const program = Effect.gen(function* () {
// yield* events().pipe(
// Stream.filter((e) => e.kind === "stream.started"),
// Stream.runForEach((e) => Effect.log(`stream started: ${e.stream.mode}`)),
// );
// });
// Effect.runPromise(program.pipe(Effect.provide(PunktfunkHostLive())));
import { Effect, type Schema as S, Stream } from "effect";
import {
EventStreamError,
PunktfunkHost,
type RequestError,
type VersionSkew,
} from "./client.js";
import type { EventStreamOptions, SseFrame } from "./sse.js";
import type { HostEvent } from "./wire.js";
export {
ApiError,
AuthError,
EventStreamError,
layer,
makeService,
PunktfunkHost,
type PunktfunkHostService,
PunktfunkHostLive,
type RequestError,
SseAuthError,
TransportError,
VersionSkew,
} from "./client.js";
export { type ConnectOptions, configDir, resolveConfig } from "./config.js";
export type { EventStreamOptions, SseFrame } from "./sse.js";
export * from "./wire.js";
/**
* The generated REST surface: wire Schemas plus a typed `HttpClient`-based client (`make`),
* from `@effect/openapi-generator` over `api/openapi.json`. Regenerate with `bun run gen`.
*/
export * as api from "./gen/punktfunk.js";
/** The decoded lifecycle-event stream of the ambient [`PunktfunkHost`]. */
export const events = (
opts?: EventStreamOptions,
): Stream.Stream<HostEvent, EventStreamError, PunktfunkHost> =>
Stream.unwrap(Effect.map(PunktfunkHost, (s) => s.events(opts)));
/** Every SSE frame verbatim (the `dropped` marker + unknown kinds included). */
export const eventsRaw = (
opts?: EventStreamOptions,
): Stream.Stream<SseFrame, EventStreamError, PunktfunkHost> =>
Stream.unwrap(Effect.map(PunktfunkHost, (s) => s.eventsRaw(opts)));
/** One management-API request under `/api/v1` on the ambient service. */
export const request = (
method: string,
path: string,
body?: unknown,
): Effect.Effect<unknown, RequestError, PunktfunkHost> =>
Effect.flatMap(PunktfunkHost, (s) => s.request(method, path, body));
/** GET + schema-validate on the ambient service (schemas from [`api`]). */
export const get = <A, I>(
path: string,
schema: S.Codec<A, I>,
): Effect.Effect<A, RequestError | VersionSkew, PunktfunkHost> =>
Effect.flatMap(PunktfunkHost, (s) => s.get(path, schema));