diff --git a/opentech/static_src/src/app/src/api/utils.js b/opentech/static_src/src/app/src/api/utils.js index ac316ae6a3a5379f4bdf0c4f3f7fffa8ab87fd53..ed1c123f417b39a540f2f157cd0bcbdbbe4505d1 100644 --- a/opentech/static_src/src/app/src/api/utils.js +++ b/opentech/static_src/src/app/src/api/utils.js @@ -1,11 +1,10 @@ -import { decamelizeKeys } from 'humps'; import Cookies from 'js-cookie'; const getBaseUrl = () => { return process.env.API_BASE_URL; }; -export function apiFetch({path, method = 'GET', params = {}, options = {}, decamelizeJSON = true}) { +export function apiFetch({path, method = 'GET', params = {}, options = {}}) { const url = new URL(getBaseUrl()); url.pathname = path; @@ -23,13 +22,6 @@ export function apiFetch({path, method = 'GET', params = {}, options = {}, decam }; } - if (decamelizeJSON === true && options.body !== undefined) { - options = { - ...options, - body: JSON.stringify(decamelizeKeys(options.body)) - } - } - return fetch(url, { ...options, headers: { diff --git a/opentech/static_src/src/app/src/redux/middleware/api.js b/opentech/static_src/src/app/src/redux/middleware/api.js index 4eafcb0541c47b485ed76074cb88fe80be8ec5ca..d808652d5eb396f47f3a0e15177e5ac6b857d93e 100644 --- a/opentech/static_src/src/app/src/redux/middleware/api.js +++ b/opentech/static_src/src/app/src/redux/middleware/api.js @@ -1,8 +1,20 @@ -import { camelizeKeys } from 'humps' +import { camelizeKeys, decamelizeKeys } from 'humps' import { apiFetch } from '@api/utils' const callApi = (endpoint) => { + // If body is an object, decamelize the keys. + const { options } = endpoint; + if (options !== undefined && typeof options.body === 'object') { + endpoint = { + ...endpoint, + options: { + ...options, + body: JSON.stringify(decamelizeKeys(options.body)) + } + } + } + return apiFetch(endpoint) .then(response => response.json().then(json => {