Skip to content

Commit

Permalink
fix(parrot-fetch): normalize json request body
Browse files Browse the repository at this point in the history
  • Loading branch information
maxb08 committed Aug 7, 2023
1 parent 3d062f8 commit b717eb4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/parrot-fetch/__tests__/ParrotFetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ describe('ParrotFetch', () => {
});
});

it('should parse the body is content-type is json', () => {
const parrotFetch = new ParrotFetch();
const normalized = parrotFetch.normalizeRequest('http://www.parrot.com/squawk', {
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ ahoy: 'matey' }),
});
expect(normalized).toMatchObject({
path: '/squawk',
body: {
ahoy: 'matey',
},
protocol: 'http:',
host: 'www.parrot.com',
});
});

it('should resolve to context fetch', () => {
const input = 'http://www.parrot.com';
const contextFetch = jest.fn(() => 'ahoy');
Expand Down
6 changes: 6 additions & 0 deletions packages/parrot-fetch/src/ParrotFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ class ParrotFetch extends Parrot {

normalizeRequest = (input, { method = 'GET', ...init } = {}) => {
const { pathname: path, query, ...parsed } = parse(input, true);
const headers = new Headers(init.headers);
let { body } = init;
if (headers.get('Content-Type') === 'application/json') {
body = JSON.parse(body);
}
return {
...init,
...parsed,
body,
path,
query: Object.keys(query) && query,
method: method && method.toUpperCase(),
Expand Down

0 comments on commit b717eb4

Please sign in to comment.