Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/templates/core/ApiRequestOptions.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export type ApiRequestOptions = {
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
readonly type?: string;
};
1 change: 1 addition & 0 deletions src/templates/core/angular/sendRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export const sendRequest = <T>(
body: body ?? formData,
withCredentials: config.WITH_CREDENTIALS,
observe: 'response',
responseType: options.type === 'Blob' ? 'blob' : undefined,
});
};
2 changes: 1 addition & 1 deletion src/templates/core/axios/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax

if (!onCancel.isCancelled) {
const response = await sendRequest<T>(config, options, url, body, formData, headers, onCancel, axiosClient);
const responseBody = getResponseBody(response);
const responseBody = getResponseBody(response, options);
const responseHeader = getResponseHeader(response, options.responseHeader);

const result: ApiResult = {
Expand Down
4 changes: 4 additions & 0 deletions src/templates/core/axios/sendRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const sendRequest = async <T>(
cancelToken: source.token,
};

if(options.type === 'Blob') {
requestConfig.responseType = 'blob';
}

onCancel(() => source.cancel('The user aborted a request.'));

try {
Expand Down
6 changes: 5 additions & 1 deletion src/templates/core/fetch/getResponseBody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const getResponseBody = async (response: Response): Promise<any> => {
export const getResponseBody = async (response: Response, options: ApiRequestOptions): Promise<any> => {
if (response.status !== 204) {
try {
if(options.type === 'Blob') {
return await response.blob();
}

const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json']
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/fetch/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C

if (!onCancel.isCancelled) {
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
const responseBody = await getResponseBody(response);
const responseBody = await getResponseBody(response, options);
const responseHeader = getResponseHeader(response, options.responseHeader);

const result: ApiResult = {
Expand Down
6 changes: 5 additions & 1 deletion src/templates/core/node/getResponseBody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const getResponseBody = async (response: Response): Promise<any> => {
export const getResponseBody = async (response: Response, options: ApiRequestOptions): Promise<any> => {
if (response.status !== 204) {
try {
if(options.type === 'Blob') {
return await response.blob();
}

const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json']
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/node/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C

if (!onCancel.isCancelled) {
const response = await sendRequest(options, url, body, formData, headers, onCancel);
const responseBody = await getResponseBody(response);
const responseBody = await getResponseBody(response, options);
const responseHeader = getResponseHeader(response, options.responseHeader);

const result: ApiResult = {
Expand Down
6 changes: 5 additions & 1 deletion src/templates/core/xhr/getResponseBody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const getResponseBody = (xhr: XMLHttpRequest): any => {
export const getResponseBody = (xhr: XMLHttpRequest, options: ApiRequestOptions): any => {
if (xhr.status !== 204) {
try {
if(options.type === 'Blob') {
return xhr.response;
}

const contentType = xhr.getResponseHeader('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json']
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/xhr/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C

if (!onCancel.isCancelled) {
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
const responseBody = getResponseBody(response);
const responseBody = getResponseBody(response, options);
const responseHeader = getResponseHeader(response, options.responseHeader);

const result: ApiResult = {
Expand Down
4 changes: 4 additions & 0 deletions src/templates/core/xhr/sendRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const sendRequest = async (
xhr.open(options.method, url, true);
xhr.withCredentials = config.WITH_CREDENTIALS;

if(options.type === 'Blob') {
xhr.responseType = 'blob';
}

headers.forEach((value, key) => {
xhr.setRequestHeader(key, value);
});
Expand Down
1 change: 1 addition & 0 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class {{{name}}}{{{@root.postfix}}} {
{{/each}}
},
{{/if}}
type: '{{>result}}',
});
}

Expand Down