@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
67 lines • 2.52 kB
JavaScript
import { AjaxResponseException } from "../Exception/AjaxResponseException";
export const request = (parameters) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(parameters.type, parameters.url, true);
xhr.setRequestHeader("Accept", "*/*");
if (parameters.headers != null)
for (let header of parameters.headers)
xhr.setRequestHeader(header.name, header.value);
if (parameters.responseType != null)
xhr.responseType = parameters.responseType;
xhr.send(parameters.data);
xhr.onreadystatechange = () => {
if (xhr.readyState !== 4)
return;
const status = xhr.status;
const response = {
value: xhr.responseType !== "arraybuffer" ? xhr.responseText : "",
status: status,
contentType: xhr.getResponseHeader("content-type"),
xhr: xhr
};
if (status >= 200 && status < 300)
resolve(response);
else
reject(new AjaxResponseException(response));
};
});
export const getJson = (url) => request({
url: url,
type: RequestType.get,
headers: [ContentType.json]
});
export const postJson = (url, data) => request({
url: url,
type: RequestType.post,
headers: [ContentType.json],
data: data
});
export var StatusCode;
(function (StatusCode) {
StatusCode[StatusCode["Ok"] = 200] = "Ok";
StatusCode[StatusCode["NoContent"] = 204] = "NoContent";
StatusCode[StatusCode["NotFound"] = 404] = "NotFound";
StatusCode[StatusCode["InternalServerError"] = 500] = "InternalServerError";
})(StatusCode || (StatusCode = {}));
export class Exception {
constructor(response) {
this.response = response;
}
}
export var RequestType;
(function (RequestType) {
RequestType.post = "POST";
RequestType.get = "GET";
// ReSharper disable once InconsistentNaming
RequestType.Delete = "DELETE";
})(RequestType || (RequestType = {}));
export var ContentType;
(function (ContentType) {
function isJsonContentType(contentType) {
return contentType.trim().toLowerCase().indexOf('application/json') === 0;
}
ContentType.isJsonContentType = isJsonContentType;
ContentType.json = { name: 'Content-Type', value: 'application/json;charset=UTF-8' };
ContentType.text = { name: 'Content-Type', value: 'text/plain' };
})(ContentType || (ContentType = {}));
//# sourceMappingURL=Ajax.js.map