UNPKG

laravel-form-validation

Version:
158 lines (157 loc) 4.75 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import axios from 'axios'; import Errors from './Errors'; import { hasFile } from "./Util"; import { serialize } from 'object-to-formdata'; var Form = /** @class */ (function () { /** * Create a new Form instance. * @return {Form} */ function Form() { this.$progress = 0; this.$pending = false; this.$errors = new Errors(); } /** * Make a get request. * * @param {String} url * @param {Object} params * @returns {Promise} */ Form.prototype.get = function (url, params) { if (params === void 0) { params = {}; } return this.submit('get', url, {}, { params: params }); }; /** * Make a post request. * * @param {String} url * @param {Object} data * @returns {Promise} */ Form.prototype.post = function (url, data) { if (data === void 0) { data = {}; } return this.submit('post', url, data); }; /** * Make a patch request. * * @param {String} url * @param {Object} data * @returns {Promise} */ Form.prototype.patch = function (url, data) { if (data === void 0) { data = {}; } return this.submit('patch', url, data); }; /** * Make a put request. * * @param {String} url * @param {Object} data * @returns {Promise} */ Form.prototype.put = function (url, data) { if (data === void 0) { data = {}; } return this.submit('put', url, data); }; /** * Make a delete request. * * @param {String} url * @param {Object} data * @returns {Promise} */ Form.prototype.delete = function (url, data) { if (data === void 0) { data = {}; } return this.submit('delete', url, data); }; /** * Submit the form to the given URL using the method specified. * * @param {String} method * @param {String} url * @param {Object} data * @param {Object} config * @return {Promise} */ Form.prototype.submit = function (method, url, data, config) { var _this = this; if (data === void 0) { data = {}; } if (config === void 0) { config = {}; } var formData = data; method = method.toLowerCase(); this.$progress = 0; this.$errors.clear(); this.$pending = true; if (hasFile(formData)) { formData = serialize(formData, { indices: true, booleansAsIntegers: true }); // Form Method Spoofing is needed to send files using PUT/PATCH/DELETE. // https://laravel.com/docs/routing#form-method-spoofing // https://github.com/laravel/framework/issues/13457 if (method !== 'post') { formData.append('_method', method); method = 'post'; } } return new Promise(function (resolve, reject) { Form.$defaults.axios.request(__assign(__assign({ url: url, method: method, data: formData }, _this.axiosConfig()), config)) .then(function (response) { resolve(response.data); }) .catch(function (error) { _this.onFail(error); reject(error); }) .then(function () { return _this.$pending = false; }); }); }; /** * Returns the axios configuration object. * * @source https://github.com/axios/axios#request-config * @return {Object} */ Form.prototype.axiosConfig = function () { var _this = this; return { onUploadProgress: function (event) { _this.$progress = Math.round((event.loaded * 100) / event.total); } }; }; /** * Handle a error response. * * @param {AxiosError} error */ Form.prototype.onFail = function (error) { var _a; /* istanbul ignore else */ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 422) { var json = error.response.data; this.$errors.record(json.errors); } }; return Form; }()); /* * Expose default values in order to let users customize behavior. */ Form.$defaults = { axios: axios }; export default Form;