import _path from 'path' ; import globby from 'globby' ; import is from 'is-type-of' ; import { Dictionary } from 'ramda' ; const convertPath = ( path ) => { const re = new RegExp ( '{(.*?)}' , 'g' ) ; return path . replace ( re , ':$1' ) ; } ; const getPath = ( prefix , path ) => `${ prefix }${ path }` . replace ( '//' , '/' ) ; const reservedMethodNames = [ 'middlewares' , 'name' , 'constructor' , 'length' , 'prototype' , 'parameters' , 'prefix' , ] ; enum allowedMethods { GET = 'get' , POST = 'post' , PUT = 'put' , PATCH = 'patch' , DELETE = 'delete' } const getFilepaths = ( dir , recursive = true , ignore = [ ] ) => { const ignoreDirs = ignore . map ( ( path => `!${ path }` ) ) ; const paths = recursive ? globby . sync ( [ '**/*.js' , '**/*.ts' , ... ignoreDirs ] , { cwd : dir } ) : globby . sync ( [ '*.js' , '*.ts' , ... ignoreDirs ] , { cwd : dir } ) ; return paths . map ( path => _path . join ( dir , path ) ) ; } ; const loadModule = ( filepath ) => { const obj = require ( filepath ) ; if ( ! obj ) return obj ; if ( obj . __esModule ) return 'default' in obj ? obj . default : obj ; return obj ; } ; const loadClass = ( filepath ) => { const cls = loadModule ( filepath ) ; if ( is . class ( cls ) ) return cls ; return false ; } ; const loadSwaggerClasses = ( dir = '' , options = { } ) => { dir = _path . resolve ( dir ) ; const { recursive = true } = options ; return getFilepaths ( dir , recursive , options . ignore ) . map ( filepath => loadClass ( filepath ) ) . filter ( cls => cls ) ; } ; const swaggerKeys = ( className , methods ) => methods . map ( m => `${ className }- ${ m }` ) ; /**
 * Sorts an object (dictionary) by value returned by the valSelector function.
 * Note that order is only guaranteed for string keys.
 */ const sortObject = < TValue > ( obj , comparisonSelector , callbackFn ? ) => { const unsortedKeys = Object . keys ( obj ) ; const sortedKeys = unsortedKeys . sort ( ( a , b ) => ( comparisonSelector ( obj [ a ] , unsortedKeys . length ) > comparisonSelector ( obj [ b ] , unsortedKeys . length ) ? 1 : comparisonSelector ( obj [ a ] , unsortedKeys . length ) < comparisonSelector ( obj [ b ] , unsortedKeys . length ) ? - 1 : 0 ) ) ; return sortedKeys . reduce ( ( sorted , k ) => { let value = obj [ k ] ; if ( callbackFn && is . function ( callbackFn ) ) { value = callbackFn ( value ) ; } sorted [ k ] = value ; return sorted ; } , { } ) ; } ; export { convertPath , getPath , getFilepaths , loadClass , loadSwaggerClasses , reservedMethodNames , allowedMethods , swaggerKeys , sortObject , } ;