All files / route JsonApiRoute.js

83.33% Statements 10/12
70% Branches 7/10
100% Functions 2/2
83.33% Lines 10/12

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      6x       6x                                                   3x         3x 1x     2x 2x 2x 2x   2x      
import { Route } from './Route'
 
function getRequiredAttribute (routeResource, attributeName) {
  Iif (!Object.prototype.hasOwnProperty.call(routeResource.attributes, attributeName)) {
    throw new Error(`Missing required resource attribute: ${attributeName}`)
  }
 
  return routeResource.attributes[attributeName]
}
 
/**
 *
 * Create a route from a Json:Api resource of the VuexJsonApiRoute-type:
 *
 * ``` json
 * {
 *   "type": "VuexJsonApiRoute",
 *   "attributes": {
 *     "module": "themodule",
 *     "method": "thehttpmethod"
 *     "url": "The URL of the Resource, can be relative to baseURL",
 *     "parameters": ["Parameters", "for", "the", "above", url"]
 *   }
 * }
 * ```
 * @class JsonApiRoute
 */
export class JsonApiRoute extends Route {
  /**
   *
   * @param {Object} routeResource
   */
  constructor (routeResource) {
    Iif (!Object.prototype.hasOwnProperty.call(routeResource, 'attributes') ||
      !Object.prototype.hasOwnProperty.call(routeResource, 'type')) {
      throw new Error('Expected json:api-like object structure')
    }
 
    if (routeResource.type.toLowerCase() !== 'vuexjsonapiroute') {
      throw new Error('Invalid resource type')
    }
 
    const module = getRequiredAttribute(routeResource, 'module')
    const method = getRequiredAttribute(routeResource, 'method')
    const url = getRequiredAttribute(routeResource, 'url')
    const parameters = routeResource.attributes.parameters || []
 
    super(module, method, url, parameters)
  }
}