forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.ts
115 lines (107 loc) · 3.6 KB
/
Client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module Client
*/
import { GRAPH_API_VERSION, GRAPH_BASE_URL } from "./Constants";
import { CustomAuthenticationProvider } from "./CustomAuthenticationProvider";
import { GraphRequest } from "./GraphRequest";
import { HTTPClient } from "./HTTPClient";
import { HTTPClientFactory } from "./HTTPClientFactory";
import { ClientOptions } from "./IClientOptions";
import { Options } from "./IOptions";
import { validatePolyFilling } from "./ValidatePolyFilling";
export class Client {
/**
* @private
* A member which stores the Client instance options
*/
private config: ClientOptions = {
baseUrl: GRAPH_BASE_URL,
debugLogging: false,
defaultVersion: GRAPH_API_VERSION,
};
/**
* @private
* A member which holds the HTTPClient instance
*/
private httpClient: HTTPClient;
/**
* @public
* @static
* To create a client instance with options and initializes the default middleware chain
* @param {Options} options - The options for client instance
* @returns The Client instance
*/
public static init(options: Options): Client {
const clientOptions: ClientOptions = {};
for (const i in options) {
if (options.hasOwnProperty(i)) {
clientOptions[i] = i === "authProvider" ? new CustomAuthenticationProvider(options[i]) : options[i];
}
}
return Client.initWithMiddleware(clientOptions);
}
/**
* @public
* @static
* To create a client instance with the Client Options
* @param {ClientOptions} clientOptions - The options object for initializing the client
* @returns The Client instance
*/
public static initWithMiddleware(clientOptions: ClientOptions): Client {
try {
return new Client(clientOptions);
} catch (error) {
throw error;
}
}
/**
* @private
* @constructor
* Creates an instance of Client
* @param {ClientOptions} clientOptions - The options to instantiate the client object
*/
private constructor(clientOptions: ClientOptions) {
try {
validatePolyFilling();
} catch (error) {
throw error;
}
for (const key in clientOptions) {
if (clientOptions.hasOwnProperty(key)) {
this.config[key] = clientOptions[key];
}
}
let httpClient: HTTPClient;
if (clientOptions.authProvider !== undefined && clientOptions.middleware !== undefined) {
const error = new Error();
error.name = "AmbiguityInInitialization";
error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain not both";
throw error;
} else if (clientOptions.authProvider !== undefined) {
httpClient = HTTPClientFactory.createWithAuthenticationProvider(clientOptions.authProvider);
} else if (clientOptions.middleware !== undefined) {
httpClient = new HTTPClient(...[].concat(clientOptions.middleware));
} else {
const error = new Error();
error.name = "InvalidMiddlewareChain";
error.message = "Unable to Create Client, Please provide either authentication provider for default middleware chain or custom middleware chain";
throw error;
}
this.httpClient = httpClient;
}
/**
* @public
* Entry point to make requests
* @param {string} path - The path string value
* @returns The graph request instance
*/
public api(path: string): GraphRequest {
return new GraphRequest(this.httpClient, this.config, path);
}
}