UNPKG

@techolution-ai/computer-vision

Version:

A JavaScript/TypeScript library for computer vision applications, providing tools for image processing, scanning, and MQTT-based messaging.

79 lines 2.52 kB
// src/helpers/http-client.ts import { prepareRequestUrl } from "./utils.js"; var HttpClient = class { constructor(options) { this.baseUrl = options.baseUrl; this.headers = options.headers || { "Content-Type": "application/json" }; this.withCredentials = options.withCredentials || false; this._makeRequest = this._makeRequest.bind(this); } async _makeRequest(url, method, body, options = {}) { let data = null; const _withCredentials = options.withCredentials !== void 0 ? options.withCredentials : this.withCredentials; const combinedHeaders = { ...this.headers, ...options.headers }; const res = await fetch(prepareRequestUrl(url, this.baseUrl), { method, headers: combinedHeaders, body: body ? JSON.stringify(body) : null, credentials: _withCredentials ? "include" : "same-origin", signal: options.signal }); try { const contentType = res.headers.get("content-type"); if (contentType && contentType.includes("application/json")) { data = await res.json(); } else if (contentType && contentType.includes("text/")) { data = await res.text(); } else { data = await res.blob(); } } catch (error) { } return { status: res.status, statusText: res.statusText, headers: res.headers, url: res.url, redirected: res.redirected, ok: res.ok, data }; } async get(url, options = {}) { return this._makeRequest(url, "GET", null, options); } async post(url, body = null, options = {}) { return this._makeRequest(url, "POST", body, options); } async put(url, body = null, options = {}) { return this._makeRequest(url, "PUT", body, options); } async delete(url, body = null, options = {}) { return this._makeRequest(url, "DELETE", body, options); } async patch(url, body = null, options = {}) { return this._makeRequest(url, "PATCH", body, options); } async head(url, body = null, options = {}) { return this._makeRequest(url, "HEAD", body, options); } async options(url, body = null, options = {}) { return this._makeRequest(url, "OPTIONS", body, options); } addHeaders(headers) { this.headers = { ...this.headers, ...headers }; } removeHeader(key) { delete this.headers[key]; } clearHeaders() { this.headers = {}; } setWithCredentials(value) { this.withCredentials = value; } }; export { HttpClient as default }; //# sourceMappingURL=http-client.js.map