import type { XRPCProcedures, XRPCQueries } from '@atcute/lexicons/ambient'; import type { AtprotoAudience } from '@atcute/lexicons/syntax'; import * as v from '@atcute/lexicons/validations'; import type { InferInput, InferOutput, ObjectSchema, XRPCBlobBodyParam, XRPCLexBodyParam, XRPCProcedureMetadata, XRPCQueryMetadata } from '@atcute/lexicons/validations'; import { type FetchHandler, type FetchHandlerObject } from './fetch-handler.ts'; type RequiredKeysOf = TType extends any ? Exclude<{ [Key in keyof TType]: TType extends Record ? Key : never; }[keyof TType], undefined> : never; type HasRequiredKeys = RequiredKeysOf extends never ? false : true; type ResponseFormat = 'json' | 'blob' | 'bytes' | 'stream'; type FormattedResponse = { json: TDef extends XRPCQueryMetadata ? InferOutput : TDef extends XRPCProcedureMetadata ? InferOutput : unknown; blob: Blob; bytes: Uint8Array; stream: ReadableStream; }; type BaseRequestOptions = { signal?: AbortSignal; headers?: HeadersInit; }; export type QueryRequestOptions = BaseRequestOptions & (TDef extends XRPCQueryMetadata ? (Params extends ObjectSchema ? { params: InferInput; } : { params?: Record; }) & (Output extends XRPCLexBodyParam ? { as?: ResponseFormat | null; } : { as: ResponseFormat | null; }) : { as: ResponseFormat | null; params?: Record; }); export type ProcedureRequestOptions = BaseRequestOptions & (TDef extends XRPCProcedureMetadata ? (Params extends ObjectSchema ? { params: InferInput; } : { params?: Record; }) & (Input extends XRPCLexBodyParam ? { input: InferInput; } : Input extends XRPCBlobBodyParam ? { input: Blob | ArrayBuffer | ArrayBufferView | ReadableStream; } : { input?: Record | Blob | ArrayBuffer | ArrayBufferView | ReadableStream; }) & (Output extends XRPCLexBodyParam ? { as?: ResponseFormat | null; } : { as: ResponseFormat | null; }) : { as: ResponseFormat | null; input?: Record | Blob | ArrayBuffer | ArrayBufferView | ReadableStream; params?: Record; }); export type CallRequestOptions = BaseRequestOptions & (TMeta extends XRPCQueryMetadata ? Output extends XRPCBlobBodyParam ? { as: ResponseFormat | null; } : { as?: ResponseFormat | null; } : TMeta extends XRPCProcedureMetadata ? Output extends XRPCBlobBodyParam ? { as: ResponseFormat | null; } : { as?: ResponseFormat | null; } : { as?: ResponseFormat | null; }) & (TMeta extends XRPCQueryMetadata ? Params extends ObjectSchema ? { params: InferInput; } : { params?: Record; } : TMeta extends XRPCProcedureMetadata ? // procedure (Params extends ObjectSchema ? { params: InferInput; } : { params?: Record; }) & (Input extends XRPCLexBodyParam ? { input: InferInput; } : Input extends XRPCBlobBodyParam ? { input: Blob | ArrayBuffer | ArrayBufferView | ReadableStream; } : { input?: Record | Blob | ArrayBuffer | ArrayBufferView | ReadableStream; }) : never); /** standard XRPC error payload structure */ export type XRPCErrorPayload = { /** error name */ error: string; /** error description */ message?: string; }; type BaseClientResponse = { /** response status */ status: number; /** response headers */ headers: Headers; }; /** represents a successful response returned by the client */ export type SuccessClientResponse = BaseClientResponse & { ok: true; /** response data */ data: TInit extends { as: infer TFormat; } ? TFormat extends ResponseFormat ? FormattedResponse[TFormat] : TFormat extends null ? null : never : TDef extends XRPCQueryMetadata ? Body extends XRPCLexBodyParam ? InferOutput : null : TDef extends XRPCProcedureMetadata ? Body extends XRPCLexBodyParam ? InferOutput : null : never; }; /** represents a failed response returned by the client */ export type FailedClientResponse = BaseClientResponse & { ok: false; /** response data */ data: XRPCErrorPayload; }; /** represents a response returned by the client */ export type ClientResponse = SuccessClientResponse | FailedClientResponse; type UnknownClientResponse = { status: number; headers: Headers; } & ({ ok: true; data: unknown; } | { ok: false; data: XRPCErrorPayload; }); type Namespaced = { mainSchema: T; }; /** options for configuring the client */ export type ClientOptions = { /** the underlying fetch handler it should make requests with */ handler: FetchHandler | FetchHandlerObject; /** * service proxy target, in the form `#`. * * common service ids include: * * - `#atproto_pds` (personal data server) * - `#atproto_labeler` (labeler service) * - `#bsky_chat` (Bluesky chat service) */ proxy?: AtprotoAudience | null; }; /** XRPC API client */ export declare class Client { #private; handler: FetchHandler; proxy: AtprotoAudience | null; constructor({ handler, proxy }: ClientOptions); /** * clones this XRPC client * * @param opts options to merge with * @returns the cloned XRPC client */ clone({ handler, proxy }?: Partial): Client; /** * performs an XRPC query request (HTTP GET) * * @param name NSID of the query * @param options query options */ get>(name: TName, ...options: HasRequiredKeys extends true ? [init: TInit] : [init?: TInit]): Promise>; /** * performs an XRPC procedure request (HTTP POST) * * @param name NSID of the procedure * @param options procedure options */ post>(name: TName, ...options: HasRequiredKeys extends true ? [init: TInit] : [init?: TInit]): Promise>; /** * performs an XRPC call with schema validation * * @param schema the lexicon schema for the endpoint, or a namespace containing mainSchema * @param options call options */ call>(schema: TMeta | Namespaced, ...options: HasRequiredKeys extends true ? [init: TInit] : [init?: TInit]): Promise>; } export declare const isXRPCErrorPayload: (input: unknown) => input is XRPCErrorPayload; type ExtractSuccessData = R extends { ok: true; data: infer D; } ? D : never; /** * takes in the response returned by the client, and either returns the data if it is a successful response, * or throws if it's a failed response. * * @example * const data = await ok(client.get('com.atproto.server.describeServer')); * // ^? ComAtprotoServerDescribeServer.Output * * @param input either a ClientResponse, or a promise that resolves to a ClientResponse * @returns the data from a successful response */ export declare const ok: { (promise: Promise): Promise>; (response: T): ExtractSuccessData; }; /** options when constructing a ClientResponseError */ export type ClientResponseErrorOptions = { status: number; headers?: Headers; data: XRPCErrorPayload; }; /** represents an error response returned by the client */ export declare class ClientResponseError extends Error { /** error name returned by service */ readonly error: string; /** error message returned by service */ readonly description?: string; /** response status */ readonly status: number; /** response headers */ readonly headers: Headers; constructor({ status, headers, data }: ClientResponseErrorOptions); } /** represents a validation error during typed calls */ export declare class ClientValidationError extends Error { /** validation target (params, input, or output) */ readonly target: 'params' | 'input' | 'output'; /** validation result */ readonly result: v.Err; constructor(target: 'params' | 'input' | 'output', result: v.Err); } export {};