2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
3 typeof define === 'function' && define.amd ? define(['exports'], factory) : |
3 typeof define === 'function' && define.amd ? define(['exports'], factory) : |
4 (factory((global.WHATWGFetch = {}))); |
4 (factory((global.WHATWGFetch = {}))); |
5 }(this, (function (exports) { 'use strict'; |
5 }(this, (function (exports) { 'use strict'; |
6 |
6 |
7 var global = |
7 /* eslint-disable no-prototype-builtins */ |
|
8 var g = |
8 (typeof globalThis !== 'undefined' && globalThis) || |
9 (typeof globalThis !== 'undefined' && globalThis) || |
9 (typeof self !== 'undefined' && self) || |
10 (typeof self !== 'undefined' && self) || |
10 (typeof global !== 'undefined' && global); |
11 // eslint-disable-next-line no-undef |
|
12 (typeof global !== 'undefined' && global) || |
|
13 {}; |
11 |
14 |
12 var support = { |
15 var support = { |
13 searchParams: 'URLSearchParams' in global, |
16 searchParams: 'URLSearchParams' in g, |
14 iterable: 'Symbol' in global && 'iterator' in Symbol, |
17 iterable: 'Symbol' in g && 'iterator' in Symbol, |
15 blob: |
18 blob: |
16 'FileReader' in global && |
19 'FileReader' in g && |
17 'Blob' in global && |
20 'Blob' in g && |
18 (function() { |
21 (function() { |
19 try { |
22 try { |
20 new Blob(); |
23 new Blob(); |
21 return true |
24 return true |
22 } catch (e) { |
25 } catch (e) { |
23 return false |
26 return false |
24 } |
27 } |
25 })(), |
28 })(), |
26 formData: 'FormData' in global, |
29 formData: 'FormData' in g, |
27 arrayBuffer: 'ArrayBuffer' in global |
30 arrayBuffer: 'ArrayBuffer' in g |
28 }; |
31 }; |
29 |
32 |
30 function isDataView(obj) { |
33 function isDataView(obj) { |
31 return obj && DataView.prototype.isPrototypeOf(obj) |
34 return obj && DataView.prototype.isPrototypeOf(obj) |
32 } |
35 } |
93 headers.forEach(function(value, name) { |
96 headers.forEach(function(value, name) { |
94 this.append(name, value); |
97 this.append(name, value); |
95 }, this); |
98 }, this); |
96 } else if (Array.isArray(headers)) { |
99 } else if (Array.isArray(headers)) { |
97 headers.forEach(function(header) { |
100 headers.forEach(function(header) { |
|
101 if (header.length != 2) { |
|
102 throw new TypeError('Headers constructor: expected name/value pair to be length 2, found' + header.length) |
|
103 } |
98 this.append(header[0], header[1]); |
104 this.append(header[0], header[1]); |
99 }, this); |
105 }, this); |
100 } else if (headers) { |
106 } else if (headers) { |
101 Object.getOwnPropertyNames(headers).forEach(function(name) { |
107 Object.getOwnPropertyNames(headers).forEach(function(name) { |
102 this.append(name, headers[name]); |
108 this.append(name, headers[name]); |
163 if (support.iterable) { |
169 if (support.iterable) { |
164 Headers.prototype[Symbol.iterator] = Headers.prototype.entries; |
170 Headers.prototype[Symbol.iterator] = Headers.prototype.entries; |
165 } |
171 } |
166 |
172 |
167 function consumed(body) { |
173 function consumed(body) { |
|
174 if (body._noBody) return |
168 if (body.bodyUsed) { |
175 if (body.bodyUsed) { |
169 return Promise.reject(new TypeError('Already read')) |
176 return Promise.reject(new TypeError('Already read')) |
170 } |
177 } |
171 body.bodyUsed = true; |
178 body.bodyUsed = true; |
172 } |
179 } |
190 } |
197 } |
191 |
198 |
192 function readBlobAsText(blob) { |
199 function readBlobAsText(blob) { |
193 var reader = new FileReader(); |
200 var reader = new FileReader(); |
194 var promise = fileReaderReady(reader); |
201 var promise = fileReaderReady(reader); |
195 reader.readAsText(blob); |
202 var match = /charset=([A-Za-z0-9_-]+)/.exec(blob.type); |
|
203 var encoding = match ? match[1] : 'utf-8'; |
|
204 reader.readAsText(blob, encoding); |
196 return promise |
205 return promise |
197 } |
206 } |
198 |
207 |
199 function readArrayBufferAsText(buf) { |
208 function readArrayBufferAsText(buf) { |
200 var view = new Uint8Array(buf); |
209 var view = new Uint8Array(buf); |
228 on the object before the Proxy is created. This change ensures |
237 on the object before the Proxy is created. This change ensures |
229 Response.bodyUsed exists on the instance, while maintaining the |
238 Response.bodyUsed exists on the instance, while maintaining the |
230 semantic of setting Request.bodyUsed in the constructor before |
239 semantic of setting Request.bodyUsed in the constructor before |
231 _initBody is called. |
240 _initBody is called. |
232 */ |
241 */ |
|
242 // eslint-disable-next-line no-self-assign |
233 this.bodyUsed = this.bodyUsed; |
243 this.bodyUsed = this.bodyUsed; |
234 this._bodyInit = body; |
244 this._bodyInit = body; |
235 if (!body) { |
245 if (!body) { |
|
246 this._noBody = true; |
236 this._bodyText = ''; |
247 this._bodyText = ''; |
237 } else if (typeof body === 'string') { |
248 } else if (typeof body === 'string') { |
238 this._bodyText = body; |
249 this._bodyText = body; |
239 } else if (support.blob && Blob.prototype.isPrototypeOf(body)) { |
250 } else if (support.blob && Blob.prototype.isPrototypeOf(body)) { |
240 this._bodyBlob = body; |
251 this._bodyBlob = body; |
278 throw new Error('could not read FormData body as blob') |
289 throw new Error('could not read FormData body as blob') |
279 } else { |
290 } else { |
280 return Promise.resolve(new Blob([this._bodyText])) |
291 return Promise.resolve(new Blob([this._bodyText])) |
281 } |
292 } |
282 }; |
293 }; |
283 |
294 } |
284 this.arrayBuffer = function() { |
295 |
285 if (this._bodyArrayBuffer) { |
296 this.arrayBuffer = function() { |
286 var isConsumed = consumed(this); |
297 if (this._bodyArrayBuffer) { |
287 if (isConsumed) { |
298 var isConsumed = consumed(this); |
288 return isConsumed |
299 if (isConsumed) { |
289 } |
300 return isConsumed |
290 if (ArrayBuffer.isView(this._bodyArrayBuffer)) { |
301 } else if (ArrayBuffer.isView(this._bodyArrayBuffer)) { |
291 return Promise.resolve( |
302 return Promise.resolve( |
292 this._bodyArrayBuffer.buffer.slice( |
303 this._bodyArrayBuffer.buffer.slice( |
293 this._bodyArrayBuffer.byteOffset, |
304 this._bodyArrayBuffer.byteOffset, |
294 this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength |
305 this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength |
295 ) |
|
296 ) |
306 ) |
297 } else { |
307 ) |
298 return Promise.resolve(this._bodyArrayBuffer) |
|
299 } |
|
300 } else { |
308 } else { |
301 return this.blob().then(readBlobAsArrayBuffer) |
309 return Promise.resolve(this._bodyArrayBuffer) |
302 } |
310 } |
303 }; |
311 } else if (support.blob) { |
304 } |
312 return this.blob().then(readBlobAsArrayBuffer) |
|
313 } else { |
|
314 throw new Error('could not read as ArrayBuffer') |
|
315 } |
|
316 }; |
305 |
317 |
306 this.text = function() { |
318 this.text = function() { |
307 var rejected = consumed(this); |
319 var rejected = consumed(this); |
308 if (rejected) { |
320 if (rejected) { |
309 return rejected |
321 return rejected |
332 |
344 |
333 return this |
345 return this |
334 } |
346 } |
335 |
347 |
336 // HTTP methods whose capitalization should be normalized |
348 // HTTP methods whose capitalization should be normalized |
337 var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']; |
349 var methods = ['CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE']; |
338 |
350 |
339 function normalizeMethod(method) { |
351 function normalizeMethod(method) { |
340 var upcased = method.toUpperCase(); |
352 var upcased = method.toUpperCase(); |
341 return methods.indexOf(upcased) > -1 ? upcased : method |
353 return methods.indexOf(upcased) > -1 ? upcased : method |
342 } |
354 } |
373 if (options.headers || !this.headers) { |
385 if (options.headers || !this.headers) { |
374 this.headers = new Headers(options.headers); |
386 this.headers = new Headers(options.headers); |
375 } |
387 } |
376 this.method = normalizeMethod(options.method || this.method || 'GET'); |
388 this.method = normalizeMethod(options.method || this.method || 'GET'); |
377 this.mode = options.mode || this.mode || null; |
389 this.mode = options.mode || this.mode || null; |
378 this.signal = options.signal || this.signal; |
390 this.signal = options.signal || this.signal || (function () { |
|
391 if ('AbortController' in g) { |
|
392 var ctrl = new AbortController(); |
|
393 return ctrl.signal; |
|
394 } |
|
395 }()); |
379 this.referrer = null; |
396 this.referrer = null; |
380 |
397 |
381 if ((this.method === 'GET' || this.method === 'HEAD') && body) { |
398 if ((this.method === 'GET' || this.method === 'HEAD') && body) { |
382 throw new TypeError('Body not allowed for GET or HEAD requests') |
399 throw new TypeError('Body not allowed for GET or HEAD requests') |
383 } |
400 } |
435 .forEach(function(line) { |
452 .forEach(function(line) { |
436 var parts = line.split(':'); |
453 var parts = line.split(':'); |
437 var key = parts.shift().trim(); |
454 var key = parts.shift().trim(); |
438 if (key) { |
455 if (key) { |
439 var value = parts.join(':').trim(); |
456 var value = parts.join(':').trim(); |
440 headers.append(key, value); |
457 try { |
|
458 headers.append(key, value); |
|
459 } catch (error) { |
|
460 console.warn('Response ' + error.message); |
|
461 } |
441 } |
462 } |
442 }); |
463 }); |
443 return headers |
464 return headers |
444 } |
465 } |
445 |
466 |
453 options = {}; |
474 options = {}; |
454 } |
475 } |
455 |
476 |
456 this.type = 'default'; |
477 this.type = 'default'; |
457 this.status = options.status === undefined ? 200 : options.status; |
478 this.status = options.status === undefined ? 200 : options.status; |
|
479 if (this.status < 200 || this.status > 599) { |
|
480 throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].") |
|
481 } |
458 this.ok = this.status >= 200 && this.status < 300; |
482 this.ok = this.status >= 200 && this.status < 300; |
459 this.statusText = options.statusText === undefined ? '' : '' + options.statusText; |
483 this.statusText = options.statusText === undefined ? '' : '' + options.statusText; |
460 this.headers = new Headers(options.headers); |
484 this.headers = new Headers(options.headers); |
461 this.url = options.url || ''; |
485 this.url = options.url || ''; |
462 this._initBody(bodyInit); |
486 this._initBody(bodyInit); |
472 url: this.url |
496 url: this.url |
473 }) |
497 }) |
474 }; |
498 }; |
475 |
499 |
476 Response.error = function() { |
500 Response.error = function() { |
477 var response = new Response(null, {status: 0, statusText: ''}); |
501 var response = new Response(null, {status: 200, statusText: ''}); |
|
502 response.status = 0; |
478 response.type = 'error'; |
503 response.type = 'error'; |
479 return response |
504 return response |
480 }; |
505 }; |
481 |
506 |
482 var redirectStatuses = [301, 302, 303, 307, 308]; |
507 var redirectStatuses = [301, 302, 303, 307, 308]; |
487 } |
512 } |
488 |
513 |
489 return new Response(null, {status: status, headers: {location: url}}) |
514 return new Response(null, {status: status, headers: {location: url}}) |
490 }; |
515 }; |
491 |
516 |
492 exports.DOMException = global.DOMException; |
517 exports.DOMException = g.DOMException; |
493 try { |
518 try { |
494 new exports.DOMException(); |
519 new exports.DOMException(); |
495 } catch (err) { |
520 } catch (err) { |
496 exports.DOMException = function(message, name) { |
521 exports.DOMException = function(message, name) { |
497 this.message = message; |
522 this.message = message; |
566 |
591 |
567 if ('responseType' in xhr) { |
592 if ('responseType' in xhr) { |
568 if (support.blob) { |
593 if (support.blob) { |
569 xhr.responseType = 'blob'; |
594 xhr.responseType = 'blob'; |
570 } else if ( |
595 } else if ( |
571 support.arrayBuffer && |
596 support.arrayBuffer |
572 request.headers.get('Content-Type') && |
|
573 request.headers.get('Content-Type').indexOf('application/octet-stream') !== -1 |
|
574 ) { |
597 ) { |
575 xhr.responseType = 'arraybuffer'; |
598 xhr.responseType = 'arraybuffer'; |
576 } |
599 } |
577 } |
600 } |
578 |
601 |
579 if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers)) { |
602 if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers || (g.Headers && init.headers instanceof g.Headers))) { |
|
603 var names = []; |
580 Object.getOwnPropertyNames(init.headers).forEach(function(name) { |
604 Object.getOwnPropertyNames(init.headers).forEach(function(name) { |
|
605 names.push(normalizeName(name)); |
581 xhr.setRequestHeader(name, normalizeValue(init.headers[name])); |
606 xhr.setRequestHeader(name, normalizeValue(init.headers[name])); |
|
607 }); |
|
608 request.headers.forEach(function(value, name) { |
|
609 if (names.indexOf(name) === -1) { |
|
610 xhr.setRequestHeader(name, value); |
|
611 } |
582 }); |
612 }); |
583 } else { |
613 } else { |
584 request.headers.forEach(function(value, name) { |
614 request.headers.forEach(function(value, name) { |
585 xhr.setRequestHeader(name, value); |
615 xhr.setRequestHeader(name, value); |
586 }); |
616 }); |
601 }) |
631 }) |
602 } |
632 } |
603 |
633 |
604 fetch.polyfill = true; |
634 fetch.polyfill = true; |
605 |
635 |
606 if (!global.fetch) { |
636 if (!g.fetch) { |
607 global.fetch = fetch; |
637 g.fetch = fetch; |
608 global.Headers = Headers; |
638 g.Headers = Headers; |
609 global.Request = Request; |
639 g.Request = Request; |
610 global.Response = Response; |
640 g.Response = Response; |
611 } |
641 } |
612 |
642 |
613 exports.Headers = Headers; |
643 exports.Headers = Headers; |
614 exports.Request = Request; |
644 exports.Request = Request; |
615 exports.Response = Response; |
645 exports.Response = Response; |