import files to git
This commit is contained in:
45
src/httpClient.ts
Normal file
45
src/httpClient.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
const isNode = typeof process === "object";
|
||||
|
||||
const $get = (url: string, cb: Function): void => {
|
||||
const req = new XMLHttpRequest();
|
||||
req.onreadystatechange = () => {
|
||||
if (req.readyState === 4 && req.status === 200) {
|
||||
cb(req.responseText);
|
||||
}
|
||||
};
|
||||
req.open("GET", url, true);
|
||||
req.send(null);
|
||||
};
|
||||
|
||||
const $post = (
|
||||
url: string,
|
||||
path: string,
|
||||
data: any,
|
||||
cb: (...args: any[]) => void
|
||||
): void => {
|
||||
const req = new XMLHttpRequest();
|
||||
req.onreadystatechange = () => {
|
||||
if (req.readyState === 4 && req.status === 200) {
|
||||
cb(JSON.parse(req.responseText));
|
||||
}
|
||||
};
|
||||
req.open("POST", url + path);
|
||||
|
||||
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
req.send(JSON.stringify(data));
|
||||
};
|
||||
|
||||
export default {
|
||||
get: (url: string): Promise<any> => {
|
||||
return new Promise((resolve) => {
|
||||
$get(url, (response) => resolve(response));
|
||||
});
|
||||
},
|
||||
post: (url: string, path: string, data: Object): Promise<any> => {
|
||||
return new Promise((resolve) => {
|
||||
$post(url, path, data, (response) => resolve(response));
|
||||
});
|
||||
},
|
||||
};
|
Reference in New Issue
Block a user