// eslint-disable-next-line global-require
export const fetch = require('react-native-fetch-polyfill').default;

export const futch = (url, opts = {}, onProgress) => {
  return new Promise((res, rej) => {
    const xhr = new global.XMLHttpRequest();
    xhr.open(opts.method || 'get', url);
    Object.keys(opts.headers).forEach((k) => {
      xhr.setRequestHeader(k, opts.headers[k]);
    });
    // for (var k in opts.headers || {}) {
    //   console.info('*');
    //   xhr.setRequestHeader(k, opts.headers[k]);
    // }
    xhr.onload = e => res(e.target);
    xhr.onerror = rej;
    if (xhr.upload && onProgress) {
      xhr.upload.onprogress = onProgress; // event.loaded / event.total * 100 ; //event.lengthComputable
    }
    xhr.send(opts.body);
  });
};
