@dile/crud 0.0.67 → 0.0.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
10
10
  url: { type: String },
11
11
  statusSuccessCodes: { type: Array },
12
12
  sendDataAsFormData: { type: Boolean },
13
+ getValidationErrors: { type: Object },
13
14
  }
14
15
  }
15
16
 
@@ -18,6 +19,9 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
18
19
  this.method = 'post';
19
20
  this.url = '';
20
21
  this.statusSuccessCodes = [200, 201];
22
+ this.getValidationErrors = (data) => {
23
+ return data.errors
24
+ }
21
25
  }
22
26
 
23
27
  get computedData() {
@@ -52,17 +56,13 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
52
56
  break
53
57
  }
54
58
  request.then((response) => {
59
+ this.dispatchResponse(response)
55
60
  if(this.statusSuccessCodes.includes(response.status)) {
56
- let res = response.data;
57
- if(res.error) {
58
- this.dispatchError(res.data);
59
- } else {
60
- this.dispatchEvent(new CustomEvent('ajax-success', {
61
- detail: res
62
- }));
63
- }
61
+ this.dispatchEvent(new CustomEvent('ajax-success', {
62
+ detail: response.data
63
+ }));
64
64
  } else {
65
- this.dispatchError(this.translations.http_unhandled_success);
65
+ this.dispatchError(this.translations.http_unhandled_success, response.data);
66
66
  }
67
67
  })
68
68
  .catch(err => {
@@ -75,54 +75,68 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
75
75
 
76
76
  describeError(err) {
77
77
  if (err.response) {
78
- const status = err.response.status;
78
+ const res = err.response;
79
+ const status = res.status;
79
80
  switch (status) {
80
81
  case 422:
82
+ this.dispatchError(res.data.message || this.translations.http_422, res.data, this.getValidationErrors(res.data));
83
+ break;
81
84
  case 400:
82
- this.dispatchError(err.response.data.message, err.response.data.errors);
85
+ this.dispatchError(res.data.message || this.translations.http_400, res.data, this.getValidationErrors(res.data));
83
86
  break;
84
87
  case 404:
85
- if(err.response.data.message) {
86
- this.dispatchError(err.response.data.message);
88
+ if(res.data.message) {
89
+ this.dispatchError(res.data.message, res.data);
87
90
  } else {
88
- this.dispatchError(this.translations.http_404);
91
+ this.dispatchError(this.translations.http_404, res.data);
89
92
  }
90
93
  break;
91
94
  case 401:
92
- this.dispatchError(this.translations.http_401);
95
+ this.dispatchError(this.translations.http_401, res.data);
93
96
  break;
94
97
  case 405:
95
- this.dispatchError(this.translations.http_405);
98
+ this.dispatchError(this.translations.http_405, res.data);
96
99
  break;
97
100
  case 413:
98
- this.dispatchError(this.translations.http_413);
101
+ this.dispatchError(this.translations.http_413, res.data);
99
102
  break;
100
103
  case 419:
101
- this.dispatchError(this.translations.http_419);
104
+ this.dispatchError(this.translations.http_419, res.data);
102
105
  break;
103
106
  case 502:
104
- this.dispatchError(this.translations.http_502);
107
+ this.dispatchError(this.translations.http_502, res.data);
105
108
  break;
106
109
  case 504:
107
- this.dispatchError(this.translations.http_504);
110
+ this.dispatchError(this.translations.http_504, res.data);
108
111
  break;
109
112
  default:
110
- this.dispatchError(this.translations.http_other_error);
113
+ this.dispatchError(this.translations.http_other_error, res.data);
111
114
  }
112
115
  } else {
113
- this.dispatchError(this.translations.http_no_response);
116
+ this.dispatchError(this.translations.http_no_response, {});
114
117
  }
115
118
  }
116
119
 
117
- dispatchError(message, errors = []) {
120
+ dispatchError(message, data, errors = []) {
118
121
  this.dispatchEvent(new CustomEvent('ajax-error', {
119
122
  detail: {
120
123
  message,
124
+ data,
121
125
  errors
122
126
  }
123
127
  }));
124
128
  }
125
129
 
130
+ dispatchResponse(response) {
131
+ this.dispatchEvent(new CustomEvent('ajax-response', {
132
+ bubbles: true,
133
+ composed: true,
134
+ detail: {
135
+ response
136
+ }
137
+ }));
138
+ }
139
+
126
140
  _prepareFormData() {
127
141
  this._createFormData();
128
142
  this._addObjectToFormData(this.data);
package/lib/i18n/en.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export const translations = {
2
2
  http_unhandled_success: "Unhandled success server response",
3
3
  http_404: "Not found error",
4
+ http_400: "Bad Request",
5
+ http_422: "Unprocessable Entity",
4
6
  http_401: "Unauthorized",
5
7
  http_405: "Method Not Allowed",
6
8
  http_413: "Content Too Large",
package/lib/i18n/es.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export const translations = {
2
2
  http_unhandled_success: "Respuesta de éxito no procesada",
3
3
  http_404: "Recurso inexistente",
4
+ http_400: "Solicitud incorrecta",
5
+ http_422: "Validación de la solicitud incorrecta",
4
6
  http_401: "No autorizado",
5
7
  http_405: "Método HTTP no permitido",
6
8
  http_413: "Contenido enviado al servidor demasiado largo",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "0.0.67",
3
+ "version": "0.0.69",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -24,12 +24,12 @@
24
24
  },
25
25
  "homepage": "https://dile-components.polydile.com/",
26
26
  "dependencies": {
27
- "@dile/ui": "^2.1.37",
27
+ "@dile/ui": "^2.1.38",
28
28
  "axios": "^1.7.2",
29
29
  "lit": "^2.7.0 || ^3.0.0"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "30f2fe016bd36491891ce254bc97590e1fc17d19"
34
+ "gitHead": "2800729c7bdb8f152e5a693851b323916feb9e79"
35
35
  }