Skip to content

Commit

Permalink
Merge pull request #67 from hamdanmahmoud/zip
Browse files Browse the repository at this point in the history
feat: zip download for APIs with response content-type of application/zip
  • Loading branch information
playforada97 authored Feb 7, 2024
2 parents 384a6d9 + 6af8859 commit 9eb775d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/components/Operation/Operation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { observer } from 'mobx-react';
import * as React from 'react';
import { last } from 'lodash';

import {
Badge,
Expand Down Expand Up @@ -121,10 +122,30 @@ export class Operation extends React.Component<OperationProps, OperationState> {

this.setState({ pendingRequest: true });
fetch(`${appendParamsToPath(path, pathParams, queryParams)}`, request)
.then((response: any) => {
.then(async (response: any) => {
const statusCode = response.status;
const statusType = mapStatusCodeToType(statusCode);
const isError = statusType === 'error';
const contentType = response.headers.get('content-type');

if (contentType?.indexOf('application/zip') !== -1 && !isError) {
const slug = last(path?.split('/') || []);
const fileName = slug || 'file';
const bytes = await response.blob();
const temporaryAnchor = document.createElement('a');
temporaryAnchor.href = URL.createObjectURL(bytes);
temporaryAnchor.setAttribute('download', fileName);
temporaryAnchor.click();
this.setState({
response: {
type: mapStatusCodeToType(statusCode),
code: statusCode || 0,
content: '',
},
});
return;
}

response.text().then(data => {
let content = data;
if (contentType && contentType.indexOf('application/json') !== -1) {
Expand Down

0 comments on commit 9eb775d

Please sign in to comment.