All files / src resource.js

98.28% Statements 57/58
96.67% Branches 29/30
100% Functions 23/23
98.28% Lines 57/58
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220                            75x                     25x   25x 11x   14x                   61x                   9x 7x   9x                   28x                 8x                   61x 27x 27x     34x                     13x       13x     12x   12x 6x   6x   2x                       18x 5x     18x     11x   11x 5x   6x   7x                     19x 7x     19x     19x 19x 8x 8x 8x 14x     6x 3x   3x   13x                       4x 1x     4x     2x   2x 1x   1x   2x                     7x     6x   6x 3x   3x   1x      
/* eslint-disable new-cap */
import List from 'models/list';
 
/**
 * The base resource
 * @param {Object} httpClient
 * @private
 */
export default class Resource {
  /**
   * Constructor
   * @param httpClient
   */
  constructor(httpClient) {
    this.httpClient = httpClient;
  }
 
  /**
   * Error handler
   * @param {*} response
   * @param {function} [cb]
   * @since 2.0.0
   * @private
   */
  static errorHandler(response, cb) {
    const error = (response && response.data) || response;
 
    if (cb) {
      return cb(error);
    }
    throw error;
  }
 
  /**
   * Get the API client
   * @returns {Object} httpClient
   * @since 2.0.0
   * @private
   */
  getClient() {
    return this.httpClient;
  }
 
  /**
   * Set the parent ID by providing the parent
   * @param parent
   * @since 1.1.1
   * @deprecated
   */
  withParent(parent) {
    if (parent && parent.id) {
      this.setParentId(parent.id);
    }
    return this;
  }
 
  /**
   * Set the parent ID
   * @param {number} parentId
   * @since 2.0.0
   * @protected
   */
  setParentId(parentId) {
    this.parentId = parentId;
  }
 
  /**
   * If the parent ID is set
   * @returns {boolean}
   * @since 2.0.0
   */
  hasParentId() {
    return !!this.parentId;
  }
 
  /**
   * Create a resource URL with the parent ID
   * @returns {string} resourceUrl
   * @since 2.0.0
   * @private
   */
  getResourceUrl() {
    if (this.constructor.resource.indexOf('_') !== -1) {
      const parts = this.constructor.resource.split('_');
      return `${parts[0]}/${this.parentId}/${parts[1]}`;
    }
 
    return this.constructor.resource;
  }
 
  /**
   * Create a resource by ID
   * @param {Object} [data]
   * @param {function} [cb]
   * @returns {Promise.<T>}
   * @since 1.0.0
   */
  create(data, cb) {
    Iif (typeof data === 'function') {
      cb = data; // eslint-disable-line no-param-reassign
    }
 
    return this.getClient()
      .post(this.getResourceUrl(), data)
      .then((response) => {
        const model = new this.constructor.model(response.data);
 
        if (cb) {
          return cb(null, model);
        }
        return model;
      })
      .catch(error => Resource.errorHandler(error.response, cb));
  }
 
  /**
   * Get a resource by ID
   * @param {number} id
   * @param {Object} [params]
   * @param {function} [cb]
   * @returns {Promise.<T>}
   * @since 1.0.0
   */
  get(id, params, cb) {
    if (typeof params === 'function') {
      cb = params; // eslint-disable-line no-param-reassign
    }
 
    return this.getClient()
      .get(`${this.getResourceUrl()}/${id}`, { params })
      .then((response) => {
        const model = new this.constructor.model(response.data);
 
        if (cb) {
          return cb(null, model);
        }
        return model;
      })
      .catch(error => Resource.errorHandler(error.response, cb));
  }
 
  /**
   * Get all resources
   * @param {Object} [params]
   * @param {function} [cb]
   * @returns {Promise.<T>}
   * @since 1.0.0
   */
  all(params, cb) {
    if (typeof params === 'function') {
      cb = params; // eslint-disable-line no-param-reassign
    }
 
    return this.getClient()
      .get(this.getResourceUrl(), { params })
      .then((response) => {
        const { _embedded, _links = [] } = response.data;
        const data = _embedded[this.constructor.resource];
        const list = new List();
        list.links = _links;
        list.push(
          ...data.map(resource => new this.constructor.model(resource)),
        );
 
        if (cb) {
          return cb(null, list);
        }
        return list;
      })
      .catch(error => Resource.errorHandler(error.response, cb));
  }
 
  /**
   * Update a resource by ID
   * @param {number} id
   * @param {Object} [data]
   * @param {function} [cb]
   * @returns {Promise.<T>}
   * @since 1.0.0
   */
  update(id, data, cb) {
    if (typeof data === 'function') {
      cb = data; // eslint-disable-line no-param-reassign
    }
 
    return this.getClient()
      .post(`${this.getResourceUrl()}/${id}`, data)
      .then((response) => {
        const model = new this.constructor.model(response.data);
 
        if (cb) {
          return cb(null, model);
        }
        return model;
      })
      .catch(error => Resource.errorHandler(error.response, cb));
  }
 
  /**
   * Delete a resource by ID
   * @param {number} id
   * @param {function} [cb]
   * @returns {Promise.<T>}
   * @since 1.0.0
   */
  delete(id, cb) {
    return this.getClient()
      .delete(`${this.getResourceUrl()}/${id}`)
      .then((response) => {
        const model = new this.constructor.model(response.data);
 
        if (cb) {
          return cb(null, model);
        }
        return model;
      })
      .catch(error => Resource.errorHandler(error.response, cb));
  }
}