@basel_mn/qv-api-client
Version:
TypeScript/JavaScript client for QV API, providing easy access to invoices, payments, and Swish integration
53 lines (52 loc) • 2.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const invoices_1 = require("./endpoints/invoices");
const payments_1 = require("./endpoints/payments");
const swish_1 = require("./endpoints/swish");
const return_1 = require("./endpoints/return");
const extend_1 = require("./endpoints/extend");
class ApiClient {
constructor(config) {
const axiosInstance = axios_1.default.create({
baseURL: config.baseURL,
headers: Object.assign({ 'Content-Type': 'application/json' }, (config.accessToken && { Authorization: `Bearer ${config.accessToken}` }))
});
// Add response interceptor for better error handling
axiosInstance.interceptors.response.use(response => response, error => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const { data, status } = error.response;
const enhancedError = new Error(data.message || data.error || 'Server error');
enhancedError.status = status;
enhancedError.details = data;
return Promise.reject(enhancedError);
}
else if (error.request) {
// The request was made but no response was received
const networkError = new Error('Network error - no response received');
networkError.status = 0;
networkError.details = { error: 'Network error' };
return Promise.reject(networkError);
}
else {
// Something happened in setting up the request that triggered an Error
const setupError = new Error(error.message || 'Request setup error');
setupError.status = -1;
setupError.details = { error: error.message };
return Promise.reject(setupError);
}
});
console.log('Axios instance created with base URL:', config.baseURL);
this.invoices = new invoices_1.InvoicesApi(axiosInstance);
this.payments = new payments_1.PaymentsApi(axiosInstance);
this.swish = new swish_1.SwishApi(axiosInstance);
this.return = new return_1.ReturnApi(axiosInstance);
this.extend = new extend_1.ExtendApi(axiosInstance);
}
}
exports.default = ApiClient;