diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2025-09-20 02:27:54 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2025-09-20 02:27:54 +0000 |
| commit | 557cadbde033fc67edc2f916cc5bfc2aac25b867 (patch) | |
| tree | d1b8fac281e9d50c9a08e8d157e78c07714bca6b /app/lib/api/odooApi.ts | |
| parent | 84d3b0070c5460f325a0189e4d50a6f74053bec1 (diff) | |
| parent | 5c3d6cfd6f68b6eb7192aba47463bd0541bfbf48 (diff) | |
Merged in add_role (pull request #1)
Add role
Diffstat (limited to 'app/lib/api/odooApi.ts')
| -rw-r--r-- | app/lib/api/odooApi.ts | 92 |
1 files changed, 50 insertions, 42 deletions
diff --git a/app/lib/api/odooApi.ts b/app/lib/api/odooApi.ts index c2c9d82..9ca6451 100644 --- a/app/lib/api/odooApi.ts +++ b/app/lib/api/odooApi.ts @@ -1,56 +1,64 @@ -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 = {}) => { - try { - const token = await getToken() - const auth = getAuth(); - const axiosParameter : axiosParameters = { - method, - url: process.env.NEXT_PUBLIC_ODOO_API_HOST + url, - headers: { Authorization: token ? token : '', ...headers }, - }; - console.log('ini adalah tipe',axiosParameter) - if (auth && typeof auth === 'object' && 'token' in auth) { - axiosParameter.headers['Token'] = auth.token; - } - if (method.toUpperCase() == 'POST') - axiosParameter.headers['Content-Type'] = - 'application/x-www-form-urlencoded'; - if (Object.keys(data).length > 0) - axiosParameter.data = new URLSearchParams( - Object.entries(data) - ).toString(); - const response = await axios(axiosParameter); - return response.data; - } catch (error) { - console.log( JSON.stringify(error)); +const odooApi = async (method: string, url: string, data: Record<string, any> = {}, headers = {}) => { + try { + const token = await getToken(); + const auth = getAuth(); + + const axiosParameter: axiosParameters = { + method, + url: process.env.NEXT_PUBLIC_ODOO_API_HOST + url, + headers: { Authorization: token ? token : "", ...headers }, + }; + + if (auth && typeof auth === "object" && "token" in auth) { + axiosParameter.headers["Token"] = (auth as any).token; } -} + const upper = method.toUpperCase(); + + // Body methods + if (upper === "POST" || upper === "PUT" || upper === "PATCH") { + axiosParameter.headers["Content-Type"] = "application/x-www-form-urlencoded"; + } + + if (Object.keys(data).length > 0 && upper !== "GET" && upper !== "HEAD") { + const entries = Object.entries(data).filter( + ([, v]) => v !== undefined && v !== null && v !== "" + ) as [string, string][]; + axiosParameter.data = new URLSearchParams(entries).toString(); + } + + const response = await axios(axiosParameter); + return response.data; + } catch (error) { + console.log(JSON.stringify(error)); + } +}; -export default odooApi
\ No newline at end of file +export default odooApi; |
