import { type InferInput, type InferPayload, type Main, type Params, type Procedure, type Query, type Restricted, type Subscription } from '@atproto/lex-schema'; import type { Agent, AgentOptions } from './agent.js'; import type { XrpcFailure } from './errors.js'; import type { XrpcResponseOptions } from './response.js'; import { XrpcResponse } from './response.js'; import type { BinaryBodyInit } from './types.js'; import type { XrpcRequestHeadersOptions } from './util.js'; /** * The query/path parameters type for an XRPC method, inferred from its schema. * * @typeParam M - The XRPC method type (Procedure, Query, or Subscription) */ export type XrpcRequestParams = InferInput; type XrpcRequestParamsOptions

= NonNullable extends P ? { params?: P; } : { params: P; }; type XrpcRequestPayload = M extends Procedure ? InferPayload : undefined; type XrpcRequestPayloadOptions = TPayload extends { body: infer B; encoding: infer E; } ? { body: B; /** * mime type hint for binary bodies * * Only needed for endpoints that accept binary input (e.g. file uploads) * when the body is a Blob-like object without a type (e.g. fetch-blob's * Blob). If the body is a Blob-like object with a type, that type will be * used as the content-type header instead of this option. * * @default "application/octet-stream" */ encoding?: E; } : { body?: undefined; encoding?: undefined; }; /** * Options for making an XRPC request, based on the method schema. * * Combines {@link XrpcRequestOptions} and {@link XrpcResponseOptions} with * method-specific params and body requirements. The type system ensures * required params/body are provided based on the method schema. * * @typeParam M - The XRPC method type (Procedure or Query) * * @example Query with params * ```typescript * const options: XrpcOptions = { * params: { limit: 50 } * } * ``` * * @example Procedure with body * ```typescript * const options: XrpcOptions = { * body: { repo: did, collection: 'app.bsky.feed.post', record: { ... } } * } * ``` */ export type XrpcOptions = XrpcRequestOptions & XrpcResponseOptions & RetryOptions; export type XrpcRequestOptions = XrpcRequestProcessingOptions & XrpcRequestHeadersOptions & XrpcRequestPayloadOptions> & XrpcRequestParamsOptions>; export type XrpcRequestProcessingOptions = { /** * AbortSignal to cancel the request. */ signal?: AbortSignal; /** * Whether to validate the request against the method's input schema. Enabling * this can help catch errors early but may have a performance cost. This * would typically only be set to `true` in development or debugging * scenarios. * * @default false */ validateRequest?: boolean; }; /** * Makes an XRPC request and throws on failure. * * This is the low-level function for making XRPC calls. * * @param agent - The {@link Agent} to use for making the request * @param ns - The lexicon method definition * @param options - Request {@link XrpcOptions options} (params, body, headers, etc.) * @returns The successful {@link XrpcResponse} * @throws {XrpcFailure} When the request fails * * @example * ```typescript * const response = await xrpc('https://bsky.network', com.atproto.identity.resolveHandle, { * params: { handle: "atproto.com" } * }) * ``` * * @example * ```typescript * const response = await xrpc(agent, app.bsky.feed.getTimeline.main, { * params: { limit: 50 } * }) * ``` */ export declare function xrpc(agentOpts: Agent | AgentOptions, ns: NonNullable extends XrpcOptions ? Main : Restricted<'This XRPC method requires an "options" argument'>): Promise>; export declare function xrpc(agentOpts: Agent | AgentOptions, ns: Main, options: XrpcOptions): Promise>; /** * Union type representing either a successful response or a failure. * * Both {@link XrpcResponse} and {@link XrpcFailure} have a `success` property * that can be used to discriminate between them. * * @typeParam M - The XRPC method type */ export type XrpcResult = XrpcResponse | XrpcFailure; /** * Makes an XRPC request without throwing on failure. * * Returns a discriminated union that can be checked via the `success` property. * This is useful for handling errors without try/catch blocks. This also allows * failure results to be typed with the method schema, which can provide better * type safety when handling errors (e.g. checking for specific error codes). * * @param agent - The {@link Agent} to use for making the request * @param ns - The lexicon method definition * @param options - Request {@link XrpcOptions options} (params, body, headers, etc.) * @returns Either a successful {@link XrpcResponse} or an {@link XrpcFailure} * * @example * ```typescript * const result = await xrpcSafe('https://example.com', app.bsky.actor.getProfile, { * params: { actor: 'alice.bsky.social' } * }) * * if (result.success) { * console.log(result.body.displayName) * } else { * console.error('Request failed:', result.error) * } * ``` */ export declare function xrpcSafe(agentOpts: Agent | AgentOptions, ns: NonNullable extends XrpcOptions ? Main : Restricted<'This XRPC method requires an "options" argument'>): Promise>; export declare function xrpcSafe(agentOpts: Agent | AgentOptions, ns: Main, options: XrpcOptions): Promise>; /** * Extracts the root cause from an error, unwrapping common fetch-related errors * such as those from undici (Node's internal fetch implementation). * * @param err - The error to extract the root cause from * @returns The root cause error, or the original error if no specific pattern is matched * @remarks This is useful for getting more specific error information from fetch-related failures, especially in Node environments using undici. */ export declare function extractFetchErrorCause(err: unknown): unknown; export type RetryOptions = { /** * Function to determine whether a request should be retried after a failure. * * @default `(failure) => failure.shouldRetry()` */ retry?: (failure: XrpcFailure, context: { counter: number; }) => boolean; /** * Maximum number of retries to allow. * * @default 0 (no retries) */ maxRetries?: number; /** * Max number of milliseconds allow between retries * * @default 30000 */ maxRetryTimeout?: number; /** * Initial number of milliseconds to wait before retrying for the first time. * * @default 500 */ minRetryTimeout?: number; /** * Factor to multiply the timeout factor between retries. * * @default 2 */ retryTimeoutFactor?: number; /** * Automatically infer timeout between retries based on header values (`Retry-After`, `RateLimit-Reset`). * * @default true */ retryHeaders?: boolean; }; export {}; //# sourceMappingURL=xrpc.d.ts.map