credix
Version:
Official SDK for Credix Credit Management System
143 lines • 5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
const axios_1 = __importDefault(require("axios"));
const index_js_1 = require("../errors/index.js");
const retry_js_1 = require("./retry.js");
/**
* HTTP Client for making API requests
*/
class HttpClient {
constructor(config) {
this.config = config;
// Create axios instance
this.axios = axios_1.default.create({
baseURL: config.baseUrl,
timeout: config.timeout,
headers: {
'Content-Type': 'application/json',
...config.headers,
},
});
// Add request interceptor
this.axios.interceptors.request.use((requestConfig) => {
// Use AuthManager if available, otherwise fall back to direct headers
if (this.config.authManager) {
const authHeaders = this.config.authManager.getAuthHeaders();
Object.assign(requestConfig.headers, authHeaders);
}
else {
// Fallback to direct auth headers
requestConfig.headers['X-API-Key'] = this.config.apiKey;
requestConfig.headers['X-API-Secret'] = this.config.secretKey;
}
// Debug logging
if (this.config.debug) {
console.log('Request:', {
method: requestConfig.method,
url: requestConfig.url,
headers: requestConfig.headers,
data: requestConfig.data,
});
}
return requestConfig;
}, (error) => Promise.reject(error));
// Add response interceptor
this.axios.interceptors.response.use((response) => {
// Debug logging
if (this.config.debug) {
console.log('Response:', {
status: response.status,
data: response.data,
headers: response.headers,
});
}
return response;
}, (error) => this.handleError(error));
}
/**
* Makes an HTTP request
*/
async request(method, path, data, options) {
const requestFn = async () => {
const config = {
method,
url: path,
data,
params: options?.params,
headers: options?.headers,
timeout: options?.timeout,
signal: options?.signal,
};
const response = await this.axios.request(config);
return response.data;
};
// Wrap with retry logic
return (0, retry_js_1.withRetry)(requestFn, {
maxRetries: this.config.maxRetries,
onRetry: (error, attempt) => {
if (this.config.debug) {
console.warn(`Retry attempt ${attempt}/${this.config.maxRetries}:`, error);
}
},
});
}
/**
* GET request
*/
async get(path, options) {
return this.request('GET', path, undefined, options);
}
/**
* POST request
*/
async post(path, data, options) {
return this.request('POST', path, data, options);
}
/**
* PUT request
*/
async put(path, data, options) {
return this.request('PUT', path, data, options);
}
/**
* PATCH request
*/
async patch(path, data, options) {
return this.request('PATCH', path, data, options);
}
/**
* DELETE request
*/
async delete(path, options) {
return this.request('DELETE', path, undefined, options);
}
/**
* Handles axios errors and converts them to OMError
*/
handleError(error) {
if (axios_1.default.isAxiosError(error)) {
const axiosError = error;
// Network error (no response)
if (!axiosError.response) {
if (axiosError.code === 'ECONNABORTED') {
throw new index_js_1.TimeoutError(`Request timeout after ${this.config.timeout}ms`, this.config.timeout);
}
throw new index_js_1.NetworkError(axiosError.message || 'Network error occurred', {
code: axiosError.code,
});
}
// Get request ID from headers if available
const requestId = axiosError.response.headers['x-request-id'];
// Create CredixError from response
throw index_js_1.CredixError.fromResponse(axiosError.response.status, axiosError.response.data, requestId);
}
// Unknown error
throw index_js_1.CredixError.fromUnknown(error);
}
}
exports.HttpClient = HttpClient;
//# sourceMappingURL=http-client.js.map