@nexle-soft/quick-desk-client
Version:
Typescript Client for Quick desk's APIs
118 lines • 4.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import axios from "axios";
import { Configuration } from "../../configuration";
export class HttpClient {
constructor() {
this.initializeAxiosInstance();
}
// Method to (re)initialize the axios instance with the current configuration
initializeAxiosInstance() {
const config = Configuration.getInstance();
if (!config.getHost() || !config.getSecretKey()) {
console.warn("Configuration is not properly initialized.");
return; // Avoid initializing axios if config is missing
}
// Initialize Axios instance with valid configuration
this.axiosInstance = axios.create({
baseURL: config.getHost(),
headers: {
"Content-Type": "application/json",
"x-application-key": config.getSecretKey(),
},
});
// Request Interceptor
this.axiosInstance.interceptors.request.use((config) => {
// Add custom logic before sending request (e.g., authentication, logging)
return config;
}, (error) => Promise.reject(error));
// Response Interceptor
this.axiosInstance.interceptors.response.use((response) => response, (error) => Promise.reject(error));
}
// Reinitialize Axios instance when configuration changes (optional)
reinitialize() {
this.initializeAxiosInstance();
}
// Generic GET method
get({ url, config }) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.axiosInstance) {
throw new Error("Axios instance is not initialized. Please check your configuration.");
}
try {
const response = yield this.axiosInstance.get(url, config);
return response.data;
}
catch (error) {
return this.handleError(error);
}
});
}
// Generic POST method
post({ url, data, config }) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.axiosInstance) {
throw new Error("Axios instance is not initialized. Please check your configuration.");
}
try {
const response = yield this.axiosInstance.post(url, data, config);
return response.data;
}
catch (error) {
return this.handleError(error);
}
});
}
// Other methods (put, delete) remain the same with the axios instance check
put({ url, data, config }) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.axiosInstance) {
throw new Error("Axios instance is not initialized. Please check your configuration.");
}
try {
const response = yield this.axiosInstance.put(url, data, config);
return response.data;
}
catch (error) {
return this.handleError(error);
}
});
}
delete({ url, config }) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.axiosInstance) {
throw new Error("Axios instance is not initialized. Please check your configuration.");
}
try {
const response = yield this.axiosInstance.delete(url, config);
return response.data;
}
catch (error) {
return this.handleError(error);
}
});
}
// Common error handling method remains unchanged
handleError(error) {
if (error.response) {
console.error("Response error:", error.response.status, error.response.data);
throw new Error(`API Error: ${error.response.status} - ${error.response.data}`);
}
else if (error.request) {
console.error("Request error:", error.request);
throw new Error("No response received from API");
}
else {
console.error("General error:", error.message);
throw new Error(`Error: ${error.message}`);
}
}
}
//# sourceMappingURL=index.js.map