src/Services/API/Http/index.ts
// #region Global Imports
import "isomorphic-unfetch";
import { stringify } from "query-string";
// #endregion Global Imports
// #region Interface Imports
import { HttpModel } from "@Interfaces";
// #endregion Interface Imports
const BaseUrl = `${process.env.NEXT_PUBLIC_API_URL}/api`;
export const Http = {
Request: async <A>(
methodType: string,
url: string,
params?: HttpModel.IRequestQueryPayload,
payload?: HttpModel.IRequestPayload
): Promise<A> => {
return new Promise((resolve, reject) => {
const query = params
? `?${stringify({
...params,
api_key: process.env.NEXT_PUBLIC_API_KEY,
})}`
: "";
fetch(`${BaseUrl}${url}${query}`, {
body: JSON.stringify(payload),
cache: "no-cache",
headers: {
"content-type": "application/json",
},
method: `${methodType}`,
})
.then(async response => {
if (response.status === 200) {
return response.json().then(resolve);
}
return reject(response);
})
.catch(e => {
reject(e);
});
});
},
};