@microsoft/microsoft-graph-client
Advanced tools
Comparing version 1.2.0 to 1.3.0
## Changelog | ||
### 1.2.0 | ||
Updates | ||
* 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)) | ||
[Refer [README.md](https://github.com/microsoftgraph/msgraph-sdk-javascript#browser) for usage] | ||
* Enum for ResponseType, which lists down the available ResponseType options in autocomplete | ||
Bug Fix | ||
* Cannot access the property "request-id" of undefined in GraphError handling | ||
### 1.1.0 | ||
@@ -4,0 +14,0 @@ New Features |
@@ -42,2 +42,9 @@ "use strict"; | ||
}); | ||
// Need to figure out type signature of select() to allow string, array of strings, and unlimited string parameters | ||
// cases.push({ | ||
// url: "https://graph.microsoft.com/beta/me?$select=displayName,jobTitle,mailNickname", | ||
// request: client.api("/me") | ||
// .version("beta") | ||
// .select("displayName", "jobTitle", "mailNickname") | ||
// }) | ||
cases.push({ | ||
@@ -44,0 +51,0 @@ url: "https://graph.microsoft.com/beta/me/people?$select=displayName,title&$count=true", |
@@ -18,2 +18,3 @@ "use strict"; | ||
"me?$filter=b&c=d": "https://graph.microsoft.com/v1.0/me?$filter=b&c=d", | ||
// oData params should work with and without $ | ||
"me?$select=displayName": "https://graph.microsoft.com/v1.0/me?$select=displayName", | ||
@@ -20,0 +21,0 @@ "me?select=displayName": "https://graph.microsoft.com/v1.0/me?select=displayName", |
export declare let oDataQueryNames: string[]; | ||
export declare const DEFAULT_VERSION = "v1.0"; | ||
export declare const GRAPH_BASE_URL = "https://graph.microsoft.com/"; | ||
/** | ||
* @constant | ||
* A package version | ||
* @NOTE: This should be kept up to date with the version used in package.json. | ||
* If you are changing this please ensure you are also changing it in package.json. | ||
*/ | ||
export declare const PACKAGE_VERSION = "1.2.0"; | ||
/** | ||
* @interface | ||
* Signature that defines callback for an authentication provider | ||
* @callback - The anonymous callback function | ||
*/ | ||
export interface AuthProviderCallback { | ||
(error: any, accessToken: string): void; | ||
} | ||
/** | ||
* @interface {@link https://github.com/bitinn/node-fetch/#options} | ||
* Signature to define the fetch request options for node environment | ||
* @property {number} [follow] - Maximum redirect count. 0 to not follow redirect | ||
* @property {number} [timeout] - Request/Response timeout in milliseconds, it resets on redirect. 0 to disable (OS limit applies) | ||
* @property {number} [compress] - Support gzip/deflate content encoding. false to disable | ||
* @property {number} [size] - Maximum response body size in bytes. 0 to disable | ||
* @property {any} [agent] - HTTP(S).Agent instance, allows custom proxy, certificate, lookup, family etc. | ||
*/ | ||
export interface NodeFetchInit { | ||
follow?: number; | ||
timeout?: number; | ||
compress?: boolean; | ||
size?: number; | ||
agent?: any; | ||
} | ||
/** | ||
* @interface | ||
* Signature to define the fetch api options which includes both fetch standard options and also the extended node fetch options | ||
* @extends RequestInit @see {@link https://fetch.spec.whatwg.org/#requestinit} | ||
* @extends NodeFetchInit | ||
*/ | ||
export interface FetchOptions extends RequestInit, NodeFetchInit { | ||
} | ||
/** | ||
* @interface | ||
* Options for initializing the Graph Client | ||
* @property {boolean} [debugLogging] - The boolean to enable/disable debug logging | ||
* @property {string} [defaultVersion] - The default version that needs to be used while making graph api request | ||
* @property {Function} [authProvider] - The function to get the authentication token | ||
* @property {string} [baseUrl] - Base url that needs to be appended to every request | ||
* @property {FetchOptions} [fetchOptions] - The options for fetch request | ||
*/ | ||
export interface Options { | ||
@@ -13,3 +57,15 @@ debugLogging?: boolean; | ||
baseUrl?: string; | ||
fetchOptions?: FetchOptions; | ||
} | ||
/** | ||
* @interface | ||
* Signature to define URL components | ||
* @template http://graph.microsoft.com/VERSION/PATH?QUERYSTRING&OTHER_QUERY_PARAMS | ||
* | ||
* @property {string} host - The host to which the request needs to be made | ||
* @property {string} version - Version of the graph endpoint | ||
* @property {string} [path] - The path of the resource request | ||
* @property {[key: string] : string|number} oDataQueryParams - The oData Query Params | ||
* @property {[key: string] : string|number} otherURLQueryParams - The other query params for a request | ||
*/ | ||
export interface URLComponents { | ||
@@ -26,2 +82,8 @@ host: string; | ||
} | ||
/** | ||
* @interface | ||
* Signature to define Default request headers | ||
* @property {string} Authorization - The authorization header | ||
* @property {string} SdkVersion - The sdk version header | ||
*/ | ||
export interface DefaultRequestHeaders { | ||
@@ -31,5 +93,24 @@ Authorization: string; | ||
} | ||
/** | ||
* @interface | ||
* Signature to define the GraphRequest callback | ||
* @callback - The anonymous callback function | ||
*/ | ||
export interface GraphRequestCallback { | ||
(error: GraphError, response: any, rawResponse?: any): void; | ||
} | ||
/** | ||
* @interface | ||
* Signature to represent the Graph error object | ||
* @NOTE: This is NOT what is returned from the Graph | ||
* GraphError is created from parsing JSON errors returned from the graph | ||
* Some fields are renamed ie, "request-id" => requestId so you can use dot notation | ||
* | ||
* @property {number} statusCode - The status code of the error | ||
* @property {string} code - The code to represent the request | ||
* @property {string} message - The error message | ||
* @property {string} requestId - The identifier for the request | ||
* @property {Date} date - The request processed date and time | ||
* @property {string} body - The original error response by the graph | ||
*/ | ||
export interface GraphError { | ||
@@ -36,0 +117,0 @@ statusCode: number; |
@@ -6,4 +6,14 @@ "use strict"; | ||
exports.GRAPH_BASE_URL = "https://graph.microsoft.com/"; | ||
/** | ||
* @constant | ||
* A package version | ||
* @NOTE: This should be kept up to date with the version used in package.json. | ||
* If you are changing this please ensure you are also changing it in package.json. | ||
*/ | ||
exports.PACKAGE_VERSION = "1.2.0"; | ||
/** | ||
* @extends | ||
* support oData params with and without $ prefix | ||
*/ | ||
exports.oDataQueryNames = exports.oDataQueryNames.concat(exports.oDataQueryNames.map(function (s) { return "$" + s; })); | ||
//# sourceMappingURL=common.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var GraphHelper = (function () { | ||
var GraphHelper = /** @class */ (function () { | ||
function GraphHelper() { | ||
} | ||
/* | ||
This conversion is required due to the following reasons: | ||
1. Body parameter of Request method of isomorphic-fetch only accepts Blob, ArrayBuffer, FormData, TypedArrays, string. | ||
2. Node.js platform does not suppport Blob, FormData. Javascript File object inherits from Blob so it is also | ||
not supported in node. Therefore content of type Blob, File, FormData will only come from browsers. | ||
3. Parallel to Javascript's arrayBuffer, node provides Buffer interface. Node's Buffer is able to send the arbitary | ||
binary data to the server successfully for both Browser and Node platform. Whereas sending binary data via | ||
ArrayBuffer or TypedArrays was only possible using Browser. To support both Node and Browser, `serializeContent` | ||
converts TypedArrays or ArrayBuffer to `Node Buffer`. | ||
4. If the data received is in JSON format, `serializeContent` converts the JSON to string. | ||
*/ | ||
GraphHelper.serializeContent = function (content) { | ||
@@ -7,0 +18,0 @@ var className = content.constructor.name; |
import { Promise } from 'es6-promise'; | ||
import 'isomorphic-fetch'; | ||
import { Options, URLComponents, GraphRequestCallback } from "./common"; | ||
import { Options, URLComponents, GraphRequestCallback, FetchOptions } from "./common"; | ||
export declare class GraphRequest { | ||
config: Options; | ||
urlComponents: URLComponents; | ||
_options: FetchOptions; | ||
_headers: { | ||
[key: string]: string | number; | ||
[key: string]: string; | ||
}; | ||
@@ -16,2 +17,6 @@ _responseType: string; | ||
}): this; | ||
option(key: string, value: any): this; | ||
options(options: { | ||
[key: string]: any; | ||
}): this; | ||
parsePath(rawPath: string): void; | ||
@@ -30,19 +35,55 @@ private urlJoin; | ||
responseType(responseType: string): GraphRequest; | ||
private addCsvQueryParamater; | ||
private addCsvQueryParameter; | ||
delete(callback?: GraphRequestCallback): Promise<any>; | ||
/** | ||
* Alias for delete call | ||
*/ | ||
del(callback?: GraphRequestCallback): Promise<any>; | ||
patch(content: any, callback?: GraphRequestCallback): Promise<any>; | ||
post(content: any, callback?: GraphRequestCallback): Promise<any>; | ||
/** | ||
* Alias for Post call | ||
*/ | ||
create(content: any, callback?: GraphRequestCallback): Promise<any>; | ||
put(content: any, callback?: GraphRequestCallback): Promise<any>; | ||
create(content: any, callback?: GraphRequestCallback): Promise<any>; | ||
/** | ||
* Alias for update call | ||
*/ | ||
update(content: any, callback?: GraphRequestCallback): Promise<any>; | ||
del(callback?: GraphRequestCallback): Promise<any>; | ||
get(callback?: GraphRequestCallback): Promise<any>; | ||
getStream(callback: GraphRequestCallback): Promise<any>; | ||
putStream(stream: any, callback: GraphRequestCallback): Promise<any>; | ||
/** | ||
* @private | ||
* Sends request and routes response to the callback or resolves to promise | ||
* @param {RequestInfo} request - The Request object or url string value | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @param {GraphRequestCallback} callback - The callback that needs to be called on response | ||
* @return The promise in case if the callback param is empty | ||
*/ | ||
private sendRequestAndRouteResponse; | ||
/** | ||
* @private | ||
* Gets the Promise that will resolve or reject with fetch api request | ||
* @param {RequestInfo} request - The Request object or url string value | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @return The Promise that resolves with Response | ||
*/ | ||
private routeResponseToPromise; | ||
private handleFetch; | ||
/** | ||
* @private | ||
* Makes request to the service by getting auth token from the auth provider | ||
* @param {RequestInfo} request - The Request object or url string value | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @param {GraphRequestCallback} callback - The callback function | ||
*/ | ||
private routeResponseToCallback; | ||
private sendRequestAndRouteResponse; | ||
getStream(callback: GraphRequestCallback): void; | ||
putStream(stream: any, callback: GraphRequestCallback): void; | ||
private getDefaultRequestHeaders; | ||
private configureRequest; | ||
/** | ||
* @private | ||
* Customizes the fetch options with the Auth token, SDKVersion header and customization applied via init, .header, .headers, .option, .options etc | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @param {string} accessToken - The access token value | ||
* @return The fetch options with customization | ||
*/ | ||
private configureRequestOptions; | ||
query(queryDictionaryOrString: string | { | ||
@@ -49,0 +90,0 @@ [key: string]: string | number; |
@@ -10,34 +10,58 @@ "use strict"; | ||
var GraphHelper_1 = require("./GraphHelper"); | ||
var GraphRequest = (function () { | ||
var GraphRequest = /** @class */ (function () { | ||
function GraphRequest(config, path) { | ||
this.config = config; | ||
this._headers = {}; | ||
this.urlComponents = { | ||
host: this.config.baseUrl, | ||
version: this.config.defaultVersion, | ||
var self = this; | ||
self.config = config; | ||
self._options = {}; | ||
self._headers = {}; | ||
self.urlComponents = { | ||
host: self.config.baseUrl, | ||
version: self.config.defaultVersion, | ||
oDataQueryParams: {}, | ||
otherURLQueryParams: {} | ||
}; | ||
this.parsePath(path); | ||
self.parsePath(path); | ||
} | ||
GraphRequest.prototype.header = function (headerKey, headerValue) { | ||
this._headers[headerKey] = headerValue; | ||
return this; | ||
var self = this; | ||
self._headers[headerKey] = headerValue; | ||
return self; | ||
}; | ||
GraphRequest.prototype.headers = function (headers) { | ||
var self = this; | ||
for (var key in headers) { | ||
this._headers[key] = headers[key]; | ||
self._headers[key] = headers[key]; | ||
} | ||
return this; | ||
return self; | ||
}; | ||
GraphRequest.prototype.option = function (key, value) { | ||
var self = this; | ||
self._options[key] = value; | ||
return self; | ||
}; | ||
GraphRequest.prototype.options = function (options) { | ||
var self = this; | ||
for (var key in options) { | ||
self._options[key] = options[key]; | ||
} | ||
return self; | ||
}; | ||
GraphRequest.prototype.parsePath = function (rawPath) { | ||
// break rawPath into this.urlComponents | ||
// strip out the base url if they passed it in | ||
if (rawPath.indexOf("https://") != -1) { | ||
rawPath = rawPath.replace("https://", ""); | ||
// find where the host ends | ||
var endOfHostStrPos = rawPath.indexOf("/"); | ||
this.urlComponents.host = "https://" + rawPath.substring(0, endOfHostStrPos); | ||
this.urlComponents.host = "https://" + rawPath.substring(0, endOfHostStrPos); // parse out the host | ||
// strip the host from rawPath | ||
rawPath = rawPath.substring(endOfHostStrPos + 1, rawPath.length); | ||
// then remove the following version | ||
var endOfVersionStrPos = rawPath.indexOf("/"); | ||
// parse out the version | ||
this.urlComponents.version = rawPath.substring(0, endOfVersionStrPos); | ||
// strip version from rawPath | ||
rawPath = rawPath.substring(endOfVersionStrPos + 1, rawPath.length); | ||
} | ||
// strip out any leading "/" | ||
if (rawPath.charAt(0) == "/") { | ||
@@ -47,3 +71,5 @@ rawPath = rawPath.substr(1); | ||
var queryStrPos = rawPath.indexOf("?"); | ||
// let afterPath = | ||
if (queryStrPos == -1) { | ||
// no query string | ||
this.urlComponents.path = rawPath; | ||
@@ -53,5 +79,10 @@ } | ||
this.urlComponents.path = rawPath.substr(0, queryStrPos); | ||
// capture query string into | ||
// this.urlComponents.oDataQueryParams | ||
// and | ||
// this.urlComponents.otherURLQueryParams | ||
var queryParams = rawPath.substring(queryStrPos + 1, rawPath.length).split("&"); | ||
for (var _i = 0, queryParams_1 = queryParams; _i < queryParams_1.length; _i++) { | ||
var queryParam = queryParams_1[_i]; | ||
//queryParam: a=b | ||
var queryParams_2 = queryParam.split("="); | ||
@@ -90,12 +121,18 @@ var key = queryParams_2[0]; | ||
}; | ||
/* | ||
* Accepts .select("displayName,birthday") | ||
* and .select(["displayName", "birthday"]) | ||
* and .select("displayName", "birthday") | ||
* | ||
*/ | ||
GraphRequest.prototype.select = function (properties) { | ||
this.addCsvQueryParamater("$select", properties, arguments); | ||
this.addCsvQueryParameter("$select", properties, arguments); | ||
return this; | ||
}; | ||
GraphRequest.prototype.expand = function (properties) { | ||
this.addCsvQueryParamater("$expand", properties, arguments); | ||
this.addCsvQueryParameter("$expand", properties, arguments); | ||
return this; | ||
}; | ||
GraphRequest.prototype.orderby = function (properties) { | ||
this.addCsvQueryParamater("$orderby", properties, arguments); | ||
this.addCsvQueryParameter("$orderby", properties, arguments); | ||
return this; | ||
@@ -127,3 +164,5 @@ }; | ||
}; | ||
GraphRequest.prototype.addCsvQueryParamater = function (propertyName, propertyValue, additionalProperties) { | ||
// helper for $select, $expand and $orderby (must be comma separated) | ||
GraphRequest.prototype.addCsvQueryParameter = function (propertyName, propertyValue, additionalProperties) { | ||
// if there are already $propertyName value there, append a "," | ||
this.urlComponents.oDataQueryParams[propertyName] = this.urlComponents.oDataQueryParams[propertyName] ? this.urlComponents.oDataQueryParams[propertyName] + "," : ""; | ||
@@ -134,5 +173,6 @@ var allValues = []; | ||
} | ||
else { | ||
else { // propertyValue passed in as array | ||
allValues = allValues.concat(propertyValue); | ||
} | ||
// merge in additionalProperties | ||
if (additionalProperties.length > 1 && typeof propertyValue === "string") { | ||
@@ -144,46 +184,106 @@ allValues = Array.prototype.slice.call(additionalProperties); | ||
GraphRequest.prototype.delete = function (callback) { | ||
var url = this.buildFullUrl(); | ||
return this.sendRequestAndRouteResponse(new Request(url, { method: RequestMethod_1.RequestMethod.DELETE, headers: new Headers() }), callback); | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.DELETE | ||
}; | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
/** | ||
* Alias for delete call | ||
*/ | ||
GraphRequest.prototype.del = function (callback) { | ||
return this.delete(callback); | ||
}; | ||
GraphRequest.prototype.patch = function (content, callback) { | ||
var url = this.buildFullUrl(); | ||
return this.sendRequestAndRouteResponse(new Request(url, { | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.PATCH, | ||
body: GraphHelper_1.GraphHelper.serializeContent(content), | ||
headers: new Headers({ 'Content-Type': 'application/json' }) | ||
}), callback); | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}; | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
GraphRequest.prototype.post = function (content, callback) { | ||
var url = this.buildFullUrl(); | ||
return this.sendRequestAndRouteResponse(new Request(url, { | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.POST, | ||
body: GraphHelper_1.GraphHelper.serializeContent(content), | ||
headers: new Headers((content.constructor !== undefined && content.constructor.name === "FormData") ? {} : { 'Content-Type': 'application/json' }) | ||
}), callback); | ||
headers: (content.constructor !== undefined && content.constructor.name === "FormData") ? {} : { | ||
"Content-Type": "application/json" | ||
} | ||
}; | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
/** | ||
* Alias for Post call | ||
*/ | ||
GraphRequest.prototype.create = function (content, callback) { | ||
return this.post(content, callback); | ||
}; | ||
GraphRequest.prototype.put = function (content, callback) { | ||
var url = this.buildFullUrl(); | ||
return this.sendRequestAndRouteResponse(new Request(url, { | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.PUT, | ||
body: GraphHelper_1.GraphHelper.serializeContent(content), | ||
headers: new Headers({ 'Content-Type': 'application/octet-stream' }) | ||
}), callback); | ||
headers: { | ||
"Content-Type": "application/octet-stream" | ||
} | ||
}; | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
GraphRequest.prototype.create = function (content, callback) { | ||
return this.post(content, callback); | ||
}; | ||
/** | ||
* Alias for update call | ||
*/ | ||
GraphRequest.prototype.update = function (content, callback) { | ||
return this.patch(content, callback); | ||
}; | ||
GraphRequest.prototype.del = function (callback) { | ||
return this.delete(callback); | ||
}; | ||
GraphRequest.prototype.get = function (callback) { | ||
var url = this.buildFullUrl(); | ||
return this.sendRequestAndRouteResponse(new Request(url, { method: RequestMethod_1.RequestMethod.GET, headers: new Headers() }), callback); | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.GET | ||
}; | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
GraphRequest.prototype.routeResponseToPromise = function (request) { | ||
GraphRequest.prototype.getStream = function (callback) { | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.GET | ||
}; | ||
self.responseType(ResponseType_1.ResponseType.STREAM); | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
GraphRequest.prototype.putStream = function (stream, callback) { | ||
var self = this, url = self.buildFullUrl(), options = { | ||
method: RequestMethod_1.RequestMethod.PUT, | ||
headers: { | ||
"Content-Type": "application/octet-stream", | ||
}, | ||
body: stream | ||
}; | ||
return self.sendRequestAndRouteResponse(url, options, callback); | ||
}; | ||
/** | ||
* @private | ||
* Sends request and routes response to the callback or resolves to promise | ||
* @param {RequestInfo} request - The Request object or url string value | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @param {GraphRequestCallback} callback - The callback that needs to be called on response | ||
* @return The promise in case if the callback param is empty | ||
*/ | ||
GraphRequest.prototype.sendRequestAndRouteResponse = function (request, options, callback) { | ||
// return a promise when Promises are supported and no callback was provided | ||
if (callback == null && typeof es6_promise_1.Promise !== "undefined") { | ||
return this.routeResponseToPromise(request, options); | ||
} | ||
else { | ||
this.routeResponseToCallback(request, options, callback); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* Gets the Promise that will resolve or reject with fetch api request | ||
* @param {RequestInfo} request - The Request object or url string value | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @return The Promise that resolves with Response | ||
*/ | ||
GraphRequest.prototype.routeResponseToPromise = function (request, options) { | ||
var _this = this; | ||
return new es6_promise_1.Promise(function (resolve, reject) { | ||
_this.routeResponseToCallback(request, function (err, body) { | ||
_this.routeResponseToCallback(request, options, function (err, body) { | ||
if (err != null) { | ||
@@ -198,20 +298,25 @@ reject(err); | ||
}; | ||
GraphRequest.prototype.handleFetch = function (request, callback, options) { | ||
/** | ||
* @private | ||
* Makes request to the service by getting auth token from the auth provider | ||
* @param {RequestInfo} request - The Request object or url string value | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @param {GraphRequestCallback} callback - The callback function | ||
*/ | ||
GraphRequest.prototype.routeResponseToCallback = function (request, options, callback) { | ||
var _this = this; | ||
((request.constructor.name === "Request") ? fetch(request) : fetch(request, options)).then(function (response) { | ||
_this.convertResponseType(response).then(function (responseValue) { | ||
ResponseHandler_1.ResponseHandler.init(response, undefined, responseValue, callback); | ||
}).catch(function (error) { | ||
ResponseHandler_1.ResponseHandler.init(response, error, undefined, callback); | ||
}); | ||
}).catch(function (error) { | ||
ResponseHandler_1.ResponseHandler.init(undefined, error, undefined, callback); | ||
}); | ||
}; | ||
GraphRequest.prototype.routeResponseToCallback = function (request, callback) { | ||
if (callback === void 0) { callback = function () { }; } | ||
var self = this; | ||
self.config.authProvider(function (err, accessToken) { | ||
if (err == null && accessToken != null) { | ||
request = self.configureRequest(request, accessToken); | ||
self.handleFetch(request, callback); | ||
options = self.configureRequestOptions(options, accessToken); | ||
fetch(request, options).then(function (response) { | ||
_this.convertResponseType(response).then(function (responseValue) { | ||
ResponseHandler_1.ResponseHandler.init(response, undefined, responseValue, callback); | ||
}).catch(function (error) { | ||
ResponseHandler_1.ResponseHandler.init(response, error, undefined, callback); | ||
}); | ||
}).catch(function (error) { | ||
ResponseHandler_1.ResponseHandler.init(undefined, error, undefined, callback); | ||
}); | ||
} | ||
@@ -223,61 +328,24 @@ else { | ||
}; | ||
GraphRequest.prototype.sendRequestAndRouteResponse = function (request, callback) { | ||
if (callback == null && typeof es6_promise_1.Promise !== "undefined") { | ||
return this.routeResponseToPromise(request); | ||
} | ||
else { | ||
this.routeResponseToCallback(request, callback || function () { }); | ||
} | ||
}; | ||
GraphRequest.prototype.getStream = function (callback) { | ||
var self = this; | ||
self.config.authProvider(function (err, accessToken) { | ||
if (err === null && accessToken !== null) { | ||
var url = self.buildFullUrl(); | ||
var options_1 = { | ||
method: RequestMethod_1.RequestMethod.GET, | ||
headers: self.getDefaultRequestHeaders(accessToken) | ||
}; | ||
self.responseType(ResponseType_1.ResponseType.STREAM); | ||
Object.keys(self._headers).forEach(function (key) { return options_1.headers[key] = self._headers[key]; }); | ||
self.handleFetch(url, callback, options_1); | ||
} | ||
else { | ||
callback(err, null); | ||
} | ||
}); | ||
}; | ||
GraphRequest.prototype.putStream = function (stream, callback) { | ||
var self = this; | ||
self.config.authProvider(function (err, accessToken) { | ||
if (err === null && accessToken !== null) { | ||
var url = self.buildFullUrl(); | ||
var options_2 = { | ||
method: RequestMethod_1.RequestMethod.PUT, | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
}, | ||
body: stream | ||
}; | ||
var defaultHeaders_1 = self.getDefaultRequestHeaders(accessToken); | ||
Object.keys(defaultHeaders_1).forEach(function (key) { return options_2.headers[key] = defaultHeaders_1[key]; }); | ||
Object.keys(self._headers).forEach(function (key) { return options_2.headers[key] = self._headers[key]; }); | ||
self.handleFetch(url, callback, options_2); | ||
} | ||
}); | ||
}; | ||
GraphRequest.prototype.getDefaultRequestHeaders = function (accessToken) { | ||
return { | ||
/** | ||
* @private | ||
* Customizes the fetch options with the Auth token, SDKVersion header and customization applied via init, .header, .headers, .option, .options etc | ||
* @param {FetchOptions} options - The options for the fetch api request | ||
* @param {string} accessToken - The access token value | ||
* @return The fetch options with customization | ||
*/ | ||
GraphRequest.prototype.configureRequestOptions = function (options, accessToken) { | ||
var self = this, defaultHeaders = { | ||
Authorization: "Bearer " + accessToken, | ||
SdkVersion: "graph-js-" + common_1.PACKAGE_VERSION | ||
}; | ||
var configuredOptions = { | ||
headers: {} | ||
}; | ||
Object.assign(configuredOptions, self.config.fetchOptions, self._options, options); | ||
Object.assign(configuredOptions.headers, defaultHeaders, self._headers, options.headers); | ||
return configuredOptions; | ||
}; | ||
GraphRequest.prototype.configureRequest = function (request, accessToken) { | ||
var self = this, defaultHeaders = self.getDefaultRequestHeaders(accessToken); | ||
Object.keys(defaultHeaders).forEach(function (key) { return request.headers.set(key, defaultHeaders[key]); }); | ||
Object.keys(self._headers).forEach(function (key) { return request.headers.set(key, self._headers[key]); }); | ||
return request; | ||
}; | ||
// append query strings to the url, accepts either a string like $select=displayName or a dictionary {"$select": "displayName"} | ||
GraphRequest.prototype.query = function (queryDictionaryOrString) { | ||
if (typeof queryDictionaryOrString === "string") { | ||
if (typeof queryDictionaryOrString === "string") { // is string | ||
var queryStr = queryDictionaryOrString; | ||
@@ -288,3 +356,3 @@ var queryKey = queryStr.split("=")[0]; | ||
} | ||
else { | ||
else { // is dictionary | ||
for (var key in queryDictionaryOrString) { | ||
@@ -296,3 +364,6 @@ this.urlComponents.otherURLQueryParams[key] = queryDictionaryOrString[key]; | ||
}; | ||
// ex: ?$select=displayName&$filter=startsWith(displayName, 'A') | ||
// does not include starting ? | ||
GraphRequest.prototype.createQueryString = function () { | ||
// need to combine first this.urlComponents.oDataQueryParams and this.urlComponents.otherURLQueryParams | ||
var q = []; | ||
@@ -327,2 +398,3 @@ if (Object.keys(this.urlComponents.oDataQueryParams).length != 0) { | ||
case ResponseType_1.ResponseType.DOCUMENT: | ||
// XMLHTTPRequest only :( | ||
responseValue = response.json(); | ||
@@ -329,0 +401,0 @@ break; |
@@ -11,2 +11,5 @@ import { Options } from "./common"; | ||
export * from "./ResponseHandler"; | ||
export * from "./tasks/OneDriveLargeFileUploadTask"; | ||
export * from "./ResponseType"; | ||
export * from "./content/BatchRequestContent"; | ||
export * from "./content/BatchResponseContent"; |
@@ -8,4 +8,5 @@ "use strict"; | ||
var GraphRequest_1 = require("./GraphRequest"); | ||
var Client = (function () { | ||
var Client = /** @class */ (function () { | ||
function Client() { | ||
// specify client defaults | ||
this.config = { | ||
@@ -24,2 +25,5 @@ debugLogging: false, | ||
}; | ||
/* | ||
* Entry point for calling api | ||
*/ | ||
Client.prototype.api = function (path) { | ||
@@ -34,3 +38,6 @@ return new GraphRequest_1.GraphRequest(this.config, path); | ||
__export(require("./ResponseHandler")); | ||
__export(require("./tasks/OneDriveLargeFileUploadTask")); | ||
__export(require("./ResponseType")); | ||
__export(require("./content/BatchRequestContent")); | ||
__export(require("./content/BatchResponseContent")); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ResponseHandler = (function () { | ||
var ResponseHandler = /** @class */ (function () { | ||
function ResponseHandler() { | ||
} | ||
ResponseHandler.init = function (res, err, resContents, callback) { | ||
if (res && res.ok) { | ||
if (res && res.ok) { // 2xx | ||
callback(null, resContents, res); | ||
} | ||
else { | ||
else { // not OK response | ||
if (err == null && res != null) | ||
if (resContents != null && resContents.error != null) | ||
if (resContents != null && resContents.error != null) // if error was passed to body | ||
callback(ResponseHandler.buildGraphErrorFromResponseObject(resContents.error, res.status), null, res); | ||
else | ||
callback(ResponseHandler.defaultGraphError(res.status), null, res); | ||
else | ||
else // pass back error as first param | ||
callback(ResponseHandler.ParseError(err), null, res); | ||
} | ||
}; | ||
/* | ||
Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo | ||
{ | ||
"error": { | ||
"code": "SearchEvents", | ||
"message": "The parameter $search is not currently supported on the Events resource.", | ||
"innerError": { | ||
"request-id": "b31c83fd-944c-4663-aa50-5d9ceb367e19", | ||
"date": "2016-11-17T18:37:45" | ||
} | ||
} | ||
} | ||
*/ | ||
ResponseHandler.ParseError = function (rawErr) { | ||
// if we couldn't find an error obj to parse, just return an object with a status code and date | ||
if (!rawErr) { | ||
@@ -22,0 +36,0 @@ return ResponseHandler.defaultGraphError(-1); |
@@ -0,1 +1,11 @@ | ||
/** | ||
* @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 declare enum ResponseType { | ||
@@ -2,0 +12,0 @@ ARRAYBUFFER = "arraybuffer", |
"use strict"; | ||
/** | ||
* @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 | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -3,0 +13,0 @@ var ResponseType; |
{ | ||
"name": "@microsoft/microsoft-graph-client", | ||
"//": "NOTE: The version here should match exactly the exported const PACKAGE_VERSION in common.ts. If you change it here, also change it there.", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Microsoft Graph Client Library", | ||
@@ -28,3 +28,5 @@ "main": "lib/src/index.js", | ||
"test": "mocha lib/spec/core", | ||
"test:types": "tsc --p spec/types && mocha spec/types" | ||
"test:types": "tsc -p spec && mocha spec/types", | ||
"test:tasks": "tsc -p spec && mocha spec/tasks", | ||
"test:content": "tsc -p spec && mocha spec/content" | ||
}, | ||
@@ -31,0 +33,0 @@ "dependencies": { |
@@ -250,2 +250,10 @@ # Microsoft Graph JavaScript Client Library | ||
### .option() and .options() | ||
You can pass in additional request options, either individually or in a dictionay. Options can be [node specific](https://github.com/bitinn/node-fetch#options) or [from fetch standard](https://fetch.spec.whatwg.org/#requestinit) | ||
```js | ||
.option("someOptionName", "someOptionValue") | ||
// or | ||
.options({"someOptionName":"someOptionValue"}) | ||
``` | ||
### .responseType() | ||
@@ -266,2 +274,6 @@ To set a custom response type, use the `.responseType(<ResponseType>)` method. Refer [ResponseType.ts](./src/ResponseType.ts) for available options. | ||
## Usage Resources | ||
1. [Large File Upload Task](/docs/tasks/LargeFileUploadTask.md) | ||
2. [Batching](/docs/content/Batching.md) | ||
## Running node samples | ||
@@ -268,0 +280,0 @@ 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: |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
355022
54
2607
364
6