Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 44 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@types/mocha": "^5.2.6",
"@types/node": "^12.0.10",
"@types/node": "^12.12.53",
"chai": "^4.2.0",
"form-data": "^3.0.0",
"gulp": "^4.0.2",
"husky": "^2.2.0",
"isomorphic-fetch": "^2.2.1",
Expand Down
5 changes: 5 additions & 0 deletions spec/core/GraphRequestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ describe("GraphRequestUtil.ts", () => {
const val = undefined;
assert.equal(serializeContent(val), undefined);
});

it("Should return 'null' for the case of null content value", () => {
const val = null;
assert.equal(serializeContent(val), "null");
});
});
});
16 changes: 16 additions & 0 deletions spec/development/workload/OneNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ describe("OneNote", function() {
}
});

it("Create a OneNote page with application/xhtml+xml page content", async () => {
try {
const body = "<!DOCTYPE html><html><head><title>A page with a block of HTML</title></head><body><p>This page contains some <i>formatted</i> <b>text</b>.</p></body></html>";
const json = await client
.api(`/me/onenote/sections/${section.id}/pages`)
.header("content-type", "application/xhtml+xml")
.post(body);
const createdPageFromHTML = json as OnenotePage;
assert.isDefined(createdPage.id);
assert.isDefined(createdPage.contentUrl);
assert.isUndefined(createdPage["random fake property that should be null"]);
} catch (error) {
throw error;
}
});

it("create a OneNote page with html page content and file attachment", async () => {
try {
const formData = new FormData();
Expand Down
48 changes: 35 additions & 13 deletions src/GraphRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @module GraphRequest
*/

import FormData from "form-data";

import { GraphError } from "./GraphError";
import { GraphErrorHandler } from "./GraphErrorHandler";
import { oDataQueryNames, serializeContent, urlJoin } from "./GraphRequestUtil";
Expand All @@ -22,7 +24,6 @@ import { MiddlewareControl } from "./middleware/MiddlewareControl";
import { MiddlewareOptions } from "./middleware/options/IMiddlewareOptions";
import { RequestMethod } from "./RequestMethod";
import { ResponseType } from "./ResponseType";

/**
* @interface
* Signature to representing key value pairs
Expand Down Expand Up @@ -302,6 +303,29 @@ export class GraphRequest {
}
}

/**
* @private
* Checks if the content-type is present in the _headers property. If not present, defaults the content-type to application/json
* @param none
* @returns nothing
*/
private setHeaderContentType(): void {
if (this._headers === undefined || this._headers === null) {
this.header("Content-Type", "application/json");
}
let isContentTypePresent = false;
const headerKeys = Object.keys(this._headers);
for (const headerKey of headerKeys) {
if (headerKey.toLowerCase() === "content-type") {
isContentTypePresent = true;
}
}
// Default the content-type to application/json in case the content-type is not present in the header
if (!isContentTypePresent) {
this.header("Content-Type", "application/json");
}
}

/**
* @public
* Sets the custom header for a request
Expand Down Expand Up @@ -550,13 +574,15 @@ export class GraphRequest {
const options: FetchOptions = {
method: RequestMethod.POST,
body: serializeContent(content),
headers:
typeof FormData !== "undefined" && content instanceof FormData
? {}
: {
"Content-Type": "application/json",
},
};
const className: string = content === undefined || content === null ? undefined : content.constructor.name;
if (typeof FormData !== "undefined" && className === "FormData") {
// Content-Type headers should not be specified in case the of FormData type content
options.headers = {};
} else {
this.setHeaderContentType();
options.headers = this._headers;
}
try {
const response = await this.send(url, options, callback);
return response;
Expand Down Expand Up @@ -591,12 +617,10 @@ export class GraphRequest {
*/
public async put(content: any, callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
this.setHeaderContentType();
const options: FetchOptions = {
method: RequestMethod.PUT,
body: serializeContent(content),
headers: {
"Content-Type": "application/json",
},
};
try {
const response = await this.send(url, options, callback);
Expand All @@ -616,12 +640,10 @@ export class GraphRequest {
*/
public async patch(content: any, callback?: GraphRequestCallback): Promise<any> {
const url = this.buildFullUrl();
this.setHeaderContentType();
const options: FetchOptions = {
method: RequestMethod.PATCH,
body: serializeContent(content),
headers: {
"Content-Type": "application/json",
},
};
try {
const response = await this.send(url, options, callback);
Expand Down
2 changes: 1 addition & 1 deletion src/GraphRequestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const urlJoin = (urlSegments: string[]): string => {
*/

export const serializeContent = (content: any): any => {
const className: string = content === undefined ? undefined : content.constructor.name;
const className: string = content === undefined || content === null ? undefined : content.constructor.name;
if (className === "Buffer" || className === "Blob" || className === "File" || className === "FormData" || typeof content === "string") {
return content;
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig-cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"module": "commonjs",
"target": "es5",
"lib": ["dom", "esnext"],
"outDir": "lib"
"outDir": "lib",
"esModuleInterop": true
},
"exclude": ["node_modules", "lib", "samples", "spec/development"],
"include": ["./src/**/*.ts", "./spec/**/*.ts"]
Expand Down
5 changes: 3 additions & 2 deletions tsconfig-es.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "es6",
"module": "CommonJS",
"target": "es6",
"lib": ["dom", "esnext"],
"outDir": "lib/es"
"outDir": "lib/es",
"esModuleInterop": true
},
"exclude": ["node_modules", "lib", "samples", "spec/**"],
"include": ["./src/**/*.ts"]
Expand Down