File size: 11,260 Bytes
8b7b267 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
// Enhanced API Client with Caching, Retry Logic, and Better Error Handling
class EnhancedAPIClient {
constructor() {
this.cache = new Map();
this.cacheExpiry = new Map();
this.defaultCacheDuration = 30000; // 30 seconds
this.maxRetries = 3;
this.retryDelay = 1000; // 1 second
}
/**
* Fetch with automatic retry and exponential backoff
*/
async fetchWithRetry(url, options = {}, retries = this.maxRetries) {
try {
const response = await fetch(url, options);
// If response is ok, return it
if (response.ok) {
return response;
}
// If we get a 429 (rate limit) or 5xx error, retry
if ((response.status === 429 || response.status >= 500) && retries > 0) {
const delay = this.retryDelay * (this.maxRetries - retries + 1);
console.warn(`Request failed with status ${response.status}, retrying in ${delay}ms... (${retries} retries left)`);
await this.sleep(delay);
return this.fetchWithRetry(url, options, retries - 1);
}
// Otherwise throw error
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
} catch (error) {
// Network error - retry if we have retries left
if (retries > 0 && error.name === 'TypeError') {
const delay = this.retryDelay * (this.maxRetries - retries + 1);
console.warn(`Network error, retrying in ${delay}ms... (${retries} retries left)`);
await this.sleep(delay);
return this.fetchWithRetry(url, options, retries - 1);
}
throw error;
}
}
/**
* Get data with caching support
*/
async get(url, options = {}) {
const cacheKey = url + JSON.stringify(options);
const cacheDuration = options.cacheDuration || this.defaultCacheDuration;
// Check cache
if (options.cache !== false && this.isCacheValid(cacheKey)) {
console.log(`📦 Cache hit for ${url}`);
return this.cache.get(cacheKey);
}
try {
const response = await this.fetchWithRetry(url, {
...options,
method: 'GET',
headers: {
'Content-Type': 'application/json',
...options.headers
}
});
const data = await response.json();
// Store in cache
if (options.cache !== false) {
this.cache.set(cacheKey, data);
this.cacheExpiry.set(cacheKey, Date.now() + cacheDuration);
}
return data;
} catch (error) {
console.error(`❌ GET request failed for ${url}:`, error);
throw error;
}
}
/**
* Post data without caching
*/
async post(url, body = {}, options = {}) {
try {
const response = await this.fetchWithRetry(url, {
...options,
method: 'POST',
headers: {
'Content-Type': 'application/json',
...options.headers
},
body: JSON.stringify(body)
});
return await response.json();
} catch (error) {
console.error(`❌ POST request failed for ${url}:`, error);
throw error;
}
}
/**
* Check if cache is valid
*/
isCacheValid(key) {
if (!this.cache.has(key)) return false;
const expiry = this.cacheExpiry.get(key);
if (!expiry || Date.now() > expiry) {
this.cache.delete(key);
this.cacheExpiry.delete(key);
return false;
}
return true;
}
/**
* Clear all cache
*/
clearCache() {
this.cache.clear();
this.cacheExpiry.clear();
console.log('🗑️ Cache cleared');
}
/**
* Clear specific cache entry
*/
clearCacheEntry(url) {
const keysToDelete = [];
for (const key of this.cache.keys()) {
if (key.startsWith(url)) {
keysToDelete.push(key);
}
}
keysToDelete.forEach(key => {
this.cache.delete(key);
this.cacheExpiry.delete(key);
});
}
/**
* Sleep utility
*/
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Batch requests with rate limiting
*/
async batchRequest(urls, options = {}) {
const batchSize = options.batchSize || 5;
const delay = options.delay || 100;
const results = [];
for (let i = 0; i < urls.length; i += batchSize) {
const batch = urls.slice(i, i + batchSize);
const batchPromises = batch.map(url => this.get(url, options));
const batchResults = await Promise.allSettled(batchPromises);
results.push(...batchResults);
// Delay between batches
if (i + batchSize < urls.length) {
await this.sleep(delay);
}
}
return results;
}
}
// Create global instance
window.apiClient = new EnhancedAPIClient();
// Enhanced notification system with toast-style notifications
class NotificationManager {
constructor() {
this.container = null;
this.createContainer();
}
createContainer() {
if (document.getElementById('notification-container')) return;
const container = document.createElement('div');
container.id = 'notification-container';
container.style.cssText = `
position: fixed;
top: 100px;
right: 20px;
z-index: 10000;
display: flex;
flex-direction: column;
gap: 10px;
pointer-events: none;
`;
document.body.appendChild(container);
this.container = container;
}
show(message, type = 'info', duration = 5000) {
const toast = document.createElement('div');
toast.className = `notification-toast notification-${type}`;
const icons = {
success: `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>`,
error: `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="15" y1="9" x2="9" y2="15"></line><line x1="9" y1="9" x2="15" y2="15"></line></svg>`,
warning: `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path><line x1="12" y1="9" x2="12" y2="13"></line><line x1="12" y1="17" x2="12.01" y2="17"></line></svg>`,
info: `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="16" x2="12" y2="12"></line><line x1="12" y1="8" x2="12.01" y2="8"></line></svg>`
};
toast.innerHTML = `
<div style="display: flex; align-items: center; gap: 12px;">
<div class="notification-icon">${icons[type] || icons.info}</div>
<div class="notification-message">${message}</div>
<button class="notification-close" onclick="this.parentElement.parentElement.remove()">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
</div>
`;
toast.style.cssText = `
min-width: 300px;
max-width: 500px;
padding: 16px 20px;
background: rgba(17, 24, 39, 0.95);
backdrop-filter: blur(20px) saturate(180%);
border: 1px solid ${this.getBorderColor(type)};
border-left: 4px solid ${this.getBorderColor(type)};
border-radius: 12px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
color: var(--text-primary);
animation: slideInRight 0.3s cubic-bezier(0.4, 0, 0.2, 1);
pointer-events: all;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
`;
this.container.appendChild(toast);
// Auto remove after duration
if (duration > 0) {
setTimeout(() => {
toast.style.animation = 'slideOutRight 0.3s cubic-bezier(0.4, 0, 0.2, 1)';
setTimeout(() => toast.remove(), 300);
}, duration);
}
}
getBorderColor(type) {
const colors = {
success: '#10b981',
error: '#ef4444',
warning: '#f59e0b',
info: '#3b82f6'
};
return colors[type] || colors.info;
}
}
// Create global notification manager
window.notificationManager = new NotificationManager();
// Enhanced show functions
window.showSuccess = (message) => window.notificationManager.show(message, 'success');
window.showError = (message) => window.notificationManager.show(message, 'error');
window.showWarning = (message) => window.notificationManager.show(message, 'warning');
window.showInfo = (message) => window.notificationManager.show(message, 'info');
// Add notification styles
const style = document.createElement('style');
style.textContent = `
@keyframes slideInRight {
from {
opacity: 0;
transform: translateX(100px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes slideOutRight {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(100px);
}
}
.notification-toast:hover {
transform: translateX(-4px);
box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
}
.notification-close {
background: none;
border: none;
color: var(--text-secondary);
cursor: pointer;
padding: 4px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
transition: all 0.2s;
}
.notification-close:hover {
background: rgba(255, 255, 255, 0.1);
color: var(--text-primary);
}
.notification-icon {
display: flex;
align-items: center;
justify-content: center;
}
.notification-message {
flex: 1;
font-size: 14px;
line-height: 1.5;
}
.notification-success .notification-icon {
color: #10b981;
}
.notification-error .notification-icon {
color: #ef4444;
}
.notification-warning .notification-icon {
color: #f59e0b;
}
.notification-info .notification-icon {
color: #3b82f6;
}
`;
document.head.appendChild(style);
console.log('✅ Enhanced API Client and Notification Manager loaded');
|