UNPKG

1.55 kBTypeScriptView Raw
1/// <reference types="node" />
2import type { Buffer } from 'node:buffer';
3import PCancelable from 'p-cancelable';
4import { RequestError } from '../core/errors.js';
5import type Request from '../core/index.js';
6import type { RequestEvents } from '../core/index.js';
7import type { Response } from '../core/response.js';
8/**
9An error to be thrown when the request is aborted with `.cancel()`.
10*/
11export declare class CancelError extends RequestError {
12 readonly response: Response;
13 constructor(request: Request);
14 /**
15 Whether the promise is canceled.
16 */
17 get isCanceled(): boolean;
18}
19export interface CancelableRequest<T extends Response | Response['body'] = Response['body']> extends PCancelable<T>, RequestEvents<CancelableRequest<T>> {
20 /**
21 A shortcut method that gives a Promise returning a JSON object.
22
23 It is semantically the same as settings `options.resolveBodyOnly` to `true` and `options.responseType` to `'json'`.
24 */
25 json: <ReturnType>() => CancelableRequest<ReturnType>;
26 /**
27 A shortcut method that gives a Promise returning a [Buffer](https://nodejs.org/api/buffer.html).
28
29 It is semantically the same as settings `options.resolveBodyOnly` to `true` and `options.responseType` to `'buffer'`.
30 */
31 buffer: () => CancelableRequest<Buffer>;
32 /**
33 A shortcut method that gives a Promise returning a string.
34
35 It is semantically the same as settings `options.resolveBodyOnly` to `true` and `options.responseType` to `'text'`.
36 */
37 text: () => CancelableRequest<string>;
38}