Skip to content

Commit

Permalink
Merge pull request #59 from jordanshatford/feat/support-blob-responses
Browse files Browse the repository at this point in the history
feat(response): basic support for blob responses
  • Loading branch information
mrlubos authored Mar 17, 2024
2 parents 3df583a + 640700e commit d370de7
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/templates/core/fetch/getResponseBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ 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 jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return await response.json();
} else if (isBinary) {
return await response.blob();
} else {
return await response.text();
}
Expand Down
6 changes: 5 additions & 1 deletion src/templates/core/node/getResponseBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ 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 jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return await response.json();
} else if (isBinary) {
return await response.blob();
} else {
return await response.text();
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/xhr/getResponseBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const getResponseBody = (xhr: XMLHttpRequest): any => {
try {
const contentType = xhr.getResponseHeader('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json']
const jsonTypes = ['application/json', 'application/problem+json'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return JSON.parse(xhr.responseText);
Expand Down
37 changes: 37 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,13 @@ export const getResponseBody = async (response: Response): Promise<any> => {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return await response.json();
} else if (isBinary) {
return await response.blob();
} else {
return await response.text();
}
Expand Down Expand Up @@ -3320,9 +3324,13 @@ export const getResponseBody = async (response: Response): Promise<any> => {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return await response.json();
} else if (isBinary) {
return await response.blob();
} else {
return await response.text();
}
Expand Down Expand Up @@ -4087,9 +4095,13 @@ export const getResponseBody = async (response: Response): Promise<any> => {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return await response.json();
} else if (isBinary) {
return await response.blob();
} else {
return await response.text();
}
Expand Down Expand Up @@ -4399,6 +4411,7 @@ export { DeprecatedService } from './services/DeprecatedService';
export { DescriptionsService } from './services/DescriptionsService';
export { DuplicateService } from './services/DuplicateService';
export { ErrorService } from './services/ErrorService';
export { FileResponseService } from './services/FileResponseService';
export { FormDataService } from './services/FormDataService';
export { HeaderService } from './services/HeaderService';
export { MultipartService } from './services/MultipartService';
Expand Down Expand Up @@ -7587,6 +7600,30 @@ export class ErrorService {
"
`;

exports[`v3 should generate: test/generated/v3/services/FileResponseService.ts 1`] = `
"import type { CancelablePromise } from '../core/CancelablePromise';
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';

export class FileResponseService {
/**
* @param id
* @returns binary Success
* @throws ApiError
*/
public static fileResponse(id: string): CancelablePromise<Blob> {
return __request(OpenAPI, {
method: 'GET',
url: '/api/v{api-version}/file/{id}',
path: {
id,
},
});
}
}
"
`;

exports[`v3 should generate: test/generated/v3/services/FormDataService.ts 1`] = `
"import type { ModelWithString } from '../models/ModelWithString';
import type { CancelablePromise } from '../core/CancelablePromise';
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/v3.fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ describe('v3.fetch', () => {
expect(result).toBeDefined();
});

it('support blob response data', async () => {
const result = await browser.evaluate(async () => {
const { FileResponseService } = (window as any).api;
return await FileResponseService.fileResponse('test');
});
expect(result).toBeDefined();
});

it('can abort the request', async () => {
let error;
try {
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/v3.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('v3.node', () => {
expect(result).toBeDefined();
});

it('support blob response data', async () => {
const { FileResponseService } = require('./generated/v3/node/index.js');
const result = await FileResponseService.fileResponse('test');
expect(result).toBeDefined();
});

it('can abort the request', async () => {
let error;
try {
Expand Down
45 changes: 44 additions & 1 deletion test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,49 @@
}
}
},
"/api/v{api-version}/file/{id}": {
"get": {
"tags": [
"FileResponse"
],
"operationId": "FileResponse",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "api-version",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"audio/*": {
"schema": {
"type": "file"
}
},
"video/*": {
"schema": {
"type": "file"
}
}
}
}
}
}
},
"/api/v{api-version}/complex": {
"get": {
"tags": [
Expand Down Expand Up @@ -3135,4 +3178,4 @@
}
}
}
}
}

0 comments on commit d370de7

Please sign in to comment.