3.8.0 • Published 5 years ago

tramway-core-router v3.8.0

Weekly downloads
91
License
MIT
Repository
github
Last release
5 years ago

Tramway is a simple router for the tramway framework. It includes:

  1. A dynamic routing system that separates routes from routing logic and is adaptable
  2. Restful routes to save time building APIs
  3. Authentication policies that allow for multiple strategies to be used and interchanged without needing the logic throughout the code. and so much more.
  4. Swappable router strategies that keep your app consistent no matter which solution you use, server or server-less.
  5. Includes http-status-codes to have status code enums.

Installation:

  1. npm install tramway-core-router

Example project

https://gitlab.com/tramwayjs/tramway-example

Documentation

Recommended Folder Structure

  • controllers
  • errors
  • policies
  • routes

Routes

Routes are where you store the routes that the Router will take.

A typical routes file in the routes folder would import Controllers and directly assign their actions to a route. The resulting JSON would be consumed and converted by the router into a standard, secure or restful route.

Here's a sample routes file. The order of routes matters and is preserved by the router at all times.

Note: The definition for controllerClass has been removed and the controller attributes only takes an instance now. The Controller action now has an attribute that takes the string name

import MainController from "../controllers/MainController";
import SecuredController from "../controllers/SecuredController";
import StandardAuthenticationPolicy from "../policies/StandardAuthenticationPolicy";
import TestController from '../controllers/TestController';
let standardAuthenticationStrategy = new StandardAuthenticationPolicy();
const routesValues = [
    {
        "methods": ["get"],
        "controller": new MainController(),
        "action": "index"
    },
    {
        "path": "/test",
        "controller": new TestController(),
        "arguments": ["id"],
        "restful": true
    },
    {
        "path": "/hello",
        "arguments": ["name"],
        "methods": ["get"],
        "controller": new MainController(),
        "action": "sayHello"
    },
    {
        "path": "/secure",
        "methods": ["get"],
        "controller": new SecuredController(),
        "action": "index",
        "policy": standardAuthenticationStrategy
    },
    {
        "arguments": ["name"],
        "methods": ["get"],
        "controller": new MainController(),
        "action": "sayHello"
    },
    {
        "arguments": ["name"],
        "methods": ["post", "put"],
        "controller": new MainController(),
        "action": "postTest"
    }
];
export default routesValues;

Route specs

AttributeExpected ValuesDefault ValuesNotes
pathUnparameterized path string"/"If no path is specified, the router will default to root.
controllerControllerundefinedIf no Controller is specified, the app will break
actionstringundefinedThe name of the Controller method dedicated to handling the route. If not a restful route and no action is specified, the app will break
restfulbooleanundefinedIf the controller is a restful controller - note that action attribute will be ignored
methods"get", "post", "put", "delete", "all""get"Indicates which http methods get assigned the controller. Restful routes will ignore this setting as it is automatically bound by implenentation
argumentsstring[]""An optional ordered array of arguments to add to the path. "id", "name" equates to "/:id/:name"
policyAuthenticationStrategyundefinedIgnored if unpresent, applies a policy or authentication strategy before allowing the router to proceed to the controller when a request is made to the

Integrating with Dependency Injection

To leverage dependency injection, just use the string name of the Controller service instead of the Controller instance itself. The same can be said for the policy.

let standardAuthenticationStrategy = new StandardAuthenticationPolicy();
const routesValues = [
    {
        "methods": ["get"],
        "controller": "controller.main",
        "action": "index"
    },
    {
        "path": "/model",
        "controller": "controller.test",
        "arguments": ["id"],
        "restful": true
    },
    {
        "path": "/hello",
        "arguments": ["name"],
        "methods": ["get"],
        "controller": "controller.main",
        "action": "sayHello"
    },
    {
        "path": "/secure",
        "methods": ["get"],
        "controller": "controller.secured",
        "action": "index",
        "policy": "policy.standard_authentication"
    },
    {
        "arguments": ["name"],
        "methods": ["get"],
        "controller": "controller.main",
        "action": "sayHello"
    },
    {
        "arguments": ["name"],
        "methods": ["post", "put"],
        "controller": "controller.main",
        "action": "postTest"
    }
];
export default routesValues;

A fully-implemented Controller will have the following dependency injection configuration:

import {
    TestRestController,
} from '../../controllers';

export default {
    "controller.test": {
        "class": TestController,
        "constructor": [
            {"type": "service", "key": "router"},
            {"type": "service", "key": "service.test"},
            {"type": "service", "key": "service.formatter"},
            {"type": "service", "key": "logger"},
        ],
        "functions": []
    },
}

Router

The Router will be called in your main server file where you create your Express server and get the routes file. This is typically at the root of your project. Once you have a router, initializing it will set up the routes and assign them to the app and return the app to be started via listen.

Here's an example usage among parts of an express server file:

import express from 'express';
import {Router, strategies} from 'tramway-core-router';
import routes from './routes/routes.js';

const PORT = 8080;

let app = express();
let {ExpressServerStrategy} = strategies;
let router = new Router(routes, new ExpressServerStrategy(app));
app = router.initialize();

Here's an example usage with dependency injection (the entire router configuration would just be a services configuration file):

import {Router, strategies} from 'tramway-core-router';
import {DependencyResolver} from 'tramway-core-dependency-injector';

const {ExpressServerStrategy} = strategies;

export default {
    "router": {
        "class": Router,
        "constructor": [
            {"type": "parameter", "key": "routes"},
            {"type": "service", "key": "express-router-strategy"},
            DependencyResolver,
        ],
    },
    "express-router-strategy": {
        "class": ExpressServerStrategy,
        "constructor": [
            {"type": "parameter", "key": "app"}, //the express app would also be containerized
        ]
    }
}

The router also exposes some static methods which can be used across your app without making another instance.

FunctionUsageNotes
buildPath(...string): string"a/b/c" === Router.buildPath("a", "b", "c")Returns a clean path given any number of strings.
buildQuery(params: Object): string"a=1&b=2&c=true" === Router.buildQuery({"a": 1, "b": 2, "c": true})Returns a query string for any associative object

In addition, you can get a specific route by name or by controller and action.

FunctionUsageNotes
getRoute(name: string): ObjectRequires adding a name key to each route in the routes config
getRouteByAction(controllerName, actionName): ObjectA helper is available in the base Controller class so all you need to do is pass the actionName which is the name of the method

Strategies

The biggest addition is strategies which helps keep your apps consistent across clients and servers and also aid in keeping your app framework agnostic by adapting a consistent format across multiple router types which can be plug and play.

All strategies must extend the RouterStrategy class and implement the prepareRoute function (and optionally override the prepareRoutes function).

import {RouterStrategy} from 'tramway-core-router';

export default MyRouterStrategy extends RouterStrategy {
    // Takes a Route or RestfulRoute entity found in {entities}.
    prepareRoute(route) {
        //adapt route to your app's routing
    }
}

ExpressServerStrategy

The strategy that comes with this package is the ExpressServerStrategy which binds the pre-configured routes to the initialized Express app at application start. If you wanted to use React Router on the client side, strategies aid in adapting such that only a piece of the router needs to be replaced.

It takes the following arguments in the constructor:

ArgumentDefaultDescription
appThe instantiated Express app
securitySecurityThe Security middleware to apply to routes and handle authentication with. It has a method, generateMiddleware which will return an Express RouteHandler function(req, res, next). By default, the ExpressServerStrategy uses the default Security class but this parameter allows it to be overidden in cases where authentication is handled by a third party.

Note, any previous implementations of an overidden Security parameter will need to move the logic to the generateMiddleware function and pass a valid Security object.

Controllers

Controllers link to actions from the routing and act to direct the flow of the application.

To create a controller, import the class and implement a derived class with static functions for each route.

import {Controller} from 'tramway-core-router'; 

Sample Controller action signature:

async index(req, res) {}

req and res represent the respective objects passed by your router. With Express the request and response objects are passed by default.

The Controller class also contains some helper functions that can be used by any child Controller - including RestfulController.

FunctionUsage
getRouter(): RouterReturns the Router class for extendability
redirect(res: Object, path: string, status: number)Calls the main redirect function in Express. Will default to a 301 status code.
getRoute(name: string)Gets route metadata by name.
getRouteByAction(action: string)Gets route for the current controller action where action is the method name.

Status codes

It's common to return different status codes with the response at the controller-level. Bundled with the tramway-core-router library is the node-http-status library by @prettymuchbryce which provides enums for different status codes.

To access the enum:

import {HttpStatus} from 'tramway-core-router';

//for 200
HttpStatus.OK;

Full table of supported Enums:

ConstantCodeStatus Text
CONTINUE100Continue
SWITCHING_PROTOCOLS101Switching Protocols
PROCESSING102Processing
OK200OK
CREATED201Created
ACCEPTED202Accepted
NON_AUTHORITATIVE_INFORMATION203Non Authoritative Information
NO_CONTENT204No Content
RESET_CONTENT205Reset Content
PARTIAL_CONTENT206Partial Content
MULTI_STATUS207Multi-Status
MULTIPLE_CHOICES300Multiple Choices
MOVED_PERMANENTLY301Moved Permanently
MOVED_TEMPORARILY302Moved Temporarily
SEE_OTHER303See Other
NOT_MODIFIED304Not Modified
USE_PROXY305Use Proxy
TEMPORARY_REDIRECT307Temporary Redirect
PERMANENT_REDIRECT308Permanent Redirect
BAD_REQUEST400Bad Request
UNAUTHORIZED401Unauthorized
PAYMENT_REQUIRED402Payment Required
FORBIDDEN403Forbidden
NOT_FOUND404Not Found
METHOD_NOT_ALLOWED405Method Not Allowed
NOT_ACCEPTABLE406Not Acceptable
PROXY_AUTHENTICATION_REQUIRED407Proxy Authentication Required
REQUEST_TIMEOUT408Request Timeout
CONFLICT409Conflict
GONE410Gone
LENGTH_REQUIRED411Length Required
PRECONDITION_FAILED412Precondition Failed
REQUEST_TOO_LONG413Request Entity Too Large
REQUEST_URI_TOO_LONG414Request-URI Too Long
UNSUPPORTED_MEDIA_TYPE415Unsupported Media Type
REQUESTED_RANGE_NOT_SATISFIABLE416Requested Range Not Satisfiable
EXPECTATION_FAILED417Expectation Failed
IM_A_TEAPOT418I'm a teapot
INSUFFICIENT_SPACE_ON_RESOURCE419Insufficient Space on Resource
METHOD_FAILURE420Method Failure
UNPROCESSABLE_ENTITY422Unprocessable Entity
LOCKED423Locked
FAILED_DEPENDENCY424Failed Dependency
PRECONDITION_REQUIRED428Precondition Required
TOO_MANY_REQUESTS429Too Many Requests
REQUEST_HEADER_FIELDS_TOO_LARGE431Request Header Fields Too Large
INTERNAL_SERVER_ERROR500Server Error
NOT_IMPLEMENTED501Not Implemented
BAD_GATEWAY502Bad Gateway
SERVICE_UNAVAILABLE503Service Unavailable
GATEWAY_TIMEOUT504Gateway Timeout
HTTP_VERSION_NOT_SUPPORTED505HTTP Version Not Supported
INSUFFICIENT_STORAGE507Insufficient Storage
NETWORK_AUTHENTICATION_REQUIRED511Network Authentication Required

Restful Controllers

If you're just writing a Restful API, it can rapidly become tedious and messy when you end up creating each part of the CRUD structure and register each route.

Much of the logic behind this process can be abstracted, such that if you already have a Provider and a linked Repository, all you will have to do is make a derived RestfulController to put it altogether.

Here's a sample RestfulController implementation if you want to have a stub.

import {controllers} from 'tramway-core-router';
import {Service} from '../services';
const {RestfulController} = controllers;

export default class TestController extends RestfulController {
    constructor(router, service, formatter, logger) {
        super(router, service);
        this.formatter = formatter;
        this.logger = logger;
    }
}

With dependency injection, adding the declaration using the RestfulController in the controller dependencies is enough.

"controller.rest.testrest": {
    "class": RestfulController,
    "constructor": [
        {"type": "service", "key": "router"},
        {"type": "service", "key": "service.testrest"}
    ],
    "functions": []
},

The RestfulController comes with pre-implemented functions.

FunctionUrlMethodResponse
getOne/:resource/:idGETA formatted resource entity
get/:resourceGETA formatted resource collection
create/:resourcePOSTInserts a resource entity
update/:resource/:idPATCHUpdates a resource entity
replace/:resource/:idPUTReplaces a resource entity
delete/:resource/:idDELETEDeletes a resource entity

You can add custom methods to deal with supplementary API routes and subresources. Writing a custom method is as simple as writing a traditional Express route handler and returning data via a helper method (sendCollection, sendEntity) to ensure consistent formatting across the API with your ResponseFormatter.

async getSubResources(req, res) {
    //handle any logic via a service you declared with dependency injection

    //If you get a collection of entities, return with this helper method.
    return this.sendCollection(res, collection, options);

    //If you get an entity, return with this helper method.
    return this.sendEntity(res, entity, options);
}

Adding extra links to a Restful Route.

The Router has the ability to get a specific route metadata which can be used for a variety of purposes. The Router is able to get a route from the config and pass it where it is needed. The route can be found by name - which requires adding a name value to the routes config, or by using the action - the name of the function in the controller.

This can be used to append extra data that can be used for execution and formatting.

The RestfulController has a new method getLinks which, using the routing config, will return all the extra links that can be passed to the links option - if you are using the HATEAOSFormatter - in the sendEntity and sendCollection helper methods.

To get started, take a restful route and add the links. The link path is relative and HATEAOSFormatter can format it for you if formatted is false - it is by default.

{
    "arguments": ["id"],
    "methods": ["get"],
    "path": "resource",
    "controller": "controllers.resource",
    "action": "getOne",
    "links": [
        {"label": "subresources", "link": "subresources", "formatted": false}
    ]
},

This will add an extra link "subresouces" which will point to the resource's respective subresources.

Policies

Policies let you regulate routing for authentication or permissions-based reasons. This allows you to write authentication code in one place, use it in the router and not have to burden the rest of the codebase with it.

To write an authentication policy, import the class and implement the stubs.

import {policies} from 'tramway-core-router';
let {AuthenticationStrategy} = policies;

Note: All functions are async functions, callbacks are no longer supported, request is passed

FunctionUsage
constructor()Sets a redirect via super(redirectRoute: string)
login(request)Implements and handles login criteria for the strategy
logout(request)Implements and handles logout criteria for the strategy
check(request)Implements and handles the check on the current status of user with regards to the policy.

If a policy is indicated with the route, it will call the framework's internal Security service which will return a result based on the check performed by the Authentication service using the Authentication strategy - which uses strategy pattern. It's at this point where the router will redirect with a 401 to the policy's redirect route if the strategy's check criteria fails.

A Policy can also be a basic Policy which provides a guard for a particular Controller action. For instance, you developed a REST API and you have a sub-resource, a policy can be created to ensure the parent exists.

import {policies, errors} from 'tramway-core-router';

const { Policy } = policies;
const { HttpNotFoundError, HttpInternalServerError } = errors;

export default class ParentPolicy extends Policy {
    constructor(service, logger) {
        this.service = service;
        this.logger = logger;
    }

    async check(request) {
        let item;

        try {
            item = this.service.getOne(request.parentId);
        } catch(e) {
            this.logger.error(e.stack);
            throw new HttpInternalServerError();
        }

        if (!item) {
            throw new HttpNotFoundError();
        }

        return {item};
    }
}

Formatters

Formatters let you standardize the format of responses and could support different content headers.

By default, the ResponseFormatter bundled with the library and used with the RestfulController will just return the entity or collection but in use cases like REST, it makes sense to have the formatter resolve the schema and content type.

To make a custom formatter, extend the ResponseFormatter and implement the formatEntity and formatCollection methods.

import {ResponseFormatter} from 'tramway-core-router';

export default class CustomResponseFormatter extends ResponseFormatter {
    /**
     * 
     * @param {Entity} entity 
     */
    formatEntity(entity) {
        return entity;
    }

    /**
     * 
     * @param {Collection} collection 
     */
    formatCollection(collection) {
        return collection;
    }
}

Http Errors

The HttpError object can be used to communicate Http errors with Tramway. Policies will now take HttpError into account when responding to an exception thrown within a Policy. A policy will still return an unauthorized response by default for backwards compatibility on its initial security-driven use case.

The following HttpError objects are available with the library. Others can be added to the library or constructed using HttpStatus.

ObjectStatus Code
HttpBadRequestError400
HttpUnauthorizedError401
HttpForbiddenError403
HttpNotFoundError404
HttpInternalServerError500
3.8.0

5 years ago

3.7.0

5 years ago

3.6.2

6 years ago

3.6.1

7 years ago

3.6.0

7 years ago

3.5.1

7 years ago

3.5.0

7 years ago

3.4.0

7 years ago

3.3.0

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago