box-node-sdk
Version:
Official SDK for Box Plaform APIs
81 lines • 3.38 kB
JavaScript
;
/**
* @fileoverview A library for making requests to the Box API.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
var bluebird_1 = require("bluebird");
var errors_1 = __importDefault(require("./util/errors"));
var stream_1 = require("stream");
var APIRequest = require('./api-request');
// ------------------------------------------------------------------------------
// Private
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Public
// ------------------------------------------------------------------------------
/**
* A library for communicating with the Box API.
*
* @param {Config} config SDK configuration object instance.
* @param {EventEmitter} eventBus The event bus for SDK events
* @constructor
*/
var APIRequestManager = /** @class */ (function () {
function APIRequestManager(config, eventBus) {
this.config = config;
this.eventBus = eventBus;
}
/**
* Make a request to the API, and get the response via callback.
*
* @param {Object} options The request options
* @returns {Promise<Response>} A promise resolving to the response object
*/
APIRequestManager.prototype.makeRequest = function (options /* FIXME */) {
// Add default APIRequestManager options to each request
var requestConfig = this.config.extend({
request: options,
});
// Make the request
var apiRequest = new APIRequest(requestConfig, this.eventBus);
return bluebird_1.Promise.fromCallback(function (callback) {
return apiRequest.execute(callback);
}).catch(function (err) { return errors_1.default.unwrapAndThrow(err); });
};
/**
* Make a request to the API, and return a read stream for the response.
*
* @param {Object} options The request options
* @returns {Stream.Readable} The response stream
*/
APIRequestManager.prototype.makeStreamingRequest = function (options /* FIXME */) {
// Add default APIRequestManager options to each request
var requestConfig = this.config.extend({
request: options,
});
// Make the request
var apiRequest = new APIRequest(requestConfig, this.eventBus);
apiRequest.execute();
var stream = apiRequest.getResponseStream();
// The request is asynchronous, so we need to wait for the stream to be
// available before we can pipe it to the pass-through stream.
// If the stream is undefined, then the request failed and we should
// propagate the error.
if (stream &&
requestConfig.disableStreamPassThrough !== true &&
options.disableStreamPassThrough !== true) {
var passThrough = new stream_1.PassThrough();
stream.pipe(passThrough);
return passThrough;
}
return stream;
};
return APIRequestManager;
}());
module.exports = APIRequestManager;
//# sourceMappingURL=api-request-manager.js.map