From 6af8859f80eb88fbcd75cdb34cba92ab223bbb3c Mon Sep 17 00:00:00 2001 From: mahamdan Date: Mon, 15 Jan 2024 20:54:40 +0200 Subject: [PATCH] feat: zip download for APIs with response content-type of application/zip --- src/components/Operation/Operation.tsx | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/components/Operation/Operation.tsx b/src/components/Operation/Operation.tsx index ff9b2f1646..1d080fc125 100644 --- a/src/components/Operation/Operation.tsx +++ b/src/components/Operation/Operation.tsx @@ -1,5 +1,6 @@ import { observer } from 'mobx-react'; import * as React from 'react'; +import { last } from 'lodash'; import { Badge, @@ -121,10 +122,30 @@ export class Operation extends React.Component { 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) {