-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterService.js
More file actions
34 lines (32 loc) · 803 Bytes
/
twitterService.js
File metadata and controls
34 lines (32 loc) · 803 Bytes
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
/**
* Created by jonlazarini on 08/03/17.
*/
import { XMLHttpRequest } from 'xmlhttprequest';
/**
*
* @param obj
* @returns {Promise}
*/
const requestService = obj => (
// creates XMLHTTPRequest - headers, body
new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(obj.method || 'GET', obj.url);
if (obj.headers) {
Object.keys(obj.headers).forEach((key) => {
xhr.setRequestHeader(key, obj.headers[key]);
});
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.responseText);
} else {
// reject(xhr.statusText);
reject(xhr.responseText);
}
};
xhr.onerror = () => reject(xhr.statusText);
xhr.send(obj.body);
})
);
export default requestService;