import {HttpClient, json} from 'aurelia-fetch-client';
import {inject} from 'aurelia-framework';
import qs from 'querystring';

export class Rest extends HttpClient {
  /**
   * Make a request to the server.
   *
   * @param {string} method
   * @param {string} path
   * @param {{}}     [body]
   * @param {{}}     [headers]
   *
   * @return {Promise}
   */
  request (method, path, body, headers) {
    let requestOptions = {
      method : method,
      headers: {
        'Accept'      : 'application/json',
        'Content-Type': 'application/json'
      }
    };

    if (typeof headers !== 'undefined') {
      requestOptions.headers = headers;
    }

    if (typeof body === 'object') {
      requestOptions.body = json(body);
    }

    return this.fetch(path, requestOptions).then(response => response.json());
  }

  /**
   * Find a resource.
   *
   * @param {string}           resource Resource to find in
   * @param {{}|string|Number} criteria Object for where clause, string / number for id.
   *
   * @return {Promise}
   */
  find (resource, criteria) {
    let requestPath = resource;

    if (criteria) {
      requestPath += typeof criteria !== 'object' ? `/${criteria}` : '?' + qs.stringify(criteria);
    }

    return this.request('get', requestPath);
  }

  /**
   * Create a new instance for resource.
   *
   * @param {string} resource
   * @param {{}}     body
   *
   * @return {Promise}
   */
  post (resource, body) {
    return this.request('post', resource, body);
  }

  /**
   * Update a resource.
   *
   * @param {string}        resource  Resource to update
   * @param {string|Number} criteria  String / number of the id to update.
   * @param {object}        body      New data for provided criteria.
   *
   * @return {Promise}
   */
  update (resource, criteria, body) {
    let requestPath = resource;

    if (criteria) {
      requestPath += `/${criteria}`;
    }

    return this.request('put', requestPath, body);
  }

  /**
   * Delete a resource.
   *
   * @param {string}        resource  The resource to delete in
   * @param {string|Number} criteria  String / number of the id to delete.
   *
   * @return {Promise}
   */
  destroy (resource, criteria) {
    let requestPath = resource;

    if (criteria) {
      requestPath += `/${criteria}`;
    }

    return this.request('delete', requestPath);
  }

  /**
   * Create a new instance for resource.
   *
   * @param {string} resource
   * @param {{}}     body
   *
   * @return {Promise}
   */
  create (resource, body) {
    return this.post(...arguments);
  }
}

@@ -125,46 +125,208 @@
 ';%0A%0A
-export class Rest extends HttpClient %7B
+@inject(HttpClient)%0Aexport class Rest %7B%0A%0A  /**%0A   * Inject the httpClient to use for requests.%0A   * @param %7BHttpClient%7D httpClient%0A   */%0A  constructor (httpClient) %7B%0A    this.client = httpClient;%0A  %7D%0A
 %0A  /
@@ -915,16 +915,23 @@
 rn this.
+client.
 fetch(pa

Use HttpClient from DI, so config gets applied