@datafuel/sdk 0.1.0 → 0.2.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.
package/src/index.ts CHANGED
@@ -17,6 +17,9 @@
17
17
  * | A start URL, many pages | `crawl` / `startCrawl` | `crawl` does |
18
18
  * | A list of known URLs | `runJob` / `createJob` | `runJob` does |
19
19
  * | A question for an AI engine | `ask` | yes |
20
+ * | A Google search | `search` | yes |
21
+ * | Many prompts or searches | `runAskJob` / `runSearchJob` | yes |
22
+ * | Earlier jobs, tasks, usage | `listJobs` / `listTasks` / `analytics` / `transactions` | yes |
20
23
  *
21
24
  * Start with plain `scrape`. Turn on `jsRendering` only when the page comes
22
25
  * back empty: it is slower and costs five times the credits on a Basic proxy.
@@ -27,15 +30,17 @@
27
30
  export { DataFuel } from "./client.js";
28
31
  export type { ClientOptions } from "./client.js";
29
32
  export { DEFAULT_BASE_URL, VERSION } from "./core.js";
30
- export type { AskOptions, CrawlOptions, MapOptions } from "./core.js";
33
+ export type { AskOptions, CrawlOptions, MapOptions, SearchOptions } from "./core.js";
31
34
  export {
32
35
  APIError,
33
36
  Blocked,
34
37
  DataFuelError,
35
38
  EngineUnavailable,
39
+ Forbidden,
36
40
  IdempotencyKeyReused,
37
41
  InsufficientCredits,
38
42
  InvalidAttributes,
43
+ JobNotCancellable,
39
44
  ModuleUnavailable,
40
45
  NoApiKey,
41
46
  NotFound,
@@ -46,10 +51,15 @@ export {
46
51
  Unavailable,
47
52
  WaitTimeout,
48
53
  } from "./errors.js";
54
+ export type { ErrorCode } from "./errors.js";
49
55
  export { Capabilities, CrawlPage, isDone, Result } from "./models.js";
50
56
  export type {
51
57
  AI,
58
+ Analytics,
59
+ AnalyticsCounts,
60
+ AnalyticsOptions,
52
61
  CallOptions,
62
+ CancelResult,
53
63
  Capability,
54
64
  CrawlResult,
55
65
  CrawlResultsPage,
@@ -57,13 +67,31 @@ export type {
57
67
  Engine,
58
68
  Format,
59
69
  JobResults,
70
+ JobsPage,
60
71
  JobStatus,
72
+ JobSummary,
73
+ JsInstruction,
74
+ JsInstructionArg,
61
75
  Link,
76
+ ListOptions,
77
+ ListTasksOptions,
62
78
  Payload,
63
79
  Profile,
64
80
  Proxy,
81
+ ProxyCountry,
82
+ ProxyLocation,
65
83
  ProxyType,
66
84
  ScrapeOptions,
85
+ PreviousPeriod,
67
86
  SiteMap,
68
87
  Status,
88
+ StatusCodeBreakdown,
89
+ TasksPage,
90
+ TaskSummary,
91
+ TaskType,
92
+ Transaction,
93
+ TransactionOperation,
94
+ TransactionsOptions,
95
+ TransactionsPage,
96
+ TransactionSum,
69
97
  } from "./models.js";
package/src/models.ts CHANGED
@@ -43,7 +43,8 @@ export interface Proxy {
43
43
  asn?: string;
44
44
  /**
45
45
  * Sticky session: the same exit across requests, `ttl` in seconds. Read by
46
- * `scrape` and `map` only, and sent in attributes rather than the envelope.
46
+ * `scrape`, `map`, URL jobs and crawls, and sent in attributes rather than
47
+ * the envelope.
47
48
  */
48
49
  sessionId?: string;
49
50
  ttl?: number;
@@ -74,6 +75,7 @@ export interface ScrapeOptions {
74
75
  jsRendering?: boolean;
75
76
  waitFor?: string;
76
77
  waitForTimeoutMs?: number;
78
+ /** An object keyed by action, e.g. `{ click: "#more" }`. `df.jsInstructions()` lists them. */
77
79
  jsInstructions?: unknown;
78
80
  blockResource?: string;
79
81
  /** Markdown only: always render just the `<main>` / `<article>` container. */
@@ -279,6 +281,9 @@ export interface CrawlStatus {
279
281
  total_cost: number;
280
282
  /** Whether the crawl reached a final state. */
281
283
  done: boolean;
284
+ /** RFC 3339. */
285
+ created_at?: string;
286
+ updated_at?: string;
282
287
  }
283
288
 
284
289
  /** One page of crawl results. */
@@ -305,6 +310,12 @@ export interface JobStatus {
305
310
  done: boolean;
306
311
  }
307
312
 
313
+ /** A cancelled job or crawl: its final progress and what was refunded. */
314
+ export interface CancelResult extends JobStatus {
315
+ refunded_tasks: number;
316
+ refunded_credits: number;
317
+ }
318
+
308
319
  /** Every task of a job. A job completes even when some of its tasks failed. */
309
320
  export interface JobResults {
310
321
  id: string;
@@ -314,6 +325,189 @@ export interface JobResults {
314
325
  tasks: Result[];
315
326
  }
316
327
 
328
+ /** A task type, as the list and analytics filters take it. */
329
+ export type TaskType = "unlocker" | "llm_scraping" | "serp" | "map" | "crawl" | (string & {});
330
+
331
+ /** Filters shared by {@link DataFuel.listJobs} and {@link DataFuel.listTasks}. */
332
+ export interface ListOptions {
333
+ status?: Status;
334
+ type?: TaskType;
335
+ /** Created on or after this day (UTC). A Date is sent as its UTC day. */
336
+ startDate?: string | Date;
337
+ /** Created on or before this day (UTC, inclusive). */
338
+ endDate?: string | Date;
339
+ /** Items per page. API default 50, max 200. */
340
+ limit?: number;
341
+ /** `nextCursor` of the previous page. */
342
+ cursor?: string;
343
+ }
344
+
345
+ /** {@link ListOptions} plus the job or crawl the tasks belong to. */
346
+ export interface ListTasksOptions extends ListOptions {
347
+ jobId?: string;
348
+ }
349
+
350
+ /** A job or crawl in a list, with the same counters as {@link JobStatus}. */
351
+ export interface JobSummary extends JobStatus {
352
+ id: string;
353
+ /** `crawl` for a crawl, otherwise the task type of the batch. */
354
+ type: TaskType;
355
+ /** RFC 3339. */
356
+ created_at: string;
357
+ updated_at: string;
358
+ }
359
+
360
+ /** One page of jobs, newest first. */
361
+ export interface JobsPage {
362
+ jobs: JobSummary[];
363
+ /** Absent on the last page. */
364
+ nextCursor?: string;
365
+ }
366
+
367
+ /** A task in a list. It has no result: fetch that with {@link DataFuel.getTask}. */
368
+ export interface TaskSummary {
369
+ id: string;
370
+ /** `null` for a task created on its own rather than by a job or crawl. */
371
+ job_id: string | null;
372
+ type: TaskType;
373
+ status: Status;
374
+ /** Absent for `llm_scraping` and `serp`. */
375
+ url?: string;
376
+ /** Charged when queued; a failed task is refunded. */
377
+ credit_cost: number;
378
+ /** RFC 3339. */
379
+ created_at: string;
380
+ processed_at?: string;
381
+ failed_at?: string;
382
+ }
383
+
384
+ /** One page of tasks, newest first. */
385
+ export interface TasksPage {
386
+ tasks: TaskSummary[];
387
+ /** Absent on the last page. */
388
+ nextCursor?: string;
389
+ }
390
+
391
+ /** What moved credits on the account. */
392
+ export type TransactionOperation =
393
+ | "plan_assignment"
394
+ | "purchase"
395
+ | "usage"
396
+ | "refund"
397
+ | "topup"
398
+ | "expiry"
399
+ | "adjustment"
400
+ | (string & {});
401
+
402
+ /** Filters and paging for {@link DataFuel.transactions}. */
403
+ export interface TransactionsOptions {
404
+ operation?: TransactionOperation;
405
+ startDate?: string | Date;
406
+ endDate?: string | Date;
407
+ /** 1-based. API default 1. */
408
+ page?: number;
409
+ /** API default 10, max 200. */
410
+ limit?: number;
411
+ }
412
+
413
+ /** One credit movement. `amount` is negative for usage and expiry. */
414
+ export interface Transaction {
415
+ id: number;
416
+ amount: number;
417
+ operation: TransactionOperation;
418
+ /** What `reference_id` points to, e.g. `task_id` or `job_id`. */
419
+ reference_type: string;
420
+ reference_id: string;
421
+ /** The balance right after this movement. */
422
+ balance_after?: number;
423
+ /** RFC 3339. */
424
+ created_at: string;
425
+ }
426
+
427
+ /** Total of one operation over the whole filtered range, not just the page. */
428
+ export interface TransactionSum {
429
+ operation: TransactionOperation;
430
+ total: number;
431
+ count: number;
432
+ }
433
+
434
+ /** One page of credit movements, newest first. */
435
+ export interface TransactionsPage {
436
+ transactions: Transaction[];
437
+ /** Movements matching the filters across all pages. */
438
+ total_count: number;
439
+ sums: TransactionSum[];
440
+ }
441
+
442
+ /** Range and grouping for {@link DataFuel.analytics}. */
443
+ export interface AnalyticsOptions {
444
+ /** API default 30 days ago. The range may span at most 365 days. */
445
+ startDate?: string | Date;
446
+ /** Inclusive. API default today. */
447
+ endDate?: string | Date;
448
+ /** Time series bucket. API default `daily`. */
449
+ interval?: "hourly" | "daily" | "weekly" | "monthly";
450
+ /** Restrict to one task type. */
451
+ module?: TaskType;
452
+ }
453
+
454
+ /** Task counts and net credits of one slice. Failed tasks are refunded and count 0 credits. */
455
+ export interface AnalyticsCounts {
456
+ total: number;
457
+ completed: number;
458
+ failed: number;
459
+ credits_used: number;
460
+ }
461
+
462
+ /** Tasks by the HTTP status the target answered. `status_code` 0 means no answer (timeout, DNS). */
463
+ export interface StatusCodeBreakdown {
464
+ status_code: number;
465
+ count: number;
466
+ completed: number;
467
+ failed: number;
468
+ credits_used: number;
469
+ avg_credits_per_request: number;
470
+ }
471
+
472
+ /** The same figures for the equally long period before, and the change in percent. */
473
+ export interface PreviousPeriod {
474
+ credits_used: number;
475
+ fulfilled_requests: number;
476
+ failed_requests: number;
477
+ failed_percentage: number;
478
+ efficiency_score: number;
479
+ credits_used_change: number;
480
+ fulfilled_requests_change: number;
481
+ failed_requests_change: number;
482
+ efficiency_score_change: number;
483
+ }
484
+
485
+ /** Usage over a date range. Percentages are 0-100. */
486
+ export interface Analytics {
487
+ summary: {
488
+ total_tasks: number;
489
+ credits_used: number;
490
+ fulfilled_requests: number;
491
+ failed_requests: number;
492
+ failed_percentage: number;
493
+ success_rate: number;
494
+ efficiency_score: number;
495
+ avg_credits_per_request: number;
496
+ avg_duration_ms: number;
497
+ previous_period?: PreviousPeriod | null;
498
+ };
499
+ timeseries: (AnalyticsCounts & { period: string })[];
500
+ by_module: (AnalyticsCounts & {
501
+ module: string;
502
+ success_rate: number;
503
+ avg_credits_per_request: number;
504
+ avg_duration_ms: number;
505
+ status_codes: StatusCodeBreakdown[];
506
+ })[];
507
+ top_targets: (AnalyticsCounts & { target: string })[];
508
+ by_status_code: StatusCodeBreakdown[];
509
+ }
510
+
317
511
  /** The account behind the API key. */
318
512
  export interface Profile {
319
513
  email: string;
@@ -324,6 +518,39 @@ export interface Profile {
324
518
  monthly_credit_limit: number;
325
519
  }
326
520
 
521
+ /** One argument of a browser action. */
522
+ export interface JsInstructionArg {
523
+ name: string;
524
+ type: string;
525
+ values?: string[];
526
+ required: boolean;
527
+ }
528
+
529
+ /** One browser action `jsInstructions` accepts. */
530
+ export interface JsInstruction {
531
+ action: string;
532
+ description: string;
533
+ /** Shape of the value: scalar, array or object. */
534
+ value: string;
535
+ args: JsInstructionArg[];
536
+ /** Whether it can target an element inside an iframe. */
537
+ iframe: boolean;
538
+ example: unknown;
539
+ }
540
+
541
+ /** A named proxy location: a city, or an ASN. */
542
+ export interface ProxyLocation {
543
+ code: string;
544
+ name: string;
545
+ }
546
+
547
+ /** A proxy country with its regions and their cities. */
548
+ export interface ProxyCountry {
549
+ code: string;
550
+ name: string;
551
+ regions: { code: string; name: string; cities: ProxyLocation[] }[];
552
+ }
553
+
327
554
  /** One task type or LLM engine, and whether it accepts new work. */
328
555
  export interface Capability {
329
556
  name: string;