@blaasvaer/frmwrk
Version:
My personal Node framework
103 lines (88 loc) • 3.24 kB
JavaScript
const url = require('url');
/**
* This is the router for the fw framework.
*
* @author Sam Blåsvær <sam@blaasvaer.dk>
* Version 1.0.0
* Copyright 2018 - present day.
*/
global.routes = [];
global.ROUTE = function ( route, action, params = null ) {
let _route = route.split('/');
let r = {
"controller": _route[1] == '' ? 'index' : _route[1],
"resource": _route[2] == '' ? '' : _route[2],
"resource_id": _route[3] == '' ? '' : _route[3],
"route": route,
"action": action,
"params": params,
}
global.routes.push( r );
};
/**
* A route can be either static or dynamic.
* A static route is defined in the controllers and dynamic routes will be resolved automatically.
* In order to figure out if we have a static route, we check the pathname and match it against the global.routes object.
* If we do not find any match we asume the route is dynamic and therefore pass '/api/:resource' on.
* @param {string} url The url to find
* @returns object
*/
function findRoute ( url ) {
let pathname = url.pathname,
url_array = pathname.split('/').filter( e => e );
let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
if ( route ) {
return route;
} else {
// Check if route is an API call
let isAPICall = pathname.split('/')[1] === 'api' ? true : false;
if ( isAPICall ) {
/**
* If checking for UUID use:
* UUID v1:
* /^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
* UUID v2:
* /^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
* UUID v3:
* /^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
* UUID v4:
* /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
* UUID v5:
* /^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
*/
let regex = /()/;
switch ( url_array.length ) {
case 2:
regex = /api\/(?<resource>[a-z_]+)/; // match /api/<resource>
route = global.routes.find( ( obj ) => obj.route === '/api/:resources' );
break;
case 3:
// Check for static route with dynamic resource_id
let static_route_path = '/' + url_array[0] + '/' + url_array[1] + '/:id';
let static_route = global.routes.find( ( obj ) => obj.route === static_route_path );
// If we don't find a static route we pass on a dynamic
if ( ! static_route ) {
regex = /api\/(?<resource>[a-z_]+)\/(?<id>[0-9]+)/; // match /api/<resource>/<id>
route = global.routes.find( ( obj ) => obj.route === '/api/:resources/:id' );
} else {
let resource = url_array[1];
regex = new RegExp(`\/api\/${resource}\/(?<id>[0-9]+)`); // match /api/resource/<id>
route = global.routes.find( ( obj ) => obj.route === '/api/' + url_array[1] + '/:id' );
}
break;
}
let regex_result = pathname.match(regex);
if ( regex_result ) {
let regex_groups = regex_result.groups;
route.resource = regex_groups.resource;
route.resource_id = regex_groups.id;
}
return route;
} else {
return global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
}
}
}
module.exports = {
findRoute
}