UNPKG

4.52 kBTypeScriptView Raw
1/// <reference types="node" />
2import http = require('http');
3export declare type Protocol = 'https:' | 'http:';
4/**
5 * @typedef {Object} HTTPRequestOptions
6 * @property {Object.<string, string>} headers - request headers
7 * @property {string} method - request method (GET/POST/etc)
8 * @property {(string)} body - request body. Sets content-type to application/json and stringifies when object
9 * @property {(boolean)} partial - do not make continuous requests while receiving a Next-Range header for GET requests
10 * @property {(number)} port - port to use
11 */
12export declare type FullHTTPRequestOptions = http.ClientRequestArgs & {
13 raw?: boolean;
14 body?: any;
15 partial?: boolean;
16 headers: http.OutgoingHttpHeaders;
17};
18export declare type HTTPRequestOptions = Partial<FullHTTPRequestOptions>;
19/**
20 * Utility for simple HTTP calls
21 * @class
22 */
23export declare class HTTP<T> {
24 static defaults: HTTPRequestOptions;
25 static create(options?: HTTPRequestOptions): typeof HTTP;
26 /**
27 * make an http GET request
28 * @param {string} url - url or path to call
29 * @param {HTTPRequestOptions} options
30 * @returns {Promise}
31 * @example
32 * ```js
33 * const http = require('http-call')
34 * await http.get('https://google.com')
35 * ```
36 */
37 static get<T>(url: string, options?: HTTPRequestOptions): Promise<HTTP<T>>;
38 /**
39 * make an http POST request
40 * @param {string} url - url or path to call
41 * @param {HTTPRequestOptions} options
42 * @returns {Promise}
43 * @example
44 * ```js
45 * const http = require('http-call')
46 * await http.post('https://google.com')
47 * ```
48 */
49 static post<T>(url: string, options?: HTTPRequestOptions): Promise<HTTP<T>>;
50 /**
51 * make an http PUT request
52 * @param {string} url - url or path to call
53 * @param {HTTPRequestOptions} options
54 * @returns {Promise}
55 * @example
56 * ```js
57 * const http = require('http-call')
58 * await http.put('https://google.com')
59 * ```
60 */
61 static put<T>(url: string, options?: HTTPRequestOptions): Promise<HTTP<T>>;
62 /**
63 * make an http PATCH request
64 * @param {string} url - url or path to call
65 * @param {HTTPRequestOptions} options
66 * @returns {Promise}
67 * @example
68 * ```js
69 * const http = require('http-call')
70 * await http.patch('https://google.com')
71 * ```
72 */
73 static patch<T>(url: string, options?: HTTPRequestOptions): Promise<HTTP<T>>;
74 /**
75 * make an http DELETE request
76 * @param {string} url - url or path to call
77 * @param {HTTPRequestOptions} options
78 * @returns {Promise}
79 * @example
80 * ```js
81 * const http = require('http-call')
82 * await http.delete('https://google.com')
83 * ```
84 */
85 static delete<T>(url: string, options?: HTTPRequestOptions): Promise<HTTP<T>>;
86 /**
87 * make a streaming request
88 * @param {string} url - url or path to call
89 * @param {HTTPRequestOptions} options
90 * @returns {Promise}
91 * @example
92 * ```js
93 * const http = require('http-call')
94 * let {response} = await http.get('https://google.com')
95 * response.on('data', console.log)
96 * ```
97 */
98 static stream(url: string, options?: HTTPRequestOptions): Promise<HTTP<unknown>>;
99 static request<T>(url: string, options?: HTTPRequestOptions): Promise<HTTP<T>>;
100 response: http.IncomingMessage;
101 request: http.ClientRequest;
102 body: T;
103 options: FullHTTPRequestOptions;
104 private _redirectRetries;
105 private _errorRetries;
106 readonly method: string;
107 readonly statusCode: number;
108 readonly secure: boolean;
109 url: string;
110 readonly headers: http.IncomingMessage['headers'];
111 readonly partial: boolean;
112 readonly ctor: typeof HTTP;
113 constructor(url: string, options?: HTTPRequestOptions);
114 _request(): Promise<void>;
115 _redirect(): Promise<void>;
116 _maybeRetry(err: Error): Promise<void>;
117 private readonly _chalk;
118 private _renderStatus;
119 private _debugRequest;
120 private _debugResponse;
121 private _renderHeaders;
122 private _performRequest;
123 private _parse;
124 private _parseBody;
125 private _getNextRange;
126 private readonly _responseOK;
127 private readonly _responseRedirect;
128 private readonly _shouldParseResponseBody;
129 private _wait;
130}
131export default HTTP;
132export declare class HTTPError extends Error {
133 statusCode: number;
134 http: HTTP<any>;
135 body: any;
136 __httpcall: any;
137 constructor(http: HTTP<any>);
138}