UNPKG

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