2023-06-01 17:52:58 -04:00
|
|
|
/**
|
2023-11-11 18:00:39 -05:00
|
|
|
* An environment's implementation of ``getUrl`` must return this type.
|
2023-06-01 17:52:58 -04:00
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type GetUrlResponse = {
|
2022-09-05 16:57:11 -04:00
|
|
|
statusCode: number;
|
|
|
|
statusMessage: string;
|
|
|
|
headers: Record<string, string>;
|
|
|
|
body: null | Uint8Array;
|
|
|
|
};
|
2023-06-01 17:52:58 -04:00
|
|
|
/**
|
|
|
|
* This can be used to control how throttling is handled in
|
|
|
|
* [[FetchRequest-setThrottleParams]].
|
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type FetchThrottleParams = {
|
2022-09-27 03:45:27 -04:00
|
|
|
maxAttempts?: number;
|
|
|
|
slotInterval?: number;
|
|
|
|
};
|
2022-09-05 16:57:11 -04:00
|
|
|
/**
|
|
|
|
* Called before any network request, allowing updated headers (e.g. Bearer tokens), etc.
|
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type FetchPreflightFunc = (req: FetchRequest) => Promise<FetchRequest>;
|
2022-09-05 16:57:11 -04:00
|
|
|
/**
|
|
|
|
* Called on the response, allowing client-based throttling logic or post-processing.
|
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type FetchProcessFunc = (req: FetchRequest, resp: FetchResponse) => Promise<FetchResponse>;
|
2022-09-05 16:57:11 -04:00
|
|
|
/**
|
|
|
|
* Called prior to each retry; return true to retry, false to abort.
|
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type FetchRetryFunc = (req: FetchRequest, resp: FetchResponse, attempt: number) => Promise<boolean>;
|
2022-09-05 16:57:11 -04:00
|
|
|
/**
|
|
|
|
* Called on Gateway URLs.
|
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type FetchGatewayFunc = (url: string, signal?: FetchCancelSignal) => Promise<FetchRequest | FetchResponse>;
|
2022-09-05 16:57:11 -04:00
|
|
|
/**
|
|
|
|
* Used to perform a fetch; use this to override the underlying network
|
|
|
|
* fetch layer. In NodeJS, the default uses the "http" and "https" libraries
|
|
|
|
* and in the browser ``fetch`` is used. If you wish to use Axios, this is
|
|
|
|
* how you would register it.
|
|
|
|
*/
|
2023-02-02 04:05:47 -05:00
|
|
|
export type FetchGetUrlFunc = (req: FetchRequest, signal?: FetchCancelSignal) => Promise<GetUrlResponse>;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* @_ignore
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
export declare class FetchCancelSignal {
|
|
|
|
#private;
|
|
|
|
constructor(request: FetchRequest);
|
|
|
|
addListener(listener: () => void): void;
|
|
|
|
get cancelled(): boolean;
|
|
|
|
checkSignal(): void;
|
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Represents a request for a resource using a URI.
|
|
|
|
*
|
2022-11-30 15:44:23 -05:00
|
|
|
* By default, the supported schemes are ``HTTP``, ``HTTPS``, ``data:``,
|
|
|
|
* and ``IPFS:``.
|
|
|
|
*
|
|
|
|
* Additional schemes can be added globally using [[registerGateway]].
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* req = new FetchRequest("https://www.ricmoo.com")
|
|
|
|
* resp = await req.send()
|
|
|
|
* resp.body.length
|
|
|
|
* //_result:
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
export declare class FetchRequest implements Iterable<[key: string, value: string]> {
|
|
|
|
#private;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2023-11-11 18:00:39 -05:00
|
|
|
* The fetch URL to request.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get url(): string;
|
|
|
|
set url(url: string);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The fetch body, if any, to send as the request body. //(default: null)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*
|
|
|
|
* When setting a body, the intrinsic ``Content-Type`` is automatically
|
|
|
|
* set and will be used if **not overridden** by setting a custom
|
|
|
|
* header.
|
|
|
|
*
|
|
|
|
* If %%body%% is null, the body is cleared (along with the
|
2023-11-11 18:00:39 -05:00
|
|
|
* intrinsic ``Content-Type``).
|
2022-09-15 22:58:45 -04:00
|
|
|
*
|
2023-11-11 18:00:39 -05:00
|
|
|
* If %%body%% is a string, the intrinsic ``Content-Type`` is set to
|
2022-09-15 22:58:45 -04:00
|
|
|
* ``text/plain``.
|
|
|
|
*
|
2023-11-11 18:00:39 -05:00
|
|
|
* If %%body%% is a Uint8Array, the intrinsic ``Content-Type`` is set to
|
2022-09-15 22:58:45 -04:00
|
|
|
* ``application/octet-stream``.
|
|
|
|
*
|
2023-11-11 18:00:39 -05:00
|
|
|
* If %%body%% is any other object, the intrinsic ``Content-Type`` is
|
2022-09-15 22:58:45 -04:00
|
|
|
* set to ``application/json``.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get body(): null | Uint8Array;
|
|
|
|
set body(body: null | string | Readonly<object> | Readonly<Uint8Array>);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Returns true if the request has a body.
|
|
|
|
*/
|
|
|
|
hasBody(): this is (FetchRequest & {
|
|
|
|
body: Uint8Array;
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* The HTTP method to use when requesting the URI. If no method
|
|
|
|
* has been explicitly set, then ``GET`` is used if the body is
|
|
|
|
* null and ``POST`` otherwise.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get method(): string;
|
|
|
|
set method(method: null | string);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The headers that will be used when requesting the URI. All
|
|
|
|
* keys are lower-case.
|
|
|
|
*
|
2023-11-11 18:00:39 -05:00
|
|
|
* This object is a copy, so any changes will **NOT** be reflected
|
2022-11-30 15:44:23 -05:00
|
|
|
* in the ``FetchRequest``.
|
|
|
|
*
|
|
|
|
* To set a header entry, use the ``setHeader`` method.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-11-30 15:44:23 -05:00
|
|
|
get headers(): Record<string, string>;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Get the header for %%key%%, ignoring case.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
getHeader(key: string): string;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Set the header for %%key%% to %%value%%. All values are coerced
|
|
|
|
* to a string.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
setHeader(key: string, value: string | number): void;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Clear all headers, resetting all intrinsic headers.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
clearHeaders(): void;
|
|
|
|
[Symbol.iterator](): Iterator<[key: string, value: string]>;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The value that will be sent for the ``Authorization`` header.
|
2022-11-30 15:44:23 -05:00
|
|
|
*
|
|
|
|
* To set the credentials, use the ``setCredentials`` method.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get credentials(): null | string;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Sets an ``Authorization`` for %%username%% with %%password%%.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
setCredentials(username: string, password: string): void;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Enable and request gzip-encoded responses. The response will
|
|
|
|
* automatically be decompressed. //(default: true)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get allowGzip(): boolean;
|
|
|
|
set allowGzip(value: boolean);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Allow ``Authentication`` credentials to be sent over insecure
|
2022-11-30 15:44:23 -05:00
|
|
|
* channels. //(default: false)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get allowInsecureAuthentication(): boolean;
|
|
|
|
set allowInsecureAuthentication(value: boolean);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2023-11-11 18:00:39 -05:00
|
|
|
* The timeout (in milliseconds) to wait for a complete response.
|
2022-11-30 15:44:23 -05:00
|
|
|
* //(default: 5 minutes)//
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get timeout(): number;
|
|
|
|
set timeout(timeout: number);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* This function is called prior to each request, for example
|
|
|
|
* during a redirection or retry in case of server throttling.
|
|
|
|
*
|
|
|
|
* This offers an opportunity to populate headers or update
|
|
|
|
* content before sending a request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get preflightFunc(): null | FetchPreflightFunc;
|
|
|
|
set preflightFunc(preflight: null | FetchPreflightFunc);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* This function is called after each response, offering an
|
|
|
|
* opportunity to provide client-level throttling or updating
|
|
|
|
* response data.
|
|
|
|
*
|
|
|
|
* Any error thrown in this causes the ``send()`` to throw.
|
|
|
|
*
|
|
|
|
* To schedule a retry attempt (assuming the maximum retry limit
|
|
|
|
* has not been reached), use [[response.throwThrottleError]].
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get processFunc(): null | FetchProcessFunc;
|
|
|
|
set processFunc(process: null | FetchProcessFunc);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* This function is called on each retry attempt.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get retryFunc(): null | FetchRetryFunc;
|
|
|
|
set retryFunc(retry: null | FetchRetryFunc);
|
2023-10-09 20:27:56 -04:00
|
|
|
/**
|
|
|
|
* This function is called to fetch content from HTTP and
|
|
|
|
* HTTPS URLs and is platform specific (e.g. nodejs vs
|
|
|
|
* browsers).
|
|
|
|
*
|
|
|
|
* This is by default the currently registered global getUrl
|
|
|
|
* function, which can be changed using [[registerGetUrl]].
|
|
|
|
* If this has been set, setting is to ``null`` will cause
|
|
|
|
* this FetchRequest (and any future clones) to revert back to
|
|
|
|
* using the currently registered global getUrl function.
|
|
|
|
*
|
|
|
|
* Setting this is generally not necessary, but may be useful
|
|
|
|
* for developers that wish to intercept requests or to
|
|
|
|
* configurege a proxy or other agent.
|
|
|
|
*/
|
|
|
|
get getUrlFunc(): FetchGetUrlFunc;
|
|
|
|
set getUrlFunc(value: null | FetchGetUrlFunc);
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Create a new FetchRequest instance with default values.
|
|
|
|
*
|
|
|
|
* Once created, each property may be set before issuing a
|
2023-03-20 12:53:37 -04:00
|
|
|
* ``.send()`` to make the request.
|
2022-11-30 15:44:23 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
constructor(url: string);
|
2023-01-22 16:47:02 -05:00
|
|
|
toString(): string;
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Update the throttle parameters used to determine maximum
|
|
|
|
* attempts and exponential-backoff properties.
|
|
|
|
*/
|
2022-09-27 03:45:27 -04:00
|
|
|
setThrottleParams(params: FetchThrottleParams): void;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Resolves to the response by sending the request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
send(): Promise<FetchResponse>;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Cancels the inflight response, causing a ``CANCELLED``
|
|
|
|
* error to be rejected from the [[send]].
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
cancel(): void;
|
|
|
|
/**
|
|
|
|
* Returns a new [[FetchRequest]] that represents the redirection
|
|
|
|
* to %%location%%.
|
|
|
|
*/
|
|
|
|
redirect(location: string): FetchRequest;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Create a new copy of this request.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
clone(): FetchRequest;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Locks all static configuration for gateways and FetchGetUrlFunc
|
|
|
|
* registration.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static lockConfig(): void;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Get the current Gateway function for %%scheme%%.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static getGateway(scheme: string): null | FetchGatewayFunc;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Use the %%func%% when fetching URIs using %%scheme%%.
|
|
|
|
*
|
|
|
|
* This method affects all requests globally.
|
|
|
|
*
|
|
|
|
* If [[lockConfig]] has been called, no change is made and this
|
|
|
|
* throws.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static registerGateway(scheme: string, func: FetchGatewayFunc): void;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Use %%getUrl%% when fetching URIs over HTTP and HTTPS requests.
|
|
|
|
*
|
|
|
|
* This method affects all requests globally.
|
|
|
|
*
|
|
|
|
* If [[lockConfig]] has been called, no change is made and this
|
|
|
|
* throws.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
static registerGetUrl(getUrl: FetchGetUrlFunc): void;
|
2023-10-09 20:27:56 -04:00
|
|
|
/**
|
|
|
|
* Creates a getUrl function that fetches content from HTTP and
|
|
|
|
* HTTPS URLs.
|
|
|
|
*
|
|
|
|
* The available %%options%% are dependent on the platform
|
|
|
|
* implementation of the default getUrl function.
|
|
|
|
*
|
|
|
|
* This is not generally something that is needed, but is useful
|
|
|
|
* when trying to customize simple behaviour when fetching HTTP
|
|
|
|
* content.
|
|
|
|
*/
|
|
|
|
static createGetUrlFunc(options?: Record<string, any>): FetchGetUrlFunc;
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Creates a function that can "fetch" data URIs.
|
|
|
|
*
|
|
|
|
* Note that this is automatically done internally to support
|
|
|
|
* data URIs, so it is not necessary to register it.
|
|
|
|
*
|
|
|
|
* This is not generally something that is needed, but may
|
|
|
|
* be useful in a wrapper to perfom custom data URI functionality.
|
|
|
|
*/
|
|
|
|
static createDataGateway(): FetchGatewayFunc;
|
|
|
|
/**
|
|
|
|
* Creates a function that will fetch IPFS (unvalidated) from
|
|
|
|
* a custom gateway baseUrl.
|
|
|
|
*
|
|
|
|
* The default IPFS gateway used internally is
|
|
|
|
* ``"https:/\/gateway.ipfs.io/ipfs/"``.
|
|
|
|
*/
|
|
|
|
static createIpfsGatewayFunc(baseUrl: string): FetchGatewayFunc;
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2023-11-11 18:00:39 -05:00
|
|
|
* The response for a FetchRequest.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
export declare class FetchResponse implements Iterable<[key: string, value: string]> {
|
|
|
|
#private;
|
|
|
|
toString(): string;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response status code.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get statusCode(): number;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response status message.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get statusMessage(): string;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The response headers. All keys are lower-case.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
|
|
|
get headers(): Record<string, string>;
|
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The response body, or ``null`` if there was no body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get body(): null | Readonly<Uint8Array>;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* The response body as a UTF-8 encoded string, or the empty
|
|
|
|
* string (i.e. ``""``) if there was no body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*
|
2022-11-30 15:44:23 -05:00
|
|
|
* An error is thrown if the body is invalid UTF-8 data.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get bodyText(): string;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* The response body, decoded as JSON.
|
|
|
|
*
|
2022-11-30 15:44:23 -05:00
|
|
|
* An error is thrown if the body is invalid JSON-encoded data
|
|
|
|
* or if there was no body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get bodyJson(): any;
|
|
|
|
[Symbol.iterator](): Iterator<[key: string, value: string]>;
|
|
|
|
constructor(statusCode: number, statusMessage: string, headers: Readonly<Record<string, string>>, body: null | Uint8Array, request?: FetchRequest);
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Return a Response with matching headers and body, but with
|
|
|
|
* an error status code (i.e. 599) and %%message%% with an
|
|
|
|
* optional %%error%%.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
makeServerError(message?: string, error?: Error): FetchResponse;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* If called within a [request.processFunc](FetchRequest-processFunc)
|
|
|
|
* call, causes the request to retry as if throttled for %%stall%%
|
|
|
|
* milliseconds.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
throwThrottleError(message?: string, stall?: number): never;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Get the header value for %%key%%, ignoring case.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
getHeader(key: string): string;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2023-11-11 18:00:39 -05:00
|
|
|
* Returns true if the response has a body.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
|
|
|
hasBody(): this is (FetchResponse & {
|
|
|
|
body: Uint8Array;
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* The request made for this response.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
get request(): null | FetchRequest;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
2022-11-30 15:44:23 -05:00
|
|
|
* Returns true if this response was a success statusCode.
|
2022-09-15 22:58:45 -04:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
ok(): boolean;
|
2022-09-15 22:58:45 -04:00
|
|
|
/**
|
|
|
|
* Throws a ``SERVER_ERROR`` if this response is not ok.
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
assertOk(): void;
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=fetch.d.ts.map
|