ng-alain-sts
Version:
Swagger to sf schema & st column in ng-alain
159 lines (158 loc) • 5.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
function coverProperty(options) {
const eachCallback = options.config.sf.propertyCallback;
const inFn = (schema, parentSchema) => {
const properties = schema.properties;
util_1.removeXml(schema);
Object.keys(properties).forEach(propertyName => {
const property = properties[propertyName];
fixDefaultProperty(propertyName, property, options);
Object.assign(property, util_1.getCustomProperty('sf', propertyName, options));
fixXml(propertyName, property, options);
util_1.removeXml(property);
['example'].forEach(k => {
delete property[k];
});
if (eachCallback) {
eachCallback({
name: propertyName,
property,
path: options.path,
method: options.method,
});
}
if (property.properties && Object.keys(property.properties).length) {
inFn(property, schema);
}
});
};
inFn(options.schema);
if (options.schema.definitions) {
Object.keys(options.schema.definitions).forEach(key => {
inFn(options.schema.definitions[key]);
});
}
return options.schema;
}
function fixDefaultProperty(name, property, options) {
fixType(name, property, options);
fixFormat(name, property, options);
fixTitle(name, property, options);
fixSingleArray(property, options);
return null;
}
function getDefaultFormatByName(name) {
switch (name) {
case 'email':
case 'mobile':
case 'id-card':
case 'color':
return name;
}
return '';
}
function fixType(name, property, options) {
}
function fixFormat(name, property, options) {
if (property.format == null) {
property.format = getDefaultFormatByName(name);
}
if (property.format.length === 0) {
delete property.format;
}
if (property.format === 'int32' ||
property.format === 'int64' ||
property.format === 'float' ||
property.format === 'double') {
delete property.format;
if (property.type !== 'integer' && property.type !== 'number') {
property.type = 'number';
}
}
}
function fixTitle(name, property, options) {
if (property.title != null) {
return;
}
const defaultTitle = options.config.propertyMapNames[name];
if (defaultTitle) {
property.title = defaultTitle;
}
if (property.title == null &&
property.description != null &&
property.description.length > 0 &&
options.config.descriptionIsTitle === true) {
property.title = property.description;
delete property.description;
}
}
function fixSingleArray(property, options) {
if (property.type !== 'array' || !property.items) {
return;
}
const keys = Object.keys(property.items);
if (keys.includes('$ref')) {
return;
}
if (keys.includes('type')) {
Object.assign(property, options.config.sf.singleArray);
delete property.items;
}
}
function fixXml(name, property, options) {
const names = options.config.st.xmlBlackNames;
if (!property.xml || !names || names.length === 0) {
return;
}
names.forEach(key => {
const value = property.xml[key];
if (typeof value === 'undefined')
return;
if (typeof property.ui === 'undefined') {
property.ui = {};
}
property.ui[key] = value;
});
}
function generator(data, options, config) {
const path = `${config.pathPrefix || ''}${options.path}`;
const method = options.method || config.sf.method || 'put';
const pathObj = data.paths[path];
if (pathObj == null || pathObj[method] == null) {
console.warn(`Not found method [${method}] or [${path}]`);
return null;
}
const oper = pathObj[method];
if (!oper.parameters || oper.parameters.length <= 0) {
console.warn(`Not found parameters in [${path},${method}]`);
return null;
}
const inBody = oper.parameters.find((w) => w.in === 'body');
if (!inBody) {
console.warn(`Not found body in [${path},${method}]`);
return null;
}
const schema = util_1.findSchemaDefinition(inBody.schema.$ref, data.definitions);
if (schema == null) {
return null;
}
const otherDefinitions = util_1.mergeDefinitions(schema, data.definitions);
if (Object.keys(otherDefinitions).length > 0) {
schema.definitions = otherDefinitions;
}
const runOpt = {
path,
method,
config,
schema,
};
coverProperty(runOpt);
const finishedCallback = config.sf.finishedCallback;
if (finishedCallback) {
finishedCallback({ schema, path, method });
}
return schema;
}
exports.generator = generator;