1 | import * as https from 'https';
|
2 | import * as http from 'http';
|
3 | import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
|
4 | import * as url from 'url';
|
5 | import { Readable, Writable } from 'stream';
|
6 | import { EventEmitter } from 'events';
|
7 | import { LookupFunction } from 'net';
|
8 |
|
9 | export { IncomingHttpHeaders, OutgoingHttpHeaders };
|
10 | export type HttpMethod = "GET" | "POST" | "DELETE" | "PUT" | "HEAD" | "OPTIONS" | "PATCH" | "TRACE" | "CONNECT";
|
11 |
|
12 | export as namespace urllib;
|
13 | export interface RequestOptions {
|
14 | /** Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Alias 'type'. */
|
15 | method?: HttpMethod;
|
16 | /** Alias method */
|
17 | type?: HttpMethod;
|
18 | /** Data to be sent. Will be stringify automatically. */
|
19 | data?: any;
|
20 | /** Force convert data to query string. */
|
21 | dataAsQueryString?: boolean;
|
22 | /** Manually set the content of payload. If set, data will be ignored. */
|
23 | content?: string | Buffer;
|
24 | /** Stream to be pipe to the remote.If set, data and content will be ignored. */
|
25 | stream?: Readable;
|
26 | /**
|
27 | * A writable stream to be piped by the response stream.
|
28 | * Responding data will be write to this stream and callback
|
29 | * will be called with data set null after finished writing.
|
30 | */
|
31 | writeStream?: Writable;
|
32 | /** consume the writeStream, invoke the callback after writeStream close. */
|
33 | consumeWriteStream?: boolean;
|
34 | /**
|
35 | * The files will send with multipart/form-data format, base on formstream.
|
36 | * If method not set, will use POST method by default.
|
37 | */
|
38 | files?: Array<Readable | Buffer | string> | object | Readable | Buffer | string;
|
39 | /** Type of request data.Could be json.If it's json, will auto set Content-Type: application/json header. */
|
40 | contentType?: string;
|
41 | /**
|
42 | * urllib default use querystring to stringify form data which don't support nested object,
|
43 | * will use qs instead of querystring to support nested object by set this option to true.
|
44 | */
|
45 | nestedQuerystring?: boolean;
|
46 | /**
|
47 | * Type of response data. Could be text or json.
|
48 | * If it's text, the callbacked data would be a String.
|
49 | * If it's json, the data of callback would be a parsed JSON Object
|
50 | * and will auto set Accept: application/json header. Default callbacked data would be a Buffer.
|
51 | */
|
52 | dataType?: string;
|
53 | /** Fix the control characters (U+0000 through U+001F) before JSON parse response. Default is false. */
|
54 | fixJSONCtlChars?: boolean;
|
55 | /** Request headers. */
|
56 | headers?: IncomingHttpHeaders;
|
57 | /** by default will convert header keys to lowercase */
|
58 | keepHeaderCase?: boolean;
|
59 | /**
|
60 | * Request timeout in milliseconds for connecting phase and response receiving phase.
|
61 | * Defaults to exports.
|
62 | * TIMEOUT, both are 5s.You can use timeout: 5000 to tell urllib use same timeout on two phase or set them seperately such as
|
63 | * timeout: [3000, 5000], which will set connecting timeout to 3s and response 5s.
|
64 | */
|
65 | timeout?: number | number[];
|
66 | /** username:password used in HTTP Basic Authorization. */
|
67 | auth?: string;
|
68 | /** username:password used in HTTP Digest Authorization. */
|
69 | digestAuth?: string;
|
70 | /** HTTP Agent object.Set false if you does not use agent. */
|
71 | agent?: http.Agent;
|
72 | /** HTTPS Agent object. Set false if you does not use agent. */
|
73 | httpsAgent?: https.Agent;
|
74 | /**
|
75 | * An array of strings or Buffers of trusted certificates.
|
76 | * If this is omitted several well known "root" CAs will be used, like VeriSign.
|
77 | * These are used to authorize connections.
|
78 | * Notes: This is necessary only if the server uses the self - signed certificate
|
79 | */
|
80 | ca?: string | Buffer | string[] | Buffer[];
|
81 | /**
|
82 | * If true, the server certificate is verified against the list of supplied CAs.
|
83 | * An 'error' event is emitted if verification fails.Default: true.
|
84 | */
|
85 | rejectUnauthorized?: boolean;
|
86 | /** A string or Buffer containing the private key, certificate and CA certs of the server in PFX or PKCS12 format. */
|
87 | pfx?: string | Buffer;
|
88 | /**
|
89 | * A string or Buffer containing the private key of the client in PEM format.
|
90 | * Notes: This is necessary only if using the client certificate authentication
|
91 | */
|
92 | key?: string | Buffer;
|
93 | /**
|
94 | * A string or Buffer containing the certificate key of the client in PEM format.
|
95 | * Notes: This is necessary only if using the client certificate authentication
|
96 | */
|
97 | cert?: string | Buffer;
|
98 | /** A string of passphrase for the private key or pfx. */
|
99 | passphrase?: string;
|
100 | /** A string describing the ciphers to use or exclude. */
|
101 | ciphers?: string;
|
102 | /** The SSL method to use, e.g.SSLv3_method to force SSL version 3. */
|
103 | secureProtocol?: string;
|
104 | /** follow HTTP 3xx responses as redirects. defaults to false. */
|
105 | followRedirect?: boolean;
|
106 | /** The maximum number of redirects to follow, defaults to 10. */
|
107 | maxRedirects?: number;
|
108 | /** Format the redirect url by your self. Default is url.resolve(from, to). */
|
109 | formatRedirectUrl?: (a: any, b: any) => void;
|
110 | /** Before request hook, you can change every thing here. */
|
111 | beforeRequest?: (...args: any[]) => void;
|
112 | /** let you get the res object when request connected, default false. alias customResponse */
|
113 | streaming?: boolean;
|
114 | /** Accept gzip response content and auto decode it, default is false. */
|
115 | gzip?: boolean;
|
116 | /** Enable timing or not, default is false. */
|
117 | timing?: boolean;
|
118 | /** Enable proxy request, default is false. */
|
119 | enableProxy?: boolean;
|
120 | /** proxy agent uri or options, default is null. */
|
121 | proxy?: string | { [key: string]: any };
|
122 | /**
|
123 | * Custom DNS lookup function, default is dns.lookup.
|
124 | * Require node >= 4.0.0(for http protocol) and node >=8(for https protocol)
|
125 | */
|
126 | lookup?: LookupFunction;
|
127 | /**
|
128 | * check request address to protect from SSRF and similar attacks.
|
129 | * It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
|
130 | * It rely on lookup and have the same version requirement.
|
131 | */
|
132 | checkAddress?: (ip: string, family: number | string) => boolean;
|
133 | /**
|
134 | * UNIX domain socket path. (Windows is not supported)
|
135 | */
|
136 | socketPath?: string;
|
137 | }
|
138 |
|
139 | export interface HttpClientResponse<T> {
|
140 | data: T;
|
141 | status: number;
|
142 | headers: OutgoingHttpHeaders;
|
143 | res: http.IncomingMessage;
|
144 | }
|
145 |
|
146 |
|
147 | /**
|
148 | * @param data Outgoing message
|
149 | * @param res http response
|
150 | */
|
151 | export type Callback<T> = (err: Error, data: T, res: http.IncomingMessage) => void;
|
152 |
|
153 | /**
|
154 | * Handle all http request, both http and https support well.
|
155 | *
|
156 | * @example
|
157 | * // GET http://httptest.cnodejs.net
|
158 | * urllib.request('http://httptest.cnodejs.net/test/get', function(err, data, res) {});
|
159 | * // POST http://httptest.cnodejs.net
|
160 | * var args = { type: 'post', data: { foo: 'bar' } };
|
161 | * urllib.request('http://httptest.cnodejs.net/test/post', args, function(err, data, res) {});
|
162 | *
|
163 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
164 | */
|
165 | export function request<T = any>(url: string | url.URL, options?: RequestOptions): Promise<HttpClientResponse<T>>;
|
166 | /**
|
167 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
168 | */
|
169 | export function request<T = any>(url: string | url.URL, callback: Callback<T>): void;
|
170 | /**
|
171 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
172 | */
|
173 | export function request<T = any>(url: string | url.URL, options: RequestOptions, callback: Callback<T>): void;
|
174 |
|
175 | /**
|
176 | * Handle request with a callback.
|
177 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
178 | */
|
179 | export function requestWithCallback<T = any>(url: string | url.URL, callback: Callback<T>): void;
|
180 | /**
|
181 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
182 | */
|
183 | export function requestWithCallback<T = any>(url: string | url.URL, options: RequestOptions, callback: Callback<T>): void;
|
184 |
|
185 | /**
|
186 | * yield urllib.requestThunk(url, args)
|
187 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
188 | */
|
189 | export function requestThunk<T = any>(url: string | url.URL, options?: RequestOptions): (callback: Callback<T>) => void;
|
190 |
|
191 | /**
|
192 | * alias to request.
|
193 | * Handle all http request, both http and https support well.
|
194 | *
|
195 | * @example
|
196 | * // GET http://httptest.cnodejs.net
|
197 | * urllib.request('http://httptest.cnodejs.net/test/get', function(err, data, res) {});
|
198 | * // POST http://httptest.cnodejs.net
|
199 | * var args = { type: 'post', data: { foo: 'bar' } };
|
200 | * urllib.request('http://httptest.cnodejs.net/test/post', args, function(err, data, res) {});
|
201 | *
|
202 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
203 | * @param options Optional, @see RequestOptions.
|
204 | */
|
205 | export function curl<T = any>(url: string | url.URL, options?: RequestOptions): Promise<HttpClientResponse<T>>;
|
206 | /**
|
207 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
208 | */
|
209 | export function curl<T = any>(url: string | url.URL, callback: Callback<T>): void;
|
210 | /**
|
211 | * @param url The URL to request, either a String or a Object that return by url.parse.
|
212 | */
|
213 | export function curl<T = any>(url: string | url.URL, options: RequestOptions, callback: Callback<T>): void;
|
214 | /**
|
215 | * The default request timeout(in milliseconds).
|
216 | */
|
217 | export const TIMEOUT: number;
|
218 | /**
|
219 | * The default request & response timeout(in milliseconds).
|
220 | */
|
221 | export const TIMEOUTS: [number, number];
|
222 |
|
223 | /**
|
224 | * Request user agent.
|
225 | */
|
226 | export const USER_AGENT: string;
|
227 |
|
228 | /**
|
229 | * Request http agent.
|
230 | */
|
231 | export const agent: http.Agent;
|
232 |
|
233 | /**
|
234 | * Request https agent.
|
235 | */
|
236 | export const httpsAgent: https.Agent;
|
237 |
|
238 | export class HttpClient<O extends RequestOptions = RequestOptions> extends EventEmitter {
|
239 | request<T = any>(url: string | url.URL): Promise<HttpClientResponse<T>>;
|
240 | request<T = any>(url: string | url.URL, options: O): Promise<HttpClientResponse<T>>;
|
241 | request<T = any>(url: string | url.URL, callback: Callback<T>): void;
|
242 | request<T = any>(url: string | url.URL, options: O, callback: Callback<T>): void;
|
243 |
|
244 | curl<T = any>(url: string | url.URL, options: O): Promise<HttpClientResponse<T>>;
|
245 | curl<T = any>(url: string | url.URL): Promise<HttpClientResponse<T>>;
|
246 | curl<T = any>(url: string | url.URL, callback: Callback<T>): void;
|
247 | curl<T = any>(url: string | url.URL, options: O, callback: Callback<T>): void;
|
248 |
|
249 | requestThunk(url: string | url.URL, options?: O): (callback: (...args: any[]) => void) => void;
|
250 | }
|
251 |
|
252 | export interface RequestOptions2 extends RequestOptions {
|
253 | retry?: number;
|
254 | retryDelay?: number;
|
255 | isRetry?: (res: HttpClientResponse<any>) => boolean;
|
256 | }
|
257 |
|
258 | /**
|
259 | * request method only return a promise,
|
260 | * compatible with async/await and generator in co.
|
261 | */
|
262 | export class HttpClient2 extends HttpClient<RequestOptions2> {}
|
263 |
|
264 | /**
|
265 | * Create a HttpClient instance.
|
266 | */
|
267 | export function create(options?: RequestOptions): HttpClient;
|