UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

59 lines (58 loc) 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "DynamicRoute", { enumerable: true, get: function() { return DynamicRoute; } }); const _AbstractRoute = require("./AbstractRoute"); const _GenericError = require("../error/GenericError"); class DynamicRoute extends _AbstractRoute.AbstractRoute { /** * Initializes the route. * * @param pathExpression Path expression used in route matching, * to generate valid path with provided params and parsing params from current path. */ constructor(name, pathExpression, controller, view, options){ super(name, pathExpression, controller, view, options); if (!pathExpression || typeof pathExpression !== 'object') { throw new _GenericError.GenericError(`The pathExpression must be object, '${typeof pathExpression}' was given.`); } this._pathExpression = pathExpression; const { matcher, toPath, extractParameters } = this._pathExpression; if (!matcher || !(matcher instanceof RegExp)) { throw new _GenericError.GenericError(`The pathExpression.matcher must be a RegExp.`, { matcher }); } if (!toPath || typeof toPath !== 'function') { throw new _GenericError.GenericError(`The pathExpression.toPath is not a function, '${typeof toPath}' was given.`); } if (!extractParameters || typeof extractParameters !== 'function') { throw new _GenericError.GenericError(`The pathExpression.extractParameters is not a function, '${typeof extractParameters}' was given.`); } } /** * @inheritDoc */ toPath(params = {}) { return this.getTrimmedPath(this._pathExpression.toPath(params)); } /** * @inheritDoc */ matches(path) { return this._pathExpression.matcher.test(this.getTrimmedPath(path)); } /** * @inheritDoc */ extractParameters(path, baseUrl) { const parsedUrl = new URL(`${baseUrl}${path}`); return this._pathExpression.extractParameters(this.getTrimmedPath(parsedUrl.pathname), { path, query: Object.fromEntries(parsedUrl.searchParams) }); } } //# sourceMappingURL=DynamicRoute.js.map