1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import { GRAPH_API_VERSION, GRAPH_BASE_URL } from "./Constants";
|
13 | import { CustomAuthenticationProvider } from "./CustomAuthenticationProvider";
|
14 | import { GraphRequest } from "./GraphRequest";
|
15 | import { HTTPClient } from "./HTTPClient";
|
16 | import { HTTPClientFactory } from "./HTTPClientFactory";
|
17 | import { ClientOptions } from "./IClientOptions";
|
18 | import { Options } from "./IOptions";
|
19 | import { validatePolyFilling } from "./ValidatePolyFilling";
|
20 |
|
21 | export class Client {
|
22 | |
23 |
|
24 |
|
25 |
|
26 | private config: ClientOptions = {
|
27 | baseUrl: GRAPH_BASE_URL,
|
28 | debugLogging: false,
|
29 | defaultVersion: GRAPH_API_VERSION,
|
30 | };
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 | private httpClient: HTTPClient;
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
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 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | public static initWithMiddleware(clientOptions: ClientOptions): Client {
|
63 | return new Client(clientOptions);
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
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 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | public api(path: string): GraphRequest {
|
105 | return new GraphRequest(this.httpClient, this.config, path);
|
106 | }
|
107 | }
|