UNPKG

3.64 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 Client
10 */
11
12import { GRAPH_API_VERSION, GRAPH_BASE_URL } from "./Constants";
13import { CustomAuthenticationProvider } from "./CustomAuthenticationProvider";
14import { GraphRequest } from "./GraphRequest";
15import { HTTPClient } from "./HTTPClient";
16import { HTTPClientFactory } from "./HTTPClientFactory";
17import { ClientOptions } from "./IClientOptions";
18import { Options } from "./IOptions";
19import { validatePolyFilling } from "./ValidatePolyFilling";
20
21export class Client {
22 /**
23 * @private
24 * A member which stores the Client instance options
25 */
26 private config: ClientOptions = {
27 baseUrl: GRAPH_BASE_URL,
28 debugLogging: false,
29 defaultVersion: GRAPH_API_VERSION,
30 };
31
32 /**
33 * @private
34 * A member which holds the HTTPClient instance
35 */
36 private httpClient: HTTPClient;
37
38 /**
39 * @public
40 * @static
41 * To create a client instance with options and initializes the default middleware chain
42 * @param {Options} options - The options for client instance
43 * @returns The Client instance
44 */
45 public static init(options: Options): Client {
46 const clientOptions: ClientOptions = {};
47 for (const i in options) {
48 if (Object.prototype.hasOwnProperty.call(options, i)) {
49 clientOptions[i] = i === "authProvider" ? new CustomAuthenticationProvider(options[i]) : options[i];
50 }
51 }
52 return Client.initWithMiddleware(clientOptions);
53 }
54
55 /**
56 * @public
57 * @static
58 * To create a client instance with the Client Options
59 * @param {ClientOptions} clientOptions - The options object for initializing the client
60 * @returns The Client instance
61 */
62 public static initWithMiddleware(clientOptions: ClientOptions): Client {
63 return new Client(clientOptions);
64 }
65
66 /**
67 * @private
68 * @constructor
69 * Creates an instance of Client
70 * @param {ClientOptions} clientOptions - The options to instantiate the client object
71 */
72 private constructor(clientOptions: ClientOptions) {
73 validatePolyFilling();
74 for (const key in clientOptions) {
75 if (Object.prototype.hasOwnProperty.call(clientOptions, key)) {
76 this.config[key] = clientOptions[key];
77 }
78 }
79 let httpClient: HTTPClient;
80 if (clientOptions.authProvider !== undefined && clientOptions.middleware !== undefined) {
81 const error = new Error();
82 error.name = "AmbiguityInInitialization";
83 error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain not both";
84 throw error;
85 } else if (clientOptions.authProvider !== undefined) {
86 httpClient = HTTPClientFactory.createWithAuthenticationProvider(clientOptions.authProvider);
87 } else if (clientOptions.middleware !== undefined) {
88 httpClient = new HTTPClient(...[].concat(clientOptions.middleware));
89 } else {
90 const error = new Error();
91 error.name = "InvalidMiddlewareChain";
92 error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain";
93 throw error;
94 }
95 this.httpClient = httpClient;
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 public api(path: string): GraphRequest {
105 return new GraphRequest(this.httpClient, this.config, path);
106 }
107}