From d88b33292d2bc9ac0dc0c6ad54a56570f76e746e Mon Sep 17 00:00:00 2001 From: Tomasz Knapik <hi@tmkn.org> Date: Wed, 30 Jan 2019 14:16:07 +0000 Subject: [PATCH] Move decamelizing to the middleware --- opentech/static_src/src/app/src/api/utils.js | 10 +--------- .../static_src/src/app/src/redux/middleware/api.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/opentech/static_src/src/app/src/api/utils.js b/opentech/static_src/src/app/src/api/utils.js index ac316ae6a..ed1c123f4 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 4eafcb054..d808652d5 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 => { -- GitLab