All files / route Router.js

85.71% Statements 6/7
100% Branches 2/2
75% Functions 3/4
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                              8x             4x                       24x 9x     24x   24x      
import { hasOwn } from '../shared/utils'
 
/**
 * Basic router implementation for the ResourcefulApi.
 *
 * Automagically creating api bound modules builds on
 * an understanding of the available routes. To
 * easily instantiate a Store bound to an endpoint,
 * route information for that endpoint must be provided.
 *
 * Since every endpoint is implemented differently and
 * the choice where this route information comes from
 * should be left to the endpoint developer, this
 * library only assumes that route loading can be a
 * asynchronous process which eventually returns and
 * has a set of `Route` objects in `this.routes`,
 * keyed by their module and methods. To ensure the latter,
 * actually adding `Route`s should be done via `addRoute`.
 *
 * @class Router
 */
export class Router {
  constructor () {
    this.routes = {}
  }
 
  /**
   * @returns {Route[]}
   */
  getRoutes () {
    return this.routes
  }
 
  async updateRoutes () {
    return this
  }
 
  /**
   *
   * @param {Route} route
   */
  addRoute (route) {
    if (!hasOwn(this.routes, route.module)) {
      this.routes[route.module] = {}
    }
 
    this.routes[route.module][route.action] = route
 
    return this
  }
}