Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cannot download items from Agenda in json format [STTNHUB-362] #1214

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions assets/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@ const defaultOptions = {
redirect: 'manual',
};

interface RequestOptions {
parseJson?: boolean;
}

function options(custom: any = {}) {
return Object.assign({}, defaultOptions, custom);
}

function checkStatus(response: Response): Promise<any> {
function checkStatus(response: Response, requestOptions: RequestOptions = {parseJson: true}): Promise<any> {
const {parseJson = true} = requestOptions;

if (response.status === 204) {
return Promise.resolve({});
} else if (response.status >= 200 && response.status < 300) {
}

if (response.status >= 200 && response.status < 300) {
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/json')) {
if (parseJson && contentType && contentType.includes('application/json')) {
return response.json();
}
return Promise.resolve(response);
} else if (response.status === 400) {
}

if (response.status === 400) {
return response.json().then((data: any) => Promise.reject({errorData: data}));
} else if (response.type === 'opaqueredirect') {
}

if (response.type === 'opaqueredirect') {
window.location.reload();
}

console.error(response);
return Promise.reject(response);
}


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

Expand Down Expand Up @@ -70,12 +83,17 @@ class Server {
* @param {Object} data
* @return {Promise}
*/
post(url: any, data: any, etag?: string) {
post(
url: any,
data: any,
etag?: string,
requestOptions: RequestOptions = {parseJson: true}
) {
return fetch(url, options({
method: 'POST',
headers: getHeaders(etag),
body: data ? JSON.stringify(data) : null,
})).then(checkStatus);
})).then((response) => checkStatus(response, requestOptions));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion assets/wire/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export function submitDownloadItems(items: any, params: any) {
}
else{
try {
const response = await server.post(url, payload);
const response = await server.post(url, payload, undefined, {parseJson: false});
const blob = await response.blob();
initiateDownload(blob);
} catch (error) {
Expand Down
Loading