forked from openlayers/openlayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.js
150 lines (136 loc) · 3.52 KB
/
net.js
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
/**
* @module ol/net
*/
import {getUid} from './util.js';
/**
* Simple JSONP helper. Supports error callbacks and a custom callback param.
* The error callback will be called when no JSONP is executed after 10 seconds.
*
* @param {string} url Request url. A 'callback' query parameter will be
* appended.
* @param {Function} callback Callback on success.
* @param {Function} [errback] Callback on error.
* @param {string} [callbackParam] Custom query parameter for the JSONP
* callback. Default is 'callback'.
*/
export function jsonp(url, callback, errback, callbackParam) {
const script = document.createElement('script');
const key = 'olc_' + getUid(callback);
function cleanup() {
delete window[key];
script.parentNode.removeChild(script);
}
script.async = true;
script.src =
url +
(url.includes('?') ? '&' : '?') +
(callbackParam || 'callback') +
'=' +
key;
const timer = setTimeout(function () {
cleanup();
if (errback) {
errback();
}
}, 10000);
window[key] = function (data) {
clearTimeout(timer);
cleanup();
callback(data);
};
document.head.appendChild(script);
}
export class ResponseError extends Error {
/**
* @param {XMLHttpRequest} response The XHR object.
*/
constructor(response) {
const message = 'Unexpected response status: ' + response.status;
super(message);
/**
* @type {string}
*/
this.name = 'ResponseError';
/**
* @type {XMLHttpRequest}
*/
this.response = response;
}
}
export class ClientError extends Error {
/**
* @param {XMLHttpRequest} client The XHR object.
*/
constructor(client) {
super('Failed to issue request');
/**
* @type {string}
*/
this.name = 'ClientError';
/**
* @type {XMLHttpRequest}
*/
this.client = client;
}
}
/**
* @param {string} url The URL.
* @return {Promise<Object>} A promise that resolves to the JSON response.
*/
export function getJSON(url) {
return new Promise(function (resolve, reject) {
/**
* @param {ProgressEvent<XMLHttpRequest>} event The load event.
*/
function onLoad(event) {
const client = event.target;
// status will be 0 for file:// urls
if (!client.status || (client.status >= 200 && client.status < 300)) {
let data;
try {
data = JSON.parse(client.responseText);
} catch (err) {
const message = 'Error parsing response text as JSON: ' + err.message;
reject(new Error(message));
return;
}
resolve(data);
return;
}
reject(new ResponseError(client));
}
/**
* @param {ProgressEvent<XMLHttpRequest>} event The error event.
*/
function onError(event) {
reject(new ClientError(event.target));
}
const client = new XMLHttpRequest();
client.addEventListener('load', onLoad);
client.addEventListener('error', onError);
client.open('GET', url);
client.setRequestHeader('Accept', 'application/json');
client.send();
});
}
/**
* @param {string} base The base URL.
* @param {string} url The potentially relative URL.
* @return {string} The full URL.
*/
export function resolveUrl(base, url) {
if (url.includes('://')) {
return url;
}
return new URL(url, base).href;
}
let originalXHR;
export function overrideXHR(xhr) {
if (typeof XMLHttpRequest !== 'undefined') {
originalXHR = XMLHttpRequest;
}
global.XMLHttpRequest = xhr;
}
export function restoreXHR() {
global.XMLHttpRequest = originalXHR;
}