2018-05-14 11:37:19 +03:00
|
|
|
import { OpenAPIParser } from '../services/OpenAPIParser';
|
2018-05-29 17:41:50 +03:00
|
|
|
import {
|
|
|
|
OpenAPIMediaType,
|
|
|
|
OpenAPIOperation,
|
|
|
|
OpenAPIParameter,
|
|
|
|
OpenAPISchema,
|
|
|
|
Referenced,
|
|
|
|
} from '../types';
|
2018-06-18 15:56:30 +03:00
|
|
|
import { isNumeric } from './helpers';
|
2017-10-12 00:01:37 +03:00
|
|
|
|
2018-06-18 15:56:30 +03:00
|
|
|
function isWildcardStatusCode(statusCode: string | number): statusCode is string {
|
|
|
|
return typeof statusCode === 'string' && /\dxx/i.test(statusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isStatusCode(statusCode: string) {
|
|
|
|
return statusCode === 'default' || isNumeric(statusCode) || isWildcardStatusCode(statusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getStatusCodeType(statusCode: string, defaultAsError = false): string {
|
2017-10-12 00:01:37 +03:00
|
|
|
if (statusCode === 'default') {
|
|
|
|
return defaultAsError ? 'error' : 'success';
|
|
|
|
}
|
|
|
|
|
2018-06-18 15:56:30 +03:00
|
|
|
let code = parseInt(statusCode, 10);
|
|
|
|
if (isWildcardStatusCode(statusCode)) {
|
|
|
|
code *= 100; // parseInt('2xx') parses to 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code < 100 || code > 599) {
|
2017-10-12 00:01:37 +03:00
|
|
|
throw new Error('invalid HTTP code');
|
|
|
|
}
|
|
|
|
let res = 'success';
|
2018-06-18 15:56:30 +03:00
|
|
|
if (code >= 300 && code < 400) {
|
2017-10-12 00:01:37 +03:00
|
|
|
res = 'redirect';
|
2018-06-18 15:56:30 +03:00
|
|
|
} else if (code >= 400) {
|
2017-10-12 00:01:37 +03:00
|
|
|
res = 'error';
|
2018-06-18 15:56:30 +03:00
|
|
|
} else if (code < 200) {
|
2017-10-12 00:01:37 +03:00
|
|
|
res = 'info';
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const operationNames = {
|
|
|
|
get: true,
|
|
|
|
post: true,
|
|
|
|
put: true,
|
|
|
|
head: true,
|
|
|
|
patch: true,
|
|
|
|
delete: true,
|
|
|
|
options: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function isOperationName(key: string): boolean {
|
|
|
|
return key in operationNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOperationSummary(operation: OpenAPIOperation): string {
|
|
|
|
return (
|
|
|
|
operation.summary ||
|
|
|
|
operation.operationId ||
|
|
|
|
(operation.description && operation.description.substring(0, 50)) ||
|
|
|
|
'<no summary>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const schemaKeywordTypes = {
|
|
|
|
multipleOf: 'number',
|
|
|
|
maximum: 'number',
|
|
|
|
exclusiveMaximum: 'number',
|
|
|
|
minimum: 'number',
|
|
|
|
exclusiveMinimum: 'number',
|
|
|
|
|
|
|
|
maxLength: 'string',
|
|
|
|
minLength: 'string',
|
|
|
|
pattern: 'string',
|
|
|
|
|
|
|
|
items: 'array',
|
|
|
|
maxItems: 'array',
|
|
|
|
minItems: 'array',
|
|
|
|
uniqueItems: 'array',
|
|
|
|
|
|
|
|
maxProperties: 'object',
|
|
|
|
minProperties: 'object',
|
|
|
|
required: 'object',
|
|
|
|
additionalProperties: 'object',
|
|
|
|
properties: 'object',
|
|
|
|
};
|
|
|
|
|
|
|
|
export function detectType(schema: OpenAPISchema): string {
|
|
|
|
if (schema.type !== undefined) {
|
|
|
|
return schema.type;
|
|
|
|
}
|
|
|
|
const keywords = Object.keys(schemaKeywordTypes);
|
2018-01-22 21:30:53 +03:00
|
|
|
for (const keyword of keywords) {
|
|
|
|
const type = schemaKeywordTypes[keyword];
|
2017-10-12 00:01:37 +03:00
|
|
|
if (schema[keyword] !== undefined) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'any';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isPrimitiveType(schema: OpenAPISchema) {
|
|
|
|
if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schema.type === 'object') {
|
|
|
|
return schema.properties !== undefined
|
|
|
|
? Object.keys(schema.properties).length === 0
|
|
|
|
: schema.additionalProperties === undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schema.type === 'array') {
|
|
|
|
if (schema.items === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isJsonLike(contentType: string): boolean {
|
|
|
|
return contentType.search(/json/i) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function langFromMime(contentType: string): string {
|
|
|
|
if (contentType.search(/xml/i) !== -1) {
|
|
|
|
return 'xml';
|
|
|
|
}
|
|
|
|
return 'clike';
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:38:49 +03:00
|
|
|
export function isNamedDefinition(pointer?: string): boolean {
|
|
|
|
return /^#\/components\/schemas\/[^\/]+$/.test(pointer || '');
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function humanizeConstraints(schema: OpenAPISchema): string[] {
|
|
|
|
const res: string[] = [];
|
|
|
|
|
|
|
|
let stringRange;
|
|
|
|
if (schema.minLength !== undefined && schema.maxLength !== undefined) {
|
|
|
|
if (schema.minLength === schema.maxLength) {
|
|
|
|
stringRange = `${schema.minLength} characters`;
|
|
|
|
} else {
|
|
|
|
stringRange = `[ ${schema.minLength} .. ${schema.maxLength} ] characters`;
|
|
|
|
}
|
2018-01-22 21:30:53 +03:00
|
|
|
} else if (schema.maxLength !== undefined) {
|
2017-10-12 00:01:37 +03:00
|
|
|
stringRange = `<= ${schema.maxLength} characters`;
|
2018-01-22 21:30:53 +03:00
|
|
|
} else if (schema.minLength !== undefined) {
|
2017-10-12 00:01:37 +03:00
|
|
|
if (schema.minLength === 1) {
|
|
|
|
stringRange = 'non-empty';
|
|
|
|
} else {
|
|
|
|
stringRange = `>= ${schema.minLength} characters`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stringRange !== undefined) {
|
|
|
|
res.push(stringRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
let numberRange;
|
|
|
|
if (schema.minimum !== undefined && schema.maximum !== undefined) {
|
|
|
|
numberRange = schema.exclusiveMinimum ? '( ' : '[ ';
|
|
|
|
numberRange += schema.minimum;
|
|
|
|
numberRange += ' .. ';
|
|
|
|
numberRange += schema.maximum;
|
|
|
|
numberRange += schema.exclusiveMaximum ? ' )' : ' ]';
|
2018-01-22 21:30:53 +03:00
|
|
|
} else if (schema.maximum !== undefined) {
|
2017-10-12 00:01:37 +03:00
|
|
|
numberRange = schema.exclusiveMaximum ? '< ' : '<= ';
|
|
|
|
numberRange += schema.maximum;
|
2018-01-22 21:30:53 +03:00
|
|
|
} else if (schema.minimum !== undefined) {
|
2017-10-12 00:01:37 +03:00
|
|
|
numberRange = schema.exclusiveMinimum ? '> ' : '>= ';
|
|
|
|
numberRange += schema.minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numberRange !== undefined) {
|
|
|
|
res.push(numberRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2017-12-15 13:17:14 +03:00
|
|
|
|
2018-02-19 14:31:18 +03:00
|
|
|
export function sortByRequired(
|
2018-03-05 18:58:19 +03:00
|
|
|
fields: Array<{ required: boolean; name: string }>,
|
2018-02-19 14:31:18 +03:00
|
|
|
order: string[] = [],
|
|
|
|
) {
|
|
|
|
fields.sort((a, b) => {
|
|
|
|
if (!a.required && b.required) {
|
|
|
|
return 1;
|
|
|
|
} else if (a.required && !b.required) {
|
|
|
|
return -1;
|
|
|
|
} else if (a.required && b.required) {
|
|
|
|
return order.indexOf(a.name) > order.indexOf(b.name) ? 1 : -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-14 11:37:19 +03:00
|
|
|
export function mergeParams(
|
|
|
|
parser: OpenAPIParser,
|
|
|
|
pathParams: Array<Referenced<OpenAPIParameter>> = [],
|
|
|
|
operationParams: Array<Referenced<OpenAPIParameter>> = [],
|
|
|
|
): Array<Referenced<OpenAPIParameter>> {
|
|
|
|
const operationParamNames = {};
|
|
|
|
operationParams.forEach(param => {
|
|
|
|
param = parser.shalowDeref(param);
|
|
|
|
operationParamNames[param.name + '_' + param.in] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// filter out path params overriden by operation ones with the same name
|
|
|
|
pathParams = pathParams.filter(param => {
|
|
|
|
param = parser.shalowDeref(param);
|
|
|
|
return !operationParamNames[param.name + '_' + param.in];
|
|
|
|
});
|
|
|
|
|
|
|
|
return pathParams.concat(operationParams);
|
|
|
|
}
|
|
|
|
|
2018-05-29 17:41:50 +03:00
|
|
|
export function mergeSimilarMediaTypes(types: Dict<OpenAPIMediaType>): Dict<OpenAPIMediaType> {
|
|
|
|
const mergedTypes = {};
|
|
|
|
Object.keys(types).forEach(name => {
|
|
|
|
const mime = types[name];
|
|
|
|
// ignore content type parameters (e.g. charset) and merge
|
|
|
|
const normalizedMimeName = name.split(';')[0].trim();
|
|
|
|
if (!mergedTypes[normalizedMimeName]) {
|
|
|
|
mergedTypes[normalizedMimeName] = mime;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mergedTypes[normalizedMimeName] = { ...mergedTypes[normalizedMimeName], ...mime };
|
|
|
|
});
|
|
|
|
|
|
|
|
return mergedTypes;
|
|
|
|
}
|
|
|
|
|
2017-12-15 13:17:14 +03:00
|
|
|
export const SECURITY_SCHEMES_SECTION = 'section/Authentication/';
|