@ima/plugin-rest-client
Version:
Generic REST API client plugin for the IMA application framework.
91 lines (90 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, /**
* Typed representation of a REST API response.
*/ "default", {
enumerable: true,
get: function() {
return Response;
}
});
const _clone = /*#__PURE__*/ _interop_require_default(require("clone"));
const _AbstractEntity = /*#__PURE__*/ _interop_require_default(require("./AbstractEntity"));
const _Request = /*#__PURE__*/ _interop_require_default(require("./Request"));
const _utils = require("./utils");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
class Response {
/**
* Initializes the response representation using the provided data.
*
* @param {{
* status: number
* headers: Object<string, string>
* body: *
* cached: boolean
* request: Request
* }} responseData The data representing this response. See the
* fields of this class for more information.
*/ constructor(responseData){
/**
* The HTTP response status code of this response.
*
* @type {number}
*/ this.status = responseData.status;
/**
* The response headers. The keys are header names in lower-case, the
* values are header values.
*
* @type {Object<string, string>}
*/ this.headers = Object.assign({}, responseData.headers);
if ($Debug) {
Object.freeze(this.headers);
}
let bodyData;
if (responseData.body instanceof Array) {
bodyData = responseData.body.map((element)=>{
if (element instanceof _AbstractEntity.default) {
return element;
} else {
return (0, _clone.default)(element);
}
});
} else if (responseData.body instanceof _AbstractEntity.default) {
bodyData = responseData.body;
} else {
bodyData = (0, _clone.default)(responseData.body);
}
/**
* The response of the body, already parsed according to the value of
* the response's {@code Content-Type} header.
*
* @type {*}
*/ this.body = bodyData;
if ($Debug) {
if (!(this.body instanceof _AbstractEntity.default)) {
(0, _utils.deepFreeze)(this.body);
}
}
/**
* The flag signalling whether this request was handled by the HTTP
* agent's cache.
*
* @type {boolean}
*/ this.cached = responseData.cached;
/**
* The REST API request that was made and resulted in this response.
*
* @type {Request}
*/ this.request = new _Request.default(responseData.request);
if ($Debug) {
Object.freeze(this);
}
}
}
//# sourceMappingURL=Response.js.map