This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
250 lines (239 loc) · 6.29 KB
/
fetch.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Download and parse a remote JSON document. Use {@link httpReq} instead
*
* @deprecated
*
* @param {string} url
* @param {string} method - HTTP method, either GET, POST or PUT
* @param {string|undefined} credentials - set to "include" if cookies should be passed along CORS requests
* @param {string} body
* @param {function} callback
*
* @returns {Promise}
*
* @example
* import { fetchJSON } from '@datawrapper/shared/fetch';
* fetchJSON('http://api.example.org', 'GET', 'include');
*/
export function fetchJSON(url, method, credentials, body, callback) {
var opts = {
method,
body,
mode: 'cors',
credentials
};
return window
.fetch(url, opts)
.then(res => {
if (!res.ok) throw new Error(res.statusText);
return res.text();
})
.then(text => {
try {
return JSON.parse(text);
} catch (Error) {
// could not parse json, so just return text
console.warn('malformed json input', text);
return text;
}
})
.then(res => {
if (callback) callback(res);
return res;
})
.catch(err => {
if (callback) {
console.error(err);
} else {
throw err;
}
});
}
/**
* Download and parse a JSON document via GET.
* Use {@link httpReq} or {@link httpReq.get} instead.
*
* @deprecated
*
* @param {string} url
* @param {string|undefined} credentials - optional, set to undefined to disable credentials
* @param {function} callback
*
* @returns {Promise}
*
* @example
* import { getJSON } from '@datawrapper/shared/fetch';
* // use it callback style
* getJSON('http://api.example.org', 'include', function(data) {
* console.log(data);
* });
* // or promise-style
* getJSON('http://api.example.org')
* .then(data => {
* console.log(data);
* });
*/
export function getJSON(url, credentials, callback) {
if (arguments.length === 2 && typeof credentials === 'function') {
// swap callback and assume default credentials
callback = credentials;
credentials = 'include';
} else if (arguments.length === 1) {
credentials = 'include';
}
return fetchJSON(url, 'GET', credentials, null, callback);
}
/**
* Download and parse a remote JSON endpoint via POST. credentials
* are included automatically.
* Use {@link httpReq} or {@link httpReq.post} instead.
*
* @deprecated
*
* @param {string} url
* @param {string} body
* @param {function} callback
*
* @returns {Promise}
* @example
* import { postJSON } from '@datawrapper/shared/fetch';
*
* postJSON('http://api.example.org', JSON.stringify({
* query: 'foo',
* page: 12
* }));
*/
export function postJSON(url, body, callback) {
return fetchJSON(url, 'POST', 'include', body, callback);
}
/**
* Download and parse a remote JSON endpoint via PUT. credentials
* are included automatically
* Use {@link httpReq} or {@link httpReq.put} instead.
*
* @deprecated
*
* @param {string} url
* @param {string} body
* @param {function} callback
*
* @returns {Promise}
* @example
* import { putJSON } from '@datawrapper/shared/fetch';
*
* putJSON('http://api.example.org', JSON.stringify({
* query: 'foo',
* page: 12
* }));
*/
export function putJSON(url, body, callback) {
return fetchJSON(url, 'PUT', 'include', body, callback);
}
/**
* @deprecated use httpReq or httpReq.patch instead
*
* Download and parse a remote JSON endpoint via PATCH. credentials
* are included automatically
*
* @param {string} url
* @param {string} body
* @param {function} callback
*
* @returns {Promise}
* @example
* import { patchJSON } from '@datawrapper/shared/fetch';
*
* patchJSON('http://api.example.org', JSON.stringify({
* query: 'foo',
* page: 12
* }));
*/
export function patchJSON(url, body, callback) {
return fetchJSON(url, 'PATCH', 'include', body, callback);
}
/**
* Download and parse a remote JSON endpoint via DELETE. credentials
* are included automatically
* Use {@link httpReq} or {@link httpReq.delete} instead.
*
* @deprecated
*
* @param {string} url
* @param {function} callback
*
* @returns {Promise}
*
* @example
* import { deleteJSON } from '@datawrapper/shared/fetch';
*
* deleteJSON('http://api.example.org/chart/123').then(() => {
* console.log('deleted!')
* });
*/
export function deleteJSON(url, callback) {
return fetchJSON(url, 'DELETE', 'include', null, callback);
}
/**
* injects a `<script>` element to the page to load a new JS script
*
* @param {string} src
* @param {function} callback
*
* @example
* import { loadScript } from '@datawrapper/shared/fetch';
*
* loadScript('/static/js/library.js', () => {
* console.log('library is loaded');
* })
*/
export function loadScript(src, callback = null) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = () => {
if (callback) callback();
resolve();
};
script.onerror = reject;
document.body.appendChild(script);
});
}
/**
* @typedef {object} opts
* @property {string} src - stylesheet URL to load
* @property {DOMElement} parentElement - DOM element to append style tag to
*/
/**
* injects a `<link>` element to the page to load a new stylesheet
*
* @param {string|opts} src
* @param {function} callback
*
* @example
* import { loadStylesheet } from '@datawrapper/shared/fetch';
*
* loadStylesheet('/static/css/library.css', () => {
* console.log('library styles are loaded');
* })
*/
export function loadStylesheet(opts, callback = null) {
if (typeof opts === 'string') {
opts = {
src: opts
};
}
if (!opts.parentElement || typeof opts.parentElement.appendChild !== 'function') {
opts.parentElement = document.head;
}
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = opts.src;
link.onload = () => {
if (callback) callback();
resolve();
};
link.onerror = reject;
opts.parentElement.appendChild(link);
});
}