Skip to content

Commit

Permalink
fix: double stringification of JSON (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnmkng authored Apr 22, 2021
1 parent 1a087e8 commit 6a3e065
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.2.2 / 2021/04/20
===================
- Fixed double stringification of JSON inputs in `.start()`, `.call()` and `.metamorph()` functions.

1.2.1 / 2021/04/20
===================
- Added missing function serialization to `.metamorph()`. See 1.2.0 release.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apify-client",
"version": "1.2.1",
"version": "1.2.2",
"description": "Apify API client for JavaScript",
"main": "src/index.js",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function serializeRequest(config) {
const contentTypeHeader = config.headers['Content-Type'] || config.headers['content-type'];
try {
const { type } = contentTypeParser.parse(contentTypeHeader);
if (type === 'application/json') {
if (type === 'application/json' && typeof config.data === 'object') {
config.data = stringifyWithFunctions(config.data);
} else {
config.data = data;
Expand Down
14 changes: 14 additions & 0 deletions test/actors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ describe('Actor methods', () => {
validateRequest(query, { actorId }, { some: 'body' }, { 'content-type': contentType });
});

test('start() works with pre-stringified JSON', async () => {
const actorId = 'some-id';
const contentType = 'application/json; charset=utf-8';
const input = JSON.stringify({ some: 'body' });

const res = await client.actor(actorId).start(input, { contentType });
expect(res.id).toEqual('run-actor');
validateRequest({}, { actorId }, { some: 'body' }, { 'content-type': contentType });

const browserRes = await page.evaluate((id, i, opts) => client.actor(id).start(i, opts), actorId, input, { contentType });
expect(browserRes).toEqual(res);
validateRequest({}, { actorId }, { some: 'body' }, { 'content-type': contentType });
});

test('start() works with functions in input', async () => {
const actorId = 'some-id';
const input = {
Expand Down
24 changes: 24 additions & 0 deletions test/runs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ describe('Run methods', () => {
validateRequest(actualQuery, { runId }, { some: 'body' }, { 'content-type': contentType });
});

test('metamorph() works with pre-stringified JSON input', async () => {
const runId = 'some-run-id';
const targetActorId = 'some-target-id';
const contentType = 'application/json; charset=utf-8';
const input = JSON.stringify({ foo: 'bar' });

const expectedRequest = [
{ targetActorId },
{ runId },
{ foo: 'bar' },
{ 'content-type': contentType },
];

const res = await client.run(runId).metamorph(targetActorId, input, { contentType });
expect(res.id).toEqual('metamorph-run');
validateRequest(...expectedRequest);

const browserRes = await page.evaluate((rId, tId, i, cType) => {
return client.run(rId).metamorph(tId, i, { contentType: cType });
}, runId, targetActorId, input, contentType);
expect(browserRes).toEqual(res);
validateRequest(...expectedRequest);
});

test('metamorph() works with functions in input', async () => {
const runId = 'some-run-id';
const targetActorId = 'some-target-id';
Expand Down

0 comments on commit 6a3e065

Please sign in to comment.