@dbs-portal/core-api
Version:
HTTP client and API utilities for DBS Portal
164 lines • 5.07 kB
JavaScript
/**
* HTTP client implementation using Axios
*/
import axios, { isAxiosError } from 'axios';
/**
* HTTP client for making requests using Axios
*/
export class HttpClient {
axiosInstance;
constructor(config = {}) {
// Create Axios instance with default configuration
const axiosConfig = {
timeout: config.timeout || 30000,
responseType: (config.responseType === 'arrayBuffer' ? 'arraybuffer' : config.responseType) || 'json',
validateStatus: config.validateStatus || (status => status >= 200 && status < 300),
};
// Add optional properties only if they exist
if (config.baseURL)
axiosConfig.baseURL = config.baseURL;
if (config.headers)
axiosConfig.headers = config.headers;
if (config.params)
axiosConfig.params = config.params;
if (config.withCredentials !== undefined)
axiosConfig.withCredentials = config.withCredentials;
if (config.maxRedirects !== undefined)
axiosConfig.maxRedirects = config.maxRedirects;
if (config.signal)
axiosConfig.signal = config.signal;
this.axiosInstance = axios.create(axiosConfig);
}
/**
* Transform Axios response to our ApiResponse format
*/
transformAxiosResponse(response) {
return {
data: response.data,
status: response.status,
statusText: response.statusText,
headers: response.headers,
config: response.config,
request: response.request,
};
}
/**
* Transform Axios error to our ApiError format
*/
transformAxiosError(error) {
const apiError = {
name: 'ApiError',
message: error.message,
config: error.config,
request: error.request,
response: error.response ? this.transformAxiosResponse(error.response) : undefined,
code: error.code || 'UNKNOWN_ERROR',
isAxiosError: true,
toJSON: () => ({
name: 'ApiError',
message: error.message,
code: error.code || 'UNKNOWN_ERROR',
status: error.response?.status,
config: error.config,
stack: error.stack,
}),
};
// Copy Error prototype methods
Object.setPrototypeOf(apiError, Error.prototype);
return apiError;
}
/**
* Makes an HTTP request using Axios
*/
async request(config) {
try {
// Transform our config to Axios config
const axiosConfig = { ...config };
// Handle responseType mapping
if (config.responseType === 'arrayBuffer') {
axiosConfig.responseType = 'arraybuffer';
}
const response = await this.axiosInstance.request(axiosConfig);
return this.transformAxiosResponse(response);
}
catch (error) {
if (isAxiosError(error)) {
throw this.transformAxiosError(error);
}
throw error;
}
}
/**
* GET request
*/
async get(url, config) {
return this.request({ ...config, url, method: 'GET' });
}
/**
* POST request
*/
async post(url, data, config) {
return this.request({ ...config, url, method: 'POST', data });
}
/**
* PUT request
*/
async put(url, data, config) {
return this.request({ ...config, url, method: 'PUT', data });
}
/**
* PATCH request
*/
async patch(url, data, config) {
return this.request({ ...config, url, method: 'PATCH', data });
}
/**
* DELETE request
*/
async delete(url, config) {
return this.request({ ...config, url, method: 'DELETE' });
}
/**
* HEAD request
*/
async head(url, config) {
return this.request({ ...config, url, method: 'HEAD' });
}
/**
* OPTIONS request
*/
async options(url, config) {
return this.request({ ...config, url, method: 'OPTIONS' });
}
/**
* Add request interceptor
*/
addRequestInterceptor(onFulfilled, onRejected) {
return this.axiosInstance.interceptors.request.use(onFulfilled, onRejected);
}
/**
* Add response interceptor
*/
addResponseInterceptor(onFulfilled, onRejected) {
return this.axiosInstance.interceptors.response.use(onFulfilled, onRejected);
}
/**
* Remove request interceptor
*/
removeRequestInterceptor(interceptorId) {
this.axiosInstance.interceptors.request.eject(interceptorId);
}
/**
* Remove response interceptor
*/
removeResponseInterceptor(interceptorId) {
this.axiosInstance.interceptors.response.eject(interceptorId);
}
/**
* Get the underlying Axios instance
*/
getAxiosInstance() {
return this.axiosInstance;
}
}
//# sourceMappingURL=http-client.js.map