1 | import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
2 | import _createClass from "@babel/runtime/helpers/createClass";
|
3 | import _inherits from "@babel/runtime/helpers/inherits";
|
4 | import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
5 | import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
6 | import _wrapNativeSuper from "@babel/runtime/helpers/wrapNativeSuper";
|
7 |
|
8 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
9 |
|
10 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
11 |
|
12 | var AJAXError = function (_Error) {
|
13 | _inherits(AJAXError, _Error);
|
14 |
|
15 | var _super = _createSuper(AJAXError);
|
16 |
|
17 | function AJAXError(message, status, url) {
|
18 | var _this;
|
19 |
|
20 | _classCallCheck(this, AJAXError);
|
21 |
|
22 | _this = _super.call(this, message);
|
23 | _this.status = void 0;
|
24 | _this.url = void 0;
|
25 | _this.status = status;
|
26 | _this.url = url;
|
27 | _this.name = _this.constructor.name;
|
28 | _this.message = message;
|
29 | return _this;
|
30 | }
|
31 |
|
32 | _createClass(AJAXError, [{
|
33 | key: "toString",
|
34 | value: function toString() {
|
35 | return "".concat(this.name, ": ").concat(this.message, " (").concat(this.status, "): ").concat(this.url);
|
36 | }
|
37 | }]);
|
38 |
|
39 | return AJAXError;
|
40 | }(_wrapNativeSuper(Error));
|
41 |
|
42 | function makeRequest(requestParameters) {
|
43 | var xhr = new XMLHttpRequest();
|
44 | xhr.open('GET', requestParameters.url, true);
|
45 |
|
46 | for (var k in requestParameters.headers) {
|
47 | if (requestParameters.headers.hasOwnProperty(k)) {
|
48 | xhr.setRequestHeader(k, requestParameters.headers[k]);
|
49 | }
|
50 | }
|
51 |
|
52 | xhr.withCredentials = requestParameters.credentials === 'include';
|
53 | return xhr;
|
54 | }
|
55 |
|
56 | export var getJSON = function getJSON(requestParameters, callback) {
|
57 | var xhr = makeRequest(requestParameters);
|
58 | xhr.setRequestHeader('Accept', 'application/json');
|
59 |
|
60 | xhr.onerror = function () {
|
61 | callback(new Error(xhr.statusText));
|
62 | };
|
63 |
|
64 | xhr.onload = function () {
|
65 | if (xhr.status >= 200 && xhr.status < 300 && xhr.response) {
|
66 | var data;
|
67 |
|
68 | try {
|
69 | data = JSON.parse(xhr.response);
|
70 | } catch (err) {
|
71 | return callback(err);
|
72 | }
|
73 |
|
74 | callback(null, data);
|
75 | } else {
|
76 | if (xhr.status === 401) {
|
77 | callback(new AJAXError("".concat(xhr.statusText), xhr.status, requestParameters.url));
|
78 | } else {
|
79 | callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url));
|
80 | }
|
81 | }
|
82 | };
|
83 |
|
84 | xhr.send();
|
85 | return xhr;
|
86 | };
|
87 | export var getArrayBuffer = function getArrayBuffer(requestParameters, callback) {
|
88 | var xhr = makeRequest(requestParameters);
|
89 | xhr.responseType = 'arraybuffer';
|
90 |
|
91 | xhr.onerror = function () {
|
92 | callback(new Error(xhr.statusText));
|
93 | };
|
94 |
|
95 | xhr.onload = function () {
|
96 | var response = xhr.response;
|
97 |
|
98 | if (response.byteLength === 0 && xhr.status === 200) {
|
99 | return callback(new Error('http status 200 returned without content.'));
|
100 | }
|
101 |
|
102 | if (xhr.status >= 200 && xhr.status < 300 && xhr.response) {
|
103 | callback(null, {
|
104 | data: response,
|
105 | cacheControl: xhr.getResponseHeader('Cache-Control'),
|
106 | expires: xhr.getResponseHeader('Expires')
|
107 | });
|
108 | } else {
|
109 | callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url));
|
110 | }
|
111 | };
|
112 |
|
113 | xhr.send();
|
114 | return xhr;
|
115 | };
|
116 |
|
117 | function sameOrigin(url) {
|
118 | var a = window.document.createElement('a');
|
119 | a.href = url;
|
120 | return a.protocol === window.document.location.protocol && a.host === window.document.location.host;
|
121 | }
|
122 |
|
123 | var transparentPngUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=';
|
124 | export var getImage = function getImage(requestParameters, callback) {
|
125 | return getArrayBuffer(requestParameters, function (err, imgData) {
|
126 | if (err) {
|
127 | callback(err);
|
128 | } else if (imgData) {
|
129 | var img = new window.Image();
|
130 | img.crossOrigin = 'anonymous';
|
131 | var URL = window.URL || window.webkitURL;
|
132 |
|
133 | img.onload = function () {
|
134 | callback(null, img);
|
135 | URL.revokeObjectURL(img.src);
|
136 | };
|
137 |
|
138 | var blob = new window.Blob([new Uint8Array(imgData.data)], {
|
139 | type: 'image/png'
|
140 | });
|
141 | img.src = imgData.data.byteLength ? URL.createObjectURL(blob) : transparentPngUrl;
|
142 | }
|
143 | });
|
144 | };
|
145 |
|
\ | No newline at end of file |