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
Added method Apify.crateAdHocWebhook() fixed #318
drobnikjbbfd5ac25d5b
diff --git a/package.json b/package.jsonindex fed3aa653..95303c056 100644--- a/package.json+++ b/package.json@@ -50,7 +50,7 @@},"dependencies": {"@apify/ps-tree": "^1.1.3",- "apify-client": "^0.5.7",+ "apify-client": "^0.5.9","apify-shared": "^0.1.23","bluebird": "^3.5.3","cheerio": "^1.0.0-rc.2",diff --git a/src/actor.js b/src/actor.jsindex 0e903b4ae..1e3d1cb3e 100644--- a/src/actor.js+++ b/src/actor.js@@ -764,3 +764,40 @@ export const getApifyProxyUrl = (options = {}) => {return `http://${username}:${password}@${hostname}:${port}`;};++/**+ *+ * Creates adhoc webhook for current actor run.+ * Webkooks are supported only for runs on Apify cloud.+ *+ * @param eventTypes {String[]} - Array of event types, which you can set for actor run, see+ * the <a href="https://apify.com/docs/webhooks#events-actor-run" target="_blank">actor run events</a> in the Apify doc.+ * @param requestUrl {String} - URL which will be requested using HTTP POST request, when actor run will be in specific event type.+ *+ * @return {Promise<Webhook|undefined>}+ *+ * @memberof module:Apify+ * @function+ * @name createAdHocWebhook+ */+export const createAdHocWebhook = async (eventTypes, requestUrl) => {+ checkParamOrThrow(eventTypes, 'eventTypes', 'Array');+ checkParamOrThrow(requestUrl, 'requestUrl', 'String');++ const runId = process.env[ENV_VARS.ACTOR_RUN_ID];+ if (!runId) {+ log.warning('Webhooks are not supported locally! We skipped createAdHocWebhook() invocation.');+ return;+ }++ const webhook = await apifyClient.webhooks.createWebhook({+ isAdHoc: true,+ eventTypes,+ condition: {+ actorRunId: runId,+ },+ requestUrl,+ });++ return webhook;+};diff --git a/src/index.js b/src/index.jsindex 1ce58a94f..15f80709a 100644--- a/src/index.js+++ b/src/index.js@@ -1,7 +1,7 @@import EventEmitter from 'events';import log from 'apify-shared/log';import { ENV_VARS } from 'apify-shared/consts';-import { main, getEnv, call, callTask, getApifyProxyUrl, metamorph } from './actor';+import { main, getEnv, call, callTask, getApifyProxyUrl, metamorph, createAdHocWebhook } from './actor';import AutoscaledPool from './autoscaling/autoscaled_pool';import BasicCrawler from './basic_crawler';import CheerioCrawler from './cheerio_crawler';@@ -58,6 +58,7 @@ module.exports = {getApifyProxyUrl,isAtHome,client: apifyClient,+ createAdHocWebhook,// Autoscaled poolAutoscaledPool,diff --git a/test/actor.js b/test/actor.jsindex 06a0eb90a..7b20cb355 100644--- a/test/actor.js+++ b/test/actor.js@@ -1135,3 +1135,36 @@ describe('Apify.getApifyProxyUrl()', () => {delete process.env[ENV_VARS.PROXY_PORT];});});+++describe('Apify.createAdhocWebhook()', () => {+ it('works', async () => {+ const runId = 'my-run-id';+ const expectedEventTypes = ['ACTOR.RUN.SUCCEEDED'];+ const expectedRequestUrl = 'http://example.com/api';++ process.env[ENV_VARS.ACTOR_RUN_ID] = runId;++ const webhooksMock = sinon.mock(Apify.client.webhooks);++ webhooksMock.expects('createWebhook')+ .withExactArgs({+ isAdHoc: true,+ eventTypes: expectedEventTypes,+ condition: {+ actorRunId: runId,+ },+ requestUrl: expectedRequestUrl,+ })+ .once()+ .returns(Promise.resolve());+++ await Apify.createAdHocWebhook(expectedEventTypes, expectedRequestUrl);++ delete process.env[ENV_VARS.ACTOR_RUN_ID];++ webhooksMock.verify();+ webhooksMock.restore();+ });+});