Skip to content

Commit

Permalink
fix: Issue when sending request with FormData
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrazier committed Jul 27, 2020
1 parent 202c31d commit 1c2ea1c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
6 changes: 6 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import {
import NetworkLogger, { ThemeName } from 'react-native-network-logger';

export default function App() {
const formData = new FormData();
formData.append('test', 'hello');
const makeRequest = () => {
fetch('https://postman-echo.com/post', {
method: 'POST',
body: JSON.stringify({ test: 'hello' }),
});
fetch('https://postman-echo.com/post?formData', {
method: 'POST',
body: formData,
});
fetch('https://httpstat.us/302');
fetch('https://httpstat.us/400');
fetch('https://httpstat.us/500');
Expand Down
12 changes: 8 additions & 4 deletions src/NetworkRequestInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BlobFileReader from 'react-native/Libraries/Blob/FileReader';
import { Headers, RequestMethod } from './types';
import fromEntries from './utils/fromEntries';

export default class NetworkRequestInfo {
type = '';
Expand Down Expand Up @@ -52,14 +53,17 @@ export default class NetworkRequestInfo {
}

private escapeQuotes(value: string) {
return value.replace(/'/g, `\\'`);
return value.replace?.(/'/g, `\\'`);
}

private stringifyFormat(data: string) {
private stringifyFormat(data: any) {
try {
return JSON.stringify(JSON.parse(data), null, '\t');
if (data?._parts?.length) {
return JSON.stringify(fromEntries(data?._parts), null, 2);
}
return JSON.stringify(JSON.parse(data), null, 2);
} catch (e) {
return data;
return `${data}`;
}
}

Expand Down
77 changes: 77 additions & 0 deletions src/__tests__/NetworkRequestInfo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,80 @@ describe('getCurlRequest', () => {
expect(info.curlRequest).toEqual("curl -XDELETE 'https://test.com'");
});
});

describe('getRequestBody', () => {
const info = new NetworkRequestInfo(
'application/json',
'GET',
'https://test.com'
);

it('should return stringified data in consistent format', () => {
info.dataSent = '{"data": {"a": 1 }}';
const result = info.getRequestBody();
expect(typeof result).toBe('string');
expect(result).toMatchInlineSnapshot(`
"{
\\"data\\": {
\\"a\\": 1
}
}"
`);
});

it('should return original object as string if stringify fails', () => {
// @ts-ignore
info.dataSent = { test: 1 };
const result = info.getRequestBody();
expect(typeof result).toBe('string');
expect(result).toEqual('[object Object]');
});

it('should process formData', () => {
const mockFormData = {
_parts: [
['test', 'hello'],
['another', 'goodbye'],
],
};
// @ts-ignore
info.dataSent = mockFormData;
const result = info.getRequestBody();
expect(typeof result).toBe('string');
expect(result).toMatchInlineSnapshot(`
"{
\\"test\\": \\"hello\\",
\\"another\\": \\"goodbye\\"
}"
`);
});
});

describe('getResponseBody', () => {
const info = new NetworkRequestInfo(
'application/json',
'GET',
'https://test.com'
);

it('should return stringified data in consistent format', () => {
info.dataSent = '{"data": {"a": 1 }}';
const result = info.getRequestBody();
expect(typeof result).toBe('string');
expect(result).toMatchInlineSnapshot(`
"{
\\"data\\": {
\\"a\\": 1
}
}"
`);
});

it('should return original object as string if stringify fails', () => {
// @ts-ignore
info.dataSent = { test: 1 };
const result = info.getRequestBody();
expect(typeof result).toBe('string');
expect(result).toEqual('[object Object]');
});
});
7 changes: 7 additions & 0 deletions src/utils/fromEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fromEntries = (arr: any[]) =>
arr.reduce((acc, [k, v]) => {
acc[k] = v;
return acc;
}, {});

export default fromEntries;

0 comments on commit 1c2ea1c

Please sign in to comment.