0
0
mirror of https://github.com/Doodle3D/Doodle3D-API synced 2024-11-14 16:27:56 +01:00
Doodle3D-API/src/rest.js
2016-04-21 15:52:23 +02:00

85 lines
1.4 KiB
JavaScript

import $ from 'jquery';
const GET_TIMEOUT = 5000;
const POST_TIMEOUT = 10000;
export function get(url) {
return send(url, 'GET');
}
export function post(url, data) {
return send(url, 'POST', data);
}
function send(url, type, data) {
const timeout = (type === 'GET') ? GET_TIMEOUT : POST_TIMEOUT;
return new Promise((resolve, reject) => {
$.ajax({
url, type, data, timeout, dataType: 'json',
success: (response) => {
if (response.status === 'success') {
resolve(response.data);
}
else {
reject(response.msg);
}
}
}).fail(reject);
});
}
/*
import 'github/fetch';
export function get (url) {
return new Promise((resolve, reject) => {
fetch(url).then((response) => {
return response.json();
}).then((json) => {
if (json.status === 'success') {
resolve(json.data);
}
else {
reject(json.msg);
}
}).catch(reject);
});
}
export function post (url, data) {
return new Promise((resolve, reject) => {
fetch(url, {
method: 'post',
enctype: 'x-www-form-urlencoded',
headers: {
'Accept': 'application/json'
},
body: JSON.stringify(data)
}).then((response) => {
return response.json();
}).then((json) => {
if (json.status === 'success') {
resolve(json.data);
}
else {
reject(json.msg);
}
}).catch(reject);
});
}
*/