Skip to content

Commit d370de7

Browse files
authored
Merge pull request #59 from jordanshatford/feat/support-blob-responses
feat(response): basic support for blob responses
2 parents 3df583a + 640700e commit d370de7

File tree

7 files changed

+106
-4
lines changed

7 files changed

+106
-4
lines changed

src/templates/core/fetch/getResponseBody.hbs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ export const getResponseBody = async (response: Response): Promise<any> => {
33
try {
44
const contentType = response.headers.get('Content-Type');
55
if (contentType) {
6-
const jsonTypes = ['application/json', 'application/problem+json']
6+
const jsonTypes = ['application/json', 'application/problem+json'];
7+
const binaryTypes = ['audio/', 'image/', 'video/'];
78
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
9+
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
810
if (isJSON) {
911
return await response.json();
12+
} else if (isBinary) {
13+
return await response.blob();
1014
} else {
1115
return await response.text();
1216
}

src/templates/core/node/getResponseBody.hbs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ export const getResponseBody = async (response: Response): Promise<any> => {
33
try {
44
const contentType = response.headers.get('Content-Type');
55
if (contentType) {
6-
const jsonTypes = ['application/json', 'application/problem+json']
6+
const jsonTypes = ['application/json', 'application/problem+json'];
7+
const binaryTypes = ['audio/', 'image/', 'video/'];
78
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
9+
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
810
if (isJSON) {
911
return await response.json();
12+
} else if (isBinary) {
13+
return await response.blob();
1014
} else {
1115
return await response.text();
1216
}

src/templates/core/xhr/getResponseBody.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const getResponseBody = (xhr: XMLHttpRequest): any => {
33
try {
44
const contentType = xhr.getResponseHeader('Content-Type');
55
if (contentType) {
6-
const jsonTypes = ['application/json', 'application/problem+json']
6+
const jsonTypes = ['application/json', 'application/problem+json'];
77
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
88
if (isJSON) {
99
return JSON.parse(xhr.responseText);

test/__snapshots__/index.spec.ts.snap

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,13 @@ export const getResponseBody = async (response: Response): Promise<any> => {
465465
const contentType = response.headers.get('Content-Type');
466466
if (contentType) {
467467
const jsonTypes = ['application/json', 'application/problem+json'];
468+
const binaryTypes = ['audio/', 'image/', 'video/'];
468469
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
470+
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
469471
if (isJSON) {
470472
return await response.json();
473+
} else if (isBinary) {
474+
return await response.blob();
471475
} else {
472476
return await response.text();
473477
}
@@ -3320,9 +3324,13 @@ export const getResponseBody = async (response: Response): Promise<any> => {
33203324
const contentType = response.headers.get('Content-Type');
33213325
if (contentType) {
33223326
const jsonTypes = ['application/json', 'application/problem+json'];
3327+
const binaryTypes = ['audio/', 'image/', 'video/'];
33233328
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
3329+
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
33243330
if (isJSON) {
33253331
return await response.json();
3332+
} else if (isBinary) {
3333+
return await response.blob();
33263334
} else {
33273335
return await response.text();
33283336
}
@@ -4087,9 +4095,13 @@ export const getResponseBody = async (response: Response): Promise<any> => {
40874095
const contentType = response.headers.get('Content-Type');
40884096
if (contentType) {
40894097
const jsonTypes = ['application/json', 'application/problem+json'];
4098+
const binaryTypes = ['audio/', 'image/', 'video/'];
40904099
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
4100+
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
40914101
if (isJSON) {
40924102
return await response.json();
4103+
} else if (isBinary) {
4104+
return await response.blob();
40934105
} else {
40944106
return await response.text();
40954107
}
@@ -4399,6 +4411,7 @@ export { DeprecatedService } from './services/DeprecatedService';
43994411
export { DescriptionsService } from './services/DescriptionsService';
44004412
export { DuplicateService } from './services/DuplicateService';
44014413
export { ErrorService } from './services/ErrorService';
4414+
export { FileResponseService } from './services/FileResponseService';
44024415
export { FormDataService } from './services/FormDataService';
44034416
export { HeaderService } from './services/HeaderService';
44044417
export { MultipartService } from './services/MultipartService';
@@ -7587,6 +7600,30 @@ export class ErrorService {
75877600
"
75887601
`;
75897602

7603+
exports[`v3 should generate: test/generated/v3/services/FileResponseService.ts 1`] = `
7604+
"import type { CancelablePromise } from '../core/CancelablePromise';
7605+
import { OpenAPI } from '../core/OpenAPI';
7606+
import { request as __request } from '../core/request';
7607+
7608+
export class FileResponseService {
7609+
/**
7610+
* @param id
7611+
* @returns binary Success
7612+
* @throws ApiError
7613+
*/
7614+
public static fileResponse(id: string): CancelablePromise<Blob> {
7615+
return __request(OpenAPI, {
7616+
method: 'GET',
7617+
url: '/api/v{api-version}/file/{id}',
7618+
path: {
7619+
id,
7620+
},
7621+
});
7622+
}
7623+
}
7624+
"
7625+
`;
7626+
75907627
exports[`v3 should generate: test/generated/v3/services/FormDataService.ts 1`] = `
75917628
"import type { ModelWithString } from '../models/ModelWithString';
75927629
import type { CancelablePromise } from '../core/CancelablePromise';

test/e2e/v3.fetch.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ describe('v3.fetch', () => {
7575
expect(result).toBeDefined();
7676
});
7777

78+
it('support blob response data', async () => {
79+
const result = await browser.evaluate(async () => {
80+
const { FileResponseService } = (window as any).api;
81+
return await FileResponseService.fileResponse('test');
82+
});
83+
expect(result).toBeDefined();
84+
});
85+
7886
it('can abort the request', async () => {
7987
let error;
8088
try {

test/e2e/v3.node.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ describe('v3.node', () => {
6262
expect(result).toBeDefined();
6363
});
6464

65+
it('support blob response data', async () => {
66+
const { FileResponseService } = require('./generated/v3/node/index.js');
67+
const result = await FileResponseService.fileResponse('test');
68+
expect(result).toBeDefined();
69+
});
70+
6571
it('can abort the request', async () => {
6672
let error;
6773
try {

test/spec/v3.json

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,49 @@
12291229
}
12301230
}
12311231
},
1232+
"/api/v{api-version}/file/{id}": {
1233+
"get": {
1234+
"tags": [
1235+
"FileResponse"
1236+
],
1237+
"operationId": "FileResponse",
1238+
"parameters": [
1239+
{
1240+
"name": "id",
1241+
"in": "path",
1242+
"required": true,
1243+
"schema": {
1244+
"type": "string"
1245+
}
1246+
},
1247+
{
1248+
"name": "api-version",
1249+
"in": "path",
1250+
"required": true,
1251+
"schema": {
1252+
"type": "string"
1253+
}
1254+
}
1255+
],
1256+
"responses": {
1257+
"200": {
1258+
"description": "Success",
1259+
"content": {
1260+
"audio/*": {
1261+
"schema": {
1262+
"type": "file"
1263+
}
1264+
},
1265+
"video/*": {
1266+
"schema": {
1267+
"type": "file"
1268+
}
1269+
}
1270+
}
1271+
}
1272+
}
1273+
}
1274+
},
12321275
"/api/v{api-version}/complex": {
12331276
"get": {
12341277
"tags": [
@@ -3135,4 +3178,4 @@
31353178
}
31363179
}
31373180
}
3138-
}
3181+
}

0 commit comments

Comments
 (0)