diff options
| author | Rafi Zadanly <rafizadanly@gmail.com> | 2022-11-17 17:28:07 +0700 |
|---|---|---|
| committer | Rafi Zadanly <rafizadanly@gmail.com> | 2022-11-17 17:28:07 +0700 |
| commit | 8f9d12c64ff25e0065439d769fe79ec416bc3df8 (patch) | |
| tree | 18dfc6aff28e6ef419410185d905a3f4f88de152 /src/helpers | |
| parent | e536e5f757a1680ffb5d212b06b0c913894879f7 (diff) | |
Fix apiOdoo function
Diffstat (limited to 'src/helpers')
| -rw-r--r-- | src/helpers/apiOdoo.js | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/helpers/apiOdoo.js b/src/helpers/apiOdoo.js index 357f93eb..e58f7fc8 100644 --- a/src/helpers/apiOdoo.js +++ b/src/helpers/apiOdoo.js @@ -1,6 +1,5 @@ import { getCookie, setCookie } from 'cookies-next'; - -const axios = require('axios'); +import axios from 'axios'; const renewToken = async () => { let token = await axios.get(process.env.SELF_HOST + '/api/token'); @@ -14,19 +13,26 @@ const getToken = async () => { return token; }; -const getOdoo = async (url) => { +const apiOdoo = async (method, url, data = {}, headers = {}) => { try { let token = await getToken(); - let res = await axios.get(process.env.ODOO_HOST + url, {headers: {Authorization: token}}); - + let axiosParameter = { + method, + url: process.env.ODOO_HOST + url, + headers: {'Authorization': token, ...headers} + } + 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(); + + let res = await axios(axiosParameter); if (res.data.status.code == 401) { await renewToken(); - return getOdoo(url); + return apiOdoo(method, url, data, headers); } return res.data.result || []; } catch (error) { - console.log(error); + console.log(error) } } -export { getOdoo };
\ No newline at end of file +export default apiOdoo;
\ No newline at end of file |
