Skip to content

Commit 0a56087

Browse files
committed
Add blob support to getResponseBody method
1 parent 9a97986 commit 0a56087

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/templates/core/fetch/getResponseBody.hbs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,26 @@ export const getResponseBody = async (response: Response): Promise<any> => {
55
if (contentType) {
66
const jsonTypes = ['application/json', 'application/problem+json']
77
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
8+
const isBlob = contentType.toLowerCase().startsWith('image/')
9+
|| contentType.toLowerCase().startsWith('application/pdf')
10+
|| contentType.toLowerCase().startsWith('application/zip')
11+
|| contentType.toLowerCase().startsWith('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
12+
|| contentType.toLowerCase().startsWith('application/octet-stream');
13+
814
if (isJSON) {
915
return await response.json();
16+
} else if (isBlob) {
17+
const blob = await response.blob();
18+
const disposition = response.headers.get('Content-Disposition');
19+
if (disposition && disposition.indexOf('attachment') !== -1) {
20+
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
21+
const matches = filenameRegex.exec(disposition);
22+
if (matches !== null && matches[1]) {
23+
const filename = decodeURIComponent(matches[1].replace(/['"]/g, ''));
24+
return new File([blob],filename,{ type: contentType })
25+
}
26+
}
27+
return blob;
1028
} else {
1129
return await response.text();
1230
}

0 commit comments

Comments
 (0)