Skip to content

Commit

Permalink
fix: missing function serialization in metamorph (apify#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnmkng authored Apr 21, 2021
1 parent 5a51f9b commit 1a087e8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.2.1 / 2021/04/20
===================
- Added missing function serialization to `.metamorph()`. See 1.2.0 release.

1.2.0 / 2021/04/20
===================
- Added function serialization to `.start()` and `.call()` function inputs. You can now define input functions as JS functions instead of having to type them out as a string.
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.0",
"version": "1.2.1",
"description": "Apify API client for JavaScript",
"main": "src/index.js",
"keywords": [
Expand Down
3 changes: 3 additions & 0 deletions src/resource_clients/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class RunClient extends ResourceClient {
method: 'POST',
data: input,
params: this._params(params),
// Apify internal property. Tells the request serialization interceptor
// to stringify functions to JSON, instead of omitting them.
stringifyFunctions: true,
};
if (options.contentType) {
request.headers = {
Expand Down
29 changes: 29 additions & 0 deletions test/runs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,35 @@ describe('Run methods', () => {
validateRequest(actualQuery, { runId }, { some: 'body' }, { 'content-type': contentType });
});

test('metamorph() works with functions in input', async () => {
const runId = 'some-run-id';
const targetActorId = 'some-target-id';
const input = {
foo: 'bar',
fn: async (a, b) => a + b,
};

const expectedRequest = [
{ targetActorId },
{ runId },
{ foo: 'bar', fn: input.fn.toString() },
{ 'content-type': 'application/json;charset=utf-8' },
];

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

const browserRes = await page.evaluate((rId, tId) => {
return client.run(rId).metamorph(tId, {
foo: 'bar',
fn: async (a, b) => a + b,
});
}, runId, targetActorId);
expect(browserRes).toEqual(res);
validateRequest(...expectedRequest);
});

test('waitForFinish() works', async () => {
const runId = 'some-run-id';
const waitSecs = 0.1;
Expand Down

0 comments on commit 1a087e8

Please sign in to comment.