diff --git a/src/openApi/v3/parser/getOperationRequestBody.ts b/src/openApi/v3/parser/getOperationRequestBody.ts index a42dbae7..1f4ef3f8 100644 --- a/src/openApi/v3/parser/getOperationRequestBody.ts +++ b/src/openApi/v3/parser/getOperationRequestBody.ts @@ -33,9 +33,8 @@ export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequ if (parameter.content) { const schema = getContent(openApi, parameter.content); - const mediaType = getMediaType(openApi, parameter.content); if (schema) { - requestBody.mediaType = mediaType; + requestBody.mediaType = getMediaType(openApi, parameter.content); if (schema?.$ref) { const model = getType(schema.$ref); requestBody.export = 'reference'; diff --git a/src/templates/core/ApiRequestOptions.hbs b/src/templates/core/ApiRequestOptions.hbs index 1419dab0..76251eac 100644 --- a/src/templates/core/ApiRequestOptions.hbs +++ b/src/templates/core/ApiRequestOptions.hbs @@ -8,7 +8,7 @@ export type ApiRequestOptions = { readonly query?: Record; readonly formData?: Record; readonly body?: any; - readonly bodyMediaType?: string; + readonly mediaType?: string; readonly responseHeader?: string; readonly errors?: Record; } diff --git a/src/templates/core/fetch/getHeaders.hbs b/src/templates/core/fetch/getHeaders.hbs index b4e6d1b2..3d1a62c7 100644 --- a/src/templates/core/fetch/getHeaders.hbs +++ b/src/templates/core/fetch/getHeaders.hbs @@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise { } if (options.body) { - if (options.bodyMediaType) { - headers.append('Content-Type', options.bodyMediaType); + if (options.mediaType) { + headers.append('Content-Type', options.mediaType); + } else if (isBlob(options.body)) { + headers.append('Content-Type', options.body.type || 'application/octet-stream'); + } else if (isString(options.body)) { + headers.append('Content-Type', 'text/plain'); } else { - if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); - } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); - } else { - headers.append('Content-Type', 'application/json'); - } + headers.append('Content-Type', 'application/json'); } } return headers; diff --git a/src/templates/core/fetch/getRequestBody.hbs b/src/templates/core/fetch/getRequestBody.hbs index 84b3a511..734f03db 100644 --- a/src/templates/core/fetch/getRequestBody.hbs +++ b/src/templates/core/fetch/getRequestBody.hbs @@ -3,7 +3,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined { return getFormData(options.formData); } if (options.body) { - if (options.bodyMediaType?.includes('/json')) { + if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) } else if (isString(options.body) || isBlob(options.body)) { return options.body; diff --git a/src/templates/core/node/getHeaders.hbs b/src/templates/core/node/getHeaders.hbs index 57021103..fc6d5516 100644 --- a/src/templates/core/node/getHeaders.hbs +++ b/src/templates/core/node/getHeaders.hbs @@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise { } if (options.body) { - if (options.bodyMediaType) { - headers.append('Content-Type', options.bodyMediaType); + if (options.mediaType) { + headers.append('Content-Type', options.mediaType); + } else if (isBinary(options.body)) { + headers.append('Content-Type', 'application/octet-stream'); + } else if (isString(options.body)) { + headers.append('Content-Type', 'text/plain'); } else { - if (isBinary(options.body)) { - headers.append('Content-Type', 'application/octet-stream'); - } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); - } else { - headers.append('Content-Type', 'application/json'); - } + headers.append('Content-Type', 'application/json'); } } return headers; diff --git a/src/templates/core/node/getRequestBody.hbs b/src/templates/core/node/getRequestBody.hbs index d66c3500..e6a827d7 100644 --- a/src/templates/core/node/getRequestBody.hbs +++ b/src/templates/core/node/getRequestBody.hbs @@ -3,7 +3,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined { return getFormData(options.formData); } if (options.body) { - if (options.bodyMediaType?.includes('/json')) { + if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) } else if (isString(options.body) || isBinary(options.body)) { return options.body; diff --git a/src/templates/core/xhr/getHeaders.hbs b/src/templates/core/xhr/getHeaders.hbs index b4e6d1b2..3d1a62c7 100644 --- a/src/templates/core/xhr/getHeaders.hbs +++ b/src/templates/core/xhr/getHeaders.hbs @@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise { } if (options.body) { - if (options.bodyMediaType) { - headers.append('Content-Type', options.bodyMediaType); + if (options.mediaType) { + headers.append('Content-Type', options.mediaType); + } else if (isBlob(options.body)) { + headers.append('Content-Type', options.body.type || 'application/octet-stream'); + } else if (isString(options.body)) { + headers.append('Content-Type', 'text/plain'); } else { - if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); - } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); - } else { - headers.append('Content-Type', 'application/json'); - } + headers.append('Content-Type', 'application/json'); } } return headers; diff --git a/src/templates/core/xhr/getRequestBody.hbs b/src/templates/core/xhr/getRequestBody.hbs index a374fb0f..0e6b1873 100644 --- a/src/templates/core/xhr/getRequestBody.hbs +++ b/src/templates/core/xhr/getRequestBody.hbs @@ -3,7 +3,7 @@ function getRequestBody(options: ApiRequestOptions): any { return getFormData(options.formData); } if (options.body) { - if (options.bodyMediaType?.includes('/json')) { + if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) } else if (isString(options.body) || isBlob(options.body)) { return options.body; diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index 6e29af60..c45ed830 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -70,7 +70,7 @@ export class {{{name}}} { {{#if parametersBody}} body: {{{parametersBody.name}}}, {{#if parametersBody.mediaType}} - bodyMediaType: '{{{parametersBody.mediaType}}}', + mediaType: '{{{parametersBody.mediaType}}}', {{/if}} {{/if}} {{#if responseHeader}} diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 93f69f28..0047c137 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -35,7 +35,7 @@ export type ApiRequestOptions = { readonly query?: Record; readonly formData?: Record; readonly body?: any; - readonly bodyMediaType?: string; + readonly mediaType?: string; readonly responseHeader?: string; readonly errors?: Record; }" @@ -181,16 +181,14 @@ async function getHeaders(options: ApiRequestOptions): Promise { } if (options.body) { - if (options.bodyMediaType) { - headers.append('Content-Type', options.bodyMediaType); + if (options.mediaType) { + headers.append('Content-Type', options.mediaType); + } else if (isBlob(options.body)) { + headers.append('Content-Type', options.body.type || 'application/octet-stream'); + } else if (isString(options.body)) { + headers.append('Content-Type', 'text/plain'); } else { - if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); - } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); - } else { - headers.append('Content-Type', 'application/json'); - } + headers.append('Content-Type', 'application/json'); } } return headers; @@ -201,7 +199,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined { return getFormData(options.formData); } if (options.body) { - if (options.bodyMediaType?.includes('/json')) { + if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) } else if (isString(options.body) || isBlob(options.body)) { return options.body; @@ -2390,7 +2388,7 @@ export type ApiRequestOptions = { readonly query?: Record; readonly formData?: Record; readonly body?: any; - readonly bodyMediaType?: string; + readonly mediaType?: string; readonly responseHeader?: string; readonly errors?: Record; }" @@ -2536,16 +2534,14 @@ async function getHeaders(options: ApiRequestOptions): Promise { } if (options.body) { - if (options.bodyMediaType) { - headers.append('Content-Type', options.bodyMediaType); + if (options.mediaType) { + headers.append('Content-Type', options.mediaType); + } else if (isBlob(options.body)) { + headers.append('Content-Type', options.body.type || 'application/octet-stream'); + } else if (isString(options.body)) { + headers.append('Content-Type', 'text/plain'); } else { - if (isBlob(options.body)) { - headers.append('Content-Type', options.body.type || 'application/octet-stream'); - } else if (isString(options.body)) { - headers.append('Content-Type', 'text/plain'); - } else { - headers.append('Content-Type', 'application/json'); - } + headers.append('Content-Type', 'application/json'); } } return headers; @@ -2556,7 +2552,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined { return getFormData(options.formData); } if (options.body) { - if (options.bodyMediaType?.includes('/json')) { + if (options.mediaType?.includes('/json')) { return JSON.stringify(options.body) } else if (isString(options.body) || isBlob(options.body)) { return options.body; @@ -4495,7 +4491,7 @@ export class ComplexService { method: 'PUT', path: \`/api/v\${OpenAPI.VERSION}/complex/\${id}\`, body: requestBody, - bodyMediaType: 'application/json-patch+json', + mediaType: 'application/json-patch+json', }); return result.body; } @@ -4792,7 +4788,7 @@ export class ParametersService { 'parameterForm': parameterForm, }, body: requestBody, - bodyMediaType: 'application/json', + mediaType: 'application/json', }); return result.body; } @@ -4837,7 +4833,7 @@ export class ParametersService { 'parameter_form': parameterForm, }, body: requestBody, - bodyMediaType: 'application/json', + mediaType: 'application/json', }); return result.body; } @@ -4858,7 +4854,7 @@ export class ParametersService { 'parameter': parameter, }, body: requestBody, - bodyMediaType: 'application/json', + mediaType: 'application/json', }); return result.body; } @@ -4879,7 +4875,7 @@ export class ParametersService { 'parameter': parameter, }, body: requestBody, - bodyMediaType: 'application/json', + mediaType: 'application/json', }); return result.body; } @@ -4908,7 +4904,7 @@ export class RequestBodyService { method: 'POST', path: \`/api/v\${OpenAPI.VERSION}/requestBody/\`, body: requestBody, - bodyMediaType: 'application/json', + mediaType: 'application/json', }); return result.body; }