parse-openapi
Version:
OpenAPI v3 parser
39 lines (38 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContent = void 0;
const isDefined_1 = require("../utils/isDefined");
const BASIC_MEDIA_TYPES = [
'application/json-patch+json',
'application/json',
'application/x-www-form-urlencoded',
'text/json',
'text/plain',
'multipart/form-data',
'multipart/mixed',
'multipart/related',
'multipart/batch',
];
const getContent = (openApi, content) => {
const basicMediaTypeWithSchema = Object.keys(content)
.filter(mediaType => {
const cleanMediaType = mediaType.split(';')[0].trim();
return BASIC_MEDIA_TYPES.includes(cleanMediaType);
})
.find(mediaType => (0, isDefined_1.isDefined)(content[mediaType]?.schema));
if (basicMediaTypeWithSchema) {
return {
mediaType: basicMediaTypeWithSchema,
schema: content[basicMediaTypeWithSchema].schema,
};
}
const firstMediaTypeWithSchema = Object.keys(content).find(mediaType => (0, isDefined_1.isDefined)(content[mediaType]?.schema));
if (firstMediaTypeWithSchema) {
return {
mediaType: firstMediaTypeWithSchema,
schema: content[firstMediaTypeWithSchema].schema,
};
}
return null;
};
exports.getContent = getContent;