All files / src Horseman.js

77.78% Statements 7/9
75% Branches 3/4
80% Functions 4/5
77.78% Lines 7/9
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69          6x                                           7x 7x       7x                 3x                     2x                         6x      
/**
 * Class to register all ActionFactory calls. Used primarially for SSR
 */
class Horseman {
  constructor() {
    this.resources = new Map();
  }
 
  /*
   * Add a resource to the internal resources map, resources are expected
   * to be in the format
   *
   * ```
   * {
   *  endpoint: "foo",
   *  action: "actionName",
   * }
   * ```
   *
   * @param {Object} r
   *
   * @throws {Error} throws if the resource object does not have the correct
   *                 format
   *
   * @return {Map}
   */
  addResource(r) {
    const { endpoint, action } = r;
    Iif (!endpoint || !action) {
      throw new Error(`Unknown resource added to horseman`);
    }
 
    this.resources.set(r.endpoint, r);
  }
 
  /**
   * Return the underlying resources map
   *
   * @return {Map}
   */
  getAllResources() {
    return this.resources;
  }
 
  /**
   * check if the resources map contains an entry for a particular endpoint
   *
   * @param {string} endpoint
   *
   * @return {bool}
   */
  hasResource(endpoint) {
    return this.resources.has(endpoint);
  }
 
  /**
   * Clear all resources from the internal map
   *
   * @return {undefined}
   */
  clearResources() {
    return this.resources.clear();
  }
}
 
const singleton = new Horseman();
 
export default singleton;