Skip to content

Commit 76daa5d

Browse files
Bug Fix: Parsing Document response type in fetch request
1 parent 2008cef commit 76daa5d

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

lib/graph-js-sdk-core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/graph-js-sdk-web.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/GraphRequest.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@ export declare class GraphRequest {
8989
[key: string]: string | number;
9090
}): GraphRequest;
9191
private createQueryString;
92+
private parseDocumentResponse;
9293
private convertResponseType;
9394
}

lib/src/GraphRequest.js

Lines changed: 42 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/GraphRequest.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GraphRequest.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -446,21 +446,35 @@ export class GraphRequest {
446446
return "";
447447
}
448448

449+
private parseDocumentResponse(response, type): Promise<any> {
450+
return new Promise((resolve, reject) => {
451+
response.text().then((xmlString) => {
452+
try {
453+
let parser = new DOMParser(),
454+
xmlDoc = parser.parseFromString(xmlString, type);
455+
resolve(xmlDoc);
456+
} catch (error) {
457+
reject(error);
458+
}
459+
});
460+
});
461+
}
462+
449463
private convertResponseType(response: Response): Promise<any> {
450-
let responseValue: any;
451-
if (!this._responseType) {
452-
this._responseType = '';
464+
let self = this,
465+
responseValue: any;
466+
if (!self._responseType) {
467+
self._responseType = '';
453468
}
454-
switch (this._responseType.toLowerCase()) {
469+
switch (self._responseType.toLowerCase()) {
455470
case ResponseType.ARRAYBUFFER:
456471
responseValue = response.arrayBuffer();
457472
break;
458473
case ResponseType.BLOB:
459474
responseValue = response.blob();
460475
break;
461476
case ResponseType.DOCUMENT:
462-
// XMLHTTPRequest only :(
463-
responseValue = response.json();
477+
responseValue = self.parseDocumentResponse(response, "text/html");
464478
break;
465479
case ResponseType.JSON:
466480
responseValue = response.json();
@@ -472,7 +486,29 @@ export class GraphRequest {
472486
responseValue = response.text();
473487
break;
474488
default:
475-
responseValue = response.json();
489+
let contentType = response.headers.get("Content-type");
490+
if (contentType !== null) {
491+
let mimeType = contentType.split(";")[0],
492+
documentContentTypes = ["text/html", "text/xml", "application/xml", "application/xhtml+xml"];
493+
if (documentContentTypes.includes(mimeType)) {
494+
responseValue = self.parseDocumentResponse(response, mimeType);
495+
} else {
496+
responseValue = response.json();
497+
}
498+
} else {
499+
/**
500+
* RFC specification {@link https://tools.ietf.org/html/rfc7231#section-3.1.1.5} says:
501+
* A sender that generates a message containing a payload body SHOULD
502+
* generate a Content-Type header field in that message unless the
503+
* intended media type of the enclosed representation is unknown to the
504+
* sender. If a Content-Type header field is not present, the recipient
505+
* MAY either assume a media type of "application/octet-stream"
506+
* ([RFC2046], Section 4.5.1) or examine the data to determine its type.
507+
*
508+
* So assuming it as a stream type so returning the body.
509+
*/
510+
responseValue = Promise.resolve(response.body);
511+
}
476512
break;
477513
}
478514
return responseValue;

0 commit comments

Comments
 (0)