Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.4 kB · 109 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import * as Effect from "effect/Effect";import type { Env } from "./config.ts";import { registryFailure, type RegistryFailure,} from "./installation-errors.ts";import { registryName } from "./installation-metadata.ts";import type { InstallationRegistry } from "./installation-registry.ts";import type { RegistryResult } from "./registry-result.ts";
type RegistryMethods = Pick< InstallationRegistry, | "reserve" | "get" | "getDomain" | "domainReplay" | "startDomain" | "updateDomain" | "list" | "start" | "replay" | "operation" | "active" | "intent" | "update">;
// Derive application types from the server contract, before Wrangler's RPC// transformation adds pipelining and disposal to the transport return values.export type InstallationRegistryClient = { [K in keyof RegistryMethods]: RegistryMethods[K] extends ( ...args: infer Args ) => Promise<RegistryResult<infer A>> ? (...args: Args) => Effect.Effect<A, RegistryFailure> : never;};
export const installationRegistryStub = ( env: Env, subject: string,): DurableObjectStub<InstallationRegistry> => env.INSTALLATIONS.get(env.INSTALLATIONS.idFromName(registryName(subject)));
// Calling a client method only describes the RPC. The response becomes a typed// failure when executed; transport exceptions remain defects. No RPC is retried.function rpc<Args extends unknown[], A>( call: (...args: Args) => Promise<RegistryResult<A>>,) { return (...args: Args) => Effect.promise(() => call(...args)).pipe( Effect.flatMap((result) => result.ok ? Effect.succeed(result.value) : Effect.fail(registryFailure(result.error)), ), );}
export function registryClient( stub: DurableObjectStub<InstallationRegistry>,): InstallationRegistryClient { return { reserve: rpc((...args: Parameters<RegistryMethods["reserve"]>) => stub.reserve(...args), ), get: rpc((...args: Parameters<RegistryMethods["get"]>) => stub.get(...args), ), getDomain: rpc((...args: Parameters<RegistryMethods["getDomain"]>) => stub.getDomain(...args), ), domainReplay: rpc((...args: Parameters<RegistryMethods["domainReplay"]>) => stub.domainReplay(...args), ), startDomain: rpc((...args: Parameters<RegistryMethods["startDomain"]>) => stub.startDomain(...args), ), updateDomain: rpc((...args: Parameters<RegistryMethods["updateDomain"]>) => stub.updateDomain(...args), ), list: rpc((...args: Parameters<RegistryMethods["list"]>) => stub.list(...args), ), start: rpc((...args: Parameters<RegistryMethods["start"]>) => stub.start(...args), ), replay: rpc((...args: Parameters<RegistryMethods["replay"]>) => stub.replay(...args), ), operation: rpc((...args: Parameters<RegistryMethods["operation"]>) => stub.operation(...args), ), active: rpc((...args: Parameters<RegistryMethods["active"]>) => stub.active(...args), ), intent: rpc((...args: Parameters<RegistryMethods["intent"]>) => stub.intent(...args), ), update: rpc((...args: Parameters<RegistryMethods["update"]>) => stub.update(...args), ), };}
export const installationRegistry = ( env: Env, subject: string,): InstallationRegistryClient => registryClient(installationRegistryStub(env, subject));