Skip to content

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.

Replace Apify.client with Apify.newClient

Ondra Urbanb4921905059e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eaa02ebf0..d40f76c7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,7 +11,9 @@ We've updated the underlying `apify-client` package which powers all communicati
the Apify API to version `1.0.0`. This means a completely new API for all internal calls.
If you use `Apify.client` calls in your code, this will be a large breaking change for you.
Visit the [client docs](https://github.com/apify/apify-client-js/blob/master/README.md)
-to see what's new.
+to see what's new in the client, but also note that we removed the default client
+available under `Apify.client` and replaced it with `Apify.newClient()` function.
+We think it's better to have separate clients for users and internal use.
Until now, local emulation of Apify Storages has been a part of the SDK. We moved the logic
into a separate package `@apify/storage-local` which shares interface with `apify-client`.
@@ -23,7 +25,7 @@ After collecting common developer mistakes, we've decided to make argument valid
You will no longer be able to pass extra arguments to functions and constructors. This is
to alleviate frustration, when you mistakenly pass `useChrome` to `PuppeteerPoolOptions`
instead of `LaunchPuppeteerOptions` and don't realize it. Before this version, SDK wouldn't
-let you know and just silently continue with Chromium. Now, it will throw an error saying
+let you know and would silently continue with Chromium. Now, it will throw an error saying
that `useChrome` is not an allowed property of `PuppeteerPoolOptions`.
Based on developer feedback, we decided to remove `--no-sandbox` from the default Puppeteer
@@ -39,7 +41,8 @@ screen-casting live view of the running browser.
Full list of changes:
- **BREAKING:** Updated `apify-client` to `1.0.0` with a completely new interface.
- This means that all client calls made using `Apify.client` will break.
+ We also removed the `Apify.client` property and replaced it with an `Apify.newClient()`
+ function that creates a new `ApifyClient` instance.
- **BREAKING:** Removed `--no-sandbox` from default Puppeteer launch arguments.
This will most likely be breaking for Linux and Docker users.
- **BREAKING:** Function argument validation is now more strict and will not accept extra
@@ -47,8 +50,14 @@ Full list of changes:
- **DEPRECATED:** `puppeteerPoolOptions.useLiveView` is now deprecated.
Use the `devtools-server` NPM package instead.
-- Updated `Apify.requestAsBrowser()` headers to the latest Firefox.
-- Updated `apify-client` to version `1.0.0`.
+- Added `postResponseFunction` to `CheerioCrawlerOptions`. It allows you to override
+ properties on the HTTP response before processing by `CheerioCrawler`.
+- Added HTTP2 support to `utils.requestAsBrowser()`. Set `useHttp2` to `true`
+ in `RequestAsBrowserOptions` to enable it.
+- Fixed handling of XML content types in `CheerioCrawler`.
+- Fixed capitalization of headers when using `utils.puppeteer.addInterceptRequestHandler`.
+- Fixed `utils.puppeteer.saveSnapshot()` overwriting screenshots with HTML on local.
+- Updated `puppeteer` to version `5.4.1` with Chrom(ium) 87.
- Removed `RequestQueueLocal` in favor of `@apify/storage-local` API emulator.
- Removed `KeyValueStoreLocal` in favor of `@apify/storage-local` API emulator.
- Removed `DatasetLocal` in favor of `@apify/storage-local` API emulator.
diff --git a/src/main.js b/src/main.js
index 5c5af42fb..2b522ab41 100644
--- a/src/main.js
+++ b/src/main.js
@@ -14,7 +14,7 @@ import Request from './request';
import { RequestList, openRequestList } from './request_list';
import { createProxyConfiguration } from './proxy_configuration';
import { openRequestQueue } from './storages/request_queue';
-import { apifyClient, getMemoryInfo, isAtHome, publicUtils } from './utils';
+import { newClient, getMemoryInfo, isAtHome, publicUtils } from './utils';
import { puppeteerUtils } from './puppeteer_utils';
import { socialUtils } from './utils_social';
import { enqueueLinks } from './enqueue_links/enqueue_links';
@@ -42,7 +42,7 @@ const exportedUtils = Object.assign(publicUtils, {
* the [Getting Started](/docs/guides/getting-started) tutorial.
*
* **Important:**
- * > The following functions: `addWebhook`, `call`, `callTask` and Apify Client available under `client` are features of the
+ * > The following functions: `addWebhook`, `call`, `callTask` and `newClient` invoke features of the
* > [Apify platform](/docs/guides/apify-platform) and require your scripts to be authenticated.
* > See the [authentication guide](/docs/guides/apify-platform#logging-into-apify-platform-from-apify-sdk) for instructions.
*
@@ -56,7 +56,7 @@ export {
metamorph,
getMemoryInfo,
isAtHome,
- apifyClient as client,
+ newClient,
addWebhook,
AutoscaledPool,
diff --git a/src/utils.js b/src/utils.js
index d13db5389..674196d01 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -51,16 +51,28 @@ const DISABLE_OUTDATED_WARNING = 'APIFY_DISABLE_OUTDATED_WARNING';
const psTreePromised = util.promisify(psTree);
/**
- * Creates an instance of ApifyClient using options as defined in the environment variables.
- * This function is exported to enable unit testing.
+ * Returns a new instance of the Apify API client. The `ApifyClient` class is provided
+ * by the <a href="https://www.npmjs.com/package/apify-client" target="_blank">apify-client</a>
+ * NPM package, and it is automatically configured using the `APIFY_API_BASE_URL`, and `APIFY_TOKEN`
+ * environment variables. You can override the token via the available options. That's useful
+ * if you want to use the client as a different Apify user than the SDK internals are using.
*
* @param {object} [options]
- * @returns {ApifyClient}
- * @ignore
+ * @param {string} [options.token]
+ * @param {string} [options.maxRetries]
+ * @param {string} [options.minDelayBetweenRetriesMillis]
+ * @memberof module:Apify
+ * @function
+ * @name newClient
+ * @return {ApifyClient}
*/
export const newClient = (options = {}) => {
- // Only set baseUrl if overridden by env var, so that 'https://api.apify.com' is used by default.
- // This simplifies local development, which should run against production unless user wants otherwise.
+ ow(options, ow.object.exactShape({
+ baseUrl: ow.optional.string.url,
+ token: ow.optional.string,
+ maxRetries: ow.optional.number,
+ minDelayBetweenRetriesMillis: ow.optional.number,
+ }));
const {
baseUrl = process.env[ENV_VARS.API_BASE_URL],
token = process.env[ENV_VARS.TOKEN],
@@ -110,28 +122,16 @@ export const logSystemInfo = () => {
};
/**
- * Gets the default instance of the `ApifyClient` class provided
- * <a href="https://docs.apify.com/api/apify-client-js/latest"
- * target="_blank">apify-client</a> by the NPM package.
- * The instance is created automatically by the Apify SDK and it is configured using the
- * `APIFY_API_BASE_URL`, `APIFY_USER_ID` and `APIFY_TOKEN` environment variables.
- *
- * The instance is used for all underlying calls to the Apify API in functions such as
- * {@link Apify#getValue} or {@link Apify#call}.
- * The settings of the client can be globally altered by calling the
- * <a href="https://docs.apify.com/api/apify-client-js/latest#ApifyClient-setOptions"
- * target="_blank">`Apify.client.setOptions()`</a> function.
- * Beware that altering these settings might have unintended effects on the entire Apify SDK package.
+ * The default instance of `ApifyClient` used internally
+ * by the SDK.
*
* @type {*}
- *
- * @memberof module:Apify
- * @name client
+ * @ignore
*/
export const apifyClient = newClient();
/**
- * Gets the default instance of the `ApifyStorageLocal` class.
+ * The default instance of the `ApifyStorageLocal` class.
* The instance is created automatically by the Apify SDK and it is configured using the
* `APIFY_LOCAL_STORAGE_DIR` environment variable.
*
@@ -139,9 +139,7 @@ export const apifyClient = newClient();
* in Apify Storages such as {@link RequestQueue}.
*
* @type {*}
- *
- * @memberof module:Apify
- * @name storageLocal
+ * @ignore
*/
export const apifyStorageLocal = newStorageLocal();
diff --git a/website/i18n/en.json b/website/i18n/en.json
index 4e7298d40..066d1bfcd 100644
--- a/website/i18n/en.json
+++ b/website/i18n/en.json
@@ -17,6 +17,9 @@
"api/basic-crawler": {
"title": "BasicCrawler"
},
+ "api/cache-container": {
+ "title": "CacheContainer"
+ },
"api/cheerio-crawler": {
"title": "CheerioCrawler"
},
@@ -83,6 +86,9 @@
"api/statistics": {
"title": "Statistics"
},
+ "api/storage-manager": {
+ "title": "StorageManager"
+ },
"api/system-status": {
"title": "SystemStatus"
},
@@ -260,6 +266,12 @@
"typedefs/memory-info": {
"title": "MemoryInfo"
},
+ "typedefs/post-response": {
+ "title": "PostResponse"
+ },
+ "typedefs/post-response-inputs": {
+ "title": "PostResponseInputs"
+ },
"typedefs/prepare-request": {
"title": "PrepareRequest"
},