forked from sindresorhus/got
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
49 lines (40 loc) · 1.53 KB
/
types.ts
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
import type {Buffer} from 'node:buffer';
import PCancelable from 'p-cancelable';
import {RequestError} from '../core/errors.js';
import type Request from '../core/index.js';
import type {RequestEvents} from '../core/index.js';
import type {Response} from '../core/response.js';
/**
An error to be thrown when the request is aborted with `.cancel()`.
*/
export class CancelError extends RequestError {
declare readonly response: Response;
constructor(request: Request) {
super('Promise was canceled', {}, request);
this.name = 'CancelError';
this.code = 'ERR_CANCELED';
}
/**
Whether the promise is canceled.
*/
get isCanceled() {
return true;
}
}
export interface CancelableRequest<T extends Response | Response['body'] = Response['body']> extends PCancelable<T>, RequestEvents<CancelableRequest<T>> {
/**
A shortcut method that gives a Promise returning a JSON object.
It is semantically the same as settings `options.resolveBodyOnly` to `true` and `options.responseType` to `'json'`.
*/
json: <ReturnType>() => CancelableRequest<ReturnType>;
/**
A shortcut method that gives a Promise returning a [Buffer](https://nodejs.org/api/buffer.html).
It is semantically the same as settings `options.resolveBodyOnly` to `true` and `options.responseType` to `'buffer'`.
*/
buffer: () => CancelableRequest<Buffer>;
/**
A shortcut method that gives a Promise returning a string.
It is semantically the same as settings `options.resolveBodyOnly` to `true` and `options.responseType` to `'text'`.
*/
text: () => CancelableRequest<string>;
}