@dile/crud
Advanced tools
Comparing version 0.0.67 to 0.0.68
@@ -13,2 +13,3 @@ import { LitElement, html } from 'lit'; | ||
sendDataAsFormData: { type: Boolean }, | ||
getValidationErrors: { type: Object }, | ||
} | ||
@@ -22,2 +23,5 @@ } | ||
this.statusSuccessCodes = [200, 201]; | ||
this.getValidationErrors = (data) => { | ||
return data.errors | ||
} | ||
} | ||
@@ -57,13 +61,9 @@ | ||
request.then((response) => { | ||
this.dispatchResponse(response) | ||
if(this.statusSuccessCodes.includes(response.status)) { | ||
let res = response.data; | ||
if(res.error) { | ||
this.dispatchError(res.data); | ||
} else { | ||
this.dispatchEvent(new CustomEvent('ajax-success', { | ||
detail: res | ||
})); | ||
} | ||
this.dispatchEvent(new CustomEvent('ajax-success', { | ||
detail: response.data | ||
})); | ||
} else { | ||
this.dispatchError(this.translations.http_unhandled_success); | ||
this.dispatchError(this.translations.http_unhandled_success, response.data); | ||
} | ||
@@ -81,45 +81,47 @@ }) | ||
if (err.response) { | ||
const status = err.response.status; | ||
const res = err.response; | ||
const status = res.status; | ||
switch (status) { | ||
case 422: | ||
case 400: | ||
this.dispatchError(err.response.data.message, err.response.data.errors); | ||
this.dispatchError(res.data.message, res.data, this.getValidationErrors(res.data)); | ||
break; | ||
case 404: | ||
if(err.response.data.message) { | ||
this.dispatchError(err.response.data.message); | ||
if(res.data.message) { | ||
this.dispatchError(res.data.message, res.data); | ||
} else { | ||
this.dispatchError(this.translations.http_404); | ||
this.dispatchError(this.translations.http_404, res.data); | ||
} | ||
break; | ||
case 401: | ||
this.dispatchError(this.translations.http_401); | ||
this.dispatchError(this.translations.http_401, res.data); | ||
break; | ||
case 405: | ||
this.dispatchError(this.translations.http_405); | ||
this.dispatchError(this.translations.http_405, res.data); | ||
break; | ||
case 413: | ||
this.dispatchError(this.translations.http_413); | ||
this.dispatchError(this.translations.http_413, res.data); | ||
break; | ||
case 419: | ||
this.dispatchError(this.translations.http_419); | ||
this.dispatchError(this.translations.http_419, res.data); | ||
break; | ||
case 502: | ||
this.dispatchError(this.translations.http_502); | ||
this.dispatchError(this.translations.http_502, res.data); | ||
break; | ||
case 504: | ||
this.dispatchError(this.translations.http_504); | ||
this.dispatchError(this.translations.http_504, res.data); | ||
break; | ||
default: | ||
this.dispatchError(this.translations.http_other_error); | ||
this.dispatchError(this.translations.http_other_error, res.data); | ||
} | ||
} else { | ||
this.dispatchError(this.translations.http_no_response); | ||
this.dispatchError(this.translations.http_no_response, {}); | ||
} | ||
} | ||
dispatchError(message, errors = []) { | ||
dispatchError(message, data, errors = []) { | ||
this.dispatchEvent(new CustomEvent('ajax-error', { | ||
detail: { | ||
message, | ||
data, | ||
errors | ||
@@ -130,2 +132,10 @@ } | ||
dispatchResponse(response) { | ||
this.dispatchEvent(new CustomEvent('ajax-response', { | ||
detail: { | ||
response | ||
} | ||
})); | ||
} | ||
_prepareFormData() { | ||
@@ -132,0 +142,0 @@ this._createFormData(); |
{ | ||
"name": "@dile/crud", | ||
"version": "0.0.67", | ||
"version": "0.0.68", | ||
"description": "Components to create a generic crud system based on Web Components and Lit", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
}, | ||
"gitHead": "30f2fe016bd36491891ce254bc97590e1fc17d19" | ||
"gitHead": "98d572545dae6e89cda4f2240bc0e5caface20e0" | ||
} |
117731
3532