Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md with download as binary example #96

Merged
merged 5 commits into from
Aug 14, 2018
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,19 @@ You can pass in additional request headers, either individually or in a dictiona
````

### .responseType()
To set a custom response type, use the `.responseType(string)` method. To see an example, check the [browser sample](samples/browser/index.html) that downloads an image and displays it in an `<img>` element.
To set a custom response type, use the `.responseType(<ResponseType>)` method. Refer [ResponseType.ts](./src/ResponseType.ts) for available options.
````js
client
.api(`/me/drive/root/children/${fileName}/content`)
.responseType(MicrosoftGraph.ResponseType.BLOB)
.get()
.then((res) => {
console.log("Downloaded..!!");
})
.catch((err) => {
throw err;
});
````

## Running node samples
You can run and debug the node samples found under [./samples/node/node-sample.js](./samples/node/node-sample.js) by running the *Run node samples* configuration from the **Debug** (Ctrl + Shift + D) menu in Visual Studio Code. Alternately, you can run the node samples from the CLI by entering `node ./samples/node/node-sample.js` (assuming you are at the root of this repo). You'll need to rename the *secrets.example.json* file to *secrets.json* and add a valid access token to it. You can get an access token by doing the following:
Expand Down
2 changes: 1 addition & 1 deletion lib/graph-js-sdk-web.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions lib/src/GraphRequest.js

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

2 changes: 1 addition & 1 deletion lib/src/GraphRequest.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/src/ResponseType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare enum ResponseType {
ARRAYBUFFER = "arraybuffer",
BLOB = "blob",
DOCUMENT = "document",
JSON = "json",
STREAM = "stream",
TEXT = "text"
}
12 changes: 12 additions & 0 deletions lib/src/ResponseType.js

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

1 change: 1 addition & 0 deletions lib/src/ResponseType.js.map

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

2 changes: 1 addition & 1 deletion lib/src/common.js

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

1 change: 1 addition & 0 deletions lib/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export declare class Client {
export * from "./GraphRequest";
export * from "./common";
export * from "./ResponseHandler";
export * from "./ResponseType";
3 changes: 2 additions & 1 deletion lib/src/index.js

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

2 changes: 1 addition & 1 deletion lib/src/index.js.map

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

2 changes: 1 addition & 1 deletion samples/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// Example of downloading the user's profile photo and displaying it in an img tag
client
.api('/me/photo/$value')
.responseType('blob')
.responseType(MicrosoftGraph.ResponseType.BLOB)
.get((err, res, rawResponse) => {
if (err) throw err;

Expand Down
19 changes: 10 additions & 9 deletions src/GraphRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'isomorphic-fetch';
import { Options, URLComponents, GraphError, oDataQueryNames, GraphRequestCallback, PACKAGE_VERSION, DefaultRequestHeaders } from "./common"
import { ResponseHandler } from "./ResponseHandler"
import { RequestMethod } from './RequestMethod';
import { ResponseType } from "./ResponseType";
import { GraphHelper } from './GraphHelper';

export class GraphRequest {
Expand Down Expand Up @@ -329,7 +330,7 @@ export class GraphRequest {
method: RequestMethod.GET,
headers: self.getDefaultRequestHeaders(accessToken)
};
self.responseType("stream");
self.responseType(ResponseType.STREAM);
Object.keys(self._headers).forEach((key) => options.headers[key] = self._headers[key] as string);
self.handleFetch(url, callback, options);
} else {
Expand Down Expand Up @@ -420,25 +421,25 @@ export class GraphRequest {
this._responseType = '';
}
switch (this._responseType.toLowerCase()) {
case "arraybuffer":
case ResponseType.ARRAYBUFFER:
responseValue = response.arrayBuffer();
break;
case "blob":
case ResponseType.BLOB:
responseValue = response.blob();
break;
case "document":
case ResponseType.DOCUMENT:
// XMLHTTPRequest only :(
responseValue = response.json();
break;
case "json":
case ResponseType.JSON:
responseValue = response.json();
break;
case "text":
responseValue = response.text();
break;
case "stream":
case ResponseType.STREAM:
responseValue = Promise.resolve(response.body);
break;
case ResponseType.TEXT:
responseValue = response.text();
break;
default:
responseValue = response.json();
break;
Expand Down
19 changes: 19 additions & 0 deletions src/ResponseType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @enum
* Enum for ResponseType values
* @property {string} ARRAYBUFFER - To download response content as an [ArrayBuffer]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer}
* @property {string} BLOB - To download content as a [binary/blob] {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob}
* @property {string} DOCUMENT - This downloads content as a json, See [this for more info]{@link https://github.com/microsoftgraph/msgraph-sdk-javascript/pull/63}
* @property {string} JSON - To download response content as a json
* @property {string} STREAM - To download response as a [stream]{@link https://nodejs.org/api/stream.html}
* @property {string} TEXT - For downloading response as a text
*/

export enum ResponseType {
ARRAYBUFFER = "arraybuffer",
BLOB = "blob",
DOCUMENT = "document",
JSON = "json",
STREAM = "stream",
TEXT = "text"
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export class Client {
export * from "./GraphRequest";
export * from "./common";
export * from "./ResponseHandler";
export * from "./ResponseType";