objection-swagger
Version:
Originally designed as Swagger definition generator for Objection.js models. Since then scope was extended to also cover Swagger-compatible snippets generation from plain JSON Schema entries as well as set of conversions that are useful for model and sche
36 lines (30 loc) • 869 B
JavaScript
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
/**
* @param {string|Object} modelClass
* @param {string[]} modelPaths
* @returns {Object}
*/
function getModelClass(modelClass, modelPaths) {
if (_.isString(modelClass)) {
if (modelPaths && modelPaths.length) {
for (const modelPath of modelPaths) {
let fullPath = path.join(process.cwd(), modelPath, modelClass);
if (!fullPath.endsWith('.js') && !fullPath.endsWith('.ts'))
fullPath += '.js';
if (fs.existsSync(fullPath)) return require(fullPath);
}
throw new Error(
`Failed to load modelClass ${modelClass} with modelPaths ${JSON.stringify(
modelPaths
)}`
);
} else {
return require(modelClass);
}
} else {
return modelClass;
}
}
module.exports = getModelClass;