Skip to content

@unthrown/orpc


@unthrown/orpc / client

client

Client

ResultClient

ts
type ResultClient<T> = T extends Client<infer UContext, infer UInput, infer UOutput, infer UError> ? (...rest) => AsyncResult<UOutput, Extract<UError, AnyORPCError>> : { [K in keyof T]: T[K] extends AnyNestedClient ? ResultClient<T[K]> : never };

Defined in: client.ts:73

The type of a createResultClient client: every procedure of T returns an AsyncResult instead of a throwing promise.

Type Parameters

Type Parameter
T extends AnyNestedClient

createResultClient()

ts
function createResultClient<T>(client): ResultClient<T>;

Defined in: client.ts:112

Wrap an oRPC client so every procedure call returns an AsyncResultfromCall applied to the whole router.

Type Parameters

Type Parameter
T extends AnyNestedClient

Parameters

ParameterTypeDescription
clientTthe oRPC client (or any nested router segment) to wrap.

Returns

ResultClient<T>

Remarks

The mirror of oRPC's own createSafeClient, producing AsyncResults instead of SafeResult tuples: inferable errors land in the error channel (the raw ORPCError union, discriminated by code), everything else is a Defect. Call options (signal, context, lastEventId) pass through untouched.

Event-iterator (streaming) procedures are out of scope: a stream does not collapse to one Result. Keep calling those on the raw client.

Example

ts
import { createResultClient } from "@unthrown/orpc/client";

const rc = createResultClient(client);

const greeting = await rc.planet
  .find({ id })
  .map((planet) => `Hello, ${planet.name}!`)
  .match({
    ok: (msg) => msg,
    err: (e) => (e.code === "NOT_FOUND" ? "Hello, void!" : "Hello, trouble!"),
    defect: () => "Hello, bug tracker!",
  });

fromCall()

ts
function fromCall<TOutput, TError>(promise): AsyncResult<TOutput, Extract<TError, AnyORPCError>>;

Defined in: client.ts:56

Lift a single oRPC call into an AsyncResult.

Type Parameters

Type ParameterDefault typeDescription
TOutput-the procedure's output type.
TErrorErrorthe call's error union; only its ORPCError arm is modeled, the rest is subtracted into the defect channel.

Parameters

ParameterTypeDescription
promisePromiseWithError<TOutput, TError>the in-flight call to lift.

Returns

AsyncResult<TOutput, Extract<TError, AnyORPCError>>

Remarks

The error channel is the call's inferable errors — the ORPCErrors the procedure declares via .errors({...}) or returns as values, extracted as Extract<TError, AnyORPCError> and discriminated by code. Any other rejection (network failure, an undeclared throw collapsed to INTERNAL_SERVER_ERROR, a malformed response) is a Defect: unmodeled, flowing past the error combinators, panicking at get.

Accepts the promise of a client procedure call or of oRPC's server-side call(procedure, input) — anything typed PromiseWithError.

Example

ts
import { fromCall } from "@unthrown/orpc/client";

const planet = await fromCall(client.planet.find({ id }));
// planet: Result<Planet, ORPCError<"NOT_FOUND", undefined>>
if (planet.isErr()) planet.error.code; // "NOT_FOUND"

Released under the MIT License.