api.js 717 Bytes
import util from '@/libs/util';

class Api {
  constructor() {
  }

  _get(url, data) {
    if (!PRODUCTION) {
      data = Object.assign({}, data, {
        debug: 'XYZ'
      });
    }

    return util.ajax.get(url, data).then(result => result.data);
  }

  _post(url, data, json = false) {
    if (!PRODUCTION) {
      url += '?debug=XYZ';
    }

    const params = json ? data : this._2params(data);

    return util.ajax.post(url, params).then(result => {
      return result.data;
    });
  }

  _2params(data) {
    const params = new URLSearchParams();

    for (let i of Object.keys(data)) {
      if (data[i]) {
        params.append(i, data[i]);
      }
    }

    return params;
  }
}

export default Api;