diff --git a/samples/browser/src/index.html b/samples/browser/src/index.html
index 8939949da..a5b00e812 100644
--- a/samples/browser/src/index.html
+++ b/samples/browser/src/index.html
@@ -12,18 +12,18 @@
-
+
Hello
-
- !
+
+ !
diff --git a/samples/browser/src/main.js b/samples/browser/src/main.js
index 8139ae31f..15457c105 100644
--- a/samples/browser/src/main.js
+++ b/samples/browser/src/main.js
@@ -23,11 +23,11 @@ const init = async () => {
bindEvents();
- let displayName = await request.getDisplayName();
- ui.setDisplayName(displayName);
+ // let displayName = await request.getDisplayName();
+ // ui.setDisplayName(displayName);
- let profileImg = await request.getProfilePicture();
- ui.setProfilePicture(profileImg);
+ // let profileImg = await request.getProfilePicture();
+ // ui.setProfilePicture(profileImg);
};
const bindEvents = () => {
diff --git a/samples/browser/src/request.js b/samples/browser/src/request.js
index 2dd89c4f2..5784816eb 100644
--- a/samples/browser/src/request.js
+++ b/samples/browser/src/request.js
@@ -13,10 +13,7 @@ let request = {
getProfilePicture: async () => {
try {
- let response = await client
- .api("/me/photo/$value")
- .responseType(MicrosoftGraph.ResponseType.BLOB)
- .get();
+ let response = await client.api("/me/photo/$value").get();
return response;
} catch (error) {
console.error(error);
diff --git a/spec/core/GraphResponseHandler.ts b/spec/core/GraphResponseHandler.ts
index efe668f50..c3da32e99 100644
--- a/spec/core/GraphResponseHandler.ts
+++ b/spec/core/GraphResponseHandler.ts
@@ -7,7 +7,7 @@
import { assert } from "chai";
-import { GraphResponseHandler } from "../../src/GraphResponseHandler";
+import { DocumentType, GraphResponseHandler } from "../../src/GraphResponseHandler";
import { ResponseType } from "../../src/ResponseType";
describe("GraphResponseHandler.ts", () => {
@@ -33,11 +33,43 @@ describe("GraphResponseHandler.ts", () => {
status: 500,
statusText: "Internal Server Error",
};
+ const status202 = {
+ status: 202,
+ statusText: "OK",
+ };
+ const status200Text = {
+ status: 200,
+ stautsText: "OK",
+ headers: {
+ "Content-Type": "text/plain",
+ },
+ };
+ const status200Json = {
+ status: 200,
+ stautsText: "OK",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ };
+ const status200Image = {
+ status: 200,
+ stautsText: "OK",
+ headers: {
+ "Content-Type": "image/jpeg",
+ },
+ };
+ const status200Unknown = {
+ status: 200,
+ statusText: "OK",
+ headers: {
+ "Content-Type": "dummy/unknown",
+ },
+ };
/* tslint:disable: no-string-literal */
describe("parseDocumentResponse", () => {
it("Should return the html string", async () => {
const response = new Response(htmlString, status200);
- const dom = await GraphResponseHandler["parseDocumentResponse"](response, GraphResponseHandler["DocumentTypes"]["TEXT_HTML"]);
+ const dom = await GraphResponseHandler["parseDocumentResponse"](response, DocumentType.TEXT_HTML);
assert.isDefined(dom);
assert.equal(typeof dom, "string");
});
@@ -50,6 +82,35 @@ describe("GraphResponseHandler.ts", () => {
assert.isUndefined(responseValue);
});
+ it("Should return empty text value for empty response", async () => {
+ const response = new Response(undefined, status202);
+ const responseValue = await GraphResponseHandler["convertResponse"](response);
+ assert.isUndefined(responseValue);
+ });
+
+ it("Should return text data for text/plain content-type", async () => {
+ const data = "text data";
+ const response = new Response(data, status200Text);
+ const responseValue = await GraphResponseHandler["convertResponse"](response);
+ assert.equal(responseValue, data);
+ });
+
+ it("Should return json data for application/json content-type", async () => {
+ const data = {
+ test: "test",
+ };
+ const response = new Response(JSON.stringify(data), status200Json);
+ const responseValue = await GraphResponseHandler["convertResponse"](response);
+ assert.equal(responseValue.test, data.test);
+ });
+
+ it("Should return raw response incase of unknown content-type", async () => {
+ const data = "test data";
+ const response = new Response(data, status200Unknown);
+ const responseValue = await GraphResponseHandler["convertResponse"](response);
+ assert.equal(responseValue, data);
+ });
+
it("Should return response value as text", async () => {
const response = new Response(htmlString, status200);
const responseValue = await GraphResponseHandler["convertResponse"](response, ResponseType.TEXT);
diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts
index dd4f9e807..286a52283 100644
--- a/src/GraphErrorHandler.ts
+++ b/src/GraphErrorHandler.ts
@@ -106,11 +106,11 @@ export class GraphErrorHandler {
*/
public static async getError(error: any = null, statusCode: number = -1, callback?: GraphRequestCallback): Promise {
let gError: GraphError;
- if (error instanceof Response) {
+ if (error && (error.constructor.name === "Response" || error.constructor.name === "Body")) {
gError = await GraphErrorHandler.constructErrorFromRawResponse(error, statusCode);
} else if (error && error.error) {
gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode);
- } else if (error instanceof Error) {
+ } else if (error && error.constructor.name === "Error") {
gError = GraphErrorHandler.constructError(error, statusCode);
} else {
gError = new GraphError(statusCode);
diff --git a/src/GraphResponseHandler.ts b/src/GraphResponseHandler.ts
index d05b9db72..6b6d1962f 100644
--- a/src/GraphResponseHandler.ts
+++ b/src/GraphResponseHandler.ts
@@ -20,26 +20,42 @@ import { ResponseType } from "./ResponseType";
* @property {string} APPLICATION_XML - The application/xml content type
* @property {string} APPLICATION_XHTML - The application/xhml+xml content type
*/
-enum DocumentType {
+export enum DocumentType {
TEXT_HTML = "text/html",
TEXT_XML = "text/xml",
APPLICATION_XML = "application/xml",
APPLICATION_XHTML = "application/xhtml+xml",
}
+/**
+ * @enum
+ * Enum for Content types
+ * @property {string} TEXT_PLAIN - The text/plain content type
+ * @property {string} APPLICATION_JSON - The application/json content type
+ */
+
+enum ContentType {
+ TEXT_PLAIN = "text/plain",
+ APPLICATION_JSON = "application/json",
+}
+
+/**
+ * @enum
+ * Enum for Content type regex
+ * @property {string} DOCUMENT - The regex to match document content types
+ * @property {string} IMAGE - The regex to match image content types
+ */
+enum ContentTypeRegexStr {
+ DOCUMENT = "^(text\\/(html|xml))|(application\\/(xml|xhtml\\+xml))$",
+ IMAGE = "^image\\/.+",
+}
+
/**
* @class
* Class for GraphResponseHandler
*/
export class GraphResponseHandler {
- /**
- * @private
- * @static
- * A member holding array of document types
- */
- private static DocumentTypes: string[] = ["text/html", "text/xml", "application/xml", "application/xhtml+xml"];
-
/**
* @private
* @static
@@ -110,7 +126,17 @@ export class GraphResponseHandler {
const contentType = clonedRawResponse.headers.get("Content-type");
if (contentType !== null) {
const mimeType = contentType.split(";")[0];
- responseValue = GraphResponseHandler.DocumentTypes.includes(mimeType) ? await GraphResponseHandler.parseDocumentResponse(clonedRawResponse, mimeType as DocumentType) : await clonedRawResponse.json();
+ if (new RegExp(ContentTypeRegexStr.DOCUMENT).test(mimeType)) {
+ responseValue = await GraphResponseHandler.parseDocumentResponse(clonedRawResponse, mimeType as DocumentType);
+ } else if (new RegExp(ContentTypeRegexStr.IMAGE).test(mimeType)) {
+ responseValue = clonedRawResponse.blob();
+ } else if (mimeType === ContentType.TEXT_PLAIN) {
+ responseValue = await clonedRawResponse.text();
+ } else if (mimeType === ContentType.APPLICATION_JSON) {
+ responseValue = await clonedRawResponse.json();
+ } else {
+ responseValue = Promise.resolve(clonedRawResponse.body);
+ }
} else {
/**
* RFC specification {@link https://tools.ietf.org/html/rfc7231#section-3.1.1.5} says: