-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.js
More file actions
369 lines (328 loc) · 9.86 KB
/
Copy pathHttpRequest.js
File metadata and controls
369 lines (328 loc) · 9.86 KB
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
364
365
366
367
368
369
/**
* HttpRequest - HTTP Client dengan fitur lengkap
*
* @class HttpRequest
* @author Ahmad Sofyan
* @version 2.0.0
* @repo https://github.com/ahmadsopyan9/HttpRequest
*
* @example
* const http = new HttpRequest('https://api.example.com');
*
* // GET request
* const data = await http.get('/users', { page: 1 });
*
* // POST request
* const result = await http.post('/users', { name: 'John' });
*
* // With timeout & retry
* const result = await http.post('/users', { name: 'John' }, {
* timeout: 5000,
* retries: 3
* });
*/
class HttpRequest {
/**
* Create a new HttpRequest instance
* @param {string} baseURL - Base URL for all requests
* @param {Object} headers - Default headers for all requests
*/
constructor(baseURL = '', headers = {}) {
this.baseURL = baseURL.endsWith('/') ? baseURL.slice(0, -1) : baseURL;
this.headers = {
'Content-Type': 'application/json',
...headers,
};
this.requestInterceptors = [];
this.responseInterceptors = [];
this.cache = new Map();
this.defaultTimeout = 30000;
this.defaultRetries = 0;
}
/**
* Clean headers from null/undefined values
* @param {Object} headers - Headers to clean
* @returns {Object} Cleaned headers
* @private
*/
_cleanHeaders(headers) {
return Object.fromEntries(
Object.entries(headers)
.filter(([_, value]) => value != null)
);
}
/**
* Build full URL
* @param {string} url - Request URL
* @returns {string} Full URL
* @private
*/
_buildURL(url) {
if (!this.baseURL) return url;
if (url.startsWith('http://') || url.startsWith('https://')) {
return url;
}
const base = this.baseURL;
const path = url.startsWith('/') ? url : '/' + url;
return base + path;
}
/**
* Add request interceptor
* @param {Function} fn - Interceptor function
*/
addRequestInterceptor(fn) {
this.requestInterceptors.push(fn);
return this;
}
/**
* Add response interceptor
* @param {Function} fn - Interceptor function
*/
addResponseInterceptor(fn) {
this.responseInterceptors.push(fn);
return this;
}
/**
* Execute HTTP request
* @param {string} method - HTTP method
* @param {string} url - Request URL
* @param {*} data - Request data
* @param {Object} options - Request options
* @param {number} options.timeout - Request timeout in ms
* @param {number} options.retries - Number of retry attempts
* @param {number} options.retryDelay - Delay between retries in ms
* @param {boolean} options.cache - Enable cache for GET requests
* @param {number} options.cacheTTL - Cache TTL in ms
* @param {AbortSignal} options.signal - Abort signal
* @param {Object} options.headers - Custom headers
* @returns {Promise<*>} Response data
*/
async request(method, url, data = null, options = {}) {
// Run request interceptors
let context = { method, url, data, options };
for (const interceptor of this.requestInterceptors) {
const result = interceptor(context);
if (result) context = result;
}
// Check cache for GET
const cacheKey = `${method}:${url}?${JSON.stringify(data)}`;
if (method === 'GET' && options.cache) {
const cached = this.cache.get(cacheKey);
if (cached) {
const now = Date.now();
const ttl = options.cacheTTL || 60000;
if (now - cached.timestamp < ttl) {
return cached.data;
}
}
}
const maxRetries = options.retries || this.defaultRetries;
const retryDelay = options.retryDelay || 1000;
let lastError;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
const result = await this._executeRequest(method, url, data, options);
// Cache result for GET
if (method === 'GET' && options.cache) {
this.cache.set(cacheKey, {
data: result,
timestamp: Date.now()
});
}
return result;
} catch (error) {
lastError = error;
if (attempt < maxRetries) {
await new Promise(resolve => setTimeout(resolve, retryDelay * (attempt + 1)));
continue;
}
throw error;
}
}
throw lastError;
}
/**
* Execute single request attempt
* @private
*/
async _executeRequest(method, url, data = null, options = {}) {
// Build URL
let endpoint = this._buildURL(url);
const timeout = options.timeout || this.defaultTimeout;
// Clean headers
const headers = {
...this._cleanHeaders(this.headers),
...this._cleanHeaders(options.headers || {}),
};
// Handle GET query params
if (method === 'GET' && data) {
const urlObj = new URL(endpoint);
const params = new URLSearchParams(urlObj.search);
for (const [key, value] of Object.entries(data)) {
if (value !== undefined && value !== null) {
params.set(key, value);
}
}
endpoint = `${urlObj.origin}${urlObj.pathname}?${params}`;
data = null;
}
// Prepare body
let body = data;
if (data && headers['Content-Type']) {
if (headers['Content-Type'].includes('application/json')) {
body = JSON.stringify(data);
} else if (headers['Content-Type'].includes('application/x-www-form-urlencoded')) {
body = new URLSearchParams(data).toString();
}
}
// Setup AbortController for timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
// Execute fetch
const response = await fetch(endpoint, {
method,
headers,
body,
signal: options.signal || controller.signal,
...options,
});
clearTimeout(timeoutId);
// Handle error response
if (!response.ok) {
let errorData;
try {
const contentType = response.headers.get('content-type') || '';
errorData = contentType.includes('application/json')
? await response.json()
: await response.text();
} catch {
errorData = 'Failed to parse error response';
}
const error = new Error(`HTTP Error ${response.status}: ${response.statusText}`);
error.status = response.status;
error.data = errorData;
error.url = endpoint;
error.method = method;
throw error;
}
// Parse response
const contentType = response.headers.get('content-type') || '';
let responseData = contentType.includes('application/json')
? await response.json()
: await response.text();
// Run response interceptors
for (const interceptor of this.responseInterceptors) {
const result = interceptor(responseData);
if (result) responseData = result;
}
return responseData;
} catch (error) {
clearTimeout(timeoutId);
if (error.name === 'AbortError') {
throw new Error(`Request timeout after ${timeout}ms`);
}
throw error;
}
}
/**
* GET request
* @param {string} url - Request URL
* @param {Object} params - Query parameters
* @param {Object} options - Request options
* @returns {Promise<*>}
*/
get(url, params = {}, options = {}) {
return this.request('GET', url, params, options);
}
/**
* POST request
* @param {string} url - Request URL
* @param {*} data - Request data
* @param {Object} options - Request options
* @returns {Promise<*>}
*/
post(url, data = null, options = {}) {
return this.request('POST', url, data, options);
}
/**
* PUT request
* @param {string} url - Request URL
* @param {*} data - Request data
* @param {Object} options - Request options
* @returns {Promise<*>}
*/
put(url, data = null, options = {}) {
return this.request('PUT', url, data, options);
}
/**
* DELETE request
* @param {string} url - Request URL
* @param {Object} options - Request options
* @returns {Promise<*>}
*/
delete(url, options = {}) {
return this.request('DELETE', url, null, options);
}
/**
* Upload file with progress
* @param {string} url - Upload URL
* @param {File} file - File to upload
* @param {Function} onProgress - Progress callback
* @param {Object} options - Request options
* @returns {Promise<*>}
*/
upload(url, file, onProgress = null, options = {}) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const fullUrl = this._buildURL(url);
xhr.open('POST', fullUrl, true);
// Set headers (except Content-Type for FormData)
for (const [key, value] of Object.entries(this.headers)) {
if (key.toLowerCase() !== 'content-type') {
xhr.setRequestHeader(key, value);
}
}
// Progress tracking
if (onProgress) {
xhr.upload.onprogress = (event) => {
if (event.lengthComputable) {
const percent = Math.round((event.loaded / event.total) * 100);
onProgress(percent, event);
}
};
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
try {
resolve(JSON.parse(xhr.responseText));
} catch {
resolve(xhr.responseText);
}
} else {
reject(new Error(`Upload failed: ${xhr.status} ${xhr.statusText}`));
}
};
xhr.onerror = () => reject(new Error('Upload failed'));
const formData = new FormData();
formData.append('file', file);
xhr.send(formData);
});
}
/**
* Clear cache
* @param {string} key - Specific cache key to clear
*/
clearCache(key = null) {
if (key) {
this.cache.delete(key);
} else {
this.cache.clear();
}
return this;
}
}
// Export
if (typeof module !== 'undefined' && module.exports) {
module.exports = HttpRequest;
}