MCPcopy Index your code
hub / github.com/WP-API/node-wpapi / invokeAndPromisify

Function invokeAndPromisify

lib/http-transport.js:191–228  ·  view source on GitHub ↗

* Submit the provided superagent request object, invoke a callback (if it was * provided), and return a promise to the response from the HTTP request. * * @private * @param {Object} request A superagent request object * @param {Function} callback A callback function (optional) * @param {Functi

( request, callback, transform )

Source from the content-addressed store, hash-verified

189 * @returns {Promise} A promise to the superagent request
190 */
191function invokeAndPromisify( request, callback, transform ) {
192 return new Promise( ( resolve, reject ) => {
193 // Fire off the result
194 request.end( ( err, result ) => {
195
196 // Return the results as a promise
197 if ( err || result.error ) {
198 reject( err || result.error );
199 } else {
200 resolve( result );
201 }
202 } );
203 } ).then( transform ).then( ( result ) => {
204 // If a node-style callback was provided, call it, but also return the
205 // result value for use via the returned Promise
206 if ( callback && typeof callback === 'function' ) {
207 callback( null, result );
208 }
209 return result;
210 }, ( err ) => {
211 // If the API provided an error object, it will be available within the
212 // superagent response object as response.body (containing the response
213 // JSON). If that object exists, it will have a .code property if it is
214 // truly an API error (non-API errors will not have a .code).
215 if ( err.response && err.response.body && err.response.body.code ) {
216 // Forward API error response JSON on to the calling method: omit
217 // all transport-specific (superagent-specific) properties
218 err = err.response.body;
219 }
220 // If a callback was provided, ensure it is called with the error; otherwise
221 // re-throw the error so that it can be handled by a Promise .catch or .then
222 if ( callback && typeof callback === 'function' ) {
223 callback( err );
224 } else {
225 throw err;
226 }
227 } );
228}
229
230/**
231 * Return the body of the request, augmented with pagination information if the

Callers 5

_httpGetFunction · 0.85
_httpPostFunction · 0.85
_httpPutFunction · 0.85
_httpDeleteFunction · 0.85
_httpHeadFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected