UNPKG

4.21 kBJavaScriptView Raw
1"use strict";
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 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.Client = void 0;
10var tslib_1 = require("tslib");
11/**
12 * @module Client
13 */
14var Constants_1 = require("./Constants");
15var CustomAuthenticationProvider_1 = require("./CustomAuthenticationProvider");
16var GraphRequest_1 = require("./GraphRequest");
17var HTTPClient_1 = require("./HTTPClient");
18var HTTPClientFactory_1 = require("./HTTPClientFactory");
19var ValidatePolyFilling_1 = require("./ValidatePolyFilling");
20var Client = /** @class */ (function () {
21 /**
22 * @private
23 * @constructor
24 * Creates an instance of Client
25 * @param {ClientOptions} clientOptions - The options to instantiate the client object
26 */
27 function Client(clientOptions) {
28 /**
29 * @private
30 * A member which stores the Client instance options
31 */
32 this.config = {
33 baseUrl: Constants_1.GRAPH_BASE_URL,
34 debugLogging: false,
35 defaultVersion: Constants_1.GRAPH_API_VERSION,
36 };
37 (0, ValidatePolyFilling_1.validatePolyFilling)();
38 for (var key in clientOptions) {
39 if (Object.prototype.hasOwnProperty.call(clientOptions, key)) {
40 this.config[key] = clientOptions[key];
41 }
42 }
43 var httpClient;
44 if (clientOptions.authProvider !== undefined && clientOptions.middleware !== undefined) {
45 var error = new Error();
46 error.name = "AmbiguityInInitialization";
47 error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain not both";
48 throw error;
49 }
50 else if (clientOptions.authProvider !== undefined) {
51 httpClient = HTTPClientFactory_1.HTTPClientFactory.createWithAuthenticationProvider(clientOptions.authProvider);
52 }
53 else if (clientOptions.middleware !== undefined) {
54 httpClient = new (HTTPClient_1.HTTPClient.bind.apply(HTTPClient_1.HTTPClient, tslib_1.__spreadArray([void 0], [].concat(clientOptions.middleware), false)))();
55 }
56 else {
57 var error = new Error();
58 error.name = "InvalidMiddlewareChain";
59 error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain";
60 throw error;
61 }
62 this.httpClient = httpClient;
63 }
64 /**
65 * @public
66 * @static
67 * To create a client instance with options and initializes the default middleware chain
68 * @param {Options} options - The options for client instance
69 * @returns The Client instance
70 */
71 Client.init = function (options) {
72 var clientOptions = {};
73 for (var i in options) {
74 if (Object.prototype.hasOwnProperty.call(options, i)) {
75 clientOptions[i] = i === "authProvider" ? new CustomAuthenticationProvider_1.CustomAuthenticationProvider(options[i]) : options[i];
76 }
77 }
78 return Client.initWithMiddleware(clientOptions);
79 };
80 /**
81 * @public
82 * @static
83 * To create a client instance with the Client Options
84 * @param {ClientOptions} clientOptions - The options object for initializing the client
85 * @returns The Client instance
86 */
87 Client.initWithMiddleware = function (clientOptions) {
88 return new Client(clientOptions);
89 };
90 /**
91 * @public
92 * Entry point to make requests
93 * @param {string} path - The path string value
94 * @returns The graph request instance
95 */
96 Client.prototype.api = function (path) {
97 return new GraphRequest_1.GraphRequest(this.httpClient, this.config, path);
98 };
99 return Client;
100}());
101exports.Client = Client;
102//# sourceMappingURL=Client.js.map
\No newline at end of file