From ce14e013d9111776feb34eb109c5c92ca3ed05d3 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Thu, 4 Apr 2024 11:31:33 +0100 Subject: [PATCH] OpenAPI client `getResponseBody` handle binary as `blob` --- .../external/backend-api/src/core/request.ts | 22 +++++++++++++------ .../workspace-package-builder.element.ts | 7 +++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/external/backend-api/src/core/request.ts b/src/Umbraco.Web.UI.Client/src/external/backend-api/src/core/request.ts index b018a07cae..051758e054 100644 --- a/src/Umbraco.Web.UI.Client/src/external/backend-api/src/core/request.ts +++ b/src/Umbraco.Web.UI.Client/src/external/backend-api/src/core/request.ts @@ -232,13 +232,21 @@ export const getResponseBody = async (response: Response): Promise => { try { const contentType = response.headers.get('Content-Type'); if (contentType) { - const jsonTypes = ['application/json', 'application/problem+json'] - const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); - if (isJSON) { - return await response.json(); - } else { - return await response.text(); - } + + const binaryTypes = ['application/octet-stream', 'application/pdf', 'application/zip']; + const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type)); + if (isBinary) { + return await response.blob(); + } + + const jsonTypes = ['application/json', 'application/problem+json'] + const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type)); + if (isJSON) { + return await response.json(); + } + + return await response.text(); + } } catch (error) { console.error(error); diff --git a/src/Umbraco.Web.UI.Client/src/packages/packages/package-builder/workspace/workspace-package-builder.element.ts b/src/Umbraco.Web.UI.Client/src/packages/packages/package-builder/workspace/workspace-package-builder.element.ts index 280298c9f6..aebb374368 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/packages/package-builder/workspace/workspace-package-builder.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/packages/package-builder/workspace/workspace-package-builder.element.ts @@ -82,10 +82,9 @@ export class UmbWorkspacePackageBuilderElement extends UmbLitElement { const data = await this.#packageRepository.getCreatePackageDownload(this._package.unique); if (!data) return; - // TODO: [LK] Need to review what the server is doing, as different data is returned depending on schema configuration. - // e.g. selecting Media items will return a ZIP file, otherwise it's an XML file. It should be consistent. - //blobDownload(data, 'package.xml', 'text/xml'); - blobDownload(data, 'package.zip', 'application/zip'); + const filename = typeof data === 'object' ? 'package.zip' : 'package.xml'; + const mimeType = typeof data === 'object' ? 'application/zip' : 'text/xml'; + blobDownload(data, filename, mimeType); } #isNameDefined() {