Skip to content

Commit 586f862

Browse files
Merge pull request #120 from microsoftgraph/Bug-Fixes
Bug fixes
2 parents 9106c92 + 424eeb5 commit 586f862

File tree

10 files changed

+517
-430
lines changed

10 files changed

+517
-430
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Bug Fixes
1313

1414
### 1.2.0
1515
Updates
16+
1617
* Two output js files, one with polyfills for Fetch-API and ES6-Promises ([lib/graph-js-sdk-web.js](./lib/graph-js-sdk-web.js)) and one without ([lib/graph-js-sdk-core.js](./lib/graph-js-sdk-core.js))
1718
[Refer [README.md](https://github.com/microsoftgraph/msgraph-sdk-javascript#browser) for usage]
1819
* Enum for ResponseType, which lists down the available ResponseType options in autocomplete

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ client
223223
````
224224

225225
### $search
226-
Pass a search string to `.search()` for searching message and person collections.
227-
````js
226+
Pass a search query string to `.search()` for searching in collections. Calling search multiple times will override previous search query. Refer graph [documentation](https://developer.microsoft.com/en-us/graph/docs/concepts/query_parameters#search-parameter) for more.
227+
```js
228228
client
229-
.api("/users")
230-
.search("Irene McGowen")
229+
.api("/me/messages")
230+
.search("from:admin")
231231
.get((err, res) => {
232-
console.log(res)
233-
})
234-
````
232+
console.log(res);
233+
});
234+
```
235235

236236
## Other API methods
237237

lib/graph-js-sdk-core.js

Lines changed: 2 additions & 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: 451 additions & 411 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.

lib/src/common.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export declare const PACKAGE_VERSION = "1.3.0";
1414
* @callback - The anonymous callback function
1515
*/
1616
export interface AuthProviderCallback {
17-
(error: any, accessToken: string): void;
17+
(error: any, accessToken: string | null): void;
1818
}
1919
/**
2020
* @interface {@link https://github.com/bitinn/node-fetch/#options}

src/GraphRequest.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ export class GraphRequest {
163163
return this;
164164
}
165165

166+
search(searchStr: string): GraphRequest {
167+
this.urlComponents.oDataQueryParams["$search"] = `"${searchStr}"`;
168+
return this;
169+
}
166170

167171
search(searchStr: string): GraphRequest {
168172
this.urlComponents.oDataQueryParams["$search"] = searchStr;
@@ -448,21 +452,39 @@ export class GraphRequest {
448452
return "";
449453
}
450454

455+
private parseDocumentResponse(response, type): Promise<any> {
456+
if (typeof DOMParser !== "undefined") {
457+
return new Promise((resolve, reject) => {
458+
response.text().then((xmlString) => {
459+
try {
460+
let parser = new DOMParser(),
461+
xmlDoc = parser.parseFromString(xmlString, type);
462+
resolve(xmlDoc);
463+
} catch (error) {
464+
reject(error);
465+
}
466+
});
467+
});
468+
} else {
469+
return Promise.resolve(response.body);
470+
}
471+
}
472+
451473
private convertResponseType(response: Response): Promise<any> {
452-
let responseValue: any;
453-
if (!this._responseType) {
454-
this._responseType = '';
474+
let self = this,
475+
responseValue: any;
476+
if (!self._responseType) {
477+
self._responseType = '';
455478
}
456-
switch (this._responseType.toLowerCase()) {
479+
switch (self._responseType.toLowerCase()) {
457480
case ResponseType.ARRAYBUFFER:
458481
responseValue = response.arrayBuffer();
459482
break;
460483
case ResponseType.BLOB:
461484
responseValue = response.blob();
462485
break;
463486
case ResponseType.DOCUMENT:
464-
// XMLHTTPRequest only :(
465-
responseValue = response.json();
487+
responseValue = self.parseDocumentResponse(response, "text/xml");
466488
break;
467489
case ResponseType.JSON:
468490
responseValue = response.json();
@@ -474,7 +496,29 @@ export class GraphRequest {
474496
responseValue = response.text();
475497
break;
476498
default:
477-
responseValue = response.json();
499+
let contentType = response.headers.get("Content-type");
500+
if (contentType !== null) {
501+
let mimeType = contentType.split(";")[0],
502+
documentContentTypes = ["text/html", "text/xml", "application/xml", "application/xhtml+xml"];
503+
if (documentContentTypes.includes(mimeType)) {
504+
responseValue = self.parseDocumentResponse(response, mimeType);
505+
} else {
506+
responseValue = response.json();
507+
}
508+
} else {
509+
/**
510+
* RFC specification {@link https://tools.ietf.org/html/rfc7231#section-3.1.1.5} says:
511+
* A sender that generates a message containing a payload body SHOULD
512+
* generate a Content-Type header field in that message unless the
513+
* intended media type of the enclosed representation is unknown to the
514+
* sender. If a Content-Type header field is not present, the recipient
515+
* MAY either assume a media type of "application/octet-stream"
516+
* ([RFC2046], Section 4.5.1) or examine the data to determine its type.
517+
*
518+
* So assuming it as a stream type so returning the body.
519+
*/
520+
responseValue = Promise.resolve(response.body);
521+
}
478522
break;
479523
}
480524
return responseValue;

src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ oDataQueryNames = oDataQueryNames.concat(oDataQueryNames.map((s) => "$" + s));
2222
* @callback - The anonymous callback function
2323
*/
2424
export interface AuthProviderCallback {
25-
(error: any, accessToken: string): void
25+
(error: any, accessToken: string | null): void
2626
}
2727

2828
/**

0 commit comments

Comments
 (0)