Skip to content
Snippets Groups Projects
Unverified Commit d88b3329 authored by Tomasz Knapik's avatar Tomasz Knapik
Browse files

Move decamelizing to the middleware

parent 0a30734d
No related branches found
No related tags found
No related merge requests found
import { decamelizeKeys } from 'humps';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
const getBaseUrl = () => { const getBaseUrl = () => {
return process.env.API_BASE_URL; 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()); const url = new URL(getBaseUrl());
url.pathname = path; url.pathname = path;
...@@ -23,13 +22,6 @@ export function apiFetch({path, method = 'GET', params = {}, options = {}, decam ...@@ -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, { return fetch(url, {
...options, ...options,
headers: { headers: {
......
import { camelizeKeys } from 'humps' import { camelizeKeys, decamelizeKeys } from 'humps'
import { apiFetch } from '@api/utils' import { apiFetch } from '@api/utils'
const callApi = (endpoint) => { 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) return apiFetch(endpoint)
.then(response => .then(response =>
response.json().then(json => { response.json().then(json => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment