/
httpClient.ts
99 lines (84 loc) · 2.4 KB
/
httpClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
DefaultRetryOptions,
RetryOptions,
} from "../../util/retry";
import { AxiosHttpClientFactory } from "./axiosHttpClient";
/**
* Available HTTP request methods to use with HttpClient.
*/
export enum HttpMethod {
Get = "GET",
Post = "POST",
Put = "PUT",
Delete = "DELETE",
Options = "OPTIONS",
Patch = "PATCH",
Head = "HEAD",
}
/**
* HTTP options to pass into HttpClient.exchange.
*/
export interface HttpClientOptions {
/** Optional HTTP request method; defaults to HtppMethod.Get */
method?: HttpMethod;
/** Optional HTTP headers to be included in the request */
headers?: { [name: string]: string };
/** Optional payload body to be sent */
body?: any;
/** Optional retry options */
retry?: RetryOptions;
/** Raw options to be passed to the underlying implementation; Please use with care */
options?: any;
}
/**
* HTTP response from HttpClient.exchange.
*/
export interface HttpResponse<T> {
/** HTTP status code */
status: number;
/** Optional HTTP headers to server returned */
headers?: { [name: string]: string };
/** Optional response body of type T */
body?: T;
}
/**
* HTTP request abstraction to be implemented by different frameworks and command line
* tools.
*/
export interface HttpClient {
/**
* Exchange the given HTTP request with the server at url.
* @param {string} url
* @param {HttpClientOptions} options
* @returns {Promise<HttpResponse<T>>}
*/
exchange<T>(url: string,
options?: HttpClientOptions): Promise<HttpResponse<T>>;
}
/**
* Factory to construct HttpClient instances.
*/
export interface HttpClientFactory {
/**
* Create a HttpClient for the given url.
* @param {string} url
* @returns {HttpClient}
*/
create(url?: string): HttpClient;
}
/**
* Default HTTP client options each implementation to should use.
* @type {{method: HttpMethod; headers: {}; retry: WrapOptions}}
*/
export const DefaultHttpClientOptions: HttpClientOptions = {
method: HttpMethod.Get,
headers: {},
retry: DefaultRetryOptions,
};
/**
* Default HttpClientFactory which gets registered in the automation-client if not a
* different HttpClientFactory implementation is configured.
* @see Configuration.http.client.factory
* @type {HttpClientFactory}
*/
export const defaultHttpClientFactory = () => new AxiosHttpClientFactory();