Skip to content

Commit d2636ca

Browse files
author
REDMOND\lahuey
committed
Removed dependency on superagent
Requests are now processed using the fetch API Response Handler needs some refactoring to handle the error vs bad response
1 parent c91286e commit d2636ca

File tree

5 files changed

+82
-49
lines changed

5 files changed

+82
-49
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
],
1010
"types": "./lib/src/index.d.ts",
1111
"devDependencies": {
12-
"@types/superagent": "^2.0.36",
1312
"@types/mocha": "^2.2.34",
1413
"browserify": "^13.1.0",
1514
"mocha": "^3.2.0",
@@ -21,7 +20,6 @@
2120
"test:types": "tsc --p spec/types && mocha spec/types"
2221
},
2322
"dependencies": {
24-
"superagent": "^3.5.2",
2523
"es6-promise": "^4.1.0"
2624
},
2725
"repository": {

src/GraphRequest.ts

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as request from 'superagent';
21
import { Promise } from 'es6-promise'
32

43

54
import { Options, URLComponents, GraphError, oDataQueryNames, GraphRequestCallback } from "./common"
65
import { ResponseHandler } from "./ResponseHandler"
6+
import { RequestMethod } from './RequestMethod';
77

88
const packageInfo = require('../../package.json');
99

@@ -199,38 +199,40 @@ export class GraphRequest {
199199

200200
delete(callback?:GraphRequestCallback):Promise<any> {
201201
let url = this.buildFullUrl();
202-
return this.sendRequestAndRouteResponse(request.del(url), callback)
202+
return this.sendRequestAndRouteResponse(
203+
new Request(url, { method: RequestMethod.DELETE, headers: new Headers() }),
204+
callback
205+
);
203206
}
204207

205208
patch(content:any, callback?:GraphRequestCallback):Promise<any> {
206209
let url = this.buildFullUrl();
207210

208211
return this.sendRequestAndRouteResponse(
209-
request
210-
.patch(url)
211-
.send(content),
212+
new Request(url, { method: RequestMethod.PATCH, body: content, headers: new Headers() }),
212213
callback
213214
);
214215
}
215216

216217
post(content:any, callback?:GraphRequestCallback):Promise<any> {
217218
let url = this.buildFullUrl();
218219
return this.sendRequestAndRouteResponse(
219-
request
220-
.post(url)
221-
.send(content),
222-
callback
220+
new Request(url, { method: RequestMethod.POST, body: content, headers: new Headers() }),
221+
callback
223222
);
224223
}
225224

226225
put(content:any, callback?:GraphRequestCallback):Promise<any> {
227226
let url = this.buildFullUrl();
228227
return this.sendRequestAndRouteResponse(
229-
request
230-
.put(url)
231-
.type('application/octet-stream')
232-
.send(content),
233-
callback
228+
new Request(
229+
url,
230+
{
231+
method: RequestMethod.PUT,
232+
body: content,
233+
headers: new Headers({ 'Content-Type' : 'application/octet-stream' })
234+
}),
235+
callback
234236
);
235237
}
236238

@@ -252,17 +254,16 @@ export class GraphRequest {
252254
get(callback?:GraphRequestCallback):Promise<any> {
253255
let url = this.buildFullUrl();
254256
return this.sendRequestAndRouteResponse(
255-
request
256-
.get(url),
257-
callback
257+
new Request(url, { method: RequestMethod.GET, headers: new Headers() }),
258+
callback
258259
);
259260
}
260261

261262

262263
// Given the built SuperAgentRequest, get an auth token from the authProvider, make the request and return a promise
263-
private routeResponseToPromise(requestBuilder:request.SuperAgentRequest) {
264+
private routeResponseToPromise(request: Request) {
264265
return new Promise((resolve, reject) => {
265-
this.routeResponseToCallback(requestBuilder, (err, body) => {
266+
this.routeResponseToCallback(request, (err, body) => {
266267
if (err != null) {
267268
reject(err);
268269
} else {
@@ -273,12 +274,17 @@ export class GraphRequest {
273274
}
274275

275276
// Given the built SuperAgentRequest, get an auth token from the authProvider, make the request and invoke the callback
276-
private routeResponseToCallback(requestBuilder:request.SuperAgentRequest, callback: GraphRequestCallback) {
277+
private routeResponseToCallback(request: Request, callback: GraphRequestCallback) {
277278
this.config.authProvider((err, accessToken) => {
278279
if (err == null && accessToken != null) {
279-
let request = this.configureRequest(requestBuilder, accessToken);
280-
request.end((err, res) => {
281-
ResponseHandler.init(err, res, callback)
280+
fetch(this.configureRequest(request, accessToken)).then((response) => {
281+
this.convertResponseType(response).then((responseValue) => {
282+
ResponseHandler.init(response, undefined, responseValue, callback);
283+
}).catch((error) => {
284+
ResponseHandler.init(response, error, undefined, callback)
285+
});
286+
}).catch((error) => {
287+
ResponseHandler.init(undefined, error, undefined, callback)
282288
});
283289
} else {
284290
callback(err, null, null);
@@ -290,20 +296,22 @@ export class GraphRequest {
290296
* Help method that's called from the final actions( .get(), .post(), etc.) that after making the request either invokes
291297
* routeResponseToCallback() or routeResponseToPromise()
292298
*/
293-
private sendRequestAndRouteResponse(requestBuilder:request.SuperAgentRequest, callback?:GraphRequestCallback):Promise<any> {
299+
private sendRequestAndRouteResponse(request: Request, callback?:GraphRequestCallback):Promise<any> {
294300
// return a promise when Promises are supported and no callback was provided
295301
if (callback == null && typeof Promise !== "undefined") {
296-
return this.routeResponseToPromise(requestBuilder);
302+
return this.routeResponseToPromise(request);
297303
} else {
298-
this.routeResponseToCallback(requestBuilder, callback || function(){});
304+
this.routeResponseToCallback(request, callback || function(){});
299305
}
300306
}
301307

302308
getStream(callback:GraphRequestCallback) {
303309
this.config.authProvider((err, accessToken) => {
304310
if (err === null && accessToken !== null) {
305311
let url = this.buildFullUrl();
306-
callback(null, this.configureRequest(request.get(url), accessToken));
312+
callback(null, this.configureRequest(
313+
new Request(url, { method: RequestMethod.GET, headers: new Headers()}),
314+
accessToken));
307315
} else {
308316
callback(err, null);
309317
}
@@ -314,8 +322,15 @@ export class GraphRequest {
314322
this.config.authProvider((err, accessToken) => {
315323
if (err === null && accessToken !== null) {
316324
let url = this.buildFullUrl();
317-
let req:request.Request = this.configureRequest(request.put(url), accessToken);
318-
req.type('application/octet-stream');
325+
let req: Request = this.configureRequest(
326+
new Request(
327+
url,
328+
{
329+
method: RequestMethod.PUT,
330+
headers: new Headers({ 'Content-Type' : 'application/octet-stream' })
331+
}),
332+
accessToken
333+
);
319334
stream
320335
.pipe(req)
321336
.on('error', function(err) {
@@ -328,16 +343,14 @@ export class GraphRequest {
328343
});
329344
}
330345

331-
private configureRequest(requestBuilder:request.SuperAgentRequest, accessToken:string):request.SuperAgentRequest {
332-
let request = requestBuilder
333-
.set('Authorization', 'Bearer ' + accessToken)
334-
.set(this._headers)
335-
.set('SdkVersion', "graph-js-" + packageInfo.version)
346+
private configureRequest(request: Request, accessToken:string): Request {
347+
request.headers.append('Authorization', 'Bearer ' + accessToken);
336348

337-
if (this._responseType !== undefined) {
338-
request.responseType(this._responseType);
349+
for (const key in this._headers) {
350+
request.headers.append(key, this._headers[key] as string);
339351
}
340352

353+
request.headers.append('SdkVersion', "graph-js-" + packageInfo.version);
341354
return request;
342355
}
343356

@@ -382,4 +395,27 @@ export class GraphRequest {
382395

383396
return "";
384397
}
398+
399+
private convertResponseType(response: Response): Promise<any> {
400+
let responseValue: any;
401+
switch (this._responseType.toLowerCase()) {
402+
case "arraybuffer" :
403+
responseValue = response.arrayBuffer();
404+
break;
405+
case "blob" :
406+
responseValue = response.blob();
407+
break;
408+
case "document" :
409+
// XMLHTTPRequest only :(
410+
responseValue = response.text();
411+
break;
412+
case "json" :
413+
responseValue = response.json();
414+
break;
415+
default:
416+
responseValue = response.text();
417+
break;
418+
}
419+
return responseValue;
420+
}
385421
}

src/RequestMethod.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum RequestMethod {
2+
GET = 'GET',
3+
PATCH = 'PATCH',
4+
POST = 'POST',
5+
PUT = 'PUT',
6+
DELETE = 'DELETE'
7+
}

src/ResponseHandler.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import {GraphRequest} from "./GraphRequest"
22
import {GraphRequestCallback, GraphError} from "./common"
33

44
export class ResponseHandler {
5-
static init(err, res, callback:GraphRequestCallback):void {
5+
static init(res, err, resContents, callback:GraphRequestCallback):void {
66
if (res && res.ok) { // 2xx
7-
callback(null, res.body, res)
7+
callback(null, resContents, res)
88
} else { // not OK response
99
if (err == null && res.error !== null) // if error was passed to body
1010
callback(ResponseHandler.ParseError(res), null, res);
@@ -13,7 +13,6 @@ export class ResponseHandler {
1313
}
1414
}
1515

16-
1716
/*
1817
Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo
1918
{
@@ -30,15 +29,10 @@ export class ResponseHandler {
3029
static ParseError(rawErr):GraphError {
3130
let errObj; // path to object containing innerError (see above schema)
3231

33-
if (!('rawResponse' in rawErr)) { // if superagent correctly parsed the JSON
3432
if (rawErr.response !== undefined && rawErr.response.body !== null && 'error' in rawErr.response.body) { // some 404s don't return an error object
3533
errObj = rawErr.response.body.error;
3634
}
37-
} else {
38-
// if there was an error parsing the JSON
39-
// possibly because of http://stackoverflow.com/a/38749510/2517012
40-
errObj = JSON.parse(rawErr.rawResponse.replace(/^\uFEFF/, '')).error;
41-
}
35+
4236

4337
// parse out statusCode
4438
let statusCode:number;

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as request from 'superagent';
2-
31
import {Options, DEFAULT_VERSION, GRAPH_BASE_URL} from "./common"
42
import {GraphRequest} from "./GraphRequest"
53

0 commit comments

Comments
 (0)