1 | /**
|
2 | * -------------------------------------------------------------------------------------------
|
3 | * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
4 | * See License in the project root for license information.
|
5 | * -------------------------------------------------------------------------------------------
|
6 | */
|
7 |
|
8 | /**
|
9 | * @module HTTPClientFactory
|
10 | */
|
11 |
|
12 | import { HTTPClient } from "./HTTPClient";
|
13 | import { AuthenticationProvider } from "./IAuthenticationProvider";
|
14 | import { AuthenticationHandler } from "./middleware/AuthenticationHandler";
|
15 | import { HTTPMessageHandler } from "./middleware/HTTPMessageHandler";
|
16 | import { Middleware } from "./middleware/IMiddleware";
|
17 | import { RedirectHandlerOptions } from "./middleware/options/RedirectHandlerOptions";
|
18 | import { RetryHandlerOptions } from "./middleware/options/RetryHandlerOptions";
|
19 | import { RedirectHandler } from "./middleware/RedirectHandler";
|
20 | import { RetryHandler } from "./middleware/RetryHandler";
|
21 | import { TelemetryHandler } from "./middleware/TelemetryHandler";
|
22 |
|
23 | /**
|
24 | * @private
|
25 | * To check whether the environment is node or not
|
26 | * @returns A boolean representing the environment is node or not
|
27 | */
|
28 | const isNodeEnvironment = (): boolean => {
|
29 | return typeof process === "object" && typeof require === "function";
|
30 | };
|
31 |
|
32 | /**
|
33 | * @class
|
34 | * Class representing HTTPClientFactory
|
35 | */
|
36 | export class HTTPClientFactory {
|
37 | /**
|
38 | * @public
|
39 | * @static
|
40 | * Creates HTTPClient with default middleware chain
|
41 | * @param {AuthenticationProvider} authProvider - The authentication provider instance
|
42 | * @returns A HTTPClient instance
|
43 | *
|
44 | * NOTE: These are the things that we need to remember while doing modifications in the below default pipeline.
|
45 | * * HTTPMessageHandler should be the last one in the middleware pipeline, because this makes the actual network call of the request
|
46 | * * TelemetryHandler should be the one prior to the last middleware in the chain, because this is the one which actually collects and appends the usage flag and placing this handler * before making the actual network call ensures that the usage of all features are recorded in the flag.
|
47 | * * The best place for AuthenticationHandler is in the starting of the pipeline, because every other handler might have to work for multiple times for a request but the auth token for
|
48 | * them will remain same. For example, Retry and Redirect handlers might be working multiple times for a request based on the response but their auth token would remain same.
|
49 | */
|
50 | public static createWithAuthenticationProvider(authProvider: AuthenticationProvider): HTTPClient {
|
51 | const authenticationHandler = new AuthenticationHandler(authProvider);
|
52 | const retryHandler = new RetryHandler(new RetryHandlerOptions());
|
53 | const telemetryHandler = new TelemetryHandler();
|
54 | const httpMessageHandler = new HTTPMessageHandler();
|
55 |
|
56 | authenticationHandler.setNext(retryHandler);
|
57 | if (isNodeEnvironment()) {
|
58 | const redirectHandler = new RedirectHandler(new RedirectHandlerOptions());
|
59 | retryHandler.setNext(redirectHandler);
|
60 | redirectHandler.setNext(telemetryHandler);
|
61 | } else {
|
62 | retryHandler.setNext(telemetryHandler);
|
63 | }
|
64 | telemetryHandler.setNext(httpMessageHandler);
|
65 | return HTTPClientFactory.createWithMiddleware(authenticationHandler);
|
66 | }
|
67 |
|
68 | /**
|
69 | * @public
|
70 | * @static
|
71 | * Creates a middleware chain with the given one
|
72 | * @property {...Middleware} middleware - The first middleware of the middleware chain or a sequence of all the Middleware handlers
|
73 | * @returns A HTTPClient instance
|
74 | */
|
75 | public static createWithMiddleware(...middleware: Middleware[]): HTTPClient {
|
76 | // Middleware should not empty or undefined. This is check is present in the HTTPClient constructor.
|
77 | return new HTTPClient(...middleware);
|
78 | }
|
79 | }
|