2021-11-26 20:57:51 +08:00
|
|
|
/// <reference types="node" />
|
2022-06-26 21:02:18 +08:00
|
|
|
import * as http from 'http';
|
|
|
|
import * as ifm from './interfaces';
|
2021-11-26 20:57:51 +08:00
|
|
|
export declare enum HttpCodes {
|
|
|
|
OK = 200,
|
|
|
|
MultipleChoices = 300,
|
|
|
|
MovedPermanently = 301,
|
|
|
|
ResourceMoved = 302,
|
|
|
|
SeeOther = 303,
|
|
|
|
NotModified = 304,
|
|
|
|
UseProxy = 305,
|
|
|
|
SwitchProxy = 306,
|
|
|
|
TemporaryRedirect = 307,
|
|
|
|
PermanentRedirect = 308,
|
|
|
|
BadRequest = 400,
|
|
|
|
Unauthorized = 401,
|
|
|
|
PaymentRequired = 402,
|
|
|
|
Forbidden = 403,
|
|
|
|
NotFound = 404,
|
|
|
|
MethodNotAllowed = 405,
|
|
|
|
NotAcceptable = 406,
|
|
|
|
ProxyAuthenticationRequired = 407,
|
|
|
|
RequestTimeout = 408,
|
|
|
|
Conflict = 409,
|
|
|
|
Gone = 410,
|
|
|
|
TooManyRequests = 429,
|
|
|
|
InternalServerError = 500,
|
|
|
|
NotImplemented = 501,
|
|
|
|
BadGateway = 502,
|
|
|
|
ServiceUnavailable = 503,
|
|
|
|
GatewayTimeout = 504
|
|
|
|
}
|
|
|
|
export declare enum Headers {
|
|
|
|
Accept = "accept",
|
|
|
|
ContentType = "content-type"
|
|
|
|
}
|
|
|
|
export declare enum MediaTypes {
|
|
|
|
ApplicationJson = "application/json"
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the proxy URL, depending upon the supplied url and proxy environment variables.
|
|
|
|
* @param serverUrl The server URL where the request will be sent. For example, https://api.github.com
|
|
|
|
*/
|
|
|
|
export declare function getProxyUrl(serverUrl: string): string;
|
|
|
|
export declare class HttpClientError extends Error {
|
|
|
|
constructor(message: string, statusCode: number);
|
|
|
|
statusCode: number;
|
|
|
|
result?: any;
|
|
|
|
}
|
2022-06-26 21:02:18 +08:00
|
|
|
export declare class HttpClientResponse {
|
2021-11-26 20:57:51 +08:00
|
|
|
constructor(message: http.IncomingMessage);
|
|
|
|
message: http.IncomingMessage;
|
|
|
|
readBody(): Promise<string>;
|
|
|
|
}
|
|
|
|
export declare function isHttps(requestUrl: string): boolean;
|
|
|
|
export declare class HttpClient {
|
|
|
|
userAgent: string | undefined;
|
2022-06-26 21:02:18 +08:00
|
|
|
handlers: ifm.RequestHandler[];
|
|
|
|
requestOptions: ifm.RequestOptions | undefined;
|
2021-11-26 20:57:51 +08:00
|
|
|
private _ignoreSslError;
|
|
|
|
private _socketTimeout;
|
|
|
|
private _allowRedirects;
|
|
|
|
private _allowRedirectDowngrade;
|
|
|
|
private _maxRedirects;
|
|
|
|
private _allowRetries;
|
|
|
|
private _maxRetries;
|
|
|
|
private _agent;
|
|
|
|
private _proxyAgent;
|
|
|
|
private _keepAlive;
|
|
|
|
private _disposed;
|
2022-06-26 21:02:18 +08:00
|
|
|
constructor(userAgent?: string, handlers?: ifm.RequestHandler[], requestOptions?: ifm.RequestOptions);
|
|
|
|
options(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
get(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
del(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
post(requestUrl: string, data: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
patch(requestUrl: string, data: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
put(requestUrl: string, data: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
head(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
|
|
|
sendStream(verb: string, requestUrl: string, stream: NodeJS.ReadableStream, additionalHeaders?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
2021-11-26 20:57:51 +08:00
|
|
|
/**
|
|
|
|
* Gets a typed object from an endpoint
|
|
|
|
* Be aware that not found returns a null. Other errors (4xx, 5xx) reject the promise
|
|
|
|
*/
|
2022-06-26 21:02:18 +08:00
|
|
|
getJson<T>(requestUrl: string, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
|
|
|
|
postJson<T>(requestUrl: string, obj: any, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
|
|
|
|
putJson<T>(requestUrl: string, obj: any, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
|
|
|
|
patchJson<T>(requestUrl: string, obj: any, additionalHeaders?: http.OutgoingHttpHeaders): Promise<ifm.TypedResponse<T>>;
|
2021-11-26 20:57:51 +08:00
|
|
|
/**
|
|
|
|
* Makes a raw http request.
|
|
|
|
* All other methods such as get, post, patch, and request ultimately call this.
|
|
|
|
* Prefer get, del, post and patch
|
|
|
|
*/
|
2022-06-26 21:02:18 +08:00
|
|
|
request(verb: string, requestUrl: string, data: string | NodeJS.ReadableStream | null, headers?: http.OutgoingHttpHeaders): Promise<HttpClientResponse>;
|
2021-11-26 20:57:51 +08:00
|
|
|
/**
|
|
|
|
* Needs to be called if keepAlive is set to true in request options.
|
|
|
|
*/
|
|
|
|
dispose(): void;
|
|
|
|
/**
|
|
|
|
* Raw request.
|
|
|
|
* @param info
|
|
|
|
* @param data
|
|
|
|
*/
|
2022-06-26 21:02:18 +08:00
|
|
|
requestRaw(info: ifm.RequestInfo, data: string | NodeJS.ReadableStream | null): Promise<HttpClientResponse>;
|
2021-11-26 20:57:51 +08:00
|
|
|
/**
|
|
|
|
* Raw request with callback.
|
|
|
|
* @param info
|
|
|
|
* @param data
|
|
|
|
* @param onResult
|
|
|
|
*/
|
2022-06-26 21:02:18 +08:00
|
|
|
requestRawWithCallback(info: ifm.RequestInfo, data: string | NodeJS.ReadableStream | null, onResult: (err?: Error, res?: HttpClientResponse) => void): void;
|
2021-11-26 20:57:51 +08:00
|
|
|
/**
|
|
|
|
* Gets an http agent. This function is useful when you need an http agent that handles
|
|
|
|
* routing through a proxy server - depending upon the url and proxy environment variables.
|
|
|
|
* @param serverUrl The server URL where the request will be sent. For example, https://api.github.com
|
|
|
|
*/
|
|
|
|
getAgent(serverUrl: string): http.Agent;
|
|
|
|
private _prepareRequest;
|
|
|
|
private _mergeHeaders;
|
|
|
|
private _getExistingOrDefaultHeader;
|
|
|
|
private _getAgent;
|
|
|
|
private _performExponentialBackoff;
|
|
|
|
private _processResponse;
|
|
|
|
}
|