- Simplified the header checks

- Names the bodyMediaType > mediaType, to align with swagger naming convention
This commit is contained in:
Ferdi Koomen 2021-04-27 13:25:20 +02:00
parent 22f4d28071
commit d7d50cefa1
10 changed files with 51 additions and 62 deletions

View File

@ -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';

View File

@ -8,7 +8,7 @@ export type ApiRequestOptions = {
readonly query?: Record<string, any>;
readonly formData?: Record<string, any>;
readonly body?: any;
readonly bodyMediaType?: string;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
}

View File

@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
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;

View File

@ -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;

View File

@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
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;

View File

@ -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;

View File

@ -20,16 +20,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
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;

View File

@ -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;

View File

@ -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}}

View File

@ -35,7 +35,7 @@ export type ApiRequestOptions = {
readonly query?: Record<string, any>;
readonly formData?: Record<string, any>;
readonly body?: any;
readonly bodyMediaType?: string;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
}"
@ -181,16 +181,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
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<string, any>;
readonly formData?: Record<string, any>;
readonly body?: any;
readonly bodyMediaType?: string;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly errors?: Record<number, string>;
}"
@ -2536,16 +2534,14 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
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;
}