Skip to content

Commit b7cc8e4

Browse files
Supporting multipart form data payload for POST request
1 parent cbc1ec5 commit b7cc8e4

File tree

11 files changed

+89
-19
lines changed

11 files changed

+89
-19
lines changed

lib/graph-js-sdk-web.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2-
// make MicrosoftGraph globally accessible
3-
// MicrosoftGraph.api() can be called directly
2+
// make MicrosoftGraph globally accessible
3+
// MicrosoftGraph.api() can be called directly
44
window.MicrosoftGraph = require('./lib/src/index.js');
55
},{"./lib/src/index.js":7}],2:[function(require,module,exports){
66
(function (Buffer){
@@ -202,7 +202,7 @@ var GraphRequest = (function () {
202202
return this.sendRequestAndRouteResponse(new Request(url, {
203203
method: RequestMethod_1.RequestMethod.POST,
204204
body: GraphHelper_1.GraphHelper.serializeContent(content),
205-
headers: new Headers({ 'Content-Type': 'application/json' })
205+
headers: new Headers((content.constructor !== undefined && content.constructor.name === "FormData") ? {} : { 'Content-Type': 'application/json' })
206206
}), callback);
207207
};
208208
GraphRequest.prototype.put = function (content, callback) {

lib/src/GraphRequest.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export declare class GraphRequest {
1414
[key: string]: string | number;
1515
}): this;
1616
parsePath(rawPath: string): void;
17-
private urlJoin(urlSegments);
17+
private urlJoin;
1818
buildFullUrl(): string;
1919
version(v: string): GraphRequest;
2020
select(properties: string | string[]): GraphRequest;
@@ -26,7 +26,7 @@ export declare class GraphRequest {
2626
skipToken(token: string): GraphRequest;
2727
count(count: boolean): GraphRequest;
2828
responseType(responseType: string): GraphRequest;
29-
private addCsvQueryParamater(propertyName, propertyValue, additionalProperties);
29+
private addCsvQueryParamater;
3030
delete(callback?: GraphRequestCallback): Promise<any>;
3131
patch(content: any, callback?: GraphRequestCallback): Promise<any>;
3232
post(content: any, callback?: GraphRequestCallback): Promise<any>;
@@ -35,15 +35,15 @@ export declare class GraphRequest {
3535
update(content: any, callback?: GraphRequestCallback): Promise<any>;
3636
del(callback?: GraphRequestCallback): Promise<any>;
3737
get(callback?: GraphRequestCallback): Promise<any>;
38-
private routeResponseToPromise(request);
39-
private routeResponseToCallback(request, callback);
40-
private sendRequestAndRouteResponse(request, callback?);
38+
private routeResponseToPromise;
39+
private routeResponseToCallback;
40+
private sendRequestAndRouteResponse;
4141
getStream(callback: GraphRequestCallback): void;
4242
putStream(stream: any, callback: Function): void;
43-
private configureRequest(request, accessToken);
43+
private configureRequest;
4444
query(queryDictionaryOrString: string | {
4545
[key: string]: string | number;
4646
}): GraphRequest;
47-
private createQueryString();
48-
private convertResponseType(response);
47+
private createQueryString;
48+
private convertResponseType;
4949
}

lib/src/GraphRequest.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.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.

lib/src/RequestMethod.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export declare enum RequestMethod {
33
PATCH = "PATCH",
44
POST = "POST",
55
PUT = "PUT",
6-
DELETE = "DELETE",
6+
DELETE = "DELETE"
77
}

spec/types/OneNote.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { assert } from 'chai'
22

33
import { getClient, randomString } from "./test-helper"
44
import { Notebook, OnenoteSection, OnenotePage } from '@microsoft/microsoft-graph-types-beta'
5+
import * as fs from "fs";
6+
import * as FormData from "form-data";
57

68
declare const describe, it;
79

@@ -59,4 +61,36 @@ describe('OneNote', function () {
5961
return Promise.resolve();
6062
});
6163
});
62-
});
64+
it("Create a OneNote page with html page content", () => {
65+
let formData = new FormData();
66+
formData.append('Presentation', fs.createReadStream('./spec/types/onenotepage.html'));
67+
return getClient()
68+
.api(`/me/onenote/sections/${section.id}/pages`)
69+
.post(formData)
70+
.then((json) => {
71+
let createdPageFromHTML = json as OnenotePage;
72+
assert.isDefined(createdPage.id);
73+
assert.isDefined(createdPage.contentUrl);
74+
assert.equal("New Page", createdPageFromHTML.title);
75+
assert.isUndefined(createdPage['invalidPropertyName']);
76+
return Promise.resolve();
77+
});
78+
});
79+
80+
it("create a OneNote page with html page content and file attachment", () => {
81+
let formData = new FormData();
82+
formData.append('Presentation', fs.createReadStream('./spec/types/onenotepage_fileattachment.html'));
83+
formData.append("fileBlock1", fs.createReadStream("./sample.png"));
84+
return getClient()
85+
.api(`/me/onenote/sections/${section.id}/pages`)
86+
.post(formData)
87+
.then((json) => {
88+
let createdPageFromHTML = json as OnenotePage;
89+
assert.isDefined(createdPage.id);
90+
assert.isDefined(createdPage.contentUrl);
91+
assert.equal("A page with rendered file attachment", createdPageFromHTML.title);
92+
assert.isUndefined(createdPage['invalidPropertyName']);
93+
return Promise.resolve();
94+
});
95+
});
96+
});

spec/types/onenotepage.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>New Page</title>
5+
</head>
6+
<body>
7+
<p>Created a OneNote page from <b>HTML</b></p>
8+
</body>
9+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>A page with rendered file attachment</title>
5+
</head>
6+
<body>
7+
<p>Here is an attached file:</p>
8+
<object data-attachment="sample.png" data="name:fileBlock1" type="image/png" />
9+
</body>
10+
</html>

spec/types/package-lock.json

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

spec/types/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"devDependencies": {
3+
"@microsoft/microsoft-graph-client": "^0.4.1",
34
"@microsoft/microsoft-graph-types": "^1.0.0",
45
"@microsoft/microsoft-graph-types-beta": "microsoftgraph/msgraph-typescript-typings#beta",
5-
"@microsoft/microsoft-graph-client": "^0.4.1",
66
"@types/chai": "^4.1.4",
7-
"chai": "^4.1.2"
7+
"@types/form-data": "^2.2.1",
8+
"chai": "^4.1.2",
9+
"form-data": "^2.3.2"
810
}
911
}

0 commit comments

Comments
 (0)