deep-resource
Version:
DEEP Resource Library
185 lines (147 loc) • 3.57 kB
JavaScript
/**
* Created by AlexanderC on 6/10/15.
*/
'use strict';
/**
* Response object
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
let Response = exports.Response = function () {
/**
* @param {Request|*} request
* @param {Object} data
* @param {String} error
*/
function Response(request, data, error) {
_classCallCheck(this, Response);
this._request = request;
this._rawError = error;
this._rawData = data;
if (this._rawError) {
this._rawError = Response._toErrorObj(this._rawError);
}
this._statusCode = null;
this._data = null;
this._error = null;
this._headers = null;
this._requestId = null;
}
/**
* @returns {String}
*/
_createClass(Response, [{
key: 'toJSON',
/**
* @returns {{requestId: String, statusCode: Number, headers: (Object|null), data: Object, error: Error}}
*/
value: function toJSON() {
return {
requestId: this.requestId,
statusCode: this.statusCode,
headers: this.headers,
data: this.data,
error: this.error
};
}
}, {
key: 'rawData',
/**
* @returns {*}
*/
get: function get() {
return this._rawData;
}
/**
* @returns {*}
*/
}, {
key: 'rawError',
get: function get() {
return this._rawError;
}
/**
* @returns {Request}
*/
}, {
key: 'request',
get: function get() {
return this._request;
}
/**
* @returns {Object}
*/
}, {
key: 'data',
get: function get() {
return this._data;
}
/**
* @returns {Number}
*/
}, {
key: 'statusCode',
get: function get() {
return this._statusCode;
}
/**
* @returns {Error}
*/
}, {
key: 'error',
get: function get() {
return this._error;
}
/**
* @returns {String}
*/
}, {
key: 'requestId',
get: function get() {
return this._requestId;
}
/**
* @returns {Object|null}
*/
}, {
key: 'headers',
get: function get() {
return this._headers;
}
/**
* @returns {Boolean}
*/
}, {
key: 'isError',
get: function get() {
return !!this.error;
}
/**
* @param {String|Error|*} rawError
* @returns {Error}
* @private
*/
}], [{
key: '_toErrorObj',
value: function _toErrorObj(rawError) {
return rawError instanceof Error ? rawError : new Error(rawError.toString());
}
}, {
key: 'ORIGINAL_REQUEST_ID_HEADER',
get: function get() {
return 'x-amzn-original-RequestId';
}
/**
* @returns {String}
*/
}, {
key: 'REQUEST_ID_HEADER',
get: function get() {
return 'x-amzn-RequestId';
}
}]);
return Response;
}();