-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathGraphRequest.ts
785 lines (724 loc) · 26.7 KB
/
GraphRequest.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module GraphRequest
*/
import { GraphClientError } from "./GraphClientError";
import { GraphError } from "./GraphError";
import { GraphErrorHandler } from "./GraphErrorHandler";
import { oDataQueryNames, serializeContent, urlJoin } from "./GraphRequestUtil";
import { GraphResponseHandler } from "./GraphResponseHandler";
import { HTTPClient } from "./HTTPClient";
import { ClientOptions } from "./IClientOptions";
import { Context } from "./IContext";
import { FetchOptions } from "./IFetchOptions";
import { GraphRequestCallback } from "./IGraphRequestCallback";
import { MiddlewareControl } from "./middleware/MiddlewareControl";
import { MiddlewareOptions } from "./middleware/options/IMiddlewareOptions";
import { RequestMethod } from "./RequestMethod";
import { ResponseType } from "./ResponseType";
/**
* @interface
* Signature to representing key value pairs
* @property {[key: string] : string | number} - The Key value pair
*/
interface KeyValuePairObjectStringNumber {
[key: string]: string | number;
}
/**
* @interface
* Signature to define URL components
* @template http://graph.microsoft.com/VERSION/PATH?QUERYSTRING&OTHER_QUERY_PARAMS
*
* @property {string} host - The host to which the request needs to be made
* @property {string} version - Version of the graph endpoint
* @property {string} [path] - The path of the resource request
* @property {KeyValuePairObjectStringNumber} oDataQueryParams - The oData Query Params
* @property {KeyValuePairObjectStringNumber} otherURLQueryParams - The other query params for a request
* @property {string[]} otherURLQueryOptions - The non key-value query parameters. Example- '/me?$whatif'
*/
export interface URLComponents {
host: string;
version: string;
path?: string;
oDataQueryParams: KeyValuePairObjectStringNumber;
otherURLQueryParams: KeyValuePairObjectStringNumber;
otherURLQueryOptions?: string[];
}
/**
* @class
* A Class representing GraphRequest
*/
export class GraphRequest {
/**
* @private
* A member variable to hold HTTPClient instance
*/
private httpClient: HTTPClient;
/**
* @private
* A member variable to hold client options
*/
private config: ClientOptions;
/**
* @private
* A member to hold URL Components data
*/
private urlComponents: URLComponents;
/**
* @private
* A member to hold custom header options for a request
*/
private _headers: HeadersInit;
/**
* @private
* A member to hold custom options for a request
*/
private _options: FetchOptions;
/**
* @private
* A member to hold the array of middleware options for a request
*/
private _middlewareOptions: MiddlewareOptions[];
/**
* @private
* A member to hold custom response type for a request
*/
private _responseType: ResponseType;
/**
* @public
* @constructor
* Creates an instance of GraphRequest
* @param {HTTPClient} httpClient - The HTTPClient instance
* @param {ClientOptions} config - The options for making request
* @param {string} path - A path string
*/
public constructor(httpClient: HTTPClient, config: ClientOptions, path: string) {
this.httpClient = httpClient;
this.config = config;
this.urlComponents = {
host: this.config.baseUrl,
version: this.config.defaultVersion,
oDataQueryParams: {},
otherURLQueryParams: {},
otherURLQueryOptions: [],
};
this._headers = {};
this._options = {};
this._middlewareOptions = [];
this.parsePath(path);
}
/**
* @private
* Parses the path string and creates URLComponents out of it
* @param {string} path - The request path string
* @returns Nothing
*/
private parsePath = (path: string): void => {
// Strips out the base of the url if they passed in
if (path.indexOf("https://") !== -1) {
path = path.replace("https://", "");
// Find where the host ends
const endOfHostStrPos = path.indexOf("/");
if (endOfHostStrPos !== -1) {
// Parse out the host
this.urlComponents.host = "https://" + path.substring(0, endOfHostStrPos);
// Strip the host from path
path = path.substring(endOfHostStrPos + 1, path.length);
}
// Remove the following version
const endOfVersionStrPos = path.indexOf("/");
if (endOfVersionStrPos !== -1) {
// Parse out the version
this.urlComponents.version = path.substring(0, endOfVersionStrPos);
// Strip version from path
path = path.substring(endOfVersionStrPos + 1, path.length);
}
}
// Strip out any leading "/"
if (path.charAt(0) === "/") {
path = path.substr(1);
}
const queryStrPos = path.indexOf("?");
if (queryStrPos === -1) {
// No query string
this.urlComponents.path = path;
} else {
this.urlComponents.path = path.substr(0, queryStrPos);
// Capture query string into oDataQueryParams and otherURLQueryParams
const queryParams = path.substring(queryStrPos + 1, path.length).split("&");
for (const queryParam of queryParams) {
this.parseQueryParameter(queryParam);
}
}
};
/**
* @private
* Adds the query parameter as comma separated values
* @param {string} propertyName - The name of a property
* @param {string|string[]} propertyValue - The vale of a property
* @param {IArguments} additionalProperties - The additional properties
* @returns Nothing
*/
private addCsvQueryParameter(propertyName: string, propertyValue: string | string[], additionalProperties: IArguments): void {
// If there are already $propertyName value there, append a ","
this.urlComponents.oDataQueryParams[propertyName] = this.urlComponents.oDataQueryParams[propertyName] ? this.urlComponents.oDataQueryParams[propertyName] + "," : "";
let allValues: string[] = [];
if (additionalProperties.length > 1 && typeof propertyValue === "string") {
allValues = Array.prototype.slice.call(additionalProperties);
} else if (typeof propertyValue === "string") {
allValues.push(propertyValue);
} else {
allValues = allValues.concat(propertyValue);
}
this.urlComponents.oDataQueryParams[propertyName] += allValues.join(",");
}
/**
* @private
* Builds the full url from the URLComponents to make a request
* @returns The URL string that is qualified to make a request to graph endpoint
*/
private buildFullUrl(): string {
const url = urlJoin([this.urlComponents.host, this.urlComponents.version, this.urlComponents.path]) + this.createQueryString();
if (this.config.debugLogging) {
console.log(url);
}
return url;
}
/**
* @private
* Builds the query string from the URLComponents
* @returns The Constructed query string
*/
private createQueryString(): string {
// Combining query params from oDataQueryParams and otherURLQueryParams
const urlComponents = this.urlComponents;
const query: string[] = [];
if (Object.keys(urlComponents.oDataQueryParams).length !== 0) {
for (const property in urlComponents.oDataQueryParams) {
if (Object.prototype.hasOwnProperty.call(urlComponents.oDataQueryParams, property)) {
query.push(property + "=" + urlComponents.oDataQueryParams[property]);
}
}
}
if (Object.keys(urlComponents.otherURLQueryParams).length !== 0) {
for (const property in urlComponents.otherURLQueryParams) {
if (Object.prototype.hasOwnProperty.call(urlComponents.otherURLQueryParams, property)) {
query.push(property + "=" + urlComponents.otherURLQueryParams[property]);
}
}
}
if (urlComponents.otherURLQueryOptions.length !== 0) {
for (const str of urlComponents.otherURLQueryOptions) {
query.push(str);
}
}
return query.length > 0 ? "?" + query.join("&") : "";
}
/**
* @private
* Parses the query parameters to set the urlComponents property of the GraphRequest object
* @param {string|KeyValuePairObjectStringNumber} queryDictionaryOrString - The query parameter
* @returns The same GraphRequest instance that is being called with
*/
private parseQueryParameter(queryDictionaryOrString: string | KeyValuePairObjectStringNumber): GraphRequest {
if (typeof queryDictionaryOrString === "string") {
if (queryDictionaryOrString.charAt(0) === "?") {
queryDictionaryOrString = queryDictionaryOrString.substring(1);
}
if (queryDictionaryOrString.indexOf("&") !== -1) {
const queryParams = queryDictionaryOrString.split("&");
for (const str of queryParams) {
this.parseQueryParamenterString(str);
}
} else {
this.parseQueryParamenterString(queryDictionaryOrString);
}
} else if (queryDictionaryOrString.constructor === Object) {
for (const key in queryDictionaryOrString) {
if (Object.prototype.hasOwnProperty.call(queryDictionaryOrString, key)) {
this.setURLComponentsQueryParamater(key, queryDictionaryOrString[key]);
}
}
}
return this;
}
/**
* @private
* Parses the query parameter of string type to set the urlComponents property of the GraphRequest object
* @param {string} queryParameter - the query parameters
* returns nothing
*/
private parseQueryParamenterString(queryParameter: string): void {
/* The query key-value pair must be split on the first equals sign to avoid errors in parsing nested query parameters.
Example-> "/me?$expand=home($select=city)" */
if (this.isValidQueryKeyValuePair(queryParameter)) {
const indexOfFirstEquals = queryParameter.indexOf("=");
const paramKey = queryParameter.substring(0, indexOfFirstEquals);
const paramValue = queryParameter.substring(indexOfFirstEquals + 1);
this.setURLComponentsQueryParamater(paramKey, paramValue);
} else {
/* Push values which are not of key-value structure.
Example-> Handle an invalid input->.query(test), .query($select($select=name)) and let the Graph API respond with the error in the URL*/
this.urlComponents.otherURLQueryOptions.push(queryParameter);
}
}
/**
* @private
* Sets values into the urlComponents property of GraphRequest object.
* @param {string} paramKey - the query parameter key
* @param {string} paramValue - the query paramter value
* @returns nothing
*/
private setURLComponentsQueryParamater(paramKey: string, paramValue: string | number): void {
if (oDataQueryNames.indexOf(paramKey) !== -1) {
const currentValue = this.urlComponents.oDataQueryParams[paramKey];
const isValueAppendable = currentValue && (paramKey === "$expand" || paramKey === "$select" || paramKey === "$orderby");
this.urlComponents.oDataQueryParams[paramKey] = isValueAppendable ? currentValue + "," + paramValue : paramValue;
} else {
this.urlComponents.otherURLQueryParams[paramKey] = paramValue;
}
}
/**
* @private
* Check if the query parameter string has a valid key-value structure
* @param {string} queryString - the query parameter string. Example -> "name=value"
* #returns true if the query string has a valid key-value structure else false
*/
private isValidQueryKeyValuePair(queryString: string): boolean {
const indexofFirstEquals = queryString.indexOf("=");
if (indexofFirstEquals === -1) {
return false;
}
const indexofOpeningParanthesis = queryString.indexOf("(");
if (indexofOpeningParanthesis !== -1 && queryString.indexOf("(") < indexofFirstEquals) {
// Example -> .query($select($expand=true));
return false;
}
return true;
}
/**
* @private
* Updates the custom headers and options for a request
* @param {FetchOptions} options - The request options object
* @returns Nothing
*/
private updateRequestOptions(options: FetchOptions): void {
const optionsHeaders: HeadersInit = { ...options.headers };
if (this.config.fetchOptions !== undefined) {
const fetchOptions: FetchOptions = { ...this.config.fetchOptions };
Object.assign(options, fetchOptions);
if (typeof this.config.fetchOptions.headers !== undefined) {
options.headers = { ...this.config.fetchOptions.headers };
}
}
Object.assign(options, this._options);
if (options.headers !== undefined) {
Object.assign(optionsHeaders, options.headers);
}
Object.assign(optionsHeaders, this._headers);
options.headers = optionsHeaders;
}
/**
* @private
* @async
* Adds the custom headers and options to the request and makes the HTTPClient send request call
* @param {RequestInfo} request - The request url string or the Request object value
* @param {FetchOptions} options - The options to make a request
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the response content
*/
private async send(request: RequestInfo, options: FetchOptions, callback?: GraphRequestCallback): Promise<any> {
let rawResponse: Response;
const middlewareControl = new MiddlewareControl(this._middlewareOptions);
this.updateRequestOptions(options);
const customHosts = this.config?.customHosts;
try {
const context: Context = await this.httpClient.sendRequest({
request,
options,
middlewareControl,
customHosts,
});
rawResponse = context.response;
const response: any = await GraphResponseHandler.getResponse(rawResponse, this._responseType, callback);
return response;
} catch (error) {
if (error instanceof GraphClientError) {
throw error;
}
let statusCode: number;
if (rawResponse) {
statusCode = rawResponse.status;
}
const gError: GraphError = await GraphErrorHandler.getError(error, statusCode, callback, rawResponse);
throw gError;
}
}
/**
* @private
* Checks if the content-type is present in the _headers property. If not present, defaults the content-type to application/json
* @param none
* @returns nothing
*/
private setHeaderContentType(): void {
if (!this._headers) {
this.header("Content-Type", "application/json");
return;
}
const headerKeys = Object.keys(this._headers);
for (const headerKey of headerKeys) {
if (headerKey.toLowerCase() === "content-type") {
return;
}
}
// Default the content-type to application/json in case the content-type is not present in the header
this.header("Content-Type", "application/json");
}
/**
* @public
* Sets the custom header for a request
* @param {string} headerKey - A header key
* @param {string} headerValue - A header value
* @returns The same GraphRequest instance that is being called with
*/
public header(headerKey: string, headerValue: string): GraphRequest {
this._headers[headerKey] = headerValue;
return this;
}
/**
* @public
* Sets the custom headers for a request
* @param {KeyValuePairObjectStringNumber | HeadersInit} headers - The request headers
* @returns The same GraphRequest instance that is being called with
*/
public headers(headers: KeyValuePairObjectStringNumber | HeadersInit): GraphRequest {
for (const key in headers) {
if (Object.prototype.hasOwnProperty.call(headers, key)) {
this._headers[key] = headers[key] as string;
}
}
return this;
}
/**
* @public
* Sets the option for making a request
* @param {string} key - The key value
* @param {any} value - The value
* @returns The same GraphRequest instance that is being called with
*/
public option(key: string, value: any): GraphRequest {
this._options[key] = value;
return this;
}
/**
* @public
* Sets the options for making a request
* @param {{ [key: string]: any }} options - The options key value pair
* @returns The same GraphRequest instance that is being called with
*/
public options(options: { [key: string]: any }): GraphRequest {
for (const key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
this._options[key] = options[key];
}
}
return this;
}
/**
* @public
* Sets the middleware options for a request
* @param {MiddlewareOptions[]} options - The array of middleware options
* @returns The same GraphRequest instance that is being called with
*/
public middlewareOptions(options: MiddlewareOptions[]): GraphRequest {
this._middlewareOptions = options;
return this;
}
/**
* @public
* Sets the api endpoint version for a request
* @param {string} version - The version value
* @returns The same GraphRequest instance that is being called with
*/
public version(version: string): GraphRequest {
this.urlComponents.version = version;
return this;
}
/**
* @public
* Sets the api endpoint version for a request
* @param {ResponseType} responseType - The response type value
* @returns The same GraphRequest instance that is being called with
*/
public responseType(responseType: ResponseType): GraphRequest {
this._responseType = responseType;
return this;
}
/**
* @public
* To add properties for select OData Query param
* @param {string|string[]} properties - The Properties value
* @returns The same GraphRequest instance that is being called with, after adding the properties for $select query
*/
/*
* Accepts .select("displayName,birthday")
* and .select(["displayName", "birthday"])
* and .select("displayName", "birthday")
*
*/
public select(properties: string | string[]): GraphRequest {
this.addCsvQueryParameter("$select", properties, arguments);
return this;
}
/**
* @public
* To add properties for expand OData Query param
* @param {string|string[]} properties - The Properties value
* @returns The same GraphRequest instance that is being called with, after adding the properties for $expand query
*/
public expand(properties: string | string[]): GraphRequest {
this.addCsvQueryParameter("$expand", properties, arguments);
return this;
}
/**
* @public
* To add properties for orderby OData Query param
* @param {string|string[]} properties - The Properties value
* @returns The same GraphRequest instance that is being called with, after adding the properties for $orderby query
*/
public orderby(properties: string | string[]): GraphRequest {
this.addCsvQueryParameter("$orderby", properties, arguments);
return this;
}
/**
* @public
* To add query string for filter OData Query param. The request URL accepts only one $filter Odata Query option and its value is set to the most recently passed filter query string.
* @param {string} filterStr - The filter query string
* @returns The same GraphRequest instance that is being called with, after adding the $filter query
*/
public filter(filterStr: string): GraphRequest {
this.urlComponents.oDataQueryParams.$filter = filterStr;
return this;
}
/**
* @public
* To add criterion for search OData Query param. The request URL accepts only one $search Odata Query option and its value is set to the most recently passed search criterion string.
* @param {string} searchStr - The search criterion string
* @returns The same GraphRequest instance that is being called with, after adding the $search query criteria
*/
public search(searchStr: string): GraphRequest {
this.urlComponents.oDataQueryParams.$search = searchStr;
return this;
}
/**
* @public
* To add number for top OData Query param. The request URL accepts only one $top Odata Query option and its value is set to the most recently passed number value.
* @param {number} n - The number value
* @returns The same GraphRequest instance that is being called with, after adding the number for $top query
*/
public top(n: number): GraphRequest {
this.urlComponents.oDataQueryParams.$top = n;
return this;
}
/**
* @public
* To add number for skip OData Query param. The request URL accepts only one $skip Odata Query option and its value is set to the most recently passed number value.
* @param {number} n - The number value
* @returns The same GraphRequest instance that is being called with, after adding the number for the $skip query
*/
public skip(n: number): GraphRequest {
this.urlComponents.oDataQueryParams.$skip = n;
return this;
}
/**
* @public
* To add token string for skipToken OData Query param. The request URL accepts only one $skipToken Odata Query option and its value is set to the most recently passed token value.
* @param {string} token - The token value
* @returns The same GraphRequest instance that is being called with, after adding the token string for $skipToken query option
*/
public skipToken(token: string): GraphRequest {
this.urlComponents.oDataQueryParams.$skipToken = token;
return this;
}
/**
* @public
* To add boolean for count OData Query param. The URL accepts only one $count Odata Query option and its value is set to the most recently passed boolean value.
* @param {boolean} isCount - The count boolean
* @returns The same GraphRequest instance that is being called with, after adding the boolean value for the $count query option
*/
public count(isCount = true): GraphRequest {
this.urlComponents.oDataQueryParams.$count = isCount.toString();
return this;
}
/**
* @public
* Appends query string to the urlComponent
* @param {string|KeyValuePairObjectStringNumber} queryDictionaryOrString - The query value
* @returns The same GraphRequest instance that is being called with, after appending the query string to the url component
*/
/*
* Accepts .query("displayName=xyz")
* and .select({ name: "value" })
*/
public query(queryDictionaryOrString: string | KeyValuePairObjectStringNumber): GraphRequest {
return this.parseQueryParameter(queryDictionaryOrString);
}
/**
* @public
* @async
* Makes a http request with GET method
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the get response
*/
public async get(callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
const options: FetchOptions = {
method: RequestMethod.GET,
};
const response = await this.send(url, options, callback);
return response;
}
/**
* @public
* @async
* Makes a http request with POST method
* @param {any} content - The content that needs to be sent with the request
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the post response
*/
public async post(content: any, callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
const options: FetchOptions = {
method: RequestMethod.POST,
body: serializeContent(content),
};
const className: string = content && content.constructor && content.constructor.name;
if (className === "FormData") {
// Content-Type headers should not be specified in case the of FormData type content
options.headers = {};
} else {
this.setHeaderContentType();
options.headers = this._headers;
}
return await this.send(url, options, callback);
}
/**
* @public
* @async
* Alias for Post request call
* @param {any} content - The content that needs to be sent with the request
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the post response
*/
public async create(content: any, callback?: GraphRequestCallback): Promise<any> {
return await this.post(content, callback);
}
/**
* @public
* @async
* Makes http request with PUT method
* @param {any} content - The content that needs to be sent with the request
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the put response
*/
public async put(content: any, callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
this.setHeaderContentType();
const options: FetchOptions = {
method: RequestMethod.PUT,
body: serializeContent(content),
};
return await this.send(url, options, callback);
}
/**
* @public
* @async
* Makes http request with PATCH method
* @param {any} content - The content that needs to be sent with the request
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the patch response
*/
public async patch(content: any, callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
this.setHeaderContentType();
const options: FetchOptions = {
method: RequestMethod.PATCH,
body: serializeContent(content),
};
return await this.send(url, options, callback);
}
/**
* @public
* @async
* Alias for PATCH request
* @param {any} content - The content that needs to be sent with the request
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the patch response
*/
public async update(content: any, callback?: GraphRequestCallback): Promise<any> {
return await this.patch(content, callback);
}
/**
* @public
* @async
* Makes http request with DELETE method
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the delete response
*/
public async delete(callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
const options: FetchOptions = {
method: RequestMethod.DELETE,
};
return await this.send(url, options, callback);
}
/**
* @public
* @async
* Alias for delete request call
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the delete response
*/
public async del(callback?: GraphRequestCallback): Promise<any> {
return await this.delete(callback);
}
/**
* @public
* @async
* Makes a http request with GET method to read response as a stream.
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the getStream response
*/
public async getStream(callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
const options = {
method: RequestMethod.GET,
};
this.responseType(ResponseType.STREAM);
return await this.send(url, options, callback);
}
/**
* @public
* @async
* Makes a http request with GET method to read response as a stream.
* @param {any} stream - The stream instance
* @param {GraphRequestCallback} [callback] - The callback function to be called in response with async call
* @returns A promise that resolves to the putStream response
*/
public async putStream(stream: any, callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
const options = {
method: RequestMethod.PUT,
headers: {
"Content-Type": "application/octet-stream",
},
body: stream,
};
return await this.send(url, options, callback);
}
}