All files / api ResourceProxy.js

67.5% Statements 27/40
44.83% Branches 13/29
72.73% Functions 16/22
67.5% Lines 27/40

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                          11x 11x 11x 11x                 19x       19x                                   1x       15x 6x     15x       6x               6x   14x         14x 14x   14x     9x                       5x                               9x                       1x                                                                                               5x           5x       4x               4x       13x       9x       4x      
import { Route } from '../route/Route'
import { hasOwn } from '../shared/utils'
 
/**
 * ResourceProxy
 *
 * A wrapper class for the available methods on a route. The actual proxied Fetch-clients
 * will only be created once the first request on a method is made.
 *
 * @class ResourceProxy
 */
export class ResourceProxy {
  constructor (resourcefulApi) {
    this.resourcefulApi = resourcefulApi
    this.routes = {}
    this.proxies = {}
    this.related = {}
  }
 
  /**
   * Adds a method to the resource
   *
   * @param {Route} route
   */
  addRoute (route) {
    Iif (!(route instanceof Route)) {
      throw new Error(`Expected Route, got ${route.constructor}`)
    }
 
    this.routes[route.action] = route
  }
 
  addRelation (relationName, relatedResourceProxy) {
    if (!(relatedResourceProxy instanceof ResourceProxy)) {
      throw new Error(`Expected ResourceProxy, got ${relatedResourceProxy.constructor}`)
    }
 
    this.related[relationName] = relatedResourceProxy
  }
 
  /**
   * Used by the resource method availability checks
   * to indicate a missing method.
   *
   * @param {string} methodName
   */
  methodNotAvailable (methodName) {
    throw new Error(`Method ${methodName} not available`)
  }
 
  _getProxyForMethod (methodName) {
    if (!hasOwn(this.proxies, methodName)) {
      this._createProxyForMethodIfMissing(methodName)
    }
 
    return this.proxies[methodName]
  }
 
  _createProxyForMethodIfMissing (methodName) {
    this.proxies[methodName] = this._doCreateProxy(
      this.resourcefulApi,
      methodName,
      this.routes[methodName]
    )
  }
 
  _doCreateProxy (api, method, route) {
    return new Proxy(() => {}, {
      apply (target, thisArg, argArray) {
        Iif (!(route instanceof Route)) {
          throw new Error('Expected Route object')
        }
 
        // add actual route as first param
        const url = route.prepare(argArray[0])
        argArray.unshift(url)
 
        switch (method) {
          case 'list':
          case 'get':
            return api.get.apply(api, argArray)
 
          case 'create':
            return api.post.apply(api, argArray)
 
          case 'replace':
            return api.put.apply(api, argArray)
 
          case 'update':
            return api.patch.apply(api, argArray)
 
          case 'delete':
            return api.delete.apply(api, argArray)
 
          default:
            throw new Error('unsupported api method: ' + method)
        }
      }
    })
  }
 
  /**
   * @param {Array} parameters
   * @param {Object} data
   *
   * @returns Function
   */
  get (parameters, data) {
    return this.allowsItemAccess()
      ? this._getProxyForMethod('get')(parameters, data)
      : this.methodNotAvailable('get')
  }
 
  /**
   * @param {Array} parameters
   * @param {Object} data
   *
   * @returns Function
   */
  list (parameters, data) {
    return this.isCollection()
      ? this._getProxyForMethod('list')(parameters, data)
      : this.methodNotAvailable('list')
  }
 
  /**
   * @param {Array} parameters
   * @param {Object} data
   *
   * @returns Function
   */
  create (parameters, data) {
    return this.allowsCreation()
      ? this._getProxyForMethod('create')(parameters, data)
      : this.methodNotAvailable('create')
  }
 
  /**
   * @param {Array} parameters
   * @param {Object} data
   *
   * @returns Function
   */
  replace (parameters, data) {
    return this.allowsReplacement
      ? this._getProxyForMethod('replace')(parameters, data)
      : this.methodNotAvailable('replace')
  }
 
  /**
   * @param {Array} parameters
   * @param {Object} data
   *
   * @returns Function
   */
  update (parameters, data) {
    return this.allowsModification()
      ? this._getProxyForMethod('update')(parameters, data)
      : this.methodNotAvailable('update')
  }
 
  /**
   * @param {Array} parameters
   * @param {Object} data
   *
   * @returns Function
   */
  delete (parameters, data) {
    return this.allowsDeletion()
      ? this._getProxyForMethod('delete')(parameters, data)
      : this.methodNotAvailable('delete')
  }
 
  isCollection () {
    return ('list' in this.routes)
  }
 
  allowsModification () {
    return ('update' in this.routes)
  }
 
  allowsReplacement () {
    return ('replace' in this.routes)
  }
 
  allowsCreation () {
    return ('create' in this.routes)
  }
 
  allowsDeletion () {
    return ('delete' in this.routes)
  }
 
  allowsItemAccess () {
    return ('get' in this.routes || 'list' in this.routes)
  }
 
  hasRelated () {
    return Object.keys(this.related).length > 0
  }
}