forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphHelper.ts
More file actions
48 lines (45 loc) · 2.08 KB
/
Copy pathGraphHelper.ts
File metadata and controls
48 lines (45 loc) · 2.08 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
export class GraphHelper {
/*
This conversion is required due to the following reasons:
1. Body parameter of Request method of isomorphic-fetch only accepts Blob, ArrayBuffer, FormData, TypedArrays, string.
2. Node.js platform does not suppport 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.
3. Parallel to Javascript's arrayBuffer, node provides Buffer interface. Node's Buffer is able to send the arbitary
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`.
4. If the data received is in JSON format, `serializeContent` converts the JSON to string.
*/
public static serializeContent(content: any): any {
let 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) {
console.log(error);
throw new Error('Invalid JSON content');
}
}
return content;
}
}