'use strict';

const axios = require('axios');
const _ = require('lodash');

axios.defaults.baseURL = 'https://www.pivotaltracker.com/services/v5/';

let apiKey;
const Pivotal = {
  getApiKey() {
    if (apiKey) {
      return Promise.resolve(apiKey);
    }

    return new Promise((resolve, reject) => {
      chrome.storage.local.get('pivotalApiKey', (values) => {
        if (!values.pivotalApiKey) {
          reject("No Pivotal API key is set");
        }

        resolve(values.pivotalApiKey);
      });
    });
  },

  setApiKey(_apiKey) {
    return this.validateApiKey(_apiKey).then(() => {
      apiKey = _apiKey;
      chrome.storage.local.set({ pivotalApiKey: apiKey });
    });
  },

  validateApiKey(apiKey) {
    return this.get('accounts', { apiKey });
  },

  makeRequest(method, path, options) {
    options = options || {};

    const makeRequest = function(apiKey) {
      options.headers = options.headers || {};
      options.headers['X-TrackerToken'] = apiKey;
      return axios[method](path, options);
    };

    if (options.apiKey) {
      return makeRequest(options.apiKey);
    } else {
      return this.getApiKey().then(makeRequest);
    }
  }
};

['get', 'delete', 'post', 'put'].forEach((method) => {
  Pivotal[method] = _.partial(Pivotal.makeRequest, method);
});

module.exports = Pivotal;

@@ -792,24 +792,30 @@
 ethod, path,
+ data,
  options) %7B%0A
@@ -822,32 +822,189 @@
     
-options = options %7C%7C %7B%7D;
+const takesData = method === 'post' %7C%7C method === 'put';%0A%0A    if (takesData) %7B%0A      options = options %7C%7C %7B%7D;%0A      data = data %7C%7C %7B%7D;%0A    %7D else %7B%0A      options = data %7C%7C %7B%7D;%0A    %7D
 %0A%0A  
@@ -1137,24 +1137,49 @@
 %5D = apiKey;%0A
+      if (takesData) %7B%0A  
       return
@@ -1198,16 +1198,22 @@
 d%5D(path,
+ data,
  options
@@ -1215,16 +1215,84 @@
 tions);%0A
+      %7D else %7B%0A        return axios%5Bmethod%5D(path, options);%0A      %7D%0A
     %7D;%0A%0A

Fix Pivotal put/post signatures