Skip to content

Commit 574974a

Browse files
U-FAREAST\ramguptU-FAREAST\ramgupt
authored andcommitted
Pass the body of POST/PUT/PATCH calls correctly.
Following are the scenarios that have been tested to verify this: 1. Tried sending the string 2. Tried sending the JSON 3. Tried sending the blob/file from browser 4. Tried sending the binary data as ArrayBuffer from browser 5. Tried sending the binary data as TypedArrays from browser 6. Tried sending the binary data as node.js buffer object using node platform
1 parent 20dc7f6 commit 574974a

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/GraphHelper.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export class GraphHelper {
2+
3+
/*
4+
This conversion is required due to the following reasons:
5+
1. Body parameter of Request method of isomorphic-fetch only accepts Blob, ArrayBuffer, FormData, TypedArrays, string.
6+
2. Node.js platform does not suppport Blob, FormData. Javascript File object inherits from Blob so it is also
7+
not supported in node. Therefore content of type Blob, File, FormData will only come from browsers.
8+
3. Parallel to Javascript's arrayBuffer, node provides Buffer interface. Node's Buffer is able to send the arbitary
9+
binary data to the server successfully for both Browser and Node platform. Whereas sending binary data via
10+
ArrayBuffer or TypedArrays was only possible using Browser. To support both Node and Browser, `serializeContent`
11+
converts TypedArrays or ArrayBuffer to `Node Buffer`.
12+
4. If the data received is in JSON format, `serializeContent` converts the JSON to string.
13+
*/
14+
public static serializeContent(content: any): any {
15+
let className: string = content.constructor.name;
16+
17+
if (className === 'Buffer'
18+
|| className === 'Blob'
19+
|| className === 'File'
20+
|| className === 'FormData'
21+
|| typeof content === 'string') {
22+
return content;
23+
}
24+
25+
if (className === 'ArrayBuffer') {
26+
content = Buffer.from(content);
27+
} else if (className === 'Int8Array'
28+
|| className === 'Int16Array'
29+
|| className === 'Int32Array'
30+
|| className === 'Uint8Array'
31+
|| className === 'Uint16Array'
32+
|| className === 'Uint32Array'
33+
|| className === 'Uint8ClampedArray'
34+
|| className === 'Float32Array'
35+
|| className === 'Float64Array'
36+
|| className === 'DataView') {
37+
content = Buffer.from(content.buffer);
38+
} else {
39+
try {
40+
content = JSON.stringify(content);
41+
} catch (error) {
42+
console.log(error);
43+
throw new Error('Invalid JSON content');
44+
}
45+
}
46+
return content;
47+
}
48+
}

src/GraphRequest.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'isomorphic-fetch';
44
import { Options, URLComponents, GraphError, oDataQueryNames, GraphRequestCallback, PACKAGE_VERSION } from "./common"
55
import { ResponseHandler } from "./ResponseHandler"
66
import { RequestMethod } from './RequestMethod';
7+
import { GraphHelper } from './GraphHelper';
78

89
export class GraphRequest {
910
config: Options;
@@ -211,7 +212,7 @@ export class GraphRequest {
211212
url,
212213
{
213214
method: RequestMethod.PATCH,
214-
body: content,
215+
body: GraphHelper.serializeContent(content),
215216
headers: new Headers(({ 'Content-Type' : 'application/json' }))
216217
}),
217218
callback
@@ -225,7 +226,7 @@ export class GraphRequest {
225226
url,
226227
{
227228
method: RequestMethod.POST,
228-
body: content,
229+
body: GraphHelper.serializeContent(content),
229230
headers: new Headers(({ 'Content-Type' : 'application/json'}))
230231
}),
231232
callback
@@ -239,7 +240,7 @@ export class GraphRequest {
239240
url,
240241
{
241242
method: RequestMethod.PUT,
242-
body: content,
243+
body: GraphHelper.serializeContent(content),
243244
headers: new Headers({ 'Content-Type' : 'application/octet-stream' })
244245
}),
245246
callback

0 commit comments

Comments
 (0)