apify 4.0.0-beta.25 → 4.0.0-beta.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ import type { RequestOptions, RequestTransform } from '@crawlee/core';
2
+ /** Request options that the `globs` and `pseudoUrls` input editors can attach to a single URL pattern. */
3
+ export interface UrlPatternRequestOptions extends Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'> {
4
+ }
5
+ /** A single item of an input schema array field using the `globs` editor. */
6
+ export interface GlobInput extends UrlPatternRequestOptions {
7
+ glob: string;
8
+ }
9
+ /** A single item of an input schema array field using the `pseudoUrls` editor. */
10
+ export interface PseudoUrlInput extends UrlPatternRequestOptions {
11
+ purl: string;
12
+ }
13
+ export interface UrlPatternFilters {
14
+ /** Value of an input schema field using the `globs` editor. */
15
+ globs?: readonly (string | GlobInput)[];
16
+ /** Value of an input schema field using the `pseudoUrls` editor. */
17
+ pseudoUrls?: readonly (string | PseudoUrlInput)[];
18
+ }
19
+ /**
20
+ * Turns the `globs` and `pseudoUrls` input editor values into a `transformRequestFunction` that skips requests
21
+ * matching no pattern (all requests pass when no pattern is given) and applies the options of the matched pattern.
22
+ * When a URL matches multiple patterns, the first one wins, with globs checked before pseudo-URLs. The matched
23
+ * pattern's options shallowly override the request's fields, including `userData` (no deep merge, like in v3).
24
+ *
25
+ * ```ts
26
+ * await enqueueLinks({ transformRequestFunction: createTransformRequestFunction({ globs: input.globs }) });
27
+ * ```
28
+ */
29
+ export declare function createTransformRequestFunction({ globs, pseudoUrls }: UrlPatternFilters): RequestTransform;
@@ -0,0 +1,52 @@
1
+ import { Minimatch } from 'minimatch';
2
+ import { purlToRegExp } from '@apify/pseudo_url';
3
+ /**
4
+ * Turns the `globs` and `pseudoUrls` input editor values into a `transformRequestFunction` that skips requests
5
+ * matching no pattern (all requests pass when no pattern is given) and applies the options of the matched pattern.
6
+ * When a URL matches multiple patterns, the first one wins, with globs checked before pseudo-URLs. The matched
7
+ * pattern's options shallowly override the request's fields, including `userData` (no deep merge, like in v3).
8
+ *
9
+ * ```ts
10
+ * await enqueueLinks({ transformRequestFunction: createTransformRequestFunction({ globs: input.globs }) });
11
+ * ```
12
+ */
13
+ export function createTransformRequestFunction({ globs = [], pseudoUrls = [] }) {
14
+ const patterns = [
15
+ ...globs.flatMap((item) => {
16
+ if (item == null) {
17
+ return [];
18
+ }
19
+ const { glob, ...options } = typeof item === 'string' ? { glob: item } : item;
20
+ const trimmedGlob = typeof glob === 'string' ? glob.trim() : '';
21
+ if (trimmedGlob.length === 0) {
22
+ return [];
23
+ }
24
+ const minimatch = new Minimatch(trimmedGlob, { nocase: true });
25
+ return [{ matches: (url) => minimatch.match(url), options }];
26
+ }),
27
+ ...pseudoUrls.flatMap((item) => {
28
+ if (item == null) {
29
+ return [];
30
+ }
31
+ const { purl, ...options } = typeof item === 'string' ? { purl: item } : item;
32
+ if (typeof purl !== 'string' || purl.trim().length === 0) {
33
+ return [];
34
+ }
35
+ let regexp;
36
+ try {
37
+ regexp = purlToRegExp(purl);
38
+ }
39
+ catch (cause) {
40
+ throw new Error(`Invalid pseudoUrl pattern '${purl}': ${cause.message}`, { cause });
41
+ }
42
+ return [{ matches: (url) => regexp.test(url), options }];
43
+ }),
44
+ ];
45
+ if (patterns.length === 0) {
46
+ return () => 'unchanged';
47
+ }
48
+ return (request) => {
49
+ const matched = patterns.find(({ matches }) => matches(request.url));
50
+ return matched ? { ...request, ...matched.options } : false;
51
+ };
52
+ }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './actor.js';
2
2
  export { ApifyStorageBackend, type ApifyStorageBackendOptions } from './apify_storage_backend.js';
3
3
  export type { RequestQueueAccessMode } from './apify_request_queue_backend.js';
4
4
  export { ArgumentValidationError } from './utils.js';
5
+ export { createTransformRequestFunction, type GlobInput, type PseudoUrlInput, type UrlPatternFilters, type UrlPatternRequestOptions, } from './enqueue_links_filters.js';
5
6
  export type { OpenStorageOptions, StorageAlias, StorageId, StorageName, StorageIdentifier, StorageIdentifierWithoutAlias, } from './storage.js';
6
7
  export { ChargeOptions, ChargeResult, ActorPricingInfo, ChargingManager } from './charging.js';
7
8
  export * from './configuration.js';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './actor.js';
2
2
  export { ApifyStorageBackend } from './apify_storage_backend.js';
3
3
  export { ArgumentValidationError } from './utils.js';
4
+ export { createTransformRequestFunction, } from './enqueue_links_filters.js';
4
5
  export { ChargingManager } from './charging.js';
5
6
  export * from './configuration.js';
6
7
  export * from './proxy_configuration.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apify",
3
- "version": "4.0.0-beta.25",
3
+ "version": "4.0.0-beta.26",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
6
  "node": ">=22.0.0"
@@ -53,16 +53,18 @@
53
53
  ]
54
54
  },
55
55
  "dependencies": {
56
- "@apify/consts": "^2.51.0",
56
+ "@apify/consts": "^2.57.0",
57
57
  "@apify/datastructures": "^2.0.3",
58
58
  "@apify/input_secrets": "^1.2.0",
59
- "@apify/log": "^2.4.3",
59
+ "@apify/log": "^2.5.48",
60
+ "@apify/pseudo_url": "^2.0.89",
60
61
  "@apify/timeout": "^0.4.0",
61
62
  "@apify/utilities": "^2.13.0",
62
63
  "@crawlee/core": "^4.0.0-beta.128",
63
64
  "@crawlee/types": "^4.0.0-beta.128",
64
65
  "@crawlee/utils": "^4.0.0-beta.128",
65
66
  "apify-client": "^2.23.4",
67
+ "minimatch": "^10.0.1",
66
68
  "semver": "^7.5.4",
67
69
  "tslib": "^2.6.2",
68
70
  "ws": "^8.18.0",