(input, init)
| 455 | } |
| 456 | |
| 457 | function fetch(input, init) { |
| 458 | return new Promise(function (resolve, reject) { |
| 459 | var request = new Request(input, init); |
| 460 | |
| 461 | if (request.signal && request.signal.aborted) { |
| 462 | return reject(new exports.DOMException("Aborted", "AbortError")); |
| 463 | } |
| 464 | |
| 465 | var xhr = new XMLHttpRequest(); |
| 466 | |
| 467 | function abortXhr() { |
| 468 | xhr.abort(); |
| 469 | } |
| 470 | |
| 471 | xhr.onload = function () { |
| 472 | var options = { |
| 473 | status: xhr.status, |
| 474 | statusText: xhr.statusText, |
| 475 | headers: parseHeaders(xhr.getAllResponseHeaders() || "") |
| 476 | }; |
| 477 | options.url = |
| 478 | "responseURL" in xhr |
| 479 | ? xhr.responseURL |
| 480 | : options.headers.get("X-Request-URL"); |
| 481 | var body = "response" in xhr ? xhr.response : xhr.responseText; |
| 482 | resolve(new Response(body, options)); |
| 483 | }; |
| 484 | |
| 485 | xhr.onerror = function () { |
| 486 | reject(new TypeError("Network request failed")); |
| 487 | }; |
| 488 | |
| 489 | xhr.ontimeout = function () { |
| 490 | reject(new TypeError("Network request failed")); |
| 491 | }; |
| 492 | |
| 493 | xhr.onabort = function () { |
| 494 | reject(new exports.DOMException("Aborted", "AbortError")); |
| 495 | }; |
| 496 | |
| 497 | xhr.open(request.method, request.url, true); |
| 498 | |
| 499 | if (request.credentials === "include") { |
| 500 | xhr.withCredentials = true; |
| 501 | } else if (request.credentials === "omit") { |
| 502 | xhr.withCredentials = false; |
| 503 | } |
| 504 | |
| 505 | if ("responseType" in xhr && support.blob) { |
| 506 | xhr.responseType = "blob"; |
| 507 | } |
| 508 | |
| 509 | request.headers.forEach(function (value, name) { |
| 510 | xhr.setRequestHeader(name, value); |
| 511 | }); |
| 512 | |
| 513 | if (request.signal) { |
| 514 | request.signal.addEventListener("abort", abortXhr); |
no test coverage detected