X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Frouter%2Findex.js;h=fd19bdf5192cfd9efd7b4c00023f1ae92265cbd1;hb=8d101705d3394039ddf2d2f3339a5e6ada911579;hp=a13ac47d62f083e588d047f7d556f7502f4b91e4;hpb=9fe1e9b22b8e753c44dec77d1dee3d0061b2e991;p=squeep-api-dingus diff --git a/lib/router/index.js b/lib/router/index.js index a13ac47..fd19bdf 100644 --- a/lib/router/index.js +++ b/lib/router/index.js @@ -7,7 +7,7 @@ const { METHODS: httpMethods } = require('http'); const common = require('../common'); -const { DingusError } = require('../errors'); +const { DingusError, RouterNoPathError, RouterNoMethodError } = require('../errors'); const PathParameter = require('./path-parameter'); // Internal identifiers for route entries. @@ -61,17 +61,43 @@ class Router { _pathToRoutePath(rawPath) { const routePath = rawPath .split('/') - .map((p) => p.startsWith(this.paramPrefix) ? new PathParameter(p.slice(this.paramPrefix.length)) : p); + .map((p) => this._pathPartMunge(p)); + if (this.ignoreTrailingSlash && routePath[routePath.length - 1] === '') { routePath.pop(); } + routePath[kPathMethods] = {}; + + Object.defineProperty(routePath, 'path', { + enumerable: false, + value: rawPath, + }); + Object.freeze(routePath); return routePath; } + /* + * Convert a path part string to parameter if needed. + * Remove escape from an escaped parameter. + * @param {String} part + * @returns {PathParameter|String} + * @private + */ + _pathPartMunge(part) { + if (part.startsWith(this.paramPrefix)) { + return new PathParameter(part.slice(this.paramPrefix.length)); + } + if (part.startsWith('\\' + this.paramPrefix)) { + return part.slice(1); + } + return part; + } + + /** * Compare checkPath to fixedPath, no param substitution, params must match. * @param {Router~RoutePath} routePath @@ -233,17 +259,17 @@ class Router { const { matchedPath, pathParams } = this._pathFind(pathParts); ctx.params = pathParams; if (matchedPath) { - ctx.matchedPath = matchedPath; + ctx.matchedPath = matchedPath.path; if (method in matchedPath[kPathMethods]) { return matchedPath[kPathMethods][method]; } if ('*' in matchedPath[kPathMethods]) { return matchedPath[kPathMethods]['*']; } - throw new DingusError('NoMethod'); + throw new RouterNoMethodError(); } ctx.unmatchedPath = pathParts; - throw new DingusError('NoPath'); + throw new RouterNoPathError(); }