/
types.ts
174 lines (155 loc) 路 4.55 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import http = require('http');
import https = require('https');
import {Readable as ReadableStream} from 'stream';
import PCancelable = require('p-cancelable');
import {URL} from 'url';
import {CookieJar} from 'tough-cookie';
import {StorageAdapter} from 'cacheable-request';
import {Omit} from 'type-fest';
import CacheableLookup from 'cacheable-lookup';
import Keyv = require('keyv');
import {Timings} from '@szmarczak/http-timer/dist';
import {Hooks} from '../known-hook-events';
import {GotError, ParseError, HTTPError, MaxRedirectsError} from '../errors';
import {ProxyStream} from '../as-stream';
export type Method =
| 'GET'
| 'POST'
| 'PUT'
| 'PATCH'
| 'HEAD'
| 'DELETE'
| 'OPTIONS'
| 'TRACE'
| 'get'
| 'post'
| 'put'
| 'patch'
| 'head'
| 'delete'
| 'options'
| 'trace';
export type ErrorCode =
| 'ETIMEDOUT'
| 'ECONNRESET'
| 'EADDRINUSE'
| 'ECONNREFUSED'
| 'EPIPE'
| 'ENOTFOUND'
| 'ENETUNREACH'
| 'EAI_AGAIN';
export type StatusCode =
| 408
| 413
| 429
| 500
| 502
| 503
| 504;
export type ResponseType = 'json' | 'buffer' | 'text';
export type URLArgument = string | https.RequestOptions | URL;
export interface Response extends http.IncomingMessage {
body: Buffer | string | any;
statusCode: number;
fromCache?: boolean;
isFromCache?: boolean;
req: http.ClientRequest;
requestUrl: string;
retryCount: number;
timings: Timings;
redirectUrls: string[];
request: { options: NormalizedOptions };
}
export type RetryFunction = (retry: number, error: Error | GotError | ParseError | HTTPError | MaxRedirectsError) => number;
export type HandlerFunction = <T extends ProxyStream | CancelableRequest<Response>>(options: Options, next: (options: Options) => T) => T;
export interface RetryOption {
retries?: RetryFunction | number;
methods?: Method[];
statusCodes?: StatusCode[];
errorCodes?: ErrorCode[];
maxRetryAfter?: number;
}
export interface NormalizedRetryOptions {
retries: RetryFunction;
methods: ReadonlySet<Method>;
statusCodes: ReadonlySet<StatusCode>;
errorCodes: ReadonlySet<ErrorCode>;
maxRetryAfter: number;
}
export type RequestFunction = typeof https.request;
export interface AgentByProtocol {
http: http.Agent;
https: https.Agent;
}
export interface Delays {
lookup?: number;
connect?: number;
secureConnect?: number;
socket?: number;
response?: number;
send?: number;
request?: number;
}
export type Headers = Record<string, string | string[]>;
// The library overrides the type definition of `agent`, `host`, 'headers and `timeout`.
export interface Options extends Omit<https.RequestOptions, 'agent' | 'timeout' | 'host' | 'headers'> {
host?: string;
body?: string | Buffer | ReadableStream;
hostname?: string;
path?: string;
socketPath?: string;
protocol?: string;
href?: string;
options?: Options;
hooks?: Partial<Hooks>;
decompress?: boolean;
stream?: boolean;
encoding?: BufferEncoding | null;
method?: Method;
retry?: number | Partial<RetryOption | NormalizedRetryOptions>;
throwHttpErrors?: boolean;
cookieJar?: CookieJar;
ignoreInvalidCookies?: boolean;
request?: RequestFunction;
agent?: http.Agent | https.Agent | boolean | AgentByProtocol;
gotTimeout?: number | Delays;
cache?: string | StorageAdapter | false;
headers?: Headers;
mutableDefaults?: boolean;
responseType?: ResponseType;
resolveBodyOnly?: boolean;
followRedirect?: boolean;
prefixUrl?: URL | string;
timeout?: number | Delays;
dnsCache?: Map<string, string> | Keyv | false;
url?: URL | string;
searchParams?: Record<string, string | number | boolean | null> | URLSearchParams | string;
query?: Options['searchParams']; // Deprecated
useElectronNet?: boolean;
form?: Record<string, any>;
json?: Record<string, any>;
context?: {[key: string]: unknown};
}
export interface NormalizedOptions extends Omit<Required<Options>, 'timeout' | 'dnsCache' | 'retry'> {
hooks: Hooks;
gotTimeout: Required<Delays>;
retry: NormalizedRetryOptions;
lookup?: CacheableLookup['lookup'];
readonly prefixUrl: string;
path: string;
hostname: string;
host: string;
}
export interface Defaults {
methods?: Method[];
options?: Options;
handler?: HandlerFunction;
mutableDefaults?: boolean;
}
export type URLOrOptions = URLArgument | (Options & {url: URLArgument});
export interface CancelableRequest<T extends http.IncomingMessage | Buffer | string | object> extends PCancelable<T> {
on(name: string, listener: () => void): CancelableRequest<T>;
json<TReturnType extends object>(): CancelableRequest<TReturnType>;
buffer<TReturnType extends Buffer>(): CancelableRequest<TReturnType>;
text<TReturnType extends string>(): CancelableRequest<TReturnType>;
}