@dile/crud 1.10.16 → 1.11.0

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.
@@ -8,6 +8,7 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
8
8
  data: { type: Object },
9
9
  method: { type: String },
10
10
  url: { type: String },
11
+ responseType: { type: String },
11
12
  statusSuccessCodes: { type: Array },
12
13
  sendDataAsFormData: { type: Boolean },
13
14
  getValidationErrors: { type: Object },
@@ -40,38 +41,51 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
40
41
  if(this.sendDataAsFormData) {
41
42
  this._prepareFormData();
42
43
  }
44
+ const responseTypeConfig = this.responseType ? { responseType: this.responseType } : undefined;
43
45
  switch(this.method.toLowerCase().trim()) {
44
46
  case 'post':
45
- request = this.axiosInstance.post(this.url, this.computedData);
47
+ request = this.axiosInstance.post(this.url, this.computedData, responseTypeConfig);
46
48
  break;
47
49
  case 'get':
48
50
  request = this.axiosInstance.get(this.url, {
49
- params: this.data
51
+ params: this.data,
52
+ ...(this.responseType && { responseType: this.responseType }),
50
53
  });
51
54
  break;
52
55
  case 'put':
53
- request = this.axiosInstance.put(this.url, this.computedData);
56
+ request = this.axiosInstance.put(this.url, this.computedData, responseTypeConfig);
54
57
  break;
55
58
  case 'delete':
56
- request = this.axiosInstance.delete(this.url, this.computedData);
59
+ request = this.axiosInstance.delete(this.url, this.computedData, responseTypeConfig);
57
60
  break;
58
61
  case 'patch':
59
- request = this.axiosInstance.patch(this.url, this.computedData);
62
+ request = this.axiosInstance.patch(this.url, this.computedData, responseTypeConfig);
60
63
  break
61
64
  }
62
65
  request.then((response) => {
63
66
  this.dispatchResponse(response)
64
67
  if(this.statusSuccessCodes.includes(response.status)) {
65
- this.dispatchEvent(new CustomEvent('ajax-success', {
66
- detail: response.data
67
- }));
68
+ let detail;
69
+ if (this.responseType === 'blob') {
70
+ const disposition = response.headers['content-disposition'] || '';
71
+ const match = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
72
+ const filename = match ? match[1].replace(/['"]/g, '') : 'download';
73
+ detail = {
74
+ blob: response.data,
75
+ filename,
76
+ contentType: response.headers['content-type'],
77
+ };
78
+ } else {
79
+ detail = response.data;
80
+ }
81
+ this.dispatchEvent(new CustomEvent('ajax-success', { detail }));
68
82
  } else {
69
83
  this.dispatchError(this.translations.http_unhandled_success, response.data);
70
84
  }
71
85
  })
72
- .catch(err => {
73
- this.describeError(err);
74
- })
86
+ .catch(async err => {
87
+ await this.describeError(err);
88
+ })
75
89
  .finally(() => {
76
90
  this.formData = null;
77
91
  this.dispatchEvent(new CustomEvent('dile-ajax-request-end', {
@@ -81,10 +95,18 @@ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
81
95
  });
82
96
  }
83
97
 
84
- describeError(err) {
98
+ async describeError(err) {
85
99
  if (err.response) {
86
100
  const res = err.response;
87
- this.dispatchResponse(res);
101
+ if (this.responseType === 'blob' && res.data instanceof Blob) {
102
+ try {
103
+ const text = await res.data.text();
104
+ res.data = JSON.parse(text);
105
+ } catch {
106
+ res.data = {};
107
+ }
108
+ }
109
+ this.dispatchResponse(res);
88
110
 
89
111
  const status = res.status;
90
112
  switch (status) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "1.10.16",
3
+ "version": "1.11.0",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "ac159d7d1e59630ff425386d67e4ebdfd9dede4f"
34
+ "gitHead": "7882377ba8aed5ce9a213a9dd09d9057743237dd"
35
35
  }