UNPKG

ar-design

Version:

AR Design is a (react | nextjs) ui library.

113 lines (112 loc) 4.04 kB
import { getApiConfig } from "./Config"; class Api { _host; _core; _url; _init; constructor(values) { this._host = values.host || (typeof window !== "undefined" ? window.location.origin : ""); this._core = values.core || ""; this._init = values.init; // Url this._url = `${this._host}/${this._core ? this._core + "/" : ""}`; } async Get(values) { if (values.input && values.input.toString().includes("?")) { values.input = values.input.toString().replace(/\/(?=\?)/, ""); } const p_response = this.CustomFetch(`${this._url}${values.input}`, { method: "GET", ...values.init, ...this._init, }); const clone = (await p_response).clone(); const response = await clone; return { p_response, response }; } async Post(values) { if (values.input && values.input.toString().includes("?")) { values.input = values.input.toString().replace(/\/(?=\?)/, ""); } const p_response = this.CustomFetch(`${this._url}${values.input}`, { method: "POST", body: JSON.stringify(values.data), ...values.init, ...this._init, }); const clone = (await p_response).clone(); const response = await clone; return { p_response, response }; } async PostWithFormData(values) { if (values.input && values.input.toString().includes("?")) { values.input = values.input.toString().replace(/\/(?=\?)/, ""); } const response = await this.CustomFetch(`${this._url}${values.input}`, { method: "POST", body: values.data, ...values.init, ...this._init, }); return response; } async Put(values) { if (values.input && values.input.toString().includes("?")) { values.input = values.input.toString().replace(/\/(?=\?)/, ""); } const response = await this.CustomFetch(`${this._url}${values.input}`, { method: "PUT", body: JSON.stringify(values.data), ...values.init, ...this._init, }); return response; } async Delete(values) { if (values.input && values.input.toString().includes("?")) { values.input = values.input.toString().replace(/\/(?=\?)/, ""); } const response = await this.CustomFetch(`${this._url}${values.input}`, { method: "DELETE", ...values.init, ...this._init, }); return response; } /** * Burada bir fetch işlemi gerçekleştirilmekte fakat farklı olarak burayı `interceptor` olarak kullanmaktayız. * @param input * @param init * @returns */ async CustomFetch(input, init = {}) { try { const config = getApiConfig(); // Request merge: init + headers + global config let requestInit = { ...init, headers: { ...config.headers, ...init.headers, }, }; // Request interceptor (runtime'da eklenmiş olabilir.) if (config.requestInterceptor) [input, requestInit] = await config.requestInterceptor(input, requestInit); // Fetch çağrısı. const response = await fetch(input, requestInit); // Response interceptor. if (config.responseInterceptor) return await config.responseInterceptor(response); // Error handling if (!response.ok) console.error(`HTTP Error ${response.status}: ${response.statusText}`); return response; } catch (error) { // Network hatası veya fetch exception throw new Error(error instanceof Error ? error.message : "Network Error"); } } } export default Api;