Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Changing signature of Apify.call()
Marek Trunkate89e9991a62b
diff --git a/src/actor.js b/src/actor.jsindex ad42c350e..2f3c2bf81 100644--- a/src/actor.js+++ b/src/actor.js@@ -392,11 +392,11 @@ export const readyFreddy = () => {* @memberof module:Apify* @function* @description Executes given act, waits for it to finish and fetches it's OUTPUT from key-value store and saves it to run.output.- * @param {Object} opts- * @param {String} opts.actId - Either act ID or username/actname.+ * @param {String} actId - Either act ID or username/actname.+ * @param {String} [input] - Act input body.+ * @param {Object} [opts]* @param {String} [opts.token] - User API token.* @param {String} [opts.build] - Build tag or number to be executed.- * @param {String} [opts.body] - Act input body.* @param {String} [opts.contentType] - Content type of the act input.* @param {String} [opts.timeoutSecs] - Time limit for act to finish. If limit is reached then run in RUNNING status is returned.Default is unlimited.@@ -404,30 +404,45 @@ export const readyFreddy = () => {* @param {String} [opts.rawBody] - If true then returns only OUTPUT value without content type and other info. Default is false.* @param {String} [opts.disableBodyParser] - If true then doesn't parse the body - ie. JSON to object. Default is false.*/-export const call = (opts) => {+export const call = (actId, input, opts) => {const { acts, keyValueStores } = apifyClient;+ checkParamOrThrow(actId, 'actId', 'String');checkParamOrThrow(opts, 'opts', 'Object');// Common options.- const { actId, token } = opts;- checkParamOrThrow(actId, 'actId', 'String');+ const { token } = opts;checkParamOrThrow(token, 'token', 'Maybe String');const defaultOpts = { actId };if (token) defaultOpts.token = token;// RunAct() options.- const { build, input } = opts;- checkParamOrThrow(build, 'build', 'Maybe String');- checkParamOrThrow(input, 'input', 'Maybe Object');+ const { build } = opts;const runActOpts = {};+ checkParamOrThrow(build, 'build', 'Maybe String');if (build) runActOpts.build = build;++ let { contentType } = opts;if (input) {- const { body, contentType } = input;- checkParamOrThrow(body, 'body', 'Buffer | String');+ // TODO: this is duplicite with setValue()'s code+ if (contentType === null || contentType === undefined) {+ contentType = 'application/json';+ try {+ // Format JSON to simplify debugging, the overheads with compression is negligible+ input = JSON.stringify(input, null, 2);+ } catch (err) {+ throw new Error(`The "input" parameter cannot be stringified to JSON: ${err.message}`);+ }+ if (input === undefined) {+ throw new Error('The "input" parameter cannot be stringified to JSON.');+ }+ }++ checkParamOrThrow(input, 'input', 'Buffer|String');checkParamOrThrow(contentType, 'contentType', 'String');- runActOpts.contentType = addCharsetToContentType(contentType);- runActOpts.body = body;++ if (contentType) runActOpts.contentType = addCharsetToContentType(contentType);+ runActOpts.body = input;}// GetAct() options.diff --git a/test/actor.js b/test/actor.jsindex 6744e33c8..1766a3b8f 100644--- a/test/actor.js+++ b/test/actor.js@@ -962,14 +962,15 @@ describe('Apify.call()', () => {const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };const runningRun = Object.assign({}, run, { status: 'RUNNING' });const finishedRun = Object.assign({}, run, { status: ACT_TASK_STATUSES.SUCCEEDED });- const input = { contentType: 'text/plain', body: 'something' };+ const input = 'something';+ const contentType = 'text/plain';const output = 'some-output';const expected = Object.assign({}, finishedRun, { output });const build = 'xxx';const actsMock = sinon.mock(Apify.client.acts);actsMock.expects('runAct')- .withExactArgs({ token, actId, contentType: `${input.contentType}; charset=utf-8`, body: input.body, build })+ .withExactArgs({ token, actId, contentType: `${contentType}; charset=utf-8`, body: input, build }).once().returns(Promise.resolve(runningRun));actsMock.expects('getRun')@@ -988,7 +989,7 @@ describe('Apify.call()', () => {.returns(Promise.resolve(output));return Apify- .call({ actId, token, input, rawBody: true, disableBodyParser: true, build })+ .call(actId, input, { contentType, token, rawBody: true, disableBodyParser: true, build }).then((callOutput) => {expect(callOutput).to.be.eql(expected);keyValueStoresMock.restore();@@ -996,7 +997,48 @@ describe('Apify.call()', () => {});});- it('works as expected with fetchInput = false', () => {+ it('stringifies to JSON', () => {+ const actId = 'some-act-id';+ const token = 'some-token';+ const defaultKeyValueStoreId = 'some-store-id';+ const run = { id: 'some-run-id', actId, defaultKeyValueStoreId };+ const runningRun = Object.assign({}, run, { status: 'RUNNING' });+ const finishedRun = Object.assign({}, run, { status: ACT_TASK_STATUSES.SUCCEEDED });+ const input = { a: 'b' };+ const output = 'some-output';+ const expected = Object.assign({}, finishedRun, { output });+ const build = 'xxx';++ const actsMock = sinon.mock(Apify.client.acts);+ actsMock.expects('runAct')+ .withExactArgs({ token, actId, contentType: 'application/json; charset=utf-8', body: JSON.stringify(input, null, 2), build })+ .once()+ .returns(Promise.resolve(runningRun));+ actsMock.expects('getRun')+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: 999999 })+ .once()+ .returns(Promise.resolve(runningRun));+ actsMock.expects('getRun')+ .withExactArgs({ token, actId, runId: run.id, waitForFinish: 999999 })+ .once()+ .returns(Promise.resolve(finishedRun));++ const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);+ keyValueStoresMock.expects('getRecord')+ .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT', rawBody: true, disableBodyParser: true })+ .once()+ .returns(Promise.resolve(output));++ return Apify+ .call(actId, input, { token, rawBody: true, disableBodyParser: true, build })+ .then((callOutput) => {+ expect(callOutput).to.be.eql(expected);+ keyValueStoresMock.restore();+ actsMock.restore();+ });+ });++ it('works as expected with fetchOuput = false', () => {const actId = 'some-act-id';const token = 'some-token';const defaultKeyValueStoreId = 'some-store-id';@@ -1022,7 +1064,7 @@ describe('Apify.call()', () => {keyValueStoresMock.expects('getRecord').never();return Apify- .call({ actId, token, fetchOutput: false })+ .call(actId, null, { token, fetchOutput: false }).then((callOutput) => {expect(callOutput).to.be.eql(finishedRun);keyValueStoresMock.restore();@@ -1054,7 +1096,7 @@ describe('Apify.call()', () => {keyValueStoresMock.expects('getRecord').never();return Apify- .call({ actId, token, timeoutSecs })+ .call(actId, null, { token, timeoutSecs }).then((callOutput) => {expect(callOutput).to.be.eql(runningRun);keyValueStoresMock.restore();