import axios from "axios"; import { getCookie, setCookie } from "cookies-next"; import { getAuth } from "./auth"; type MethodType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD"; type HeaderMap = Record; type PayloadMap = Record; interface AxiosParameters { method: MethodType; url: string; headers: HeaderMap; data?: string; } interface AuthPayload { token?: string; email?: string; // properti lain biarkan unknown [key: string]: unknown; } const renewToken = async (): Promise => { const token = await axios.get( `${process.env.NEXT_PUBLIC_ODOO_API_HOST}/api/token` ); setCookie("token", token.data.result); return token.data.result as string; }; const getToken = async (): Promise => { let token = getCookie("token") as string | undefined; if (token == null) token = await renewToken(); return token; }; const odooApi = async ( method: MethodType, url: string, data: PayloadMap = {}, headers: HeaderMap = {} ) => { try { const bearer = await getToken(); const authObj = getAuth() as AuthPayload | string | null; const axiosParameter: AxiosParameters = { method, url: `${process.env.NEXT_PUBLIC_ODOO_API_HOST}${url}`, headers: { Authorization: bearer ?? "", ...headers }, }; // pasang header Token bila ada if (authObj && typeof authObj === "object" && "token" in authObj) { const t = authObj.token; if (typeof t === "string" && t) { axiosParameter.headers["Token"] = t; } } const upper = method.toUpperCase() as MethodType; // Body methods if (upper === "POST" || upper === "PUT" || upper === "PATCH") { axiosParameter.headers["Content-Type"] = "application/x-www-form-urlencoded"; } // hanya kirim body untuk method yang pakai body if ( Object.keys(data).length > 0 && upper !== "GET" && upper !== "HEAD" ) { // filter undefined/null/'' agar field opsional tidak terkirim const entries = Object.entries(data).filter( ([, v]) => typeof v === "string" && v !== "" ) as [string, string][]; axiosParameter.data = new URLSearchParams(entries).toString(); } const response = await axios(axiosParameter); return response.data as unknown; } catch (error) { console.log(JSON.stringify(error)); throw error; } }; export default odooApi;