jaecoo-kelapagading/service/local-api-service.ts

83 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-01-20 01:04:42 +00:00
/**
* Service untuk memanggil API lokal (localhost:8800)
* Menggunakan endpoint dari .env
2026-01-20 12:43:12 +00:00
*
2026-01-20 01:04:42 +00:00
* Contoh penggunaan:
* - Set NEXT_PUBLIC_API_BASE_URL=http://localhost:8800 di file .env.local
* - Atau gunakan NEXT_PUBLIC_LOCAL_API_URL jika ingin endpoint terpisah
*/
2026-01-20 12:43:12 +00:00
import { httpGet, httpPost } from "./http-config/http-base-services";
2026-01-20 01:04:42 +00:00
import {
httpGetInterceptor,
httpPostInterceptor,
httpPutInterceptor,
httpDeleteInterceptor,
} from "./http-config/http-interceptor-services";
// Service untuk endpoint yang tidak memerlukan authentication
// Menggunakan axios-base-instance.ts
export async function getDataFromLocal(endpoint: string, headers?: any) {
return await httpGet(endpoint, headers);
}
2026-01-20 12:43:12 +00:00
export async function postDataToLocal(
endpoint: string,
data: any,
headers?: any,
) {
2026-01-20 01:04:42 +00:00
return await httpPost(endpoint, data, headers);
}
// Service untuk endpoint yang memerlukan authentication
// Menggunakan axios-interceptor-instance.ts dengan Bearer token
export async function getDataWithAuth(endpoint: string, headers?: any) {
2026-01-20 12:43:12 +00:00
return await httpGet(endpoint, headers);
2026-01-20 01:04:42 +00:00
}
2026-01-20 12:43:12 +00:00
export async function postDataWithAuth(
endpoint: string,
data: any,
headers?: any,
) {
2026-01-20 01:04:42 +00:00
return await httpPostInterceptor(endpoint, data, headers);
}
2026-01-20 12:43:12 +00:00
export async function updateDataWithAuth(
endpoint: string,
data: any,
headers?: any,
) {
2026-01-20 01:04:42 +00:00
return await httpPutInterceptor(endpoint, data, headers);
}
export async function deleteDataWithAuth(endpoint: string, headers?: any) {
return await httpDeleteInterceptor(endpoint, headers);
}
// Contoh implementasi spesifik untuk endpoint /api/users
// Gunakan service dengan auth jika endpoint memerlukan token
export async function getUsers() {
return await httpGetInterceptor("/users");
}
export async function getUserById(id: string | number) {
return await httpGetInterceptor(`/users/${id}`);
}
export async function createUser(data: any) {
return await httpPostInterceptor("/users", data);
}
export async function updateUser(id: string | number, data: any) {
return await httpPutInterceptor(`/users/${id}`, data);
}
export async function deleteUser(id: string | number) {
return await httpDeleteInterceptor(`/users/${id}`);
}