@happy-ts/fetch-t
Version:
Abortable fetch wrapper with the ability to specify the return type.
207 lines (202 loc) • 8.51 kB
TypeScript
import { AsyncResult, IOResult } from 'happy-rusty';
/**
* Name of abort error;
*/
declare const ABORT_ERROR: "AbortError";
/**
* Name of timeout error;
*/
declare const TIMEOUT_ERROR: "TimeoutError";
/**
* Represents the response of a fetch operation, encapsulating the result data or any error that occurred.
*
* @typeParam T - The type of the data expected in the response.
*/
type FetchResponse<T, E = any> = AsyncResult<T, E>;
/**
* Defines the structure and behavior of a fetch task, including the ability to abort the task and check its status.
*
* @typeParam T - The type of the data expected in the response.
*/
interface FetchTask<T> {
/**
* Aborts the fetch task, optionally with a reason for the abortion.
*
* @param reason - An optional parameter to indicate why the task was aborted.
*/
abort(reason?: any): void;
/**
* Indicates whether the fetch task has been aborted.
*/
readonly aborted: boolean;
/**
* The response of the fetch task, represented as an `AsyncResult`.
*/
readonly response: FetchResponse<T>;
}
/**
* Specifies the expected response type of the fetch request.
*/
type FetchResponseType = 'text' | 'arraybuffer' | 'blob' | 'json';
/**
* Represents the progress of a fetch operation.
*/
interface FetchProgress {
/**
* The total number of bytes to be received.
*/
totalByteLength: number;
/**
* The number of bytes received so far.
*/
completedByteLength: number;
}
/**
* Extends the standard `RequestInit` interface from the Fetch API to include additional custom options.
*/
interface FetchInit extends RequestInit {
/**
* Indicates whether the fetch request should be abortable.
*/
abortable?: boolean;
/**
* Specifies the expected response type of the fetch request.
*/
responseType?: FetchResponseType;
/**
* Specifies the maximum time in milliseconds to wait for the fetch request to complete.
*/
timeout?: number;
/**
* Specifies a function to be called when the fetch request makes progress.
* @param progressResult - The progress of the fetch request.
*/
onProgress?: (progressResult: IOResult<FetchProgress>) => void;
/**
* Specifies a function to be called when the fetch request receives a chunk of data.
* @param chunk - The chunk of data received.
*/
onChunk?: (chunk: Uint8Array) => void;
}
/**
* Represents an error that occurred during a fetch operation when the response is not ok.
*/
declare class FetchError extends Error {
/**
* The name of the error.
*/
name: string;
/**
* The status code of the response.
*/
status: number;
constructor(message: string, status: number);
}
/**
* Fetches a resource from the network as a text string and returns a `FetchTask` representing the operation.
*
* @typeParam T - The expected type of the response data.
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
* @returns A `FetchTask` representing the operation with a `string` response.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
abortable: true;
responseType: 'text';
}): FetchTask<string>;
/**
* Fetches a resource from the network as an ArrayBuffer and returns a `FetchTask` representing the operation.
*
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
* @returns A `FetchTask` representing the operation with an `ArrayBuffer` response.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
abortable: true;
responseType: 'arraybuffer';
}): FetchTask<ArrayBuffer>;
/**
* Fetches a resource from the network as a Blob and returns a `FetchTask` representing the operation.
*
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
* @returns A `FetchTask` representing the operation with a `Blob` response.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
abortable: true;
responseType: 'blob';
}): FetchTask<Blob>;
/**
* Fetches a resource from the network and parses it as JSON, returning a `FetchTask` representing the operation.
*
* @typeParam T - The expected type of the parsed JSON data.
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
* @returns A `FetchTask` representing the operation with a response parsed as JSON.
*/
declare function fetchT<T>(url: string | URL, init: FetchInit & {
abortable: true;
responseType: 'json';
}): FetchTask<T>;
/**
* Fetches a resource from the network as a text string and returns a `FetchResponse` representing the operation.
*
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, specifying the response type as 'text'.
* @returns A `FetchResponse` representing the operation with a `string` response.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
responseType: 'text';
}): FetchResponse<string, Error>;
/**
* Fetches a resource from the network as an ArrayBuffer and returns a `FetchResponse` representing the operation.
*
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, specifying the response type as 'arraybuffer'.
* @returns A `FetchResponse` representing the operation with an `ArrayBuffer` response.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
responseType: 'arraybuffer';
}): FetchResponse<ArrayBuffer, Error>;
/**
* Fetches a resource from the network as a Blob and returns a `FetchResponse` representing the operation.
*
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, specifying the response type as 'blob'.
* @returns A `FetchResponse` representing the operation with a `Blob` response.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
responseType: 'blob';
}): FetchResponse<Blob, Error>;
/**
* Fetches a resource from the network and parses it as JSON, returning a `FetchResponse` representing the operation.
*
* @typeParam T - The expected type of the parsed JSON data.
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, specifying the response type as 'json'.
* @returns A `FetchResponse` representing the operation with a response parsed as JSON.
*/
declare function fetchT<T>(url: string | URL, init: FetchInit & {
responseType: 'json';
}): FetchResponse<T, Error>;
/**
* Fetches a resource from the network and returns a `FetchTask` representing the operation with a generic `Response`.
*
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, indicating that the operation should be abortable.
* @returns A `FetchTask` representing the operation with a generic `Response`.
*/
declare function fetchT(url: string | URL, init: FetchInit & {
abortable: true;
}): FetchTask<Response>;
/**
* Fetches a resource from the network and returns a `FetchResponse` or `FetchTask` based on the provided options.
*
* @typeParam T - The expected type of the response data when not using a specific `responseType`.
* @param url - The resource to fetch. Can be a URL object or a string representing a URL.
* @param init - Additional options for the fetch operation, including custom `FetchInit` properties.
* @returns A `FetchResponse` representing the operation with a `Response` object.
*/
declare function fetchT(url: string | URL, init?: FetchInit): FetchResponse<Response>;
export { ABORT_ERROR, FetchError, type FetchInit, type FetchProgress, type FetchResponse, type FetchResponseType, type FetchTask, TIMEOUT_ERROR, fetchT };
//# sourceMappingURL=types.d.ts.map