1 | ;
|
2 | /**
|
3 | * -------------------------------------------------------------------------------------------
|
4 | * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
5 | * See License in the project root for license information.
|
6 | * -------------------------------------------------------------------------------------------
|
7 | */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.HTTPClientFactory = void 0;
|
10 | var tslib_1 = require("tslib");
|
11 | /**
|
12 | * @module HTTPClientFactory
|
13 | */
|
14 | var HTTPClient_1 = require("./HTTPClient");
|
15 | var AuthenticationHandler_1 = require("./middleware/AuthenticationHandler");
|
16 | var HTTPMessageHandler_1 = require("./middleware/HTTPMessageHandler");
|
17 | var RedirectHandlerOptions_1 = require("./middleware/options/RedirectHandlerOptions");
|
18 | var RetryHandlerOptions_1 = require("./middleware/options/RetryHandlerOptions");
|
19 | var RedirectHandler_1 = require("./middleware/RedirectHandler");
|
20 | var RetryHandler_1 = require("./middleware/RetryHandler");
|
21 | var TelemetryHandler_1 = require("./middleware/TelemetryHandler");
|
22 | /**
|
23 | * @private
|
24 | * To check whether the environment is node or not
|
25 | * @returns A boolean representing the environment is node or not
|
26 | */
|
27 | var isNodeEnvironment = function () {
|
28 | return typeof process === "object" && typeof require === "function";
|
29 | };
|
30 | /**
|
31 | * @class
|
32 | * Class representing HTTPClientFactory
|
33 | */
|
34 | var HTTPClientFactory = /** @class */ (function () {
|
35 | function HTTPClientFactory() {
|
36 | }
|
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 | HTTPClientFactory.createWithAuthenticationProvider = function (authProvider) {
|
51 | var authenticationHandler = new AuthenticationHandler_1.AuthenticationHandler(authProvider);
|
52 | var retryHandler = new RetryHandler_1.RetryHandler(new RetryHandlerOptions_1.RetryHandlerOptions());
|
53 | var telemetryHandler = new TelemetryHandler_1.TelemetryHandler();
|
54 | var httpMessageHandler = new HTTPMessageHandler_1.HTTPMessageHandler();
|
55 | authenticationHandler.setNext(retryHandler);
|
56 | if (isNodeEnvironment()) {
|
57 | var redirectHandler = new RedirectHandler_1.RedirectHandler(new RedirectHandlerOptions_1.RedirectHandlerOptions());
|
58 | retryHandler.setNext(redirectHandler);
|
59 | redirectHandler.setNext(telemetryHandler);
|
60 | }
|
61 | else {
|
62 | retryHandler.setNext(telemetryHandler);
|
63 | }
|
64 | telemetryHandler.setNext(httpMessageHandler);
|
65 | return HTTPClientFactory.createWithMiddleware(authenticationHandler);
|
66 | };
|
67 | /**
|
68 | * @public
|
69 | * @static
|
70 | * Creates a middleware chain with the given one
|
71 | * @property {...Middleware} middleware - The first middleware of the middleware chain or a sequence of all the Middleware handlers
|
72 | * @returns A HTTPClient instance
|
73 | */
|
74 | HTTPClientFactory.createWithMiddleware = function () {
|
75 | var middleware = [];
|
76 | for (var _i = 0; _i < arguments.length; _i++) {
|
77 | middleware[_i] = arguments[_i];
|
78 | }
|
79 | // Middleware should not empty or undefined. This is check is present in the HTTPClient constructor.
|
80 | return new (HTTPClient_1.HTTPClient.bind.apply(HTTPClient_1.HTTPClient, tslib_1.__spreadArray([void 0], middleware, false)))();
|
81 | };
|
82 | return HTTPClientFactory;
|
83 | }());
|
84 | exports.HTTPClientFactory = HTTPClientFactory;
|
85 | //# sourceMappingURL=HTTPClientFactory.js.map |
\ | No newline at end of file |