83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
/**
|
|
* Service untuk memanggil API lokal (localhost:8800)
|
|
* Menggunakan endpoint dari .env
|
|
*
|
|
* 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
|
|
*/
|
|
|
|
import { httpGet, httpPost } from "./http-config/http-base-services";
|
|
|
|
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);
|
|
}
|
|
|
|
export async function postDataToLocal(
|
|
endpoint: string,
|
|
data: any,
|
|
headers?: any,
|
|
) {
|
|
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) {
|
|
return await httpGet(endpoint, headers);
|
|
}
|
|
|
|
export async function postDataWithAuth(
|
|
endpoint: string,
|
|
data: any,
|
|
headers?: any,
|
|
) {
|
|
return await httpPostInterceptor(endpoint, data, headers);
|
|
}
|
|
|
|
export async function updateDataWithAuth(
|
|
endpoint: string,
|
|
data: any,
|
|
headers?: any,
|
|
) {
|
|
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}`);
|
|
}
|