@@ -13,6 +13,7 @@ var url = require( 'url' );
13
13
var WPRequest = require ( './constructors/wp-request' ) ;
14
14
var checkMethodSupport = require ( './util/check-method-support' ) ;
15
15
var objectReduce = require ( './util/object-reduce' ) ;
16
+ var isEmptyObject = require ( './util/is-empty-object' ) ;
16
17
17
18
/**
18
19
* Conditionally set basic authentication on a server request object
@@ -78,6 +79,29 @@ function mergeUrl( endpoint, linkPath ) {
78
79
return url . format ( request ) ;
79
80
}
80
81
82
+ /**
83
+ * Extract the body property from the superagent response, or else try to parse
84
+ * the response text to get a JSON object.
85
+ *
86
+ * @param {Object } response The response object from the HTTP request
87
+ * @param {String } response.text The response content as text
88
+ * @param {Object } response.body The response content as a JS object
89
+ * @returns {Object } The response content as a JS object
90
+ */
91
+ function extractResponseBody ( response ) {
92
+ var responseBody = response . body ;
93
+ if ( isEmptyObject ( responseBody ) && response . type === 'text/html' ) {
94
+ // Response may have come back as HTML due to caching plugin; try to parse
95
+ // the response text into JSON
96
+ try {
97
+ responseBody = JSON . parse ( response . text ) ;
98
+ } catch ( e ) {
99
+ // Swallow errors, it's OK to fall back to returning the body
100
+ }
101
+ }
102
+ return responseBody ;
103
+ }
104
+
81
105
/**
82
106
* If the response is not paged, return the body as-is. If pagination
83
107
* information is present in the response headers, parse those headers into
@@ -98,46 +122,48 @@ function mergeUrl( endpoint, linkPath ) {
98
122
* @returns {Object } The body of the HTTP request, conditionally augmented with
99
123
* pagination metadata
100
124
*/
101
- function paginateResponse ( result , endpoint , httpTransport ) {
125
+ function createPaginationObject ( result , endpoint , httpTransport ) {
126
+ var _paging = null ;
127
+
102
128
if ( ! result . headers || ! result . headers [ 'x-wp-totalpages' ] ) {
103
129
// No headers: return as-is
104
- return result ;
130
+ return _paging ;
105
131
}
106
132
107
133
var totalPages = result . headers [ 'x-wp-totalpages' ] ;
108
134
109
135
if ( ! totalPages || totalPages === '0' ) {
110
136
// No paging: return as-is
111
- return result ;
137
+ return _paging ;
112
138
}
113
139
114
140
// Decode the link header object
115
141
var links = result . headers . link ? parseLinkHeader ( result . headers . link ) : { } ;
116
142
117
143
// Store pagination data from response headers on the response collection
118
- result . body . _paging = {
144
+ _paging = {
119
145
total : result . headers [ 'x-wp-total' ] ,
120
146
totalPages : totalPages ,
121
147
links : links
122
148
} ;
123
149
124
150
// Create a WPRequest instance pre-bound to the "next" page, if available
125
151
if ( links . next ) {
126
- result . body . _paging . next = new WPRequest ( {
152
+ _paging . next = new WPRequest ( {
127
153
transport : httpTransport ,
128
154
endpoint : mergeUrl ( endpoint , links . next )
129
155
} ) ;
130
156
}
131
157
132
158
// Create a WPRequest instance pre-bound to the "prev" page, if available
133
159
if ( links . prev ) {
134
- result . body . _paging . prev = new WPRequest ( {
160
+ _paging . prev = new WPRequest ( {
135
161
transport : httpTransport ,
136
162
endpoint : mergeUrl ( endpoint , links . prev )
137
163
} ) ;
138
164
}
139
165
140
- return result ;
166
+ return _paging ;
141
167
}
142
168
143
169
// HTTP-Related Helpers
@@ -153,7 +179,6 @@ function paginateResponse( result, endpoint, httpTransport ) {
153
179
* @return {Promise } A promise to the superagent request
154
180
*/
155
181
function invokeAndPromisify ( request , callback , transform ) {
156
-
157
182
return new Promise ( function ( resolve , reject ) {
158
183
// Fire off the result
159
184
request . end ( function ( err , result ) {
@@ -206,7 +231,12 @@ function invokeAndPromisify( request, callback, transform ) {
206
231
function returnBody ( wpreq , result ) {
207
232
var endpoint = wpreq . _options . endpoint ;
208
233
var httpTransport = wpreq . transport ;
209
- return paginateResponse ( result , endpoint , httpTransport ) . body ;
234
+ var body = extractResponseBody ( result ) ;
235
+ var _paging = createPaginationObject ( result , endpoint , httpTransport ) ;
236
+ if ( _paging ) {
237
+ body . _paging = _paging ;
238
+ }
239
+ return body ;
210
240
}
211
241
212
242
/**
0 commit comments