@@ -16,6 +16,25 @@ if (typeof Promise === 'undefined') {
1616 polyfill ( ) ;
1717}
1818
19+ /**
20+ * The error structure returned when a network call fails
21+ */
22+ class ResponseError extends Error {
23+ /**
24+ * Construct a new ResponseError
25+ * @param {string } message - an message to return instead of the the default error message
26+ * @param {string } path - the requested path
27+ * @param {Object } response - the object returned by Axios
28+ */
29+ constructor ( message , path , response ) {
30+ super ( message ) ;
31+ this . path = path ;
32+ this . request = response . config ;
33+ this . response = response ;
34+ this . status = response . status ;
35+ }
36+ }
37+
1938/**
2039 * Requestable wraps the logic for making http requests to the API
2140 */
@@ -208,7 +227,16 @@ class Requestable {
208227
209228 return this . _request ( 'GET' , path , options )
210229 . then ( ( response ) => {
211- results . push . apply ( results , response . data ) ;
230+ let thisGroup ;
231+ if ( response . data instanceof Array ) {
232+ thisGroup = response . data ;
233+ } else if ( response . data . items instanceof Array ) {
234+ thisGroup = response . data . items ;
235+ } else {
236+ let message = `cannot figure out how to append ${ response . data } to the result set` ;
237+ throw new ResponseError ( message , path , response ) ;
238+ }
239+ results . push . apply ( results , thisGroup ) ;
212240
213241 const nextUrl = getNextPage ( response . headers . link ) ;
214242 if ( nextUrl ) {
@@ -231,24 +259,6 @@ module.exports = Requestable;
231259// ////////////////////////// //
232260// Private helper functions //
233261// ////////////////////////// //
234- /**
235- * The error structure returned when a network call fails
236- */
237- class ResponseError extends Error {
238- /**
239- * Construct a new ResponseError
240- * @param {string } path - the requested path
241- * @param {Object } response - the object returned by Axios
242- */
243- constructor ( path , response ) {
244- super ( `error making request ${ response . config . method } ${ response . config . url } ` ) ;
245- this . path = path ;
246- this . request = response . config ;
247- this . response = response ;
248- this . status = response . status ;
249- }
250- }
251-
252262const METHODS_WITH_NO_BODY = [ 'GET' , 'HEAD' , 'DELETE' ] ;
253263function methodHasNoBody ( method ) {
254264 return METHODS_WITH_NO_BODY . indexOf ( method ) !== - 1 ;
@@ -267,8 +277,9 @@ function getNextPage(linksHeader = '') {
267277
268278function callbackErrorOrThrow ( cb , path ) {
269279 return function handler ( response ) {
270- log ( `error making request ${ response . config . method } ${ response . config . url } ${ JSON . stringify ( response . data ) } ` ) ;
271- let error = new ResponseError ( path , response ) ;
280+ let message = `error making request ${ response . config . method } ${ response . config . url } ` ;
281+ let error = new ResponseError ( message , path , response ) ;
282+ log ( `${ message } ${ JSON . stringify ( response . data ) } ` ) ;
272283 if ( cb ) {
273284 log ( 'going to error callback' ) ;
274285 cb ( error ) ;
0 commit comments