ar-design
Version:
AR Design is a (react | nextjs) ui library.
136 lines (135 loc) • 4.7 kB
JavaScript
class Api {
_host;
_core;
_token;
_url;
constructor(values) {
this._host = values.host || (typeof window !== "undefined" ? window.location.origin : "");
this._core = values.core || "";
this._token = values.token;
// 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",
headers: {
...this.HeaderProperties(),
...values.headers,
},
});
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",
headers: { ...this.HeaderProperties(), ...values.headers },
body: JSON.stringify(values.data),
...values.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",
headers: { ...this.HeaderProperties(), ...values.headers },
body: values.data,
...values.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",
headers: {
...this.HeaderProperties(),
...values.headers,
},
body: JSON.stringify(values.data),
...values.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",
headers: {
...this.HeaderProperties(),
...values.headers,
},
});
return response;
}
HeaderProperties = () => {
return {
Accept: "application/json",
"Content-Type": "application/json",
...(this._token && { Authorization: `Bearer ${this.Cookies(this._token)}` }),
};
};
Cookies = (name) => {
if (typeof window === "undefined")
return undefined;
const cookies = document.cookie.split("; ");
const cookieObject = [];
cookies.forEach((cookie) => {
const [key, value] = cookie.split("=");
cookieObject.push({ key: key, value: value });
});
return decodeURIComponent(cookieObject.find((x) => x.key === name)?.value ?? "");
};
/**
* 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 {
// # Request Interceptor
const request = await fetch(input, init);
// # Response Interceptor
// Error Handling
if (!request.ok) {
switch (request.status) {
case 400:
console.error("400");
break;
case 401:
console.error("401");
break;
case 404:
console.error("404");
break;
default:
console.error(`Unexpected Error: ${request.status}`);
}
}
// Return
return request;
}
catch (error) {
throw error;
}
}
}
export default Api;