summaryrefslogtreecommitdiff
path: root/app/lib/api
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib/api')
-rw-r--r--app/lib/api/odooApi.ts78
1 files changed, 52 insertions, 26 deletions
diff --git a/app/lib/api/odooApi.ts b/app/lib/api/odooApi.ts
index 9ca6451..f172158 100644
--- a/app/lib/api/odooApi.ts
+++ b/app/lib/api/odooApi.ts
@@ -2,62 +2,88 @@ import axios from "axios";
import { getCookie, setCookie } from "cookies-next";
import { getAuth } from "./auth";
-type axiosParameters = {
- method: string;
+type MethodType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD";
+type HeaderMap = Record<string, string>;
+type PayloadMap = Record<string, string>;
+
+interface AxiosParameters {
+ method: MethodType;
url: string;
- headers: {
- Authorization: string;
- "Content-Type"?: string;
- Token?: string;
- };
+ headers: HeaderMap;
data?: string;
-};
+}
+
+interface AuthPayload {
+ token?: string;
+ email?: string;
+ // properti lain biarkan unknown
+ [key: string]: unknown;
+}
-const renewToken = async () => {
- const token = await axios.get(process.env.NEXT_PUBLIC_ODOO_API_HOST + "/api/token");
+const renewToken = async (): Promise<string> => {
+ const token = await axios.get(
+ `${process.env.NEXT_PUBLIC_ODOO_API_HOST}/api/token`
+ );
setCookie("token", token.data.result);
- return token.data.result;
+ return token.data.result as string;
};
-const getToken = async () => {
+const getToken = async (): Promise<string> => {
let token = getCookie("token") as string | undefined;
- if (token == undefined) token = await renewToken();
+ if (token == null) token = await renewToken();
return token;
};
-const odooApi = async (method: string, url: string, data: Record<string, any> = {}, headers = {}) => {
+const odooApi = async (
+ method: MethodType,
+ url: string,
+ data: PayloadMap = {},
+ headers: HeaderMap = {}
+) => {
try {
- const token = await getToken();
- const auth = getAuth();
+ const bearer = await getToken();
+ const authObj = getAuth() as AuthPayload | string | null;
- const axiosParameter: axiosParameters = {
+ const axiosParameter: AxiosParameters = {
method,
- url: process.env.NEXT_PUBLIC_ODOO_API_HOST + url,
- headers: { Authorization: token ? token : "", ...headers },
+ url: `${process.env.NEXT_PUBLIC_ODOO_API_HOST}${url}`,
+ headers: { Authorization: bearer ?? "", ...headers },
};
- if (auth && typeof auth === "object" && "token" in auth) {
- axiosParameter.headers["Token"] = (auth as any).token;
+ // pasang header Token bila ada
+ if (authObj && typeof authObj === "object" && "token" in authObj) {
+ const t = authObj.token;
+ if (typeof t === "string" && t) {
+ axiosParameter.headers["Token"] = t;
+ }
}
- const upper = method.toUpperCase();
+ const upper = method.toUpperCase() as MethodType;
// Body methods
if (upper === "POST" || upper === "PUT" || upper === "PATCH") {
- axiosParameter.headers["Content-Type"] = "application/x-www-form-urlencoded";
+ axiosParameter.headers["Content-Type"] =
+ "application/x-www-form-urlencoded";
}
- if (Object.keys(data).length > 0 && upper !== "GET" && upper !== "HEAD") {
+ // hanya kirim body untuk method yang pakai body
+ if (
+ Object.keys(data).length > 0 &&
+ upper !== "GET" &&
+ upper !== "HEAD"
+ ) {
+ // filter undefined/null/'' agar field opsional tidak terkirim
const entries = Object.entries(data).filter(
- ([, v]) => v !== undefined && v !== null && v !== ""
+ ([, v]) => typeof v === "string" && v !== ""
) as [string, string][];
axiosParameter.data = new URLSearchParams(entries).toString();
}
const response = await axios(axiosParameter);
- return response.data;
+ return response.data as unknown;
} catch (error) {
console.log(JSON.stringify(error));
+ throw error;
}
};