Skip to content

Commit

Permalink
Use JSON instead of urlencoded form data
Browse files Browse the repository at this point in the history
  • Loading branch information
SemaiCZE committed Dec 2, 2017
1 parent d537017 commit 52b62ac
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 68 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"exenv": "^1.2.1",
"express": "^4.13.4",
"file-saver": "^1.3.3",
"flat": "^2.0.1",
"flow-bin": "^0.46.0",
"global": "^4.3.1",
"immutable": "^3.8.1",
Expand Down
30 changes: 4 additions & 26 deletions src/redux/helpers/api/tools.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import statusCode from 'statuscode';
import { addNotification } from '../../modules/notifications';
import flatten from 'flat';

import { logout } from '../../modules/auth';
import { isTokenValid, decode } from '../../helpers/token';
Expand All @@ -14,28 +13,6 @@ export const API_BASE = process.env.API_BASE || 'http://localhost:4000/v1';
const maybeShash = endpoint => (endpoint.indexOf('/') === 0 ? '' : '/');
const getUrl = endpoint => API_BASE + maybeShash(endpoint) + endpoint;

export const flattenBody = body => {
const flattened = flatten(body, { delimiter: ':' });
body = {};

Object.keys(flattened).map(key => {
// 'a:b:c:d' => 'a[b][c][d]'
const bracketedKey = key.replace(/:([^:$]+)/g, '[$1]');
body[bracketedKey] = flattened[key];
});

return body;
};

const createFormData = body => {
const data = new FormData();
const flattened = flattenBody(body);
for (let key in flattened) {
data.append(key, flattened[key]);
}
return data;
};

const maybeQuestionMark = (endpoint, query) =>
Object.keys(query).length === 0
? ''
Expand All @@ -51,18 +28,19 @@ export const createRequest = (endpoint, query = {}, method, headers, body) =>
fetch(getUrl(assembleEndpoint(endpoint, query)), {
method,
headers,
body: body ? createFormData(body) : undefined
body: body ? JSON.stringify(body) : undefined
});

export const getHeaders = (headers, accessToken) => {
const jsonHeaders = { 'Content-Type': 'application/json', ...headers };
if (accessToken) {
return {
Authorization: `Bearer ${accessToken}`,
...headers
...jsonHeaders
};
}

return headers;
return jsonHeaders;
};

/**
Expand Down
37 changes: 3 additions & 34 deletions test/redux/helpers/api/tools-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { expect } from 'chai';

import {
getHeaders,
assembleEndpoint,
flattenBody
assembleEndpoint
} from '../../../../src/redux/helpers/api/tools';

describe('API middleware and helper functions', () => {
describe('(Helper functions)', () => {
it('must add access token to the headers', () => {
expect(getHeaders({ a: 'b' }, 'abcd')).to.eql({
a: 'b',
Authorization: 'Bearer abcd'
Authorization: 'Bearer abcd',
'Content-Type': 'application/json'
});
});

Expand All @@ -33,36 +33,5 @@ describe('API middleware and helper functions', () => {
'http://www.blabla.com/abcd'
);
});

it('must flatten the body object to urlencoded format', () => {
expect(flattenBody({})).to.eql({});
expect(flattenBody([])).to.eql({});
expect(flattenBody({ a: 'b' })).to.eql({ a: 'b' });
expect(flattenBody({ a: { b: 'c' } })).to.eql({ 'a[b]': 'c' });
expect(flattenBody({ a: { b: { c: 'd' } } })).to.eql({ 'a[b][c]': 'd' });
expect(flattenBody({ a: { b: ['c', 'd'] } })).to.eql({
'a[b][0]': 'c',
'a[b][1]': 'd'
});
expect(
flattenBody({
a: [
{
b: [{ c: 'A' }, { d: 'B' }]
},
[{ f: 'C', g: 'D' }, { h: 'E' }]
]
})
).to.eql({
'a[0][b][0][c]': 'A',
'a[0][b][1][d]': 'B',
'a[1][0][f]': 'C',
'a[1][0][g]': 'D',
'a[1][1][h]': 'E'
});

// not a POJO
expect(() => flattenBody({ a: new MouseEvent('click') })).to.throw();
});
});
});
8 changes: 1 addition & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2991,12 +2991,6 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"

flat@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/flat/-/flat-2.0.1.tgz#70e29188a74be0c3c89409eed1fa9577907ae32f"
dependencies:
is-buffer "~1.1.2"

flatten@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
Expand Down Expand Up @@ -3861,7 +3855,7 @@ is-binary-path@^1.0.0:
dependencies:
binary-extensions "^1.0.0"

is-buffer@^1.1.5, is-buffer@~1.1.2:
is-buffer@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc"

Expand Down

0 comments on commit 52b62ac

Please sign in to comment.