summaryrefslogtreecommitdiff
path: root/app/lib/api/odooApi.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib/api/odooApi.ts')
-rw-r--r--app/lib/api/odooApi.ts61
1 files changed, 36 insertions, 25 deletions
diff --git a/app/lib/api/odooApi.ts b/app/lib/api/odooApi.ts
index c2c9d82..fab3e0c 100644
--- a/app/lib/api/odooApi.ts
+++ b/app/lib/api/odooApi.ts
@@ -25,32 +25,43 @@ const getToken = async () => {
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 = {}, 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();
+
+ // Set Content-Type untuk method yang kirim body
+ 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') {
+ 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