aspect-api-js
Version:
Package for aspect SaaS platform for JS
52 lines (47 loc) • 1.38 kB
JavaScript
import axios from "axios";
import { RouteFormatter } from "../utils/RouteFormatter.js";
export const AccessMethod = {
Post: 'POST',
Get: 'GET',
Put: 'PUT',
Delete: 'DELETE'
};
export class HttpExchanger {
constructor(debug = false) {
this.debug = debug;
this.formatter = new RouteFormatter();
}
async request(url, data, method = AccessMethod.Get, headers = {}) {
try {
const response = await axios.request({
url: method === AccessMethod.Get
? this.formatter.make(url, data)
: this.formatter.format(url, data),
method: method,
data: data,
headers: headers
});
return {
response: response.data,
status: response.status,
failed: response.status !== 200
};
}
catch (error) {
if(this.debug) {
console.log(error.response, url, method, data);
}
else {
console.clear();
}
return {
response: error,
status: error.status,
failed: true
};
}
}
getFormatter() {
return this.formatter;
}
}