File size: 8,096 Bytes
b190b45 |
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 359 360 361 362 363 |
# API Helper Utilities
## Overview
The `APIHelper` class provides a comprehensive set of utilities for making API requests, handling authentication, and managing common operations across the application.
## Features
- β
**Token Management**: Automatic JWT expiration checking
- β
**API Requests**: Simplified fetch with error handling
- β
**Data Extraction**: Smart array extraction from various response formats
- β
**Health Monitoring**: Periodic API health checks
- β
**UI Helpers**: Toast notifications, formatting utilities
- β
**Performance**: Debounce and throttle functions
---
## Usage
### Basic Import
```javascript
import { APIHelper } from '../../shared/js/utils/api-helper.js';
```
---
## API Methods
### Authentication
#### `getHeaders()`
Returns headers with optional Authorization token. Automatically checks token expiration.
```javascript
const headers = APIHelper.getHeaders();
// Returns: { 'Content-Type': 'application/json', 'Authorization': 'Bearer <token>' }
```
#### `isTokenExpired(token)`
Checks if a JWT token is expired.
```javascript
const expired = APIHelper.isTokenExpired(token);
// Returns: boolean
```
---
### API Requests
#### `fetchAPI(url, options)`
Fetch data with automatic authorization and error handling.
```javascript
// GET request
const data = await APIHelper.fetchAPI('/api/market/top?limit=10');
// POST request
const result = await APIHelper.fetchAPI('/api/sentiment/analyze', {
method: 'POST',
body: JSON.stringify({ text: 'Bitcoin is great!' })
});
```
---
### Data Processing
#### `extractArray(data, keys)`
Intelligently extract arrays from various response formats.
```javascript
// Works with direct arrays
const arr1 = APIHelper.extractArray([1, 2, 3]);
// Works with nested data
const arr2 = APIHelper.extractArray({ markets: [...] }, ['markets', 'data']);
// Works with objects
const arr3 = APIHelper.extractArray({ item1: {}, item2: {} });
```
---
### Health Monitoring
#### `checkHealth()`
Check API health status.
```javascript
const health = await APIHelper.checkHealth();
// Returns: { status: 'online', healthy: true, data: {...} }
```
#### `monitorHealth(callback, interval)`
Setup periodic health monitoring.
```javascript
const intervalId = APIHelper.monitorHealth((health) => {
console.log('API Status:', health.status);
if (!health.healthy) {
console.warn('API is down!');
}
}, 30000); // Check every 30 seconds
// Later, stop monitoring
clearInterval(intervalId);
```
---
### UI Utilities
#### `showToast(message, type, duration)`
Display toast notifications.
```javascript
APIHelper.showToast('Operation successful!', 'success');
APIHelper.showToast('Something went wrong', 'error');
APIHelper.showToast('Please wait...', 'info');
APIHelper.showToast('Check your input', 'warning');
```
#### `formatCurrency(amount, currency)`
Format numbers as currency.
```javascript
const formatted = APIHelper.formatCurrency(1234.56);
// Returns: "$1,234.56"
```
#### `formatPercentage(value, decimals)`
Format values as percentages.
```javascript
const percent = APIHelper.formatPercentage(2.5);
// Returns: "+2.50%"
```
#### `formatNumber(num, options)`
Format numbers with locale settings.
```javascript
const formatted = APIHelper.formatNumber(1000000);
// Returns: "1,000,000"
```
---
### Performance Utilities
#### `debounce(func, wait)`
Debounce function calls.
```javascript
const debouncedSearch = APIHelper.debounce((query) => {
console.log('Searching:', query);
}, 300);
// Call multiple times, only executes once after 300ms
debouncedSearch('bitcoin');
debouncedSearch('ethereum');
debouncedSearch('solana');
```
#### `throttle(func, limit)`
Throttle function calls.
```javascript
const throttledScroll = APIHelper.throttle(() => {
console.log('Scroll event');
}, 100);
window.addEventListener('scroll', throttledScroll);
```
---
## Complete Example: Building a Page
```javascript
import { APIHelper } from '../../shared/js/utils/api-helper.js';
class YourPage {
constructor() {
this.data = [];
this.healthMonitor = null;
}
async init() {
// Setup health monitoring
this.healthMonitor = APIHelper.monitorHealth((health) => {
console.log('API Health:', health.status);
});
// Load data
await this.loadData();
// Setup event listeners
this.bindEvents();
}
async loadData() {
try {
// Fetch data using APIHelper
const response = await APIHelper.fetchAPI('/api/your-endpoint');
// Extract array safely
this.data = APIHelper.extractArray(response, ['data', 'items']);
// Render
this.render();
// Show success
APIHelper.showToast('Data loaded successfully!', 'success');
} catch (error) {
console.error('Load error:', error);
// Use fallback data
this.data = this.getDemoData();
this.render();
// Show error
APIHelper.showToast('Using demo data', 'warning');
}
}
bindEvents() {
// Debounced search
const searchInput = document.getElementById('search');
const debouncedSearch = APIHelper.debounce((query) => {
this.filterData(query);
}, 300);
searchInput?.addEventListener('input', (e) => {
debouncedSearch(e.target.value);
});
}
render() {
// Render your data
this.data.forEach(item => {
const price = APIHelper.formatCurrency(item.price);
const change = APIHelper.formatPercentage(item.change);
console.log(`${item.name}: ${price} (${change})`);
});
}
getDemoData() {
return [
{ name: 'Bitcoin', price: 50000, change: 2.5 },
{ name: 'Ethereum', price: 3000, change: -1.2 }
];
}
destroy() {
// Cleanup
if (this.healthMonitor) {
clearInterval(this.healthMonitor);
}
}
}
// Initialize
const page = new YourPage();
page.init();
```
---
## Best Practices
### 1. Always Use APIHelper for Fetch Requests
```javascript
// β
Good
const data = await APIHelper.fetchAPI('/api/endpoint');
// β Avoid
const response = await fetch('/api/endpoint');
const data = await response.json();
```
### 2. Extract Arrays Safely
```javascript
// β
Good
const items = APIHelper.extractArray(response, ['items', 'data']);
// β Avoid (can fail)
const items = response.items;
```
### 3. Use Debounce for User Input
```javascript
// β
Good
const debouncedHandler = APIHelper.debounce(handler, 300);
input.addEventListener('input', debouncedHandler);
// β Avoid (too many calls)
input.addEventListener('input', handler);
```
### 4. Monitor API Health
```javascript
// β
Good
APIHelper.monitorHealth((health) => {
updateUI(health.status);
});
// β Avoid (no health awareness)
// Just hope the API is up
```
---
## Token Expiration
The `APIHelper` automatically checks JWT token expiration:
1. **On Every Request**: Before adding Authorization header
2. **Automatic Removal**: Expired tokens are removed from localStorage
3. **Graceful Degradation**: Requests continue without auth if token expired
```javascript
// Token is checked automatically
const data = await APIHelper.fetchAPI('/api/protected-route');
// If token expired, it's removed and request proceeds without auth
```
---
## Error Handling
All `APIHelper` methods handle errors gracefully:
```javascript
try {
const data = await APIHelper.fetchAPI('/api/endpoint');
// Use data
} catch (error) {
// Error is already logged by APIHelper
// Use fallback data
const data = getDemoData();
}
```
---
## Browser Compatibility
- β
Modern browsers (ES6+ modules)
- β
Chrome 61+
- β
Firefox 60+
- β
Safari 11+
- β
Edge 16+
---
## License
Part of Crypto Monitor ULTIMATE project.
|