Skip to content

Commit 437a798

Browse files
authored
fix: Cannot download items from Agenda in json format [STTNHUB-362] (#1214)
* fix: handle both JSON and Blob responses for downloads * refactore logic * address comment * update logic * update params name * optimze code
1 parent 3d903ca commit 437a798

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

assets/server.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,42 @@ const defaultOptions = {
33
redirect: 'manual',
44
};
55

6+
interface RequestOptions {
7+
parseJson?: boolean;
8+
}
9+
610
function options(custom: any = {}) {
711
return Object.assign({}, defaultOptions, custom);
812
}
913

10-
function checkStatus(response: Response): Promise<any> {
14+
function checkStatus(response: Response, requestOptions: RequestOptions = {parseJson: true}): Promise<any> {
15+
const {parseJson = true} = requestOptions;
16+
1117
if (response.status === 204) {
1218
return Promise.resolve({});
13-
} else if (response.status >= 200 && response.status < 300) {
19+
}
20+
21+
if (response.status >= 200 && response.status < 300) {
1422
const contentType = response.headers.get('Content-Type');
15-
if (contentType && contentType.includes('application/json')) {
23+
if (parseJson && contentType && contentType.includes('application/json')) {
1624
return response.json();
1725
}
1826
return Promise.resolve(response);
19-
} else if (response.status === 400) {
27+
}
28+
29+
if (response.status === 400) {
2030
return response.json().then((data: any) => Promise.reject({errorData: data}));
21-
} else if (response.type === 'opaqueredirect') {
31+
}
32+
33+
if (response.type === 'opaqueredirect') {
2234
window.location.reload();
2335
}
2436

2537
console.error(response);
2638
return Promise.reject(response);
2739
}
2840

41+
2942
function getHeaders(etag: any) {
3043
const headers: any = {'Content-Type': 'application/json'};
3144

@@ -70,12 +83,17 @@ class Server {
7083
* @param {Object} data
7184
* @return {Promise}
7285
*/
73-
post(url: any, data: any, etag?: string) {
86+
post(
87+
url: any,
88+
data: any,
89+
etag?: string,
90+
requestOptions: RequestOptions = {parseJson: true}
91+
) {
7492
return fetch(url, options({
7593
method: 'POST',
7694
headers: getHeaders(etag),
7795
body: data ? JSON.stringify(data) : null,
78-
})).then(checkStatus);
96+
})).then((response) => checkStatus(response, requestOptions));
7997
}
8098

8199
/**

assets/wire/actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ export function submitDownloadItems(items: any, params: any) {
447447
}
448448
else{
449449
try {
450-
const response = await server.post(url, payload);
450+
const response = await server.post(url, payload, undefined, {parseJson: false});
451451
const blob = await response.blob();
452452
initiateDownload(blob);
453453
} catch (error) {

0 commit comments

Comments
 (0)