This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
site-base/spark/resources/assets/js/forms/http.js
2017-11-03 16:26:07 +11:00

57 lines
1.3 KiB
JavaScript
Vendored

module.exports = {
/**
* Helper method for making POST HTTP requests.
*/
post(uri, form) {
return Spark.sendForm('post', uri, form);
},
/**
* Helper method for making PUT HTTP requests.
*/
put(uri, form) {
return Spark.sendForm('put', uri, form);
},
/**
* Helper method for making PATCH HTTP requests.
*/
patch(uri, form) {
return Spark.sendForm('patch', uri, form);
},
/**
* Helper method for making DELETE HTTP requests.
*/
delete(uri, form) {
return Spark.sendForm('delete', uri, form);
},
/**
* Send the form to the back-end server.
*
* This function will clear old errors, update "busy" status, etc.
*/
sendForm(method, uri, form) {
return new Promise((resolve, reject) => {
form.startProcessing();
axios[method](uri, JSON.parse(JSON.stringify(form)))
.then(response => {
form.finishProcessing();
resolve(response.data);
})
.catch(errors => {
form.setErrors(errors.response.data.errors);
reject(errors.response.data);
});
});
}
};