scrapfly-sdk 0.7.3 → 0.10.0

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.
Files changed (75) hide show
  1. package/esm/browserconfig.d.ts +59 -0
  2. package/esm/browserconfig.d.ts.map +1 -0
  3. package/esm/browserconfig.js +207 -0
  4. package/esm/client.d.ts +209 -1
  5. package/esm/client.d.ts.map +1 -1
  6. package/esm/client.js +902 -5
  7. package/esm/crawl.d.ts +118 -0
  8. package/esm/crawl.d.ts.map +1 -0
  9. package/esm/crawl.js +230 -0
  10. package/esm/crawlerconfig.d.ts +119 -0
  11. package/esm/crawlerconfig.d.ts.map +1 -0
  12. package/esm/crawlerconfig.js +398 -0
  13. package/esm/crawlerresult.d.ts +253 -0
  14. package/esm/crawlerresult.d.ts.map +1 -0
  15. package/esm/crawlerresult.js +327 -0
  16. package/esm/errors.d.ts +4 -0
  17. package/esm/errors.d.ts.map +1 -1
  18. package/esm/errors.js +6 -0
  19. package/esm/extractionconfig.d.ts +2 -0
  20. package/esm/extractionconfig.d.ts.map +1 -1
  21. package/esm/extractionconfig.js +10 -0
  22. package/esm/main.d.ts +9 -1
  23. package/esm/main.d.ts.map +1 -1
  24. package/esm/main.js +6 -1
  25. package/esm/monitoringconfig.d.ts +36 -0
  26. package/esm/monitoringconfig.d.ts.map +1 -0
  27. package/esm/monitoringconfig.js +10 -0
  28. package/esm/polyfill.d.ts +2 -0
  29. package/esm/polyfill.d.ts.map +1 -1
  30. package/esm/polyfill.js +12 -0
  31. package/esm/scrapeconfig.d.ts +27 -0
  32. package/esm/scrapeconfig.d.ts.map +1 -1
  33. package/esm/scrapeconfig.js +44 -1
  34. package/esm/screenshotconfig.d.ts +2 -2
  35. package/esm/screenshotconfig.d.ts.map +1 -1
  36. package/esm/utils.d.ts.map +1 -1
  37. package/esm/utils.js +16 -2
  38. package/package.json +1 -1
  39. package/script/browserconfig.d.ts +59 -0
  40. package/script/browserconfig.d.ts.map +1 -0
  41. package/script/browserconfig.js +211 -0
  42. package/script/client.d.ts +209 -1
  43. package/script/client.d.ts.map +1 -1
  44. package/script/client.js +901 -4
  45. package/script/crawl.d.ts +118 -0
  46. package/script/crawl.d.ts.map +1 -0
  47. package/script/crawl.js +257 -0
  48. package/script/crawlerconfig.d.ts +119 -0
  49. package/script/crawlerconfig.d.ts.map +1 -0
  50. package/script/crawlerconfig.js +402 -0
  51. package/script/crawlerresult.d.ts +253 -0
  52. package/script/crawlerresult.d.ts.map +1 -0
  53. package/script/crawlerresult.js +335 -0
  54. package/script/errors.d.ts +4 -0
  55. package/script/errors.d.ts.map +1 -1
  56. package/script/errors.js +9 -1
  57. package/script/extractionconfig.d.ts +2 -0
  58. package/script/extractionconfig.d.ts.map +1 -1
  59. package/script/extractionconfig.js +10 -0
  60. package/script/main.d.ts +9 -1
  61. package/script/main.d.ts.map +1 -1
  62. package/script/main.js +17 -1
  63. package/script/monitoringconfig.d.ts +36 -0
  64. package/script/monitoringconfig.d.ts.map +1 -0
  65. package/script/monitoringconfig.js +13 -0
  66. package/script/polyfill.d.ts +2 -0
  67. package/script/polyfill.d.ts.map +1 -1
  68. package/script/polyfill.js +14 -1
  69. package/script/scrapeconfig.d.ts +27 -0
  70. package/script/scrapeconfig.d.ts.map +1 -1
  71. package/script/scrapeconfig.js +44 -1
  72. package/script/screenshotconfig.d.ts +2 -2
  73. package/script/screenshotconfig.d.ts.map +1 -1
  74. package/script/utils.d.ts.map +1 -1
  75. package/script/utils.js +16 -2
@@ -0,0 +1,402 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CrawlerConfig = void 0;
4
+ const errors_js_1 = require("./errors.js");
5
+ const VALID_CONTENT_FORMATS = new Set([
6
+ 'html',
7
+ 'clean_html',
8
+ 'markdown',
9
+ 'text',
10
+ 'json',
11
+ 'extracted_data',
12
+ 'page_metadata',
13
+ ]);
14
+ const VALID_WEBHOOK_EVENTS = new Set([
15
+ 'crawler_started',
16
+ 'crawler_url_visited',
17
+ 'crawler_url_skipped',
18
+ 'crawler_url_discovered',
19
+ 'crawler_url_failed',
20
+ 'crawler_stopped',
21
+ 'crawler_cancelled',
22
+ 'crawler_finished',
23
+ ]);
24
+ /**
25
+ * Configuration for a Scrapfly Crawler API job.
26
+ *
27
+ * Mirrors the documented `POST /crawl` request body field-for-field. Fields left
28
+ * unset are dropped from the serialized output so the server applies its own defaults.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const config = new CrawlerConfig({
33
+ * url: 'https://web-scraping.dev/products',
34
+ * page_limit: 10,
35
+ * content_formats: ['markdown'],
36
+ * });
37
+ * ```
38
+ */
39
+ class CrawlerConfig {
40
+ constructor(options) {
41
+ Object.defineProperty(this, "url", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ Object.defineProperty(this, "page_limit", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "max_depth", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "max_duration", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ Object.defineProperty(this, "max_api_credit", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ Object.defineProperty(this, "exclude_paths", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: void 0
76
+ });
77
+ Object.defineProperty(this, "include_only_paths", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "ignore_base_path_restriction", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "follow_external_links", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(this, "allowed_external_domains", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+ Object.defineProperty(this, "follow_internal_subdomains", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ Object.defineProperty(this, "allowed_internal_subdomains", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ Object.defineProperty(this, "headers", {
114
+ enumerable: true,
115
+ configurable: true,
116
+ writable: true,
117
+ value: void 0
118
+ });
119
+ Object.defineProperty(this, "delay", {
120
+ enumerable: true,
121
+ configurable: true,
122
+ writable: true,
123
+ value: void 0
124
+ });
125
+ Object.defineProperty(this, "user_agent", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: void 0
130
+ });
131
+ Object.defineProperty(this, "max_concurrency", {
132
+ enumerable: true,
133
+ configurable: true,
134
+ writable: true,
135
+ value: void 0
136
+ });
137
+ Object.defineProperty(this, "rendering_delay", {
138
+ enumerable: true,
139
+ configurable: true,
140
+ writable: true,
141
+ value: void 0
142
+ });
143
+ Object.defineProperty(this, "use_sitemaps", {
144
+ enumerable: true,
145
+ configurable: true,
146
+ writable: true,
147
+ value: void 0
148
+ });
149
+ Object.defineProperty(this, "respect_robots_txt", {
150
+ enumerable: true,
151
+ configurable: true,
152
+ writable: true,
153
+ value: void 0
154
+ });
155
+ Object.defineProperty(this, "ignore_no_follow", {
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ value: void 0
160
+ });
161
+ Object.defineProperty(this, "cache", {
162
+ enumerable: true,
163
+ configurable: true,
164
+ writable: true,
165
+ value: void 0
166
+ });
167
+ Object.defineProperty(this, "cache_ttl", {
168
+ enumerable: true,
169
+ configurable: true,
170
+ writable: true,
171
+ value: void 0
172
+ });
173
+ Object.defineProperty(this, "cache_clear", {
174
+ enumerable: true,
175
+ configurable: true,
176
+ writable: true,
177
+ value: void 0
178
+ });
179
+ Object.defineProperty(this, "content_formats", {
180
+ enumerable: true,
181
+ configurable: true,
182
+ writable: true,
183
+ value: void 0
184
+ });
185
+ Object.defineProperty(this, "extraction_rules", {
186
+ enumerable: true,
187
+ configurable: true,
188
+ writable: true,
189
+ value: void 0
190
+ });
191
+ Object.defineProperty(this, "asp", {
192
+ enumerable: true,
193
+ configurable: true,
194
+ writable: true,
195
+ value: void 0
196
+ });
197
+ Object.defineProperty(this, "proxy_pool", {
198
+ enumerable: true,
199
+ configurable: true,
200
+ writable: true,
201
+ value: void 0
202
+ });
203
+ Object.defineProperty(this, "country", {
204
+ enumerable: true,
205
+ configurable: true,
206
+ writable: true,
207
+ value: void 0
208
+ });
209
+ Object.defineProperty(this, "webhook_name", {
210
+ enumerable: true,
211
+ configurable: true,
212
+ writable: true,
213
+ value: void 0
214
+ });
215
+ Object.defineProperty(this, "webhook_events", {
216
+ enumerable: true,
217
+ configurable: true,
218
+ writable: true,
219
+ value: void 0
220
+ });
221
+ this.validateOptions(options);
222
+ if (typeof options.url !== 'string' || options.url.trim() === '') {
223
+ throw new errors_js_1.CrawlerConfigError('url is required and must be a non-empty string');
224
+ }
225
+ this.url = options.url;
226
+ // Mutually exclusive path filters
227
+ if (options.exclude_paths && options.include_only_paths) {
228
+ throw new errors_js_1.CrawlerConfigError('exclude_paths and include_only_paths are mutually exclusive');
229
+ }
230
+ // Bounds checks (mirroring the public docs)
231
+ if (options.page_limit !== undefined && options.page_limit < 0) {
232
+ throw new errors_js_1.CrawlerConfigError('page_limit must be >= 0 (0 = unlimited)');
233
+ }
234
+ if (options.max_depth !== undefined && options.max_depth < 0) {
235
+ throw new errors_js_1.CrawlerConfigError('max_depth must be >= 0');
236
+ }
237
+ if (options.rendering_delay !== undefined && (options.rendering_delay < 0 || options.rendering_delay > 25000)) {
238
+ throw new errors_js_1.CrawlerConfigError('rendering_delay must be between 0 and 25000 (ms)');
239
+ }
240
+ if (options.delay !== undefined && (options.delay < 0 || options.delay > 15000)) {
241
+ throw new errors_js_1.CrawlerConfigError('delay must be between 0 and 15000 (ms)');
242
+ }
243
+ if (options.cache_ttl !== undefined && (options.cache_ttl < 0 || options.cache_ttl > 604800)) {
244
+ throw new errors_js_1.CrawlerConfigError('cache_ttl must be between 0 and 604800 (seconds, max 7 days)');
245
+ }
246
+ if (options.max_duration !== undefined && (options.max_duration < 15 || options.max_duration > 10800)) {
247
+ throw new errors_js_1.CrawlerConfigError('max_duration must be between 15 and 10800 (seconds)');
248
+ }
249
+ if (options.max_api_credit !== undefined && options.max_api_credit < 0) {
250
+ throw new errors_js_1.CrawlerConfigError('max_api_credit must be >= 0 (0 = no limit)');
251
+ }
252
+ if (options.exclude_paths && options.exclude_paths.length > 100) {
253
+ throw new errors_js_1.CrawlerConfigError('exclude_paths is limited to 100 entries');
254
+ }
255
+ if (options.include_only_paths && options.include_only_paths.length > 100) {
256
+ throw new errors_js_1.CrawlerConfigError('include_only_paths is limited to 100 entries');
257
+ }
258
+ if (options.allowed_external_domains && options.allowed_external_domains.length > 250) {
259
+ throw new errors_js_1.CrawlerConfigError('allowed_external_domains is limited to 250 entries');
260
+ }
261
+ if (options.allowed_internal_subdomains && options.allowed_internal_subdomains.length > 250) {
262
+ throw new errors_js_1.CrawlerConfigError('allowed_internal_subdomains is limited to 250 entries');
263
+ }
264
+ // Validate content_formats values
265
+ if (options.content_formats) {
266
+ for (const fmt of options.content_formats) {
267
+ if (!VALID_CONTENT_FORMATS.has(fmt)) {
268
+ throw new errors_js_1.CrawlerConfigError(`Invalid content_formats value: ${fmt}`);
269
+ }
270
+ }
271
+ }
272
+ // Validate webhook_events values
273
+ if (options.webhook_events) {
274
+ for (const ev of options.webhook_events) {
275
+ if (!VALID_WEBHOOK_EVENTS.has(ev)) {
276
+ throw new errors_js_1.CrawlerConfigError(`Invalid webhook_events value: ${ev}`);
277
+ }
278
+ }
279
+ }
280
+ // Assign all the optional fields by reference
281
+ this.page_limit = options.page_limit;
282
+ this.max_depth = options.max_depth;
283
+ this.max_duration = options.max_duration;
284
+ this.max_api_credit = options.max_api_credit;
285
+ this.exclude_paths = options.exclude_paths;
286
+ this.include_only_paths = options.include_only_paths;
287
+ this.ignore_base_path_restriction = options.ignore_base_path_restriction;
288
+ this.follow_external_links = options.follow_external_links;
289
+ this.allowed_external_domains = options.allowed_external_domains;
290
+ this.follow_internal_subdomains = options.follow_internal_subdomains;
291
+ this.allowed_internal_subdomains = options.allowed_internal_subdomains;
292
+ this.headers = options.headers;
293
+ this.delay = options.delay;
294
+ this.user_agent = options.user_agent;
295
+ this.max_concurrency = options.max_concurrency;
296
+ this.rendering_delay = options.rendering_delay;
297
+ this.use_sitemaps = options.use_sitemaps;
298
+ this.respect_robots_txt = options.respect_robots_txt;
299
+ this.ignore_no_follow = options.ignore_no_follow;
300
+ this.cache = options.cache;
301
+ this.cache_ttl = options.cache_ttl;
302
+ this.cache_clear = options.cache_clear;
303
+ this.content_formats = options.content_formats;
304
+ this.extraction_rules = options.extraction_rules;
305
+ this.asp = options.asp;
306
+ this.proxy_pool = options.proxy_pool;
307
+ this.country = options.country;
308
+ this.webhook_name = options.webhook_name;
309
+ this.webhook_events = options.webhook_events;
310
+ }
311
+ validateOptions(options) {
312
+ const validKeys = new Set([
313
+ 'url',
314
+ 'page_limit',
315
+ 'max_depth',
316
+ 'max_duration',
317
+ 'max_api_credit',
318
+ 'exclude_paths',
319
+ 'include_only_paths',
320
+ 'ignore_base_path_restriction',
321
+ 'follow_external_links',
322
+ 'allowed_external_domains',
323
+ 'follow_internal_subdomains',
324
+ 'allowed_internal_subdomains',
325
+ 'headers',
326
+ 'delay',
327
+ 'user_agent',
328
+ 'max_concurrency',
329
+ 'rendering_delay',
330
+ 'use_sitemaps',
331
+ 'respect_robots_txt',
332
+ 'ignore_no_follow',
333
+ 'cache',
334
+ 'cache_ttl',
335
+ 'cache_clear',
336
+ 'content_formats',
337
+ 'extraction_rules',
338
+ 'asp',
339
+ 'proxy_pool',
340
+ 'country',
341
+ 'webhook_name',
342
+ 'webhook_events',
343
+ ]);
344
+ for (const key in options) {
345
+ if (!validKeys.has(key)) {
346
+ throw new errors_js_1.CrawlerConfigError(`Invalid option provided: ${key}`);
347
+ }
348
+ }
349
+ }
350
+ /**
351
+ * Serialize to a flat object suitable for `JSON.stringify()` as a `POST /crawl` body.
352
+ *
353
+ * Unlike `ScrapeConfig.toApiParams()`, the crawler API takes its config as a JSON
354
+ * **body**, not URL query parameters. The API key is therefore NOT included here —
355
+ * it's added to the URL by the client.
356
+ *
357
+ * Fields left undefined are dropped so the server applies its own documented
358
+ * defaults (e.g. `respect_robots_txt=true`).
359
+ */
360
+ toApiParams() {
361
+ const params = { url: this.url };
362
+ const fields = [
363
+ 'page_limit',
364
+ 'max_depth',
365
+ 'max_duration',
366
+ 'max_api_credit',
367
+ 'exclude_paths',
368
+ 'include_only_paths',
369
+ 'ignore_base_path_restriction',
370
+ 'follow_external_links',
371
+ 'allowed_external_domains',
372
+ 'follow_internal_subdomains',
373
+ 'allowed_internal_subdomains',
374
+ 'headers',
375
+ 'delay',
376
+ 'user_agent',
377
+ 'max_concurrency',
378
+ 'rendering_delay',
379
+ 'use_sitemaps',
380
+ 'respect_robots_txt',
381
+ 'ignore_no_follow',
382
+ 'cache',
383
+ 'cache_ttl',
384
+ 'cache_clear',
385
+ 'content_formats',
386
+ 'extraction_rules',
387
+ 'asp',
388
+ 'proxy_pool',
389
+ 'country',
390
+ 'webhook_name',
391
+ 'webhook_events',
392
+ ];
393
+ for (const key of fields) {
394
+ const v = this[key];
395
+ if (v !== undefined) {
396
+ params[key] = v;
397
+ }
398
+ }
399
+ return params;
400
+ }
401
+ }
402
+ exports.CrawlerConfig = CrawlerConfig;
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Crawler job status values returned by the API.
3
+ *
4
+ * Note: there is **no** `COMPLETED` or `FAILED` value. A finished crawl always
5
+ * has `status === 'DONE'`; success vs. failure is signaled by `is_success`.
6
+ * `CANCELLED` is a separate terminal state set when the user cancels the job.
7
+ */
8
+ export type CrawlerStatusValue = 'PENDING' | 'RUNNING' | 'DONE' | 'CANCELLED';
9
+ /**
10
+ * Reasons documented for why a crawler stopped.
11
+ */
12
+ export type CrawlerStopReason = 'no_more_urls' | 'page_limit' | 'max_duration' | 'max_api_credit' | 'seed_url_failed' | 'user_cancelled' | 'crawler_error' | 'no_api_credit_left' | 'storage_error';
13
+ /**
14
+ * Per-job state metrics returned in the status response and webhook payloads.
15
+ *
16
+ * Note on nullable fields: while the crawler is in `PENDING` (before any
17
+ * worker has picked up the job), `start_time`, `stop_time`, and `stop_reason`
18
+ * are all `null`. They become populated once the crawl starts progressing.
19
+ * `duration` is always a number (0 when nothing has happened yet).
20
+ */
21
+ export interface CrawlerState {
22
+ urls_visited: number;
23
+ urls_extracted: number;
24
+ urls_failed: number;
25
+ urls_skipped: number;
26
+ urls_to_crawl: number;
27
+ api_credit_used: number;
28
+ duration: number;
29
+ /** `null` until the crawler stops. */
30
+ stop_reason: CrawlerStopReason | null;
31
+ /** `null` until the crawler actually starts processing (still PENDING). */
32
+ start_time: number | null;
33
+ /** `null` until the crawler stops. */
34
+ stop_time: number | null;
35
+ }
36
+ /**
37
+ * Wraps the JSON response of `GET /crawl/{uuid}/status`.
38
+ *
39
+ * Strict parsing: every field documented in the public API contract is
40
+ * required. A missing or wrong-typed field throws a clear error at parse
41
+ * time so API contract drift surfaces loud and early instead of silently
42
+ * producing zero-valued metrics.
43
+ */
44
+ export declare class CrawlerStatus {
45
+ crawler_uuid: string;
46
+ status: CrawlerStatusValue;
47
+ is_finished: boolean;
48
+ /** `null` while the crawler is still running; bool once terminal. */
49
+ is_success: boolean | null;
50
+ state: CrawlerState;
51
+ constructor(data: Record<string, any>);
52
+ /** True while the crawler is still working (PENDING or RUNNING). */
53
+ get isRunning(): boolean;
54
+ /** True if the crawler reached a terminal state successfully. */
55
+ get isComplete(): boolean;
56
+ /** True if the crawler reached a terminal state but failed. */
57
+ get isFailed(): boolean;
58
+ /** True if the crawler was cancelled by the user. */
59
+ get isCancelled(): boolean;
60
+ /**
61
+ * Rough progress estimate based on visited vs. extracted URLs (0–100).
62
+ * Returns 0 when nothing has been extracted yet.
63
+ */
64
+ get progressPct(): number;
65
+ }
66
+ /**
67
+ * Single URL entry from `GET /crawl/{uuid}/urls`.
68
+ *
69
+ * The endpoint streams one record per line as `text/plain`. For `visited`
70
+ * URLs each line is just the URL; for `failed` or `skipped` URLs the line is
71
+ * `url,reason` (reason is the first token after the first comma — URLs never
72
+ * contain a comma once percent-encoded, so this is unambiguous).
73
+ *
74
+ * Streaming text is used because the endpoint is expected to scale to
75
+ * millions of URLs per job; JSON is not a suitable wire format at that
76
+ * volume.
77
+ */
78
+ export interface CrawlerUrlEntry {
79
+ url: string;
80
+ /** Only populated when the caller passed an explicit `status` filter. */
81
+ status?: 'visited' | 'pending' | 'failed' | 'skipped';
82
+ /** Only present for `failed` or `skipped` URLs. */
83
+ reason?: string;
84
+ }
85
+ /**
86
+ * Wraps the streaming text response of `GET /crawl/{uuid}/urls`.
87
+ *
88
+ * The server returns one record per line with `Content-Type: text/plain`;
89
+ * this class parses that stream into a materialised list for caller
90
+ * convenience. For very large jobs, prefer {@link ScrapflyClient.crawlUrlsStream}
91
+ * (if provided) or call the endpoint via `client.fetch` directly and iterate
92
+ * the body as a stream.
93
+ *
94
+ * Pagination metadata fields (`page`, `per_page`) are echoes of the caller's
95
+ * request parameters — the wire protocol carries no global `total`, only the
96
+ * records in the current page. Use `urls.length` for the page size and
97
+ * request further pages by incrementing `page` until an empty response.
98
+ */
99
+ export declare class CrawlerUrls {
100
+ urls: CrawlerUrlEntry[];
101
+ page: number;
102
+ per_page: number;
103
+ constructor(urls: CrawlerUrlEntry[], page: number, perPage: number);
104
+ /**
105
+ * Parse the raw text body returned by `GET /crawl/{uuid}/urls`.
106
+ *
107
+ * - Empty lines are ignored (trailing newlines, blank records).
108
+ * - `visited` status → one URL per line, record has `{url, status: 'visited'}`.
109
+ * - `failed`/`skipped` status → `url,reason`, record includes `reason`.
110
+ * - When the caller didn't pass a `status` filter, the server defaults to
111
+ * `visited`; we pass that as the `statusHint` so every parsed record gets
112
+ * the right status tag.
113
+ *
114
+ * @param body Raw text body of the response.
115
+ * @param statusHint The status filter passed on the request (for tagging).
116
+ * @param page Caller-provided page (echoed on the response object).
117
+ * @param perPage Caller-provided per_page (echoed on the response object).
118
+ */
119
+ static fromText(body: string, statusHint: 'visited' | 'pending' | 'failed' | 'skipped', page: number, perPage: number): CrawlerUrls;
120
+ }
121
+ /**
122
+ * Wraps the JSON-mode response of `GET /crawl/{uuid}/contents` (i.e. `plain=false`).
123
+ *
124
+ * The `contents` field maps each URL to a map of format → content. When
125
+ * `plain=true` is used instead, {@link ScrapflyClient.crawlContents} returns
126
+ * a raw `string` directly rather than this class.
127
+ *
128
+ * Strict parsing: `contents` and `links` are both required per the documented
129
+ * contract. A response missing either field indicates an API contract change
130
+ * and throws at parse time rather than producing an empty envelope that looks
131
+ * valid.
132
+ */
133
+ export declare class CrawlerContents {
134
+ contents: Record<string, Record<string, string>>;
135
+ links: {
136
+ crawled_urls?: string;
137
+ next?: string | null;
138
+ prev?: string | null;
139
+ };
140
+ constructor(data: Record<string, any>);
141
+ }
142
+ /**
143
+ * Artifact type accepted by `GET /crawl/{uuid}/artifact?type=...`.
144
+ */
145
+ export type CrawlerArtifactType = 'warc' | 'har';
146
+ /**
147
+ * Holds the raw bytes of a downloaded WARC or HAR artifact.
148
+ *
149
+ * **Note on parsing:** the SDK does NOT bundle WARC or HAR parsers. Use a dedicated
150
+ * library (e.g. `warcio` on npm for WARC) if you need to walk the records. The
151
+ * `save()` method is provided for the common case of writing the artifact to disk.
152
+ */
153
+ export declare class CrawlerArtifact {
154
+ type: CrawlerArtifactType;
155
+ data: Uint8Array;
156
+ constructor(type: CrawlerArtifactType, data: Uint8Array);
157
+ /** Write the artifact to a file on disk. */
158
+ save(filepath: string): Promise<void>;
159
+ }
160
+ /**
161
+ * Fields common to every crawler webhook payload.
162
+ */
163
+ export interface CrawlerWebhookCommon {
164
+ crawler_uuid: string;
165
+ project: string;
166
+ env: string;
167
+ action: string;
168
+ state: CrawlerState;
169
+ }
170
+ /**
171
+ * Webhook for the four "lifecycle" events that share an identical payload shape:
172
+ * `crawler_started`, `crawler_stopped`, `crawler_cancelled`, `crawler_finished`.
173
+ *
174
+ * Verified against `apps/scrapfly/web-app/src/Template/Docs/crawler-api/webhooks_example/`.
175
+ */
176
+ export interface CrawlerLifecycleWebhook {
177
+ event: 'crawler_started' | 'crawler_stopped' | 'crawler_cancelled' | 'crawler_finished';
178
+ payload: CrawlerWebhookCommon & {
179
+ seed_url: string;
180
+ links: {
181
+ status: string;
182
+ };
183
+ };
184
+ }
185
+ /** Payload for `crawler_url_visited` events. */
186
+ export interface CrawlerUrlVisitedWebhook {
187
+ event: 'crawler_url_visited';
188
+ payload: CrawlerWebhookCommon & {
189
+ url: string;
190
+ scrape: {
191
+ status_code: number;
192
+ country?: string;
193
+ log_uuid?: string;
194
+ log_url?: string;
195
+ content: Record<string, string>;
196
+ };
197
+ };
198
+ }
199
+ /** Payload for `crawler_url_skipped` events. */
200
+ export interface CrawlerUrlSkippedWebhook {
201
+ event: 'crawler_url_skipped';
202
+ payload: CrawlerWebhookCommon & {
203
+ /** Map of skipped URL → reason (e.g. `"page_limit"`). */
204
+ urls: Record<string, string>;
205
+ };
206
+ }
207
+ /** Payload for `crawler_url_discovered` events. */
208
+ export interface CrawlerUrlDiscoveredWebhook {
209
+ event: 'crawler_url_discovered';
210
+ payload: CrawlerWebhookCommon & {
211
+ origin: string;
212
+ discovered_urls: string[];
213
+ };
214
+ }
215
+ /** Payload for `crawler_url_failed` events. */
216
+ export interface CrawlerUrlFailedWebhook {
217
+ event: 'crawler_url_failed';
218
+ payload: CrawlerWebhookCommon & {
219
+ url: string;
220
+ error: string;
221
+ scrape_config: Record<string, any>;
222
+ links: {
223
+ log: string | null;
224
+ };
225
+ };
226
+ }
227
+ /**
228
+ * Discriminated union of every crawler webhook payload type.
229
+ *
230
+ * Use {@link parseCrawlerWebhook} to safely turn an `unknown` body into one of these.
231
+ */
232
+ export type CrawlerWebhookPayload = CrawlerLifecycleWebhook | CrawlerUrlVisitedWebhook | CrawlerUrlSkippedWebhook | CrawlerUrlDiscoveredWebhook | CrawlerUrlFailedWebhook;
233
+ /**
234
+ * Parse a raw webhook body (JSON-decoded object) into a typed crawler webhook payload.
235
+ *
236
+ * Throws `Error` if the body is not an object, has no `event` field, or carries an
237
+ * unknown event name. Use this in your webhook handler after `JSON.parse()`.
238
+ *
239
+ * @example
240
+ * ```ts
241
+ * import { parseCrawlerWebhook } from '@scrapfly/scrapfly-sdk';
242
+ *
243
+ * app.post('/webhook', async (req, res) => {
244
+ * const body = await req.json();
245
+ * const webhook = parseCrawlerWebhook(body);
246
+ * if (webhook.event === 'crawler_finished') {
247
+ * console.log('done', webhook.payload.state.urls_visited);
248
+ * }
249
+ * });
250
+ * ```
251
+ */
252
+ export declare function parseCrawlerWebhook(body: unknown): CrawlerWebhookPayload;
253
+ //# sourceMappingURL=crawlerresult.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crawlerresult.d.ts","sourceRoot":"","sources":["../src/crawlerresult.ts"],"names":[],"mappings":"AAoDA;;;;;;GAMG;AACH,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,SAAS,GAAG,MAAM,GAAG,WAAW,CAAC;AAE9E;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,cAAc,GACd,YAAY,GACZ,cAAc,GACd,gBAAgB,GAChB,iBAAiB,GACjB,gBAAgB,GAChB,eAAe,GACf,oBAAoB,GACpB,eAAe,CAAC;AAEpB;;;;;;;GAOG;AACH,MAAM,WAAW,YAAY;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,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