From f99e0aba70efad0deb907d8e27f09fc9f527c8a4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 17 Feb 2023 17:07:50 +0700 Subject: Refactor --- src/core/api/odooApi.js | 47 ++++++++++++++++++++++++++++++++++++++++ src/core/api/searchSuggestApi.js | 12 ++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/core/api/odooApi.js create mode 100644 src/core/api/searchSuggestApi.js (limited to 'src/core/api') diff --git a/src/core/api/odooApi.js b/src/core/api/odooApi.js new file mode 100644 index 00000000..59d88faa --- /dev/null +++ b/src/core/api/odooApi.js @@ -0,0 +1,47 @@ +import axios from 'axios' +import camelcaseObjectDeep from 'camelcase-object-deep' +import { getCookie, setCookie } from 'cookies-next' +import { getAuth } from '../utils/auth' + +const renewToken = async () => { + let token = await axios.get(process.env.ODOO_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 +} + +const maxConnectionAttempt = 15 +let connectionAttempt = 0 + +const odooApi = async (method, url, data = {}, headers = {}) => { + connectionAttempt++ + try { + let token = await getToken() + const auth = getAuth() + + let axiosParameter = { + method, + url: process.env.ODOO_HOST + url, + headers: {'Authorization': token, ...headers} + } + if (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() + + let res = await axios(axiosParameter) + if (res.data.status.code == 401 && connectionAttempt < maxConnectionAttempt) { + await renewToken() + return odooApi(method, url, data, headers) + } + return camelcaseObjectDeep(res.data.result) || [] + } catch (error) { + console.log(error) + } +} + +export default odooApi; \ No newline at end of file diff --git a/src/core/api/searchSuggestApi.js b/src/core/api/searchSuggestApi.js new file mode 100644 index 00000000..b5edebda --- /dev/null +++ b/src/core/api/searchSuggestApi.js @@ -0,0 +1,12 @@ +import axios from "axios" + +const searchSuggestApi = async ({ query }) => { + const dataSearchSuggest = await axios(`${process.env.SELF_HOST}/api/shop/suggest?q=${query.trim()}`) + return dataSearchSuggest +} + +searchSuggestApi.defaultProps = { + query: '' +} + +export default searchSuggestApi \ No newline at end of file -- cgit v1.2.3 From f66b12fd1d0b83af0d7230d7b1565fbe00afbe3c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 22 Feb 2023 11:03:34 +0700 Subject: prettier --- src/core/api/odooApi.js | 12 +++++++----- src/core/api/searchSuggestApi.js | 8 +++++--- 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src/core/api') diff --git a/src/core/api/odooApi.js b/src/core/api/odooApi.js index 59d88faa..202c355e 100644 --- a/src/core/api/odooApi.js +++ b/src/core/api/odooApi.js @@ -25,13 +25,15 @@ const odooApi = async (method, url, data = {}, headers = {}) => { const auth = getAuth() let axiosParameter = { - method, + method, url: process.env.ODOO_HOST + url, - headers: {'Authorization': token, ...headers} + headers: { Authorization: token, ...headers } } if (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() + 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 && connectionAttempt < maxConnectionAttempt) { @@ -44,4 +46,4 @@ const odooApi = async (method, url, data = {}, headers = {}) => { } } -export default odooApi; \ No newline at end of file +export default odooApi diff --git a/src/core/api/searchSuggestApi.js b/src/core/api/searchSuggestApi.js index b5edebda..e4445c9a 100644 --- a/src/core/api/searchSuggestApi.js +++ b/src/core/api/searchSuggestApi.js @@ -1,7 +1,9 @@ -import axios from "axios" +import axios from 'axios' const searchSuggestApi = async ({ query }) => { - const dataSearchSuggest = await axios(`${process.env.SELF_HOST}/api/shop/suggest?q=${query.trim()}`) + const dataSearchSuggest = await axios( + `${process.env.SELF_HOST}/api/shop/suggest?q=${query.trim()}` + ) return dataSearchSuggest } @@ -9,4 +11,4 @@ searchSuggestApi.defaultProps = { query: '' } -export default searchSuggestApi \ No newline at end of file +export default searchSuggestApi -- cgit v1.2.3