index.d.ts 11 KB

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