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';
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: {
......
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 => {
......
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