1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 | /**
* HTTP core: typed requests against the API base, requests against absolute
* node URLs (upload grants, mailboxes), pagination and polling helpers.
* Every function here is pure apart from the injected `fetch`/`sleep`.
*/
import type { Client } from './client.js';
import type { Body, ElementOf, Method, MethodKey, Params, PathsWith, Query, Res } from './types.js';
export type HttpRequest = Readonly<{
method: Method;
url: string;
headers: Readonly<Record<string, string>>;
body?: BodyInit;
}>;
export type RequestOptions<P extends PathsWith<M>, M extends MethodKey> = Readonly<{
/** Values for the `{name}` placeholders in the path. */
params?: Params<P, M>;
/** Query-string parameters; undefined/null values are skipped. */
query?: Query<P, M>;
/** JSON request body (some DELETEs carry one). */
body?: Body<P, M>;
}>;
export type UrlRequestOptions = Readonly<{
/** Bearer token for the endpoint (from the grant or the mint). */
token?: string | null;
query?: Readonly<Record<string, string | number | boolean | null | undefined>>;
/** Extra headers, merged over the computed ones. */
headers?: Readonly<Record<string, string>>;
/** Raw body, sent as-is. */
body?: BodyInit;
fetch?: typeof fetch;
timeoutMs?: number;
}>;
export type PollOptions = Readonly<{
/** Initial interval between polls (default 1000). */
intervalMs?: number;
/** Backoff cap: the interval doubles up to this (default 8000). */
maxIntervalMs?: number;
/** Overall deadline (default 120000). */
timeoutMs?: number;
/** Message for the timeout error. */
message?: string;
sleep?: (ms: number) => Promise<void>;
}>;
export type UploadSource = Uint8Array | ArrayBuffer | Blob | ReadableStream<Uint8Array>;
/** Percent-encode one URL component, using %20 (never +) for spaces. */
export declare const encode: (value: string) => string;
/** Build a query string with its leading `?`, or '' when nothing is set. */
export declare const queryString: (query?: object | null) => string;
/** Build the request for an API call without sending it. */
export declare const buildRequest: <M extends MethodKey, P extends PathsWith<M>>(client: Client, method: M, path: P, options?: RequestOptions<P, M>) => HttpRequest;
/**
* Turn a raw status + body into the parsed JSON value, throwing an api error
* on non-2xx statuses and a transport error on unparseable success bodies.
* An empty 2xx body decodes to `{}`.
*/
export declare const decodeResponse: (status: number, body: string) => unknown;
/**
* Execute an API request. METHOD and PATH are spec keys, so the options and
* the result are typed from the OpenAPI document:
*
* await request(client, 'get', '/domains/check', { query: { domain: 'x.ro' } })
*
* Throws an api error on non-2xx responses and a transport error on
* connection/parse failures.
*/
export declare const request: <M extends MethodKey, P extends PathsWith<M>>(client: Client, method: M, path: P, options?: RequestOptions<P, M>) => Promise<Res<P, M>>;
export declare const get: <P extends PathsWith<"get">>(client: Client, path: P, options?: RequestOptions<P, "get">) => Promise<Res<P, "get">>;
export declare const post: <P extends PathsWith<"post">>(client: Client, path: P, options?: RequestOptions<P, "post">) => Promise<Res<P, "post">>;
export declare const put: <P extends PathsWith<"put">>(client: Client, path: P, options?: RequestOptions<P, "put">) => Promise<Res<P, "put">>;
export declare const patch: <P extends PathsWith<"patch">>(client: Client, path: P, options?: RequestOptions<P, "patch">) => Promise<Res<P, "patch">>;
export declare const del: <P extends PathsWith<"delete">>(client: Client, path: P, options?: RequestOptions<P, "delete">) => Promise<Res<P, "delete">>;
/**
* Execute a request against an absolute URL rather than the API base — for
* the node-hosted endpoints the API hands out (an IPFS upload grant, a p2p
* app's mailbox). Throws like `request`.
*/
export declare const requestUrl: <T = unknown>(method: Method, url: string, options?: UrlRequestOptions) => Promise<T>;
type PageOptions<P extends PathsWith<'get'>> = Readonly<{
params?: Params<P, 'get'>;
query?: Query<P, 'get'>;
}>;
/**
* Follow cursor pagination for a list endpoint, collecting every item under
* KEY (e.g. 'registrations'). `cursor` is advanced from each response's
* `next_cursor` until it is absent.
*/
export declare const paginate: <P extends PathsWith<"get">, K extends keyof Res<P, "get"> & string>(client: Client, path: P, key: K, options?: PageOptions<P>) => Promise<readonly ElementOf<Res<P, "get">[K]>[]>;
/**
* Follow offset pagination for a list endpoint, collecting every item under
* KEY (e.g. 'pins'). The page `limit` defaults to 100; `offset` advances by
* the rows each page actually returns and paging stops on the first short
* or empty page. The response's `total` is deliberately ignored — it can
* move while the pages are walked.
*/
export declare const paginateOffset: <P extends PathsWith<"get">, K extends keyof Res<P, "get"> & string>(client: Client, path: P, key: K, options?: PageOptions<P>) => Promise<readonly ElementOf<Res<P, "get">[K]>[]>;
/**
* Poll STEP until DONE holds for its result, sleeping between attempts with
* exponential backoff (the interval doubles up to `maxIntervalMs`). Returns
* the first result that satisfies DONE; throws a timeout error (carrying the
* last result) when the deadline passes first.
*/
export declare const pollUntil: <T>(step: () => Promise<T>, done: (result: T) => boolean, options?: PollOptions) => Promise<T>;
/**
* POST raw bytes to an IPFS upload-grant URL (from `ipfs.startUpload`). The
* token is single-use and may be null once consumed. `format: 'car'`
* appends ?format=car for a client-packed CAR archive.
*/
export declare const uploadBytes: (url: string, token: string | null | undefined, source: UploadSource, options?: Readonly<{
format?: "file" | "car";
fetch?: typeof fetch;
timeoutMs?: number;
}>) => Promise<unknown>;
export {};
|