import { normalize } from 'normalizr';
import HttpError from 'standard-http-error';



export default function requestBuilder(url, requestOpts = {}, { handleResponse = false, normalizeSchema = false, handleError = false } = {}) {
  return (payload, meta = {}) => {
    const callUrl = typeof url === 'function' ? url(payload, meta) : url;
    const callOpts = {
      ...requestOpts,
    };

    let headers = {};

    if (meta.authToken) {
      headers['Authorization'] = `Bearer ${meta.authToken}`;
    }
    if (typeof callOpts.headers === 'function') {
      headers = callOpts.headers(headers, payload, meta);
    }
    if (headers.Authorization) {
      headers['Access-Control-Allow-Credentials'] = true;
      callOpts.credentials = 'include';
    }

    if (typeof callOpts.body === 'function') {
      callOpts.body = callOpts.body(...args);
    }
    if (typeof callOpts.body !== 'undefined' && typeof callOpts.body !== 'string') {
      callOpts.body = JSON.stringify(callOpts.body);
    }

    callOpts.headers = headers;

    return fetch(callUrl, callOpts)
      .then((response) => {
        switch (response.status) {
          case 200:
          case 201:
            return response;  // Keep going!
          default:
            throw new HttpError(response.status, { response });
        }
      })
      .then((response) => handleResponse ? handleResponse(response, payload, meta) : response.json())
      .then((data) => normalizeSchema ? normalize(data, normalizeSchema) : data)
      .catch(handleError ? (err) => handleError(err, payload, meta) : (err) => {
        throw err;
      });
  };
}

@@ -78,16 +78,58 @@
 rror';%0A%0A
+%0Aconst hasBody = %5B'POST', 'PUT', 'PATCH'%5D;
 %0A%0Aexport
@@ -448,16 +448,91 @@
 rs = %7B%7D;
+%0A    let body = hasBody.includes(requestOpts.method) ? payload : undefined;
 %0A%0A    if
@@ -919,33 +919,24 @@
 n') %7B%0A      
-callOpts.
 body = callO
@@ -948,15 +948,18 @@
 ody(
-...args
+body, meta
 );%0A 
@@ -974,33 +974,24 @@
  if (typeof 
-callOpts.
 body !== 'un
@@ -1005,33 +1005,24 @@
 ' && typeof 
-callOpts.
 body !== 'st
@@ -1036,25 +1036,16 @@
 %7B%0A      
-callOpts.
 body = J
@@ -1062,22 +1062,111 @@
 ify(
-callOpts.body)
+body);%0A      headers%5B'Accept'%5D = 'application/json';%0A      headers%5B'Content-Type'%5D = 'application/json'
 ;%0A  
@@ -1201,16 +1201,42 @@
 headers;
+%0A    callOpts.body = body;
 %0A%0A    re

Rework request body, only allow on POST, PUT, PATCH