UNPKG

11.9 kBTypeScriptView Raw
1/**
2 * A class for configuring HttpClients.
3 */
4export declare class HttpClientConfiguration {
5 /**
6 * The base URL to be prepended to each Request's url before sending.
7 */
8 baseUrl: string;
9 /**
10 * Default values to apply to init objects when creating Requests. Note that
11 * defaults cannot be applied when Request objects are manually created because
12 * Request provides its own defaults and discards the original init object.
13 * See also https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
14 */
15 defaults: RequestInit;
16 /**
17 * Interceptors to be added to the HttpClient.
18 */
19 interceptors: Interceptor[];
20 /**
21 * Sets the baseUrl.
22 *
23 * @param baseUrl The base URL.
24 * @returns The chainable instance of this configuration object.
25 * @chainable
26 */
27 withBaseUrl(baseUrl: string): HttpClientConfiguration;
28 /**
29 * Sets the defaults.
30 *
31 * @param defaults The defaults.
32 * @returns The chainable instance of this configuration object.
33 * @chainable
34 */
35 withDefaults(defaults: RequestInit): HttpClientConfiguration;
36 /**
37 * Adds an interceptor to be run on all requests or responses.
38 *
39 * @param interceptor An object with request, requestError,
40 * response, or responseError methods. request and requestError act as
41 * resolve and reject handlers for the Request before it is sent.
42 * response and responseError act as resolve and reject handlers for
43 * the Response after it has been received.
44 * @returns The chainable instance of this configuration object.
45 * @chainable
46 */
47 withInterceptor(interceptor: Interceptor): HttpClientConfiguration;
48 /**
49 * Applies a configuration that addresses common application needs, including
50 * configuring same-origin credentials, and using rejectErrorResponses.
51 * @returns The chainable instance of this configuration object.
52 * @chainable
53 */
54 useStandardConfiguration(): HttpClientConfiguration;
55 /**
56 * Causes Responses whose status codes fall outside the range 200-299 to reject.
57 * The fetch API only rejects on network errors or other conditions that prevent
58 * the request from completing, meaning consumers must inspect Response.ok in the
59 * Promise continuation to determine if the server responded with a success code.
60 * This method adds a response interceptor that causes Responses with error codes
61 * to be rejected, which is common behavior in HTTP client libraries.
62 * @returns The chainable instance of this configuration object.
63 * @chainable
64 */
65 rejectErrorResponses(): HttpClientConfiguration;
66 withRetry(config?: RetryConfiguration): HttpClientConfiguration;
67}
68/**
69 * An HTTP client based on the Fetch API.
70 */
71export declare class HttpClient {
72 /**
73 * The current number of active requests.
74 * Requests being processed by interceptors are considered active.
75 */
76 activeRequestCount: number;
77 /**
78 * Indicates whether or not the client is currently making one or more requests.
79 */
80 isRequesting: boolean;
81 /**
82 * Indicates whether or not the client has been configured.
83 */
84 isConfigured: boolean;
85 /**
86 * The base URL set by the config.
87 */
88 baseUrl: string;
89 /**
90 * The default request init to merge with values specified at request time.
91 */
92 defaults: RequestInit;
93 /**
94 * The interceptors to be run during requests.
95 */
96 interceptors: Interceptor[];
97 /**
98 * Creates an instance of HttpClient.
99 */
100 constructor();
101 /**
102 * Configure this client with default settings to be used by all requests.
103 *
104 * @param config A configuration object, or a function that takes a config
105 * object and configures it.
106 * @returns The chainable instance of this HttpClient.
107 * @chainable
108 */
109 configure(config: RequestInit | ((config: HttpClientConfiguration) => void) | HttpClientConfiguration): HttpClient;
110 /**
111 * Starts the process of fetching a resource. Default configuration parameters
112 * will be applied to the Request. The constructed Request will be passed to
113 * registered request interceptors before being sent. The Response will be passed
114 * to registered Response interceptors before it is returned.
115 *
116 * See also https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
117 *
118 * @param input The resource that you wish to fetch. Either a
119 * Request object, or a string containing the URL of the resource.
120 * @param init An options object containing settings to be applied to
121 * the Request.
122 * @returns A Promise for the Response from the fetch request.
123 */
124 fetch(input: Request | string, init?: RequestInit): Promise<Response>;
125 buildRequest(input: string | Request, init: RequestInit): Request;
126 /**
127 * Calls fetch as a GET request.
128 *
129 * @param input The resource that you wish to fetch. Either a
130 * Request object, or a string containing the URL of the resource.
131 * @param init An options object containing settings to be applied to
132 * the Request.
133 * @returns A Promise for the Response from the fetch request.
134 */
135 get(input: Request | string, init?: RequestInit): Promise<Response>;
136 /**
137 * Calls fetch with request method set to POST.
138 *
139 * @param input The resource that you wish to fetch. Either a
140 * Request object, or a string containing the URL of the resource.
141 * @param body The body of the request.
142 * @param init An options object containing settings to be applied to
143 * the Request.
144 * @returns A Promise for the Response from the fetch request.
145 */
146 post(input: Request | string, body?: any, init?: RequestInit): Promise<Response>;
147 /**
148 * Calls fetch with request method set to PUT.
149 *
150 * @param input The resource that you wish to fetch. Either a
151 * Request object, or a string containing the URL of the resource.
152 * @param body The body of the request.
153 * @param init An options object containing settings to be applied to
154 * the Request.
155 * @returns A Promise for the Response from the fetch request.
156 */
157 put(input: Request | string, body?: any, init?: RequestInit): Promise<Response>;
158 /**
159 * Calls fetch with request method set to PATCH.
160 *
161 * @param input The resource that you wish to fetch. Either a
162 * Request object, or a string containing the URL of the resource.
163 * @param body The body of the request.
164 * @param init An options object containing settings to be applied to
165 * the Request.
166 * @returns A Promise for the Response from the fetch request.
167 */
168 patch(input: Request | string, body?: any, init?: RequestInit): Promise<Response>;
169 /**
170 * Calls fetch with request method set to DELETE.
171 *
172 * @param input The resource that you wish to fetch. Either a
173 * Request object, or a string containing the URL of the resource.
174 * @param body The body of the request.
175 * @param init An options object containing settings to be applied to
176 * the Request.
177 * @returns A Promise for the Response from the fetch request.
178 */
179 delete(input: Request | string, body?: any, init?: RequestInit): Promise<Response>;
180}
181/**
182 * Interceptors can process requests before they are sent, and responses
183 * before they are returned to callers.
184 */
185export interface Interceptor {
186 /**
187 * Called with the request before it is sent. Request interceptors can modify and
188 * return the request, or return a new one to be sent. If desired, the interceptor
189 * may return a Response in order to short-circuit the HTTP request itself.
190 *
191 * @param request The request to be sent.
192 * @returns The existing request, a new request or a response; or a Promise for any of these.
193 */
194 request?: (request: Request) => Request | Response | Promise<Request | Response>;
195 /**
196 * Handles errors generated by previous request interceptors. This function acts
197 * as a Promise rejection handler. It may rethrow the error to propagate the
198 * failure, or return a new Request or Response to recover.
199 *
200 * @param error The rejection value from the previous interceptor.
201 * @returns The existing request, a new request or a response; or a Promise for any of these.
202 */
203 requestError?: (error: any) => Request | Response | Promise<Request | Response>;
204 /**
205 * Called with the response after it is received. Response interceptors can modify
206 * and return the Response, or create a new one to be returned to the caller.
207 *
208 * @param response The response.
209 * @returns The response; or a Promise for one.
210 */
211 response?: (response: Response, request?: Request) => Response | Promise<Response>;
212 /**
213 * Handles fetch errors and errors generated by previous interceptors. This
214 * function acts as a Promise rejection handler. It may rethrow the error
215 * to propagate the failure, or return a new Response to recover.
216 *
217 * @param error The rejection value from the fetch request or from a
218 * previous interceptor.
219 * @returns The response; or a Promise for one.
220 */
221 responseError?: (error: any, request?: Request, httpClient?: HttpClient) => Response | Promise<Response>;
222}
223export declare type ValidInterceptorMethodName = keyof Interceptor;
224/**
225 * The init object used to initialize a fetch Request.
226 * See https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
227 */
228export interface RequestInit {
229 /**
230 * The request method, e.g., GET, POST.
231 */
232 method?: string;
233 /**
234 * Any headers you want to add to your request, contained within a Headers object or an object literal with ByteString values.
235 */
236 headers?: Headers | Object;
237 /**
238 * Any body that you want to add to your request: this can be a Blob, BufferSource, FormData,
239 * URLSearchParams, ReadableStream, or USVString object.
240 *
241 * Note that a request using the GET or HEAD method cannot have a body.
242 */
243 body?: Blob | BufferSource | FormData | URLSearchParams | ReadableStream | string | null;
244 /**
245 * The mode you want to use for the request, e.g., cors, no-cors, same-origin, or navigate.
246 * The default is cors.
247 *
248 * In Chrome the default is no-cors before Chrome 47 and same-origin starting with Chrome 47.
249 */
250 mode?: string;
251 /**
252 * The request credentials you want to use for the request: omit, same-origin, or include.
253 * The default is omit.
254 *
255 * In Chrome the default is same-origin before Chrome 47 and include starting with Chrome 47.
256 */
257 credentials?: string;
258 /**
259 * The cache mode you want to use for the request: default, no-store, reload, no-cache, or force-cache.
260 */
261 cache?: string;
262 /**
263 * The redirect mode to use: follow, error, or manual.
264 *
265 * In Chrome the default is follow before Chrome 47 and manual starting with Chrome 47.
266 */
267 redirect?: string;
268 /**
269 * A USVString specifying no-referrer, client, or a URL. The default is client.
270 */
271 referrer?: string;
272 /**
273 * Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
274 */
275 integrity?: string;
276 /**
277 * An AbortSignal to set request’s signal.
278 */
279 signal?: AbortSignal | null;
280}
281export interface RetryConfiguration {
282 maxRetries: number;
283 interval?: number;
284 strategy?: number | ((retryCount: number) => number);
285 minRandomInterval?: number;
286 maxRandomInterval?: number;
287 counter?: number;
288 requestClone?: Request;
289 doRetry?: (response: Response, request: Request) => boolean | Promise<boolean>;
290 beforeRetry?: (request: Request, client: HttpClient) => Request | Promise<Request>;
291}
292/**
293* Serialize an object to JSON. Useful for easily creating JSON fetch request bodies.
294*
295* @param body The object to be serialized to JSON.
296* @param replacer The JSON.stringify replacer used when serializing.
297* @returns A JSON string.
298*/
299export declare function json(body: any, replacer?: any): string;
300export declare const retryStrategy: {
301 fixed: 0;
302 incremental: 1;
303 exponential: 2;
304 random: 3;
305};
306export declare class RetryInterceptor implements Interceptor {
307 retryConfig: RetryConfiguration;
308 constructor(retryConfig?: RetryConfiguration);
309 request(request: Request): Request;
310 response(response: Response, request?: Request): Response;
311 responseError(error: Response, request?: Request, httpClient?: HttpClient): Promise<Response>;
312}
\No newline at end of file