0.0.1 • Published 8 years ago

ks-route-parser v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

Route parser

It a simple route parser accepting a list of YML files and return a list of resolved routes like :

home:
  pattern: '/home'
  post:
    controller: 'ControllerMock:add'
    middlewares: [
      controller: 'Auth:isAuth'
    ]

And creates a new instance of :

/**
 * The route definition class
 */
export default class Route {

  /**
   * Create a new route
   * @param  {string} pattern            The pattern URL
   * @param  {string} method             The HTTP method
   * @param  {Object} controllerInstance The controller instance object
   * @param  {string} controllerMethod   The controller method
   * @param  {Object[]} middlewares = [] The middleware list to start before the route is resolved
   */
  constructor (pattern, method, controller, controllerMethod, middlewares = []) {
    /**
     * The pattern URL
     * @type {string}
     */
    this.pattern = pattern
    /**
     * The HTTP method
     * @type {string}
     */
    this.method = method
    /**
     * The controller object
     * @type {Object}
     */
    this.controller = controller
    /**
     * The controller method
     * @type {string}
     */
    this.controllerMethod = controllerMethod
    /**
     * The middleware list to start before the route is resolved
     * @type {Object[]}
     */
    this.middlewares = middlewares
  }

}

Usage

You can simply call this :

import RouteParser from 'route-parser'

/**
 * Create a new route parser
 */
const routeParser = new RouteParser()
/**
 * Get a list of YML files to parse
 * Returns the route parsed with the above model
 */
const routes = routeParser.parseRoutes(['path/to/file/routes.yml'])