UNPKG

@logcomex/aylawc-core

Version:

An experimental library of web components

39 lines 1.51 kB
import { HttpError } from './http.interface'; export class XmlRequest { constructor() { this.headers = {}; this.useDefaultHeaders = true; this.defaultHttpHeaders = { 'Content-Type': 'application/json' }; } getDefaultHeaders() { return this.defaultHttpHeaders; } setHeaders(headers) { this.headers = headers; } // eslint-disable-next-line @typescript-eslint/promise-function-async request(method, url, payload) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open(method, url); if (this.useDefaultHeaders) this.headers = Object.assign(Object.assign({}, this.headers), this.defaultHttpHeaders); for (const [headerProperty, headerValue] of Object.entries(this.headers)) xhr.setRequestHeader(headerProperty, headerValue); xhr.onload = () => { const { status, response } = xhr; if (status >= 200 && status < 300) resolve(JSON.parse(response)); reject(new HttpError({ status, response: JSON.parse(response) })); }; xhr.onerror = () => { const { status, response } = xhr; reject(new HttpError({ status, response: JSON.parse(response) })); }; xhr.send(JSON.stringify(payload)); }); } } //# sourceMappingURL=xml-request.adapter.js.map