UNPKG

2.29 kBPlain TextView Raw
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 MiddlewareFactory
10 */
11
12import { AuthenticationProvider } from "../IAuthenticationProvider";
13import { AuthenticationHandler } from "./AuthenticationHandler";
14import { HTTPMessageHandler } from "./HTTPMessageHandler";
15import { Middleware } from "./IMiddleware";
16import { RedirectHandlerOptions } from "./options/RedirectHandlerOptions";
17import { RetryHandlerOptions } from "./options/RetryHandlerOptions";
18import { RedirectHandler } from "./RedirectHandler";
19import { RetryHandler } from "./RetryHandler";
20import { TelemetryHandler } from "./TelemetryHandler";
21
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 */
27const isNodeEnvironment = (): boolean => {
28 return typeof process === "object" && typeof require === "function";
29};
30
31/**
32 * @class
33 * Class containing function(s) related to the middleware pipelines.
34 */
35export class MiddlewareFactory {
36 /**
37 * @public
38 * @static
39 * Returns the default middleware chain an array with the middleware handlers
40 * @param {AuthenticationProvider} authProvider - The authentication provider instance
41 * @returns an array of the middleware handlers of the default middleware chain
42 */
43 public static getDefaultMiddlewareChain(authProvider: AuthenticationProvider): Middleware[] {
44 const middleware: Middleware[] = [];
45 const authenticationHandler = new AuthenticationHandler(authProvider);
46 const retryHandler = new RetryHandler(new RetryHandlerOptions());
47 const telemetryHandler = new TelemetryHandler();
48 const httpMessageHandler = new HTTPMessageHandler();
49
50 middleware.push(authenticationHandler);
51 middleware.push(retryHandler);
52 if (isNodeEnvironment()) {
53 const redirectHandler = new RedirectHandler(new RedirectHandlerOptions());
54 middleware.push(redirectHandler);
55 }
56 middleware.push(telemetryHandler);
57 middleware.push(httpMessageHandler);
58
59 return middleware;
60 }
61}