2021-12-14 09:25:46 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2021-12-14 09:25:46 +03:00
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* HTTP requests utility.
|
|
|
|
*
|
|
|
|
* This module implements the functionality of HTTP requests.
|
|
|
|
*/
|
2021-12-14 09:25:46 +03:00
|
|
|
window.App.Utils.Http = (function () {
|
2022-01-17 17:22:12 +03:00
|
|
|
/**
|
|
|
|
* Perform an HTTP request.
|
|
|
|
*
|
|
|
|
* @param {String} method
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} data
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2021-12-14 09:25:46 +03:00
|
|
|
function request(method, url, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch(App.Utils.Url.siteUrl(url), {
|
|
|
|
method,
|
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
redirect: 'follow',
|
|
|
|
referrer: 'no-referrer',
|
|
|
|
body: data ? JSON.stringify(data) : undefined
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response
|
|
|
|
.text()
|
|
|
|
.then((message) => {
|
|
|
|
const error = new Error(message);
|
|
|
|
error.status = response.status;
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
resolve(json);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-17 17:22:12 +03:00
|
|
|
/**
|
|
|
|
* Upload the provided file.
|
|
|
|
*
|
|
|
|
* @param {String} method
|
|
|
|
* @param {String} url
|
|
|
|
* @param {File} file
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
function upload(method, url, file) {
|
2021-12-14 09:25:46 +03:00
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
formData.append('file', file, file.name);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch(App.Utils.Url.siteUrl(url), {
|
|
|
|
method,
|
|
|
|
redirect: 'follow',
|
|
|
|
referrer: 'no-referrer',
|
|
|
|
body: formData
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response
|
|
|
|
.text()
|
|
|
|
.then((message) => {
|
|
|
|
const error = new Error(message);
|
|
|
|
error.status = response.status;
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
resolve(json);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-17 17:22:12 +03:00
|
|
|
/**
|
|
|
|
* Download the requested URL.
|
|
|
|
*
|
|
|
|
* @param {String} method
|
|
|
|
* @param {String} url
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
function download(method, url) {
|
2021-12-14 09:25:46 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch(App.Utils.Url.siteUrl(url), {
|
|
|
|
method,
|
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
redirect: 'follow',
|
|
|
|
referrer: 'no-referrer'
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response
|
|
|
|
.text()
|
|
|
|
.then((message) => {
|
|
|
|
const error = new Error(message);
|
|
|
|
error.status = response.status;
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
return response.arrayBuffer();
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
resolve(json);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
request,
|
|
|
|
upload,
|
|
|
|
download
|
|
|
|
};
|
|
|
|
})();
|