OpenAPI client getResponseBody handle binary as blob

This commit is contained in:
leekelleher
2024-04-04 11:31:33 +01:00
parent 84006bb722
commit ce14e013d9
2 changed files with 18 additions and 11 deletions

View File

@@ -232,13 +232,21 @@ export const getResponseBody = async (response: Response): Promise<any> => {
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);

View File

@@ -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() {