@trpc/client
Version:
221 lines • 9.1 kB
text/typescript
import { t as TRPCConnectionState } from "./subscriptions-DcqRZc4h.mjs";
import { Observable, Observer } from "@trpc/server/observable";
import { DefaultErrorShape, InferrableClientTypes, Maybe, TRPCErrorResponse, TRPCResultMessage, TRPCSuccessResponse, inferClientTypes } from "@trpc/server/unstable-core-do-not-import";
//#region src/internals/types.d.ts
/**
* A subset of the standard fetch function type needed by tRPC internally.
* @see fetch from lib.dom.d.ts
* @remarks
* If you need a property that you know exists but doesn't exist on this
* interface, go ahead and add it.
*/
type FetchEsque = (input: RequestInfo | URL | string, init?: RequestInit | RequestInitEsque) => Promise<ResponseEsque>;
/**
* A simpler version of the native fetch function's type for packages with
* their own fetch types, such as undici and node-fetch.
*/
type NativeFetchEsque = (url: URL | string, init?: NodeFetchRequestInitEsque) => Promise<ResponseEsque>;
interface NodeFetchRequestInitEsque {
body?: string;
}
/**
* A subset of the standard RequestInit properties needed by tRPC internally.
* @see RequestInit from lib.dom.d.ts
* @remarks
* If you need a property that you know exists but doesn't exist on this
* interface, go ahead and add it.
*/
interface RequestInitEsque {
/**
* Sets the request's body.
*/
body?: FormData | string | null | Uint8Array<ArrayBuffer> | Blob | File;
/**
* Sets the request's associated headers.
*/
headers?: [string, string][] | Record<string, string>;
/**
* The request's HTTP-style method.
*/
method?: string;
/**
* Sets the request's signal.
*/
signal?: AbortSignal | undefined;
}
/**
* A subset of the standard ReadableStream properties needed by tRPC internally.
* @see ReadableStream from lib.dom.d.ts
*/
type WebReadableStreamEsque = {
getReader: () => ReadableStreamDefaultReader<Uint8Array>;
};
type NodeJSReadableStreamEsque = {
on(eventName: string | symbol, listener: (...args: any[]) => void): NodeJSReadableStreamEsque;
};
/**
* A subset of the standard Response properties needed by tRPC internally.
* @see Response from lib.dom.d.ts
*/
interface ResponseEsque {
readonly ok: boolean;
readonly body?: NodeJSReadableStreamEsque | WebReadableStreamEsque | null;
/**
* @remarks
* The built-in Response::json() method returns Promise<any>, but
* that's not as type-safe as unknown. We use unknown because we're
* more type-safe. You do want more type safety, right? 😉
*/
json(): Promise<unknown>;
}
/**
* @internal
*/
type NonEmptyArray<TItem> = [TItem, ...TItem[]];
type ClientContext = Record<string, unknown>;
/**
* The options of a call made with the client, such as
* `client.post.byId.query(input, opts)`.
* @public
*/
interface TRPCProcedureOptions {
/**
* Context for the links, such as a flag a `splitLink` reads.
* @see https://trpc.io/docs/client/links#managing-context
*/
context?: ClientContext;
/**
* Aborts the call. With `httpBatchLink`, the request is only aborted once
* every call in its batch is, so an aborted call can still get a result.
* @see https://trpc.io/docs/client/vanilla/aborting-procedure-calls
*/
signal?: AbortSignal;
}
//#endregion
//#region src/TRPCClientError.d.ts
type inferErrorShape<TInferrable extends InferrableClientTypes> = inferClientTypes<TInferrable>['errorShape'];
/** What a client error has, whatever its router. */
interface TRPCClientErrorBase<TShape extends DefaultErrorShape> {
readonly message: string;
/** The error as the server's error formatter shaped it, if it came from the server. */
readonly shape: Maybe<TShape>;
/** The shape's `data`, such as the tRPC code and HTTP status. */
readonly data: Maybe<TShape['data']>;
}
/**
* The shape of a client error for a router or procedure, without the
* class. Use it to type errors you pass around, such as in React state.
* @see https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types
*/
type TRPCClientErrorLike<TInferrable extends InferrableClientTypes> = TRPCClientErrorBase<inferErrorShape<TInferrable>>;
/**
* Whether a value is a `TRPCClientError`, typed for a router or procedure.
* @see https://trpc.io/docs/client/vanilla/error-handling#catching-a-thrown-error
*/
declare function isTRPCClientError<TInferrable extends InferrableClientTypes>(cause: unknown): cause is TRPCClientError<TInferrable>;
/**
* The error a client call throws, whether the server sent an error or the
* request failed.
* @see https://trpc.io/docs/client/vanilla/error-handling
*/
declare class TRPCClientError<TRouterOrProcedure extends InferrableClientTypes> extends Error implements TRPCClientErrorBase<inferErrorShape<TRouterOrProcedure>> {
/** The error this one wraps, such as a network error. */
readonly cause: Error | undefined;
/** The error as the server's error formatter shaped it, if it came from the server. */
readonly shape: Maybe<inferErrorShape<TRouterOrProcedure>>;
/** The shape's `data`, such as the tRPC code and HTTP status. */
readonly data: Maybe<inferErrorShape<TRouterOrProcedure>['data']>;
/**
* More about the error, if any. HTTP errors may have the `response` and
* its `responseJSON`; a failed fetch or unparsable response may have
* neither.
*/
meta: Record<string, unknown> | undefined;
constructor(message: string, opts?: {
result?: Maybe<TRPCErrorResponse<inferErrorShape<TRouterOrProcedure>>>;
cause?: Error;
meta?: Record<string, unknown>;
});
/**
* Turns an error response, an `Error` or another object into a
* `TRPCClientError`. A `TRPCClientError` is returned as it is.
*/
static from<TRouterOrProcedure extends InferrableClientTypes>(_cause: Error | TRPCErrorResponse<any> | object, opts?: {
meta?: Record<string, unknown>;
cause?: Error;
}): TRPCClientError<TRouterOrProcedure>;
}
//#endregion
//#region src/links/types.d.ts
/**
* Context an operation carries through the links, set per call.
* @see https://trpc.io/docs/client/links#managing-context
* @internal
*/
interface OperationContext extends Record<string, unknown> {}
/**
* A procedure call as it goes through the links.
* @internal
*/
type Operation<TInput = unknown> = {
/** A number unique to the call within its client. */
id: number;
type: 'mutation' | 'query' | 'subscription';
input: TInput;
path: string;
context: OperationContext;
signal: Maybe<AbortSignal>;
};
interface HeadersInitEsque {
[Symbol.iterator](): IterableIterator<[string, string]>;
}
/**
* Request headers: a `Headers`-like iterable, or an object.
* @internal
*/
type HTTPHeaders = HeadersInitEsque | Record<string, string[] | string | undefined>;
/**
* The `fetch` the HTTP links call: only its `(url, options)` form.
*/
type TRPCFetch = (url: string, options?: RequestInit) => Promise<ResponseEsque>;
/** What a link gets when the client is created. Empty for now. */
interface TRPCClientRuntime {}
/**
* A result a link passes back: data, a subscription's `started` or
* `stopped` event, or a change of connection state.
* @internal
*/
interface OperationResultEnvelope<TOutput, TError> {
result: TRPCResultMessage<TOutput>['result'] | TRPCSuccessResponse<TOutput>['result'] | TRPCConnectionState<TError>;
context?: OperationContext;
}
/**
* The observable of results a link returns for an operation.
* @internal
*/
type OperationResultObservable<TInferrable extends InferrableClientTypes, TOutput> = Observable<OperationResultEnvelope<TOutput, TRPCClientError<TInferrable>>, TRPCClientError<TInferrable>>;
/**
* An observer of a link's results.
* @internal
*/
type OperationResultObserver<TInferrable extends InferrableClientTypes, TOutput> = Observer<OperationResultEnvelope<TOutput, TRPCClientError<TInferrable>>, TRPCClientError<TInferrable>>;
/**
* Handles one operation: pass it on with `next(op)`, or send it and
* return the results.
* @see https://trpc.io/docs/client/links#creating-a-custom-link
* @internal
*/
type OperationLink<TInferrable extends InferrableClientTypes, TInput = unknown, TOutput = unknown> = (opts: {
op: Operation<TInput>;
next: (op: Operation<TInput>) => OperationResultObservable<TInferrable, TOutput>;
}) => OperationResultObservable<TInferrable, TOutput>;
/**
* A link: a step every call goes through, such as logging or sending it
* over HTTP. It's called once when the client is created.
* @see https://trpc.io/docs/client/links#creating-a-custom-link
* @public
*/
type TRPCLink<TInferrable extends InferrableClientTypes> = (opts: TRPCClientRuntime) => OperationLink<TInferrable>;
//#endregion
export { NonEmptyArray as _, OperationResultEnvelope as a, TRPCClientRuntime as c, TRPCClientError as d, TRPCClientErrorBase as f, NativeFetchEsque as g, FetchEsque as h, OperationLink as i, TRPCFetch as l, isTRPCClientError as m, Operation as n, OperationResultObservable as o, TRPCClientErrorLike as p, OperationContext as r, OperationResultObserver as s, HTTPHeaders as t, TRPCLink as u, TRPCProcedureOptions as v };
//# sourceMappingURL=types-DO-GCB2q.d.mts.map