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
Apify.call - wrapping input to input field and adding some tests
Marek Trunkatdc136b21584e
diff --git a/src/actor.js b/src/actor.jsindex 851e9d32e..236f68f21 100644--- a/src/actor.js+++ b/src/actor.js@@ -289,14 +289,18 @@ export const call = (opts) => {if (token) defaultOpts.token = token;// RunAct() options.- const { build, body, contentType } = opts;+ const { build, input } = opts;checkParamOrThrow(build, 'build', 'Maybe String');- checkParamOrThrow(body, 'body', 'Maybe Buffer | String');- checkParamOrThrow(contentType, 'contentType', 'Maybe String');+ checkParamOrThrow(input, 'input', 'Maybe Object');const runActOpts = {};- if (contentType) runActOpts.contentType = contentType;if (build) runActOpts.build = build;- if (body) runActOpts.build = body;+ if (input) {+ const { body, contentType } = input;+ checkParamOrThrow(body, 'body', 'Buffer | String');+ checkParamOrThrow(contentType, 'contentType', 'String');+ runActOpts.contentType = contentType;+ runActOpts.body = body;+ }// GetAct() options.const { timeoutSecs, fetchOutput = true } = opts;@@ -331,7 +335,7 @@ export const call = (opts) => {.getRun(Object.assign({}, defaultOpts, { waitForFinish, runId: run.id })).then((updatedRun) => {if (!_.contains(ACT_TASK_TERMINAL_STATUSES, updatedRun.status)) return waitForRunToFinish(updatedRun);- if (!fetchOutput) return run;+ if (!fetchOutput) return updatedRun;return addOutputToRun(updatedRun);});diff --git a/test/actor.js b/test/actor.jsindex caadd97dd..9546a4689 100644--- a/test/actor.js+++ b/test/actor.js@@ -216,7 +216,7 @@ const setEnv = (env) => {if (env.timeoutAt) process.env.APIFY_TIMEOUT_AT = env.timeoutAt.toISOString();if (env.defaultKeyValueStoreId) process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID = env.defaultKeyValueStoreId;};-/*+describe('Apify.getEnv()', () => {it('works with null values', () => {const expectedEnv = getEmptyEnv();@@ -726,7 +726,7 @@ describe('Apify.readyFreddy()', () => {Apify.readyFreddy();return testWatchFileWillBecomeEmpty(process.env.APIFY_WATCH_FILE, 1000);});-});*/+});describe('Apify.call()', () => {it('works as expected', () => {@@ -736,12 +736,14 @@ 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 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 })+ .withExactArgs({ token, actId, contentType: input.contentType, body: input.body, build }).once().returns(Promise.resolve(runningRun));actsMock.expects('getRun')@@ -755,12 +757,12 @@ describe('Apify.call()', () => {const keyValueStoresMock = sinon.mock(Apify.client.keyValueStores);keyValueStoresMock.expects('getRecord')- .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT' })+ .withExactArgs({ storeId: run.defaultKeyValueStoreId, key: 'OUTPUT', rawBody: true, disableBodyParser: true }).once().returns(Promise.resolve(output));return Apify- .call({ actId, token })+ .call({ actId, token, input, rawBody: true, disableBodyParser: true, build }).then((callOutput) => {expect(callOutput).to.be.eql(expected);keyValueStoresMock.restore();@@ -768,6 +770,40 @@ describe('Apify.call()', () => {});});+ it('works as expected with fetchInput = false', () => {+ 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 actsMock = sinon.mock(Apify.client.acts);+ actsMock.expects('runAct')+ .withExactArgs({ token, actId })+ .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').never();++ return Apify+ .call({ actId, token, fetchOutput: false })+ .then((callOutput) => {+ expect(callOutput).to.be.eql(finishedRun);+ keyValueStoresMock.restore();+ actsMock.restore();+ });+ });+it('timeouts as expected with unfinished run', () => {const actId = 'some-act-id';const token = 'some-token';