@wizecorp/stratusjs
Version:
Stratus React Framework
136 lines • 4.36 kB
JavaScript
/**
* HTTP service for making API calls
*/
export class HttpService {
constructor(options = {}) {
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'HttpService'
});
Object.defineProperty(this, "baseURL", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "defaultHeaders", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "timeout", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.baseURL = options.baseURL || '';
this.defaultHeaders = options.defaultHeaders || {
'Content-Type': 'application/json'
};
this.timeout = options.timeout || 10000;
}
async get(url, config = {}) {
return this.request('GET', url, undefined, config);
}
async post(url, data, config = {}) {
return this.request('POST', url, data, config);
}
async put(url, data, config = {}) {
return this.request('PUT', url, data, config);
}
async delete(url, config = {}) {
return this.request('DELETE', url, undefined, config);
}
async patch(url, data, config = {}) {
return this.request('PATCH', url, data, config);
}
async request(method, url, data, config = {}) {
const fullURL = this.buildURL(url, config.baseURL);
const headers = { ...this.defaultHeaders, ...config.headers };
const timeout = config.timeout || this.timeout;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(fullURL, {
method,
headers,
body: data ? JSON.stringify(data) : undefined,
signal: controller.signal
});
clearTimeout(timeoutId);
if (!response.ok) {
throw new HttpError(`HTTP ${response.status}: ${response.statusText}`, response.status, response);
}
// Handle different response types
const contentType = response.headers.get('content-type');
if (contentType?.includes('application/json')) {
return await response.json();
}
if (contentType?.includes('text/')) {
return await response.text();
}
return await response.blob();
}
catch (error) {
clearTimeout(timeoutId);
if (error instanceof Error && error.name === 'AbortError') {
throw new HttpError('Request timeout', 408);
}
throw error;
}
}
buildURL(url, baseURL) {
const base = baseURL || this.baseURL;
if (!base)
return url;
if (url.startsWith('http://') || url.startsWith('https://'))
return url;
return `${base.replace(/\/$/, '')}/${url.replace(/^\//, '')}`;
}
/**
* Set default headers for all requests
*/
setDefaultHeaders(headers) {
this.defaultHeaders = { ...this.defaultHeaders, ...headers };
}
/**
* Set authorization header
*/
setAuthToken(token, type = 'Bearer') {
this.setDefaultHeaders({
'Authorization': `${type} ${token}`
});
}
/**
* Remove authorization header
*/
clearAuthToken() {
delete this.defaultHeaders['Authorization'];
}
}
/**
* HTTP Error class
*/
export class HttpError extends Error {
constructor(message, status, response) {
super(message);
Object.defineProperty(this, "status", {
enumerable: true,
configurable: true,
writable: true,
value: status
});
Object.defineProperty(this, "response", {
enumerable: true,
configurable: true,
writable: true,
value: response
});
this.name = 'HttpError';
}
}
//# sourceMappingURL=HttpService.js.map