fix(request): respect mediaType

This commit is contained in:
Maarten Van Hoof 2021-04-07 14:59:15 +02:00
parent 34c6216492
commit 2eb3529d63
13 changed files with 57 additions and 18 deletions

View File

@ -3,4 +3,5 @@ import type { Model } from './Model';
export interface OperationParameter extends Model {
in: 'path' | 'query' | 'header' | 'formData' | 'body' | 'cookie';
prop: string;
mediaType: string | null;
}

View File

@ -42,6 +42,7 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame
enum: [],
enums: [],
properties: [],
mediaType: null,
};
if (parameter.$ref) {

View File

@ -0,0 +1,10 @@
import type { Dictionary } from '../../../utils/types';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiMediaType } from '../interfaces/OpenApiMediaType';
export function getMediaType(openApi: OpenApi, content: Dictionary<OpenApiMediaType>): string | null {
return (
Object.keys(content).find(key => ['application/json-patch+json', 'application/json', 'text/json', 'text/plain', 'multipart/mixed', 'multipart/related', 'multipart/batch'].includes(key)) ||
null
);
}

View File

@ -27,6 +27,7 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame
enum: [],
enums: [],
properties: [],
mediaType: null,
};
if (parameter.$ref) {

View File

@ -4,6 +4,7 @@ import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiRequestBody } from '../interfaces/OpenApiRequestBody';
import { getComment } from './getComment';
import { getContent } from './getContent';
import { getMediaType } from './getMediaType';
import { getModel } from './getModel';
import { getType } from './getType';
@ -27,11 +28,14 @@ export function getOperationRequestBody(openApi: OpenApi, parameter: OpenApiRequ
enum: [],
enums: [],
properties: [],
mediaType: null,
};
if (parameter.content) {
const schema = getContent(openApi, parameter.content);
const mediaType = getMediaType(openApi, parameter.content);
if (schema) {
requestBody.mediaType = mediaType;
if (schema?.$ref) {
const model = getType(schema.$ref);
requestBody.export = 'reference';

View File

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

View File

@ -20,12 +20,16 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
if (options.body) {
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');
if (options.bodyMediaType) {
headers.append('Content-Type', options.bodyMediaType);
} else {
headers.append('Content-Type', 'application/json');
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');
}
}
}
return headers;

View File

@ -3,7 +3,9 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
return getFormData(options.formData);
}
if (options.body) {
if (isString(options.body) || isBlob(options.body)) {
if (options.bodyMediaType.includes('/json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body)) {
return options.body;
} else {
return JSON.stringify(options.body);

View File

@ -20,12 +20,16 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
if (options.body) {
if (isBinary(options.body)) {
headers.append('Content-Type', 'application/octet-stream');
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
if (options.bodyMediaType) {
headers.append('Content-Type', options.bodyMediaType);
} else {
headers.append('Content-Type', 'application/json');
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');
}
}
}
return headers;

View File

@ -3,7 +3,9 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
return getFormData(options.formData);
}
if (options.body) {
if (isString(options.body) || isBinary(options.body)) {
if (options.bodyMediaType.includes('/json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body)) {
return options.body;
} else {
return JSON.stringify(options.body);

View File

@ -20,12 +20,16 @@ async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
}
if (options.body) {
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');
if (options.bodyMediaType) {
headers.append('Content-Type', options.bodyMediaType);
} else {
headers.append('Content-Type', 'application/json');
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');
}
}
}
return headers;

View File

@ -3,7 +3,9 @@ function getRequestBody(options: ApiRequestOptions): any {
return getFormData(options.formData);
}
if (options.body) {
if (isString(options.body) || isBlob(options.body)) {
if (options.bodyMediaType.includes('/json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body)) {
return options.body;
} else {
return JSON.stringify(options.body);

View File

@ -69,6 +69,9 @@ export class {{{name}}} {
{{/if}}
{{#if parametersBody}}
body: {{{parametersBody.name}}},
{{#if parametersBody.mediaType}}
bodyMediaType: '{{{parametersBody.mediaType}}}',
{{/if}}
{{/if}}
{{#if responseHeader}}
responseHeader: '{{{responseHeader}}}',