diff --git a/dist/index.d.ts b/dist/index.d.ts index c00c2fc..4808332 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,17 +1,13 @@ -import { Application, Request, Response, NextFunction } from 'express'; +import { Application, RequestHandler } from 'express'; export declare const errorLabel = "[ERROR] express-automatic-routes:"; -export declare type ValidMethods = 'CHECKOUT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LOCK' | 'MERGE' | 'MKACTIVITY' | 'MKCOL' | 'MOVE' | 'M-SEARCH' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PURGE' | 'PUT' | 'REPORT' | 'SEARCH' | 'SUBSCRIBE' | 'TRACE' | 'UNLOCK' | 'UNSUBSCRIBE'; -interface Middleware { - (req: Request & T, res: Response, next: NextFunction): void; -} -declare type Route = (request: Request, response: Response) => any; -declare type MiddlewareRoute = { - middleware: Middleware | Middleware[]; - handler: Route; +export type ValidMethods = 'CHECKOUT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LOCK' | 'MERGE' | 'MKACTIVITY' | 'MKCOL' | 'MOVE' | 'M-SEARCH' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PURGE' | 'PUT' | 'REPORT' | 'SEARCH' | 'SUBSCRIBE' | 'TRACE' | 'UNLOCK' | 'UNSUBSCRIBE'; +type MiddlewareRoute = { + middleware: RequestHandler | RequestHandler[]; + handler: RequestHandler; }; -export declare type RouteOptions = Route | MiddlewareRoute; +export type RouteOptions = RequestHandler | MiddlewareRoute; export interface Resource { - middleware?: Middleware | Middleware[]; + middleware?: RequestHandler | RequestHandler[]; delete?: RouteOptions; get?: RouteOptions; head?: RouteOptions; @@ -23,6 +19,7 @@ export interface Resource { interface ExpressAutoroutesOptions { dir: string; log?: boolean; + mount?: string; } export default function (express: Application, options: ExpressAutoroutesOptions): void; export {}; diff --git a/dist/index.js b/dist/index.js index f985dbb..7da798a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,8 +1,12 @@ "use strict"; -var __spreadArray = (this && this.__spreadArray) || function (to, from) { - for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) - to[j] = from[i]; - return to; +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; @@ -38,6 +42,10 @@ var validMethods = [ 'unlock', 'unsubscribe', ]; +// Global config options, so we don't need to pass around these settings across recursive functions +var configOptions = { + mount: '', +}; function scan(express, baseDir, current, log) { if (log === void 0) { log = false; } var combined = path_1.default.join(baseDir, current); @@ -84,7 +92,7 @@ function replaceParamsToken(token) { function autoload(express, fullPath, url, log) { var module = loadModule(fullPath, log); if (typeof module !== 'function') { - throw new Error(exports.errorLabel + " module " + fullPath + " must be valid js/ts module and should export route methods definitions"); + throw new Error("".concat(exports.errorLabel, " module ").concat(fullPath, " must be valid js/ts module and should export route methods definitions")); } var routes = module(express); var middleware = undefined; @@ -94,10 +102,12 @@ function autoload(express, fullPath, url, log) { for (var _i = 0, _a = Object.entries(routes); _i < _a.length; _i++) { var _b = _a[_i], method = _b[0], route = _b[1]; if (validMethods.includes(method)) { + // Prepend the mount configuration to the url + var endpointUrl = configOptions.mount + url; //@ts-ignore - express[method].apply(express, __spreadArray([url], extract(middleware, route))); + express[method].apply(express, __spreadArray([endpointUrl], extract(middleware, route), false)); if (log) { - console.info(method.toUpperCase() + " " + url + " => " + fullPath); + console.info("".concat(method.toUpperCase(), " ").concat(url, " => ").concat(fullPath)); } } } @@ -119,36 +129,36 @@ function extract(middleware, routeOptions) { ? middleware : [middleware]; if (typeof routeOptions === 'function') { - return __spreadArray(__spreadArray([], routeMiddleware), [routeOptions]); + return __spreadArray(__spreadArray([], routeMiddleware, true), [routeOptions], false); } else { routeOptions.middleware = routeOptions.middleware === undefined ? [] : routeOptions.middleware; if (Array.isArray(routeOptions.middleware)) { - return __spreadArray(__spreadArray(__spreadArray([], routeMiddleware), routeOptions.middleware), [ + return __spreadArray(__spreadArray(__spreadArray([], routeMiddleware, true), routeOptions.middleware, true), [ routeOptions.handler, - ]); + ], false); } else { - return __spreadArray(__spreadArray([], routeMiddleware), [routeOptions.middleware, routeOptions.handler]); + return __spreadArray(__spreadArray([], routeMiddleware, true), [routeOptions.middleware, routeOptions.handler], false); } } } function default_1(express, options) { - var _a; + var _a, _b; var log = (_a = options.log) !== null && _a !== void 0 ? _a : true; if (!express) { - var message = exports.errorLabel + " express application must be passed"; + var message = "".concat(exports.errorLabel, " express application must be passed"); log && console.log(message); throw new Error(message); } if (!options.dir) { - var message = exports.errorLabel + " dir must be specified"; + var message = "".concat(exports.errorLabel, " dir must be specified"); log && console.error(message); throw new Error(message); } if (typeof options.dir !== 'string') { - var message = exports.errorLabel + " dir must be the path of autoroutes-directory"; + var message = "".concat(exports.errorLabel, " dir must be the path of autoroutes-directory"); log && console.error(message); throw new Error(message); } @@ -163,20 +173,29 @@ function default_1(express, options) { dirPath = path_1.default.join(process_1.default.cwd(), process_1.default.argv[1], '..', options.dir); } if (!fs_1.default.existsSync(dirPath)) { - var message = exports.errorLabel + " dir " + dirPath + " does not exists"; + var message = "".concat(exports.errorLabel, " dir ").concat(dirPath, " does not exists"); log && console.error(message); throw new Error(message); } if (!fs_1.default.statSync(dirPath).isDirectory()) { - var message = exports.errorLabel + " dir " + dirPath + " must be a directory"; + var message = "".concat(exports.errorLabel, " dir ").concat(dirPath, " must be a directory"); log && console.error(message); throw new Error(message); } + // Save the mount option in the global config variable, so we don't need to pass it through scan and other recursive function calls + configOptions.mount = (_b = options.mount) !== null && _b !== void 0 ? _b : ''; try { scan(express, dirPath, '', options.log); } catch (error) { - log && console.error(error.message); + if (log) { + if (error instanceof Error) { + console.error(error.message); + } + else { + console.error(error); + } + } throw error; } } diff --git a/dist/index.js.map b/dist/index.js.map index f0faba0..fd78cde 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA,oDAA6B;AAC7B,8CAAuB;AACvB,0CAAmB;AAEN,QAAA,UAAU,GAAG,mCAAmC,CAAA;AA6B7D,IAAM,YAAY,GAAG;IACnB,UAAU;IACV,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,YAAY;IACZ,OAAO;IACP,MAAM;IACN,UAAU;IACV,QAAQ;IACR,SAAS;IACT,OAAO;IACP,MAAM;IACN,OAAO;IACP,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,WAAW;IACX,OAAO;IACP,QAAQ;IACR,aAAa;CACd,CAAA;AA0BD,SAAS,IAAI,CACX,OAAoB,EACpB,OAAe,EACf,OAAe,EACf,GAAoB;IAApB,oBAAA,EAAA,WAAoB;IAEpB,IAAM,QAAQ,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IAC5C,IAAM,YAAY,GAAG,YAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,YAAY,CAAC,WAAW,EAAE,EAAE;QAC9B,KAAoB,UAAwB,EAAxB,KAAA,YAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAxB,cAAwB,EAAxB,IAAwB,EAAE;YAAzC,IAAM,KAAK,SAAA;YACd,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,CAAA;SACvD;KACF;SAAM,IAAI,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,EAAE;QACnD,QAAQ,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,OAAO,CAAC,EAAE,GAAG,C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,QAAQ,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QACvC,IAAM,OAAO,GAAM,kBAAU,aAAQ,OAAO,yBAAsB,CAAA;QAClE,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAE7B,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAA;KACzB;IAED,IAAI;QACF,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;KACxC;IAAC,OAAO,KAAK,EAAE;QACd,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAEnC,MAAM,KAAK,CAAA;KACZ;AACH,CAAC;AA1DD,4BA0DC"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,oDAA6B;AAC7B,8CAAuB;AACvB,0CAAmB;AAEN,QAAA,UAAU,GAAG,mCAAmC,CAAA;AA2B7D,IAAM,YAAY,GAAG;IACnB,UAAU;IACV,MAAM;IACN,QAAQ;IACR,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,YAAY;IACZ,OAAO;IACP,MAAM;IACN,UAAU;IACV,QAAQ;IACR,SAAS;IACT,OAAO;IACP,MAAM;IACN,OAAO;IACP,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,WAAW;IACX,OAAO;IACP,QAAQ;IACR,aAAa;CACd,CAAA;AAoBD,mGAAmG;AACnG,IAAM,aAAa,GAAG;IACpB,KAAK,EAAE,EAAE;CACV,CAAA;AAED,SAAS,IAAI,CACX,OAAoB,EACpB,OAAe,EACf,OAAe,EACf,GAAoB;IAApB,oBAAA,EAAA,WAAoB;IAEpB,IAAM,QAAQ,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IAC5C,IAAM,YAAY,GAAG,YAAE,CAAC,QAAQ,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MAAM,KAAK,CAAA;KACZ;AACH,CAAC;AAlED,4BAkEC"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index ddfc0d3..4d0535e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { Application, Request, Response, NextFunction } from 'express' +import { Application, RequestHandler } from 'express' import process from 'process' import path from 'path' @@ -31,8 +31,6 @@ export type ValidMethods = | 'UNLOCK' | 'UNSUBSCRIBE' - - const validMethods = [ 'checkout', 'copy', @@ -59,21 +57,15 @@ const validMethods = [ 'unsubscribe', ] -interface Middleware { - (req: Request & T, res: Response, next: NextFunction): void -} - -type Route = (request: Request, response: Response) => any - type MiddlewareRoute = { - middleware: Middleware | Middleware[] - handler: Route + middleware: RequestHandler | RequestHandler[] + handler: RequestHandler } -export type RouteOptions = Route | MiddlewareRoute +export type RouteOptions = RequestHandler | MiddlewareRoute export interface Resource { - middleware?: Middleware | Middleware[] + middleware?: RequestHandler | RequestHandler[] delete?: RouteOptions get?: RouteOptions head?: RouteOptions @@ -160,7 +152,7 @@ function autoload( const routes = module(express) - let middleware: undefined | Middleware | Middleware[] = undefined + let middleware: undefined | RequestHandler | RequestHandler[] = undefined if (routes.middleware) { middleware = routes.middleware } @@ -194,15 +186,15 @@ function loadModule(path: string, log: boolean) { } function extract( - middleware: undefined | Middleware | Middleware[], + middleware: undefined | RequestHandler | RequestHandler[], routeOptions: RouteOptions -): Middleware[] { - const routeMiddleware: Middleware[] = +): RequestHandler[] { + const routeMiddleware: RequestHandler[] = middleware === undefined ? [] : Array.isArray(middleware) - ? middleware - : [middleware] + ? middleware + : [middleware] if (typeof routeOptions === 'function') { return [...routeMiddleware, routeOptions] @@ -285,8 +277,13 @@ export default function ( try { scan(express, dirPath, '', options.log) } catch (error) { - log && console.error(error.message) - + if (log) { + if (error instanceof Error) { + console.error(error.message) + } else { + console.error(error) + } + } throw error } }