nebstore-client
Version:
Nebula client node package
52 lines (51 loc) • 1.54 kB
JavaScript
import axios from 'axios';
const baseUrl = 'https://api.usenebula.io/v1/client';
export class ApiClient {
constructor(token) {
this.token = token;
}
async apiRequest(options) {
var _a, _b;
const { method, url, body, params, headers } = options;
try {
const response = await axios({
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' },
});
}
}