All files / lib/net Http.ts

14.28% Statements 4/28
100% Branches 0/0
0% Functions 0/1
14.28% Lines 4/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              1x   1x 1x                                                     1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { Base } from '~/straps';
 
export const Http = {
  request: function (options) {
    // Code borrowed from Coffee Script and extended:
    var xhr = new globalThis.XMLHttpRequest();
    xhr.open((options.method || 'get').toUpperCase(), options.url, Base.pick(options.async, true));
    if (options.mimeType) xhr.overrideMimeType(options.mimeType);
    xhr.onload = function () {
      var status = xhr.status;
      if (status === 0 || status === 200) {
        if (options.onLoad) {
          options.onLoad.call(xhr, xhr.responseText);
        }
      } else {
        // @ts-expect-error
        xhr.onerror();
      }
    };
    xhr.onerror = function () {
      var status = xhr.status,
        message = 'Could not load "' + options.url + '" (Status: ' + status + ')';
      if (options.onError) {
        options.onError(message, status);
      } else {
        throw new Error(message);
      }
    };
    return xhr.send(null);
  },
};