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
Adding getInput()
Marek Trunkat6fe30c6adbb6
diff --git a/src/index.js b/src/index.jsindex 65798f325..b0ba29dbb 100644--- a/src/index.js+++ b/src/index.js@@ -7,7 +7,7 @@ import BasicCrawler from './basic_crawler';import CheerioCrawler from './cheerio_crawler';import { pushData, openDataset } from './dataset';import events, { initializeEvents, stopEvents } from './events';-import { getValue, setValue, openKeyValueStore } from './key_value_store';+import { getValue, setValue, getInput, openKeyValueStore } from './key_value_store';import { launchPuppeteer } from './puppeteer';import PuppeteerCrawler from './puppeteer_crawler';import PuppeteerPool from './puppeteer_pool';@@ -79,6 +79,7 @@ module.exports = {// Key-value storegetValue,setValue,+ getInput,openKeyValueStore,// Puppeteerdiff --git a/src/key_value_store.js b/src/key_value_store.jsindex e3ccc9a38..1682b1702 100644--- a/src/key_value_store.js+++ b/src/key_value_store.js@@ -6,7 +6,7 @@ import contentTypeParser from 'content-type';import LruCache from 'apify-shared/lru_cache';import mime from 'mime';import { KEY_VALUE_STORE_KEY_REGEX } from 'apify-shared/regexs';-import { ENV_VARS, LOCAL_STORAGE_SUBDIRS } from 'apify-shared/consts';+import { ENV_VARS, LOCAL_STORAGE_SUBDIRS, KEY_VALUE_STORE_KEYS } from 'apify-shared/consts';import { checkParamOrThrow, parseBody } from 'apify-client/build/utils';import {addCharsetToContentType, apifyClient, ensureDirExists, openRemoteStorage, openLocalStorage, ensureTokenOrLocalStorageEnvExists,@@ -594,8 +594,6 @@ export const openKeyValueStore = (storeIdOrName, options = {}) => {: openRemoteStorage(storeIdOrName, ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID, KeyValueStore, storesCache, getOrCreateKeyValueStore);};-//-/*** Gets a value from the default {@link KeyValueStore} associated with the current actor run.*@@ -625,8 +623,7 @@ export const openKeyValueStore = (storeIdOrName, options = {}) => {* @name getValue* @function*/-export const getValue = key => openKeyValueStore().then(store => store.getValue(key));-+export const getValue = async key => openKeyValueStore().then(store => store.getValue(key));/*** Stores or deletes a value in the default {@link KeyValueStore} associated with the current actor run.@@ -667,4 +664,29 @@ export const getValue = key => openKeyValueStore().then(store => store.getValue(* @name setValue* @function*/-export const setValue = (key, value, options) => openKeyValueStore().then(store => store.setValue(key, value, options));+export const setValue = async (key, value, options) => openKeyValueStore().then(store => store.setValue(key, value, options));++/**+ * Gets the actor input value from the default {@link KeyValueStore} associated with the current actor run.+ *+ * This is just a convenient shortcut for [`keyValueStore.getValue('INPUT')`](keyvaluestore#KeyValueStore+getValue).+ * For example, calling the following code:+ * ```javascript+ * const input = await Apify.getInput();+ * ```+ *+ * is equivalent to:+ * ```javascript+ * const store = await Apify.openKeyValueStore();+ * await store.getValue('INPUT');+ * ```+ *+ * For more information, see [`Apify.openKeyValueStore()`](#module_Apify.openKeyValueStore)+ * and [`keyValueStore.getValue()`](keyvaluestore#KeyValueStore+getValue).+ *+ * @returns {Promise<Object>}+ * Returns a promise that resolves once the record is stored.+ * @memberof module:Apify+ * @name getInput+ */+export const getInput = async () => getValue(process.env[ENV_VARS.INPUT_KEY] || KEY_VALUE_STORE_KEYS.INPUT);diff --git a/test/key_value_store.js b/test/key_value_store.jsindex 205b7ee28..5683834de 100644--- a/test/key_value_store.js+++ b/test/key_value_store.js@@ -2,7 +2,7 @@ import chai, { expect } from 'chai';import chaiAsPromised from 'chai-as-promised';import sinon from 'sinon';import path from 'path';-import { ENV_VARS } from 'apify-shared/consts';+import { ENV_VARS, KEY_VALUE_STORE_KEYS } from 'apify-shared/consts';import { KeyValueStoreLocal, KeyValueStore, maybeStringify, getFileNameRegexp, LOCAL_STORAGE_SUBDIR } from '../build/key_value_store';import * as utils from '../build/utils';import * as Apify from '../build/index';@@ -475,4 +475,23 @@ describe('KeyValueStore', () => {expectDirEmpty(storeDir);});});++ describe('getInput', async () => {+ it('should work', async () => {+ process.env[ENV_VARS.LOCAL_STORAGE_DIR] = 'something';+ const defaultStore = await Apify.openKeyValueStore();++ // Uses default value.+ defaultStore.getValue = async key => expect(key).to.be.eql(KEY_VALUE_STORE_KEYS.INPUT);+ await Apify.getInput();++ // Uses value from env var.+ process.env[ENV_VARS.INPUT_KEY] = 'some-value';+ defaultStore.getValue = async key => expect(key).to.be.eql('some-value');+ await Apify.getInput();++ delete process.env[ENV_VARS.LOCAL_STORAGE_DIR];+ delete process.env[ENV_VARS.INPUT_KEY];+ });+ });});