diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-20 09:27:05 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-20 09:27:05 +0700 |
| commit | 5c3d6cfd6f68b6eb7192aba47463bd0541bfbf48 (patch) | |
| tree | d1b8fac281e9d50c9a08e8d157e78c07714bca6b /app/lib/api/odooApi.ts | |
| parent | d6a56e8be29c1e8ea77d970434b8c441f23acdef (diff) | |
<Miqdad> add role
Diffstat (limited to 'app/lib/api/odooApi.ts')
| -rw-r--r-- | app/lib/api/odooApi.ts | 55 |
1 files changed, 26 insertions, 29 deletions
diff --git a/app/lib/api/odooApi.ts b/app/lib/api/odooApi.ts index fab3e0c..9ca6451 100644 --- a/app/lib/api/odooApi.ts +++ b/app/lib/api/odooApi.ts @@ -1,31 +1,31 @@ -import axios from "axios" +import axios from "axios"; import { getCookie, setCookie } from "cookies-next"; import { getAuth } from "./auth"; type axiosParameters = { - method : string, - url : string, - headers : { - Authorization : string, - 'Content-Type'? : string, - Token? : string - }, - data ?: string -} + method: string; + url: string; + headers: { + Authorization: string; + "Content-Type"?: string; + Token?: string; + }; + data?: string; +}; const renewToken = async () => { - const token = await axios.get(process.env.NEXT_PUBLIC_ODOO_API_HOST + '/api/token') - setCookie('token', token.data.result) - return token.data.result + const token = await axios.get(process.env.NEXT_PUBLIC_ODOO_API_HOST + "/api/token"); + setCookie("token", token.data.result); + return token.data.result; }; const getToken = async () => { - let token = getCookie('token') - if (token == undefined) token = await renewToken() - return token + let token = getCookie("token") as string | undefined; + if (token == undefined) token = await renewToken(); + return token; }; -const odooApi = async (method: string, url: string, data = {}, headers = {}) => { +const odooApi = async (method: string, url: string, data: Record<string, any> = {}, headers = {}) => { try { const token = await getToken(); const auth = getAuth(); @@ -33,24 +33,23 @@ const odooApi = async (method: string, url: string, data = {}, headers = {}) => const axiosParameter: axiosParameters = { method, url: process.env.NEXT_PUBLIC_ODOO_API_HOST + url, - headers: { Authorization: token ? token : '', ...headers }, + headers: { Authorization: token ? token : "", ...headers }, }; - if (auth && typeof auth === 'object' && 'token' in auth) { - axiosParameter.headers['Token'] = (auth as any).token; + if (auth && typeof auth === "object" && "token" in auth) { + axiosParameter.headers["Token"] = (auth as any).token; } const upper = method.toUpperCase(); - // Set Content-Type untuk method yang kirim body - if (upper === 'POST' || upper === 'PUT' || upper === 'PATCH') { - axiosParameter.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + // Body methods + if (upper === "POST" || upper === "PUT" || upper === "PATCH") { + axiosParameter.headers["Content-Type"] = "application/x-www-form-urlencoded"; } - // Hanya kirim body untuk method yang memang pakai body - if (Object.keys(data).length > 0 && upper !== 'GET' && upper !== 'HEAD') { + if (Object.keys(data).length > 0 && upper !== "GET" && upper !== "HEAD") { const entries = Object.entries(data).filter( - ([, v]) => v !== undefined && v !== null && v !== '' + ([, v]) => v !== undefined && v !== null && v !== "" ) as [string, string][]; axiosParameter.data = new URLSearchParams(entries).toString(); } @@ -62,6 +61,4 @@ const odooApi = async (method: string, url: string, data = {}, headers = {}) => } }; - - -export default odooApi
\ No newline at end of file +export default odooApi; |
