siputzx-api
Version:
Module Wrapper For Siputzx API
126 lines • 4.12 kB
JavaScript
import axios from 'axios';
import { ApiHelper } from '../utils/api.js';
export class SiputzxAPI {
constructor(config = {}) {
this.endpointsLoaded = false;
this.endpointsList = null;
this.baseURL = config.BASE_URL || 'https://api.siputzx.my.id';
this.apikey = config.apikey;
this.client = axios.create({
baseURL: this.baseURL,
headers: {
'Content-Type': 'application/json',
...(this.apikey && { 'Authorization': `Bearer ${this.apikey}` })
}
});
// Auto-initialize endpoints
this.initializeEndpoints();
}
/**
* Make a GET request to the API
*/
async get(endpoint, params = {}) {
const queryString = ApiHelper.createQueryString(params);
const url = `${endpoint}${queryString ? `?${queryString}` : ''}`;
try {
const response = await this.client.get(url);
return response.data;
}
catch (error) {
this.handleError(error);
throw error;
}
}
/**
* Make a POST request to the API
*/
async post(endpoint, data = {}) {
try {
const response = await this.client.post(endpoint, data);
return response.data;
}
catch (error) {
this.handleError(error);
throw error;
}
}
/**
* Get list of all available API endpoints
*/
async getEndpoints() {
const response = await this.get('/api/get');
this.endpointsList = response;
return response;
}
/**
* Initialize dynamic endpoints by fetching endpoint list from API
*/
async initializeEndpoints() {
if (this.endpointsLoaded)
return;
try {
const endpoints = await this.getEndpoints();
this.setupDynamicEndpoints(endpoints);
this.endpointsLoaded = true;
}
catch (error) {
console.error('Failed to initialize endpoints:', error);
}
}
/**
* Setup dynamic endpoints based on API response
*/
setupDynamicEndpoints(apiResponse) {
if (!apiResponse || !apiResponse.routes)
return;
Object.entries(apiResponse.routes).forEach(([categoryName, category]) => {
const categoryKey = categoryName.toLowerCase();
if (!this[categoryKey]) {
this[categoryKey] = {};
}
category.endpoints.forEach(endpoint => {
const functionName = ApiHelper.createFunctionName(endpoint.name);
const exampleParams = ApiHelper.parseExampleParams(endpoint.example);
this[categoryKey][functionName] = async (params = {}) => {
if (endpoint.method === 'GET') {
return this.get(endpoint.path, params);
}
else if (endpoint.method === 'POST') {
return this.post(endpoint.path, params);
}
};
this[categoryKey][functionName].endpoint = endpoint;
this[categoryKey][functionName].exampleParams = exampleParams;
});
});
}
/**
* Handle API errors
*/
handleError(error) {
var _a, _b;
if (axios.isAxiosError(error)) {
const status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status;
const data = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data;
console.error(`API Error (${status}):`, data);
}
else {
console.error('Unexpected error:', error);
}
}
/**
* Set API key
*/
setApiKey(apikey) {
this.apikey = apikey;
this.client.defaults.headers.common['Authorization'] = `Bearer ${apikey}`;
}
/**
* Set base URL
*/
setBaseURL(baseURL) {
this.baseURL = baseURL;
this.client.defaults.baseURL = baseURL;
}
}
//# sourceMappingURL=SiputzxAPI.js.map