Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions samples/browser/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
</head>

<body style="background: #f3f3f3;">
<div class="fileUploadParent">
<!-- <div class="fileUploadParent">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why keeping commented code in the repo?

<img id="profileImg" width="100px" src="" alt="profileImg" />
<input id="uploadProfile" type="file" class="fileUpload" onchange="request.updateProfilePicture()" />
<div class="imageInput">
<img width="20px" src="./upload-user.svg" />
</div>
</div>
</div> -->
<div display="inline-block">
<h2>
<span>Hello </span>
<span id="displayName" style="display: inline-block; vertical-align: middle;"></span>
<span> !</span>
<!-- <span id="displayName" style="display: inline-block; vertical-align: middle;"></span> -->
<span>!</span>
</h2>
</div>
<div class="main">
Expand Down
8 changes: 4 additions & 4 deletions samples/browser/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const init = async () => {

bindEvents();

let displayName = await request.getDisplayName();
ui.setDisplayName(displayName);
// let displayName = await request.getDisplayName();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: why keeping commented code, instead of deleting?

// ui.setDisplayName(displayName);

let profileImg = await request.getProfilePicture();
ui.setProfilePicture(profileImg);
// let profileImg = await request.getProfilePicture();
// ui.setProfilePicture(profileImg);
};

const bindEvents = () => {
Expand Down
5 changes: 1 addition & 4 deletions samples/browser/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
65 changes: 63 additions & 2 deletions spec/core/GraphResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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");
});
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/GraphErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ export class GraphErrorHandler {
*/
public static async getError(error: any = null, statusCode: number = -1, callback?: GraphRequestCallback): Promise<GraphError> {
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);
Expand Down
44 changes: 35 additions & 9 deletions src/GraphResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down