UNPKG

siputzx-api

Version:
133 lines 4.44 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SiputzxAPI = void 0; const axios_1 = __importDefault(require("axios")); const api_1 = require("../utils/api"); 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_1.default.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 = api_1.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 = api_1.ApiHelper.createFunctionName(endpoint.name); const exampleParams = api_1.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_1.default.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; } } exports.SiputzxAPI = SiputzxAPI; //# sourceMappingURL=SiputzxAPI.js.map