UNPKG

@miracledevs/paradigm-express-webapi

Version:

An easy to use MVC WebApi implementation over Express.

63 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionUrl = exports.RouteParameterType = void 0; var RouteParameterType; (function (RouteParameterType) { RouteParameterType[RouteParameterType["Segment"] = 0] = "Segment"; RouteParameterType[RouteParameterType["QueryStringVariable"] = 1] = "QueryStringVariable"; })(RouteParameterType = exports.RouteParameterType || (exports.RouteParameterType = {})); class RouteParameter { constructor(name, dataType, parameterType) { this.name = name; this.dataType = dataType; this.parameterType = parameterType; } } const UrlRegEx = { UrlExpression: /[^\/?]+/gi, QueryStringExpression: /[^\?\&?]+/gi, execute: (regex, value) => { if (!value) return []; let matches; let results = []; while (matches = regex.exec(value)) { results.push(matches[0]); } return results; } }; class ActionUrl { constructor() { this.segments = []; this.queryString = []; this.parameters = []; } static parse(url, query, actionType) { if (!actionType) throw new Error("Can't create an action url without an action type."); const actionUrl = new ActionUrl(); if (!url && !query) return actionUrl; url = url || ''; query = query || ''; const segments = UrlRegEx.execute(UrlRegEx.UrlExpression, url); const variables = UrlRegEx.execute(UrlRegEx.QueryStringExpression, query); let typeIndex = 0; segments.forEach(x => actionUrl.segments.push(x)); variables.forEach(x => actionUrl.queryString.push(x)); for (const part of segments) { if (!part.startsWith(":")) continue; actionUrl.parameters.push(new RouteParameter(part.substr(1, part.length - 1), actionType.parameters[typeIndex++], RouteParameterType.Segment)); } for (const part of variables) { if (!part.startsWith(":")) continue; actionUrl.parameters.push(new RouteParameter(part.substr(1, part.length - 1), actionType.parameters[typeIndex++], RouteParameterType.QueryStringVariable)); } return actionUrl; } } exports.ActionUrl = ActionUrl; //# sourceMappingURL=action-url.js.map