UNPKG

nebstore-client

Version:
59 lines (58 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiClient = void 0; const axios_1 = __importDefault(require("axios")); const baseUrl = 'https://api.usenebula.io/v1/client'; class ApiClient { constructor(token) { this.token = token; } async apiRequest(options) { var _a, _b; const { method, url, body, params, headers } = options; try { const response = await (0, axios_1.default)({ method, url: `${baseUrl}${url}`, headers: { Authorization: `Bearer ${this.token}`, ...headers, }, data: body, params, }); return response.data; } catch (error) { return { success: false, message: ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || 'API request failed', }; } } // For POST requests async post(url, body) { return this.apiRequest({ method: 'POST', url, body }); } // For GET requests async get(url, params) { return this.apiRequest({ method: 'GET', url, params }); } // For DELETE requests async delete(url) { return this.apiRequest({ method: 'DELETE', url }); } // for Uploads async uploadFile(url, formData) { return this.apiRequest({ method: 'POST', url, body: formData, headers: { 'Content-Type': 'multipart/form-data' }, }); } } exports.ApiClient = ApiClient;