UNPKG

6.92 kBTypeScriptView Raw
1/**
2 * This file is part of the @egodigital/egoose distribution.
3 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
4 *
5 * @egodigital/egoose is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation, version 3.
8 *
9 * @egodigital/egoose is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17/// <reference types="node" />
18import * as HTTP from 'http';
19import { Readable, Writable } from 'stream';
20import * as url from 'url';
21/**
22 * Possible input values for a request body.
23 */
24export declare type HttpRequestBody = string | Buffer | HttpRequestBodyProvider | Readable;
25/**
26 * Possible values for a request body.
27 */
28export declare type HttpRequestBodyValue = string | Buffer | Readable;
29/**
30 * A function, which provides a request body value.
31 *
32 * @return {HttpRequestBodyValue|Promise<HttpRequestBodyValue>} The result with the body to send.
33 */
34export declare type HttpRequestBodyProvider = () => HttpRequestBodyValue | Promise<HttpRequestBodyValue>;
35/**
36 * Options for a HTTP request.
37 */
38export interface HttpRequestOptions {
39 /**
40 * Do not normalize the names of HTTP request headers.
41 */
42 doNotNormalizeHeaders?: boolean;
43 /**
44 * The custom headers to send.
45 */
46 headers?: any;
47 /**
48 * The path to the (UNIX) socket.
49 */
50 socket?: string;
51 /**
52 * Custom request timeout.
53 */
54 timeout?: number;
55}
56/**
57 * Options for a HTTP request with a body.
58 */
59export interface HttpRequestOptionsWithBody extends HttpRequestOptions {
60 /**
61 * The body to send.
62 */
63 body?: HttpRequestBody;
64 /**
65 * The custom string encoding for the input body to use.
66 */
67 encoding?: string;
68}
69/**
70 * A possible value for a request URI.
71 */
72export declare type HttpRequestUrl = string | url.Url;
73/**
74 * A response of a HTTP request.
75 */
76export interface HttpResponse {
77 /**
78 * The status code.
79 */
80 code: number;
81 /**
82 * The response headers.
83 */
84 headers: HTTP.IncomingHttpHeaders;
85 /**
86 * Pipes the response body to a target.
87 *
88 * @param {Writable} target The target stream.
89 *
90 * @return this
91 */
92 pipe(target: Writable): Writable;
93 /**
94 * Reads the response body.
95 *
96 * @return {Promise<Buffer>} The promise with the data.
97 */
98 readBody(): Promise<Buffer>;
99 /**
100 * Reads the body and handles it as JSON object.
101 *
102 * @param {string} [enc] The custom string encoding to use.
103 *
104 * @return {Promise<T>} The promise with the parsed JSON object.
105 */
106 readJSON<T = any>(enc?: string): Promise<T>;
107 /**
108 * Reads the body and handles it as string.
109 *
110 * @param {string} [enc] The custom string encoding to use.
111 *
112 * @return {Promise<string>} The promise with response body as string.
113 */
114 readString(enc?: string): Promise<string>;
115 /**
116 * The request context.
117 */
118 request: HTTP.ClientRequest;
119 /**
120 * The response context.
121 */
122 response: any;
123 /**
124 * The status message.
125 */
126 status: string;
127}
128/**
129 * Does a HTTP 'CONNECT' request.
130 *
131 * @param {HttpRequestUrl} u The URL to call.
132 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
133 *
134 * @return {Promise<HttpResponse>} The promise with the response.
135 */
136export declare function CONNECT(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
137/**
138 * Does a HTTP 'DELETE' request.
139 *
140 * @param {HttpRequestUrl} u The URL to call.
141 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
142 *
143 * @return {Promise<HttpResponse>} The promise with the response.
144 */
145export declare function DELETE(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
146/**
147 * Does a HTTP 'GET' request.
148 *
149 * @param {HttpRequestUrl} u The URL to call.
150 * @param {HttpRequestOptions} [opts] Options for the request.
151 *
152 * @return {Promise<HttpResponse>} The promise with the response.
153 */
154export declare function GET(u: HttpRequestUrl, opts?: HttpRequestOptions): Promise<HttpResponse>;
155/**
156 * Does a HTTP 'HEAD' request.
157 *
158 * @param {HttpRequestUrl} u The URL to call.
159 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
160 *
161 * @return {Promise<HttpResponse>} The promise with the response.
162 */
163export declare function HEAD(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
164/**
165 * Does a HTTP 'OPTIONS' request.
166 *
167 * @param {HttpRequestUrl} u The URL to call.
168 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
169 *
170 * @return {Promise<HttpResponse>} The promise with the response.
171 */
172export declare function OPTIONS(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
173/**
174 * Does a HTTP 'PATCH' request.
175 *
176 * @param {HttpRequestUrl} u The URL to call.
177 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
178 *
179 * @return {Promise<HttpResponse>} The promise with the response.
180 */
181export declare function PATCH(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
182/**
183 * Does a HTTP 'POST' request.
184 *
185 * @param {HttpRequestUrl} u The URL to call.
186 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
187 *
188 * @return {Promise<HttpResponse>} The promise with the response.
189 */
190export declare function POST(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
191/**
192 * Does a HTTP 'PUT' request.
193 *
194 * @param {HttpRequestUrl} u The URL to call.
195 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
196 *
197 * @return {Promise<HttpResponse>} The promise with the response.
198 */
199export declare function PUT(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
200/**
201 * Does a HTTP 'GET' request.
202 *
203 * @param {string} method The method.
204 * @param {HttpRequestUrl} u The URL to call.
205 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
206 *
207 * @return {Promise<HttpResponse>} The promise with the response.
208 */
209export declare function request(method: string, u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;
210/**
211 * Does a HTTP 'TRACE' request.
212 *
213 * @param {HttpRequestUrl} u The URL to call.
214 * @param {HttpRequestOptionsWithBody} [opts] Options for the request.
215 *
216 * @return {Promise<HttpResponse>} The promise with the response.
217 */
218export declare function TRACE(u: HttpRequestUrl, opts?: HttpRequestOptionsWithBody): Promise<HttpResponse>;