forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRequestUtil.ts
More file actions
60 lines (55 loc) · 2.88 KB
/
GraphRequestUtil.ts
File metadata and controls
60 lines (55 loc) · 2.88 KB
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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module GraphRequestUtil
*/
/**
* To hold list of OData query params
*/
export const oDataQueryNames = ["$select", "$expand", "$orderby", "$filter", "$top", "$skip", "$skipToken", "$count"];
/**
* To construct the URL by appending the segments with "/"
* @param {string[]} urlSegments - The array of strings
* @returns The constructed URL string
*/
export const urlJoin = (urlSegments: string[]): string => {
const removePostSlash = (s) => s.replace(/\/+$/, "");
const removePreSlash = (s) => s.replace(/^\/+/, "");
const joiner = (pre, cur) => [removePostSlash(pre), removePreSlash(cur)].join("/");
const parts = Array.prototype.slice.call(urlSegments);
return parts.reduce(joiner);
};
/**
* Serializes the content
* @param {any} content - The content value that needs to be serialized
* @returns The serialized content
*
* Note:
* This conversion is required due to the following reasons:
* Body parameter of Request method of isomorphic-fetch only accepts Blob, ArrayBuffer, FormData, TypedArrays string.
* Node.js platform does not support Blob, FormData. Javascript File object inherits from Blob so it is also not supported in node. Therefore content of type Blob, File, FormData will only come from browsers.
* Parallel to ArrayBuffer in javascript, node provides Buffer interface. Node's Buffer is able to send the arbitrary binary data to the server successfully for both Browser and Node platform. Whereas sending binary data via ArrayBuffer or TypedArrays was only possible using Browser. To support both Node and Browser, `serializeContent` converts TypedArrays or ArrayBuffer to `Node Buffer`.
* If the data received is in JSON format, `serializeContent` converts the JSON to string.
*/
export const serializeContent = (content: any): any => {
const className: string = content.constructor.name;
if (className === "Buffer" || className === "Blob" || className === "File" || className === "FormData" || typeof content === "string") {
return content;
}
if (className === "ArrayBuffer") {
content = Buffer.from(content);
} else if (className === "Int8Array" || className === "Int16Array" || className === "Int32Array" || className === "Uint8Array" || className === "Uint16Array" || className === "Uint32Array" || className === "Uint8ClampedArray" || className === "Float32Array" || className === "Float64Array" || className === "DataView") {
content = Buffer.from(content.buffer);
} else {
try {
content = JSON.stringify(content);
} catch (error) {
throw new Error("Unable to stringify the content");
}
}
return content;
};