UNPKG

nralcm

Version:

This is a framework based on NodeJs to manage rest api request lifecycle

32 lines (31 loc) 1.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const exceptions_1 = require("../../exceptions"); /** * Maps Route with controller, * If not found throws not Found exception * @param context HttpContext Object * @returns route */ function ControllerMapper(context, restApiConfiguration) { const urlParts = getUrlParts(context.request.url); const route = restApiConfiguration.routes.find(route => urlParts[0] == route.path); if (route && urlParts.length >= 1) { return route; } throw new exceptions_1.NotFoundException(); } exports.ControllerMapper = ControllerMapper; /** * Function to get url parts after api excluding query string * @param url request url */ function getUrlParts(url) { url = url.substring(url.indexOf("api") + 3); url = url.startsWith("/") ? url.substring(1) : url; const queryStringIndex = url.indexOf("?"); if (queryStringIndex !== -1) { url = url.split("?")[0]; } return url ? url.split("/") : []; }