@decodo/sdk-ts 2.5.0 → 2.7.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/README.md CHANGED
@@ -436,6 +436,60 @@ const coffeeTaskId = batch.queries[0].id;
436
436
  await client.webScrapingApi.getResults(coffeeTaskId);
437
437
  ```
438
438
 
439
+ ### Search
440
+
441
+ Runs a web search and returns the results page. Requires `apiKey`:
442
+
443
+ ```typescript
444
+ const serp = await client.webScrapingApi.search({
445
+ query: 'laptop reviews',
446
+ geo: 'United States',
447
+ });
448
+ ```
449
+
450
+ ### Map
451
+
452
+ Discovers the links on a site. Requires `apiKey`:
453
+
454
+ ```typescript
455
+ const map = await client.webScrapingApi.map({
456
+ url: 'https://example.com',
457
+ limit: 100,
458
+ });
459
+
460
+ const links = map.results[0].content.links;
461
+ ```
462
+
463
+ ### Crawl
464
+
465
+ Starts a crawl, polls its status and returns every page once it settles. Requires `apiKey`:
466
+
467
+ ```typescript
468
+ const { status, pages } = await client.webScrapingApi.crawl(
469
+ { url: 'https://example.com', limit: 50, output: 'markdown' },
470
+ { pollInterval: 5_000, timeout: 600_000 },
471
+ );
472
+ ```
473
+
474
+ To drive a crawl step by step, start it and poll separately for status and pages:
475
+
476
+ ```typescript
477
+ const job = await client.webScrapingApi.startCrawl({ url: 'https://example.com' });
478
+
479
+ const progress = await client.webScrapingApi.getCrawlStatus(job.crawl_id);
480
+ console.log(progress.status); // 'pending' | 'done' | 'faulted' | 'cancelled' | 'truncated'
481
+
482
+ const pages = await client.webScrapingApi.getCrawlResults(job.crawl_id);
483
+
484
+ const firstPage = await client.webScrapingApi.listCrawlPages(job.crawl_id);
485
+
486
+ if (firstPage.next) {
487
+ await client.webScrapingApi.listCrawlPages(job.crawl_id, { cursor: firstPage.next });
488
+ }
489
+
490
+ await client.webScrapingApi.cancelCrawl(job.crawl_id);
491
+ ```
492
+
439
493
  ## Supported targets
440
494
 
441
495
  Each target accepts one primary input parameter (`url`, `query`, `product_id`, or `prompt`) together with optional configuration. The examples below show the minimum payload to call `client.webScrapingApi.scrape(...)`.
@@ -130,7 +130,7 @@ var HttpClient = class {
130
130
  errorBody = await res.json();
131
131
  } catch {
132
132
  }
133
- const message = errorBody?.message ?? `HTTP ${res.status}`;
133
+ const message = errorBody?.message ?? errorBody?.error ?? `HTTP ${res.status}`;
134
134
  if (res.status === 401 || res.status === 403) {
135
135
  throw new AuthenticationError(message);
136
136
  }
@@ -2754,6 +2754,17 @@ var BundledSchema = class _BundledSchema {
2754
2754
  }
2755
2755
  };
2756
2756
 
2757
+ // src/generated/data-api.ts
2758
+ var dataApiEndpoints = {
2759
+ runSearch: { method: "POST", path: "/v1/search" },
2760
+ mapSite: { method: "POST", path: "/v1/map" },
2761
+ listCrawls: { method: "GET", path: "/v1/crawl" },
2762
+ startCrawl: { method: "POST", path: "/v1/crawl" },
2763
+ listCrawlPages: { method: "GET", path: "/v1/crawl/{id}" },
2764
+ getCrawlStatus: { method: "GET", path: "/v1/crawl/{id}/status" },
2765
+ cancelCrawl: { method: "POST", path: "/v1/crawl/{id}/cancel" }
2766
+ };
2767
+
2757
2768
  // src/api/web-scraping-api.ts
2758
2769
  var SCRAPER_API_ROUTES = {
2759
2770
  scrape: "/v2/scrape",
@@ -2761,7 +2772,23 @@ var SCRAPER_API_ROUTES = {
2761
2772
  };
2762
2773
  var DATA_API_ROUTES = {
2763
2774
  scrape: "/v1/scrape",
2764
- task: "/v1/task"
2775
+ task: "/v1/task",
2776
+ dataApi: dataApiEndpoints
2777
+ };
2778
+ var DEFAULT_CRAWL_POLL_INTERVAL_MS = 5e3;
2779
+ var DEFAULT_CRAWL_TIMEOUT_MS = 36e5;
2780
+ var sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
2781
+ var nextCursor = (next) => {
2782
+ if (!next) {
2783
+ return void 0;
2784
+ }
2785
+ const cursor = new URL(next).searchParams.get("cursor");
2786
+ if (!cursor) {
2787
+ throw new Error(
2788
+ `Crawl results next link has no cursor: ${next}. Use getCrawlResults() to fetch every page.`
2789
+ );
2790
+ }
2791
+ return cursor;
2765
2792
  };
2766
2793
  var WebScrapingApi = class {
2767
2794
  http;
@@ -2802,6 +2829,96 @@ var WebScrapingApi = class {
2802
2829
  );
2803
2830
  return res ?? null;
2804
2831
  }
2832
+ dataApiPath(endpoint, caller) {
2833
+ if (!this.routes.dataApi) {
2834
+ throw new Error(
2835
+ `${caller}() requires webScrapingApi.apiKey. A Basic token cannot call the Data API.`
2836
+ );
2837
+ }
2838
+ return this.routes.dataApi[endpoint].path;
2839
+ }
2840
+ crawlPath(endpoint, crawlId, caller) {
2841
+ return this.dataApiPath(endpoint, caller).replace(
2842
+ "{id}",
2843
+ encodeURIComponent(crawlId)
2844
+ );
2845
+ }
2846
+ async search(params) {
2847
+ return this.http.post(
2848
+ this.dataApiPath("runSearch", "search"),
2849
+ params
2850
+ );
2851
+ }
2852
+ async map(params) {
2853
+ return this.http.post(
2854
+ this.dataApiPath("mapSite", "map"),
2855
+ params
2856
+ );
2857
+ }
2858
+ async startCrawl(params) {
2859
+ return this.http.post(
2860
+ this.dataApiPath("startCrawl", "startCrawl"),
2861
+ params
2862
+ );
2863
+ }
2864
+ async getCrawlStatus(crawlId) {
2865
+ return this.http.get(
2866
+ this.crawlPath("getCrawlStatus", crawlId, "getCrawlStatus")
2867
+ );
2868
+ }
2869
+ async listCrawlPages(crawlId, { cursor, content = true } = {}) {
2870
+ const query = {};
2871
+ if (content) {
2872
+ query.type = "content";
2873
+ }
2874
+ if (cursor) {
2875
+ query.cursor = cursor;
2876
+ }
2877
+ const res = await this.http.get(
2878
+ this.crawlPath("listCrawlPages", crawlId, "listCrawlPages"),
2879
+ query
2880
+ );
2881
+ return { pages: res.url, next: nextCursor(res.next), total: res.total };
2882
+ }
2883
+ async getCrawlResults(crawlId, { content = true } = {}) {
2884
+ const path = this.crawlPath("listCrawlPages", crawlId, "getCrawlResults");
2885
+ const pages = [];
2886
+ let query = content ? "?type=content" : "";
2887
+ for (; ; ) {
2888
+ const page = await this.http.get(
2889
+ `${path}${query}`
2890
+ );
2891
+ pages.push(...page.url);
2892
+ if (!page.next) {
2893
+ return pages;
2894
+ }
2895
+ query = new URL(page.next).search;
2896
+ }
2897
+ }
2898
+ async cancelCrawl(crawlId) {
2899
+ return this.http.post(
2900
+ this.crawlPath("cancelCrawl", crawlId, "cancelCrawl"),
2901
+ void 0
2902
+ );
2903
+ }
2904
+ async crawl(params, {
2905
+ pollInterval = DEFAULT_CRAWL_POLL_INTERVAL_MS,
2906
+ timeout = DEFAULT_CRAWL_TIMEOUT_MS
2907
+ } = {}) {
2908
+ const { crawl_id: crawlId } = await this.startCrawl(params);
2909
+ const deadline = Date.now() + timeout;
2910
+ let status = await this.getCrawlStatus(crawlId);
2911
+ while (status.status === "pending") {
2912
+ if (Date.now() + pollInterval > deadline) {
2913
+ throw new TimeoutError(
2914
+ `Crawl ${crawlId} did not settle within ${timeout}ms. It is still running; call cancelCrawl('${crawlId}') to stop it.`
2915
+ );
2916
+ }
2917
+ await sleep(pollInterval);
2918
+ status = await this.getCrawlStatus(crawlId);
2919
+ }
2920
+ return { status, pages: await this.getCrawlResults(crawlId) };
2921
+ }
2805
2922
  };
2806
2923
 
2807
2924
  // src/client.ts
@@ -777,6 +777,207 @@ export type DecodoSchema = {
777
777
  getSharedParameters: () => Record<string, unknown>;
778
778
  readonly version?: string;
779
779
  };
780
+ export interface RunSearchRequest {
781
+ query: string;
782
+ url?: string;
783
+ uule?: string;
784
+ gl?: string;
785
+ geo?: string;
786
+ locale?: string;
787
+ input_encoding?: string;
788
+ page?: number;
789
+ [k: string]: unknown;
790
+ }
791
+ export type RunSearchResponse = {
792
+ status?: "done";
793
+ url?: string;
794
+ organic?: {
795
+ link?: string;
796
+ original_link?: string;
797
+ title?: string;
798
+ description?: string;
799
+ extensions?: unknown[];
800
+ rank?: number;
801
+ [k: string]: unknown;
802
+ }[];
803
+ top_stories?: {
804
+ title?: string;
805
+ link?: string;
806
+ site?: string;
807
+ ago?: string;
808
+ rank?: number;
809
+ [k: string]: unknown;
810
+ }[];
811
+ people_also_ask?: {
812
+ question: string;
813
+ link?: string;
814
+ [k: string]: unknown;
815
+ }[];
816
+ related_searches?: {
817
+ query?: string;
818
+ link?: string;
819
+ [k: string]: unknown;
820
+ }[];
821
+ answer_box?: {
822
+ [k: string]: unknown;
823
+ };
824
+ search_parameters?: {
825
+ engine?: string;
826
+ query?: string;
827
+ locale?: string;
828
+ gl?: string;
829
+ [k: string]: unknown;
830
+ };
831
+ [k: string]: unknown;
832
+ } | {
833
+ status: "failed";
834
+ status_code?: number;
835
+ message: string;
836
+ task_id?: string;
837
+ [k: string]: unknown;
838
+ };
839
+ export interface MapSiteRequest {
840
+ url: string;
841
+ limit?: number;
842
+ sitemap?: "include" | "only" | "skip";
843
+ domain_filter?: "domain" | "subdomain" | "all";
844
+ geo?: string;
845
+ locale?: string;
846
+ JS_rendering?: boolean;
847
+ follow_redirects?: boolean;
848
+ select_paths?: string | string[];
849
+ exclude_paths?: string | string[];
850
+ [k: string]: unknown;
851
+ }
852
+ export interface MapSiteResponse {
853
+ results: {
854
+ content: {
855
+ links: {
856
+ url: string;
857
+ }[];
858
+ };
859
+ status_code: number;
860
+ url: string;
861
+ sitemap_status: "include" | "only" | "skip";
862
+ map_id: string;
863
+ created_at: string;
864
+ updated_at: string;
865
+ }[];
866
+ }
867
+ export interface StartCrawlRequest {
868
+ url: string;
869
+ limit?: number;
870
+ max_depth?: number;
871
+ sitemap?: "include" | "only" | "skip";
872
+ domain_filter?: "domain" | "subdomain" | "all";
873
+ output?: "markdown" | "html";
874
+ geo?: string;
875
+ locale?: string;
876
+ follow_redirects?: boolean;
877
+ JS_retry?: boolean;
878
+ max_duration?: number;
879
+ callback_url?: string;
880
+ passthrough?: string;
881
+ select_paths?: string | string[];
882
+ exclude_paths?: string | string[];
883
+ [k: string]: unknown;
884
+ }
885
+ export interface StartCrawlResponse {
886
+ target: "crawl";
887
+ url: string;
888
+ created_at: string;
889
+ crawl_id: string;
890
+ status: "pending" | "done" | "faulted" | "cancelled" | "truncated";
891
+ crawl_status: string;
892
+ results_tasks: string;
893
+ results_content: string;
894
+ }
895
+ export interface ListCrawlPagesResponse {
896
+ crawl_id: string;
897
+ url: {
898
+ task_id?: string;
899
+ status: "pending" | "done" | "faulted";
900
+ created_at: string;
901
+ updated_at: string;
902
+ url: string;
903
+ depth: number;
904
+ parent_url?: string;
905
+ result_key?: string;
906
+ content?: string;
907
+ content_error?: string;
908
+ crawl_status: string;
909
+ results_tasks: string;
910
+ results_content: string;
911
+ }[];
912
+ next?: string;
913
+ total?: number;
914
+ }
915
+ export interface GetCrawlStatusResponse {
916
+ target: "crawl";
917
+ url: string;
918
+ created_at: string;
919
+ crawl_id: string;
920
+ status: "pending" | "done" | "faulted" | "cancelled" | "truncated";
921
+ reason?: "max_duration" | "vendor_limit" | "no_pages_found";
922
+ sitemap_status: "include" | "only" | "skip";
923
+ output: "markdown" | "html";
924
+ discovered: number;
925
+ completed: number;
926
+ failed: number;
927
+ in_flight: number;
928
+ sitemaps: number;
929
+ requests: number;
930
+ }
931
+ export interface CancelCrawlResponse {
932
+ target: "crawl";
933
+ url: string;
934
+ created_at: string;
935
+ crawl_id: string;
936
+ status: "pending" | "done" | "faulted" | "cancelled" | "truncated";
937
+ crawl_status: string;
938
+ results_tasks: string;
939
+ results_content: string;
940
+ }
941
+ declare const dataApiEndpoints: {
942
+ readonly runSearch: {
943
+ readonly method: "POST";
944
+ readonly path: "/v1/search";
945
+ };
946
+ readonly mapSite: {
947
+ readonly method: "POST";
948
+ readonly path: "/v1/map";
949
+ };
950
+ readonly listCrawls: {
951
+ readonly method: "GET";
952
+ readonly path: "/v1/crawl";
953
+ };
954
+ readonly startCrawl: {
955
+ readonly method: "POST";
956
+ readonly path: "/v1/crawl";
957
+ };
958
+ readonly listCrawlPages: {
959
+ readonly method: "GET";
960
+ readonly path: "/v1/crawl/{id}";
961
+ };
962
+ readonly getCrawlStatus: {
963
+ readonly method: "GET";
964
+ readonly path: "/v1/crawl/{id}/status";
965
+ };
966
+ readonly cancelCrawl: {
967
+ readonly method: "POST";
968
+ readonly path: "/v1/crawl/{id}/cancel";
969
+ };
970
+ };
971
+ export type CrawlOptions = {
972
+ pollInterval?: number;
973
+ timeout?: number;
974
+ };
975
+ export type CrawlResultsOptions = {
976
+ content?: boolean;
977
+ };
978
+ export type CrawlPagesOptions = CrawlResultsOptions & {
979
+ cursor?: string;
980
+ };
780
981
  export type ResultEntry = {
781
982
  content: unknown;
782
983
  status_code: number;
@@ -834,9 +1035,20 @@ export type TaskMetadata = {
834
1035
  export type TaskResultsResponse = {
835
1036
  results: ResultEntry[];
836
1037
  };
1038
+ export type CrawlPage = ListCrawlPagesResponse["url"][number];
1039
+ export type CrawlPageList = {
1040
+ pages: CrawlPage[];
1041
+ next?: string;
1042
+ total?: number;
1043
+ };
1044
+ export type CrawlResult = {
1045
+ status: GetCrawlStatusResponse;
1046
+ pages: CrawlPage[];
1047
+ };
837
1048
  export type WebScrapingApiRoutes = {
838
1049
  scrape: string;
839
1050
  task: string;
1051
+ dataApi?: typeof dataApiEndpoints;
840
1052
  };
841
1053
  export declare class WebScrapingApi {
842
1054
  private readonly http;
@@ -849,6 +1061,16 @@ export declare class WebScrapingApi {
849
1061
  scrapeBatch(params: BatchRequest): Promise<BatchResponse>;
850
1062
  getStatus(taskId: string): Promise<TaskMetadata>;
851
1063
  getResults(taskId: string): Promise<TaskResultsResponse | null>;
1064
+ private dataApiPath;
1065
+ private crawlPath;
1066
+ search(params: RunSearchRequest): Promise<RunSearchResponse>;
1067
+ map(params: MapSiteRequest): Promise<MapSiteResponse>;
1068
+ startCrawl(params: StartCrawlRequest): Promise<StartCrawlResponse>;
1069
+ getCrawlStatus(crawlId: string): Promise<GetCrawlStatusResponse>;
1070
+ listCrawlPages(crawlId: string, { cursor, content }?: CrawlPagesOptions): Promise<CrawlPageList>;
1071
+ getCrawlResults(crawlId: string, { content }?: CrawlResultsOptions): Promise<CrawlPage[]>;
1072
+ cancelCrawl(crawlId: string): Promise<CancelCrawlResponse>;
1073
+ crawl(params: StartCrawlRequest, { pollInterval, timeout, }?: CrawlOptions): Promise<CrawlResult>;
852
1074
  }
853
1075
  export type WebScrapingApiCredentials = {
854
1076
  token: string;
@@ -1,10 +1,14 @@
1
1
  import { HttpClient } from '../http.js';
2
2
  import type { DecodoSchema } from '../schema/types.js';
3
+ import { dataApiEndpoints } from '../generated/data-api.js';
4
+ import type { CancelCrawlResponse, GetCrawlStatusResponse, MapSiteRequest, MapSiteResponse, RunSearchRequest, RunSearchResponse, StartCrawlRequest, StartCrawlResponse } from '../generated/data-api.js';
3
5
  import type { ScrapeRequest, BatchRequest } from '../generated/targets.js';
4
- import type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse } from '../types/responses.js';
6
+ import type { CrawlOptions, CrawlPagesOptions, CrawlResultsOptions } from '../types/requests.js';
7
+ import type { CrawlPage, CrawlPageList, CrawlResult, SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse } from '../types/responses.js';
5
8
  export type WebScrapingApiRoutes = {
6
9
  scrape: string;
7
10
  task: string;
11
+ dataApi?: typeof dataApiEndpoints;
8
12
  };
9
13
  export declare const SCRAPER_API_ROUTES: WebScrapingApiRoutes;
10
14
  export declare const DATA_API_ROUTES: WebScrapingApiRoutes;
@@ -19,4 +23,14 @@ export declare class WebScrapingApi {
19
23
  scrapeBatch(params: BatchRequest): Promise<BatchResponse>;
20
24
  getStatus(taskId: string): Promise<TaskMetadata>;
21
25
  getResults(taskId: string): Promise<TaskResultsResponse | null>;
26
+ private dataApiPath;
27
+ private crawlPath;
28
+ search(params: RunSearchRequest): Promise<RunSearchResponse>;
29
+ map(params: MapSiteRequest): Promise<MapSiteResponse>;
30
+ startCrawl(params: StartCrawlRequest): Promise<StartCrawlResponse>;
31
+ getCrawlStatus(crawlId: string): Promise<GetCrawlStatusResponse>;
32
+ listCrawlPages(crawlId: string, { cursor, content }?: CrawlPagesOptions): Promise<CrawlPageList>;
33
+ getCrawlResults(crawlId: string, { content }?: CrawlResultsOptions): Promise<CrawlPage[]>;
34
+ cancelCrawl(crawlId: string): Promise<CancelCrawlResponse>;
35
+ crawl(params: StartCrawlRequest, { pollInterval, timeout, }?: CrawlOptions): Promise<CrawlResult>;
22
36
  }
@@ -1,6 +1,7 @@
1
1
  import { prettifyError } from 'zod';
2
- import { ValidationError } from '../errors.js';
2
+ import { TimeoutError, ValidationError } from '../errors.js';
3
3
  import { BundledSchema } from '../schema/bundled-schema.js';
4
+ import { dataApiEndpoints } from '../generated/data-api.js';
4
5
  export const SCRAPER_API_ROUTES = {
5
6
  scrape: '/v2/scrape',
6
7
  task: '/v3/task',
@@ -8,6 +9,20 @@ export const SCRAPER_API_ROUTES = {
8
9
  export const DATA_API_ROUTES = {
9
10
  scrape: '/v1/scrape',
10
11
  task: '/v1/task',
12
+ dataApi: dataApiEndpoints,
13
+ };
14
+ const DEFAULT_CRAWL_POLL_INTERVAL_MS = 5_000;
15
+ const DEFAULT_CRAWL_TIMEOUT_MS = 3_600_000;
16
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
17
+ const nextCursor = (next) => {
18
+ if (!next) {
19
+ return undefined;
20
+ }
21
+ const cursor = new URL(next).searchParams.get('cursor');
22
+ if (!cursor) {
23
+ throw new Error(`Crawl results next link has no cursor: ${next}. Use getCrawlResults() to fetch every page.`);
24
+ }
25
+ return cursor;
11
26
  };
12
27
  export class WebScrapingApi {
13
28
  http;
@@ -46,4 +61,65 @@ export class WebScrapingApi {
46
61
  const res = await this.http.get(`${this.routes.task}/${taskId}/results`);
47
62
  return res ?? null;
48
63
  }
64
+ dataApiPath(endpoint, caller) {
65
+ if (!this.routes.dataApi) {
66
+ throw new Error(`${caller}() requires webScrapingApi.apiKey. A Basic token cannot call the Data API.`);
67
+ }
68
+ return this.routes.dataApi[endpoint].path;
69
+ }
70
+ crawlPath(endpoint, crawlId, caller) {
71
+ return this.dataApiPath(endpoint, caller).replace('{id}', encodeURIComponent(crawlId));
72
+ }
73
+ async search(params) {
74
+ return this.http.post(this.dataApiPath('runSearch', 'search'), params);
75
+ }
76
+ async map(params) {
77
+ return this.http.post(this.dataApiPath('mapSite', 'map'), params);
78
+ }
79
+ async startCrawl(params) {
80
+ return this.http.post(this.dataApiPath('startCrawl', 'startCrawl'), params);
81
+ }
82
+ async getCrawlStatus(crawlId) {
83
+ return this.http.get(this.crawlPath('getCrawlStatus', crawlId, 'getCrawlStatus'));
84
+ }
85
+ async listCrawlPages(crawlId, { cursor, content = true } = {}) {
86
+ const query = {};
87
+ if (content) {
88
+ query.type = 'content';
89
+ }
90
+ if (cursor) {
91
+ query.cursor = cursor;
92
+ }
93
+ const res = await this.http.get(this.crawlPath('listCrawlPages', crawlId, 'listCrawlPages'), query);
94
+ return { pages: res.url, next: nextCursor(res.next), total: res.total };
95
+ }
96
+ async getCrawlResults(crawlId, { content = true } = {}) {
97
+ const path = this.crawlPath('listCrawlPages', crawlId, 'getCrawlResults');
98
+ const pages = [];
99
+ let query = content ? '?type=content' : '';
100
+ for (;;) {
101
+ const page = await this.http.get(`${path}${query}`);
102
+ pages.push(...page.url);
103
+ if (!page.next) {
104
+ return pages;
105
+ }
106
+ query = new URL(page.next).search;
107
+ }
108
+ }
109
+ async cancelCrawl(crawlId) {
110
+ return this.http.post(this.crawlPath('cancelCrawl', crawlId, 'cancelCrawl'), undefined);
111
+ }
112
+ async crawl(params, { pollInterval = DEFAULT_CRAWL_POLL_INTERVAL_MS, timeout = DEFAULT_CRAWL_TIMEOUT_MS, } = {}) {
113
+ const { crawl_id: crawlId } = await this.startCrawl(params);
114
+ const deadline = Date.now() + timeout;
115
+ let status = await this.getCrawlStatus(crawlId);
116
+ while (status.status === 'pending') {
117
+ if (Date.now() + pollInterval > deadline) {
118
+ throw new TimeoutError(`Crawl ${crawlId} did not settle within ${timeout}ms. It is still running; call cancelCrawl('${crawlId}') to stop it.`);
119
+ }
120
+ await sleep(pollInterval);
121
+ status = await this.getCrawlStatus(crawlId);
122
+ }
123
+ return { status, pages: await this.getCrawlResults(crawlId) };
124
+ }
49
125
  }
@@ -0,0 +1,209 @@
1
+ export interface RunSearchRequest {
2
+ query: string;
3
+ url?: string;
4
+ uule?: string;
5
+ gl?: string;
6
+ geo?: string;
7
+ locale?: string;
8
+ input_encoding?: string;
9
+ page?: number;
10
+ [k: string]: unknown;
11
+ }
12
+ export type RunSearchResponse = {
13
+ status?: 'done';
14
+ url?: string;
15
+ organic?: {
16
+ link?: string;
17
+ original_link?: string;
18
+ title?: string;
19
+ description?: string;
20
+ extensions?: unknown[];
21
+ rank?: number;
22
+ [k: string]: unknown;
23
+ }[];
24
+ top_stories?: {
25
+ title?: string;
26
+ link?: string;
27
+ site?: string;
28
+ ago?: string;
29
+ rank?: number;
30
+ [k: string]: unknown;
31
+ }[];
32
+ people_also_ask?: {
33
+ question: string;
34
+ link?: string;
35
+ [k: string]: unknown;
36
+ }[];
37
+ related_searches?: {
38
+ query?: string;
39
+ link?: string;
40
+ [k: string]: unknown;
41
+ }[];
42
+ answer_box?: {
43
+ [k: string]: unknown;
44
+ };
45
+ search_parameters?: {
46
+ engine?: string;
47
+ query?: string;
48
+ locale?: string;
49
+ gl?: string;
50
+ [k: string]: unknown;
51
+ };
52
+ [k: string]: unknown;
53
+ } | {
54
+ status: 'failed';
55
+ status_code?: number;
56
+ message: string;
57
+ task_id?: string;
58
+ [k: string]: unknown;
59
+ };
60
+ export interface MapSiteRequest {
61
+ url: string;
62
+ limit?: number;
63
+ sitemap?: 'include' | 'only' | 'skip';
64
+ domain_filter?: 'domain' | 'subdomain' | 'all';
65
+ geo?: string;
66
+ locale?: string;
67
+ JS_rendering?: boolean;
68
+ follow_redirects?: boolean;
69
+ select_paths?: string | string[];
70
+ exclude_paths?: string | string[];
71
+ [k: string]: unknown;
72
+ }
73
+ export interface MapSiteResponse {
74
+ results: {
75
+ content: {
76
+ links: {
77
+ url: string;
78
+ }[];
79
+ };
80
+ status_code: number;
81
+ url: string;
82
+ sitemap_status: 'include' | 'only' | 'skip';
83
+ map_id: string;
84
+ created_at: string;
85
+ updated_at: string;
86
+ }[];
87
+ }
88
+ export interface ListCrawlsQuery {
89
+ status?: 'pending' | 'done' | 'faulted' | 'cancelled' | 'truncated';
90
+ cursor?: string;
91
+ }
92
+ export interface ListCrawlsResponse {
93
+ crawls: {
94
+ id: string;
95
+ url: string;
96
+ status: string;
97
+ output: 'markdown' | 'html';
98
+ }[];
99
+ cursor?: string;
100
+ }
101
+ export interface StartCrawlRequest {
102
+ url: string;
103
+ limit?: number;
104
+ max_depth?: number;
105
+ sitemap?: 'include' | 'only' | 'skip';
106
+ domain_filter?: 'domain' | 'subdomain' | 'all';
107
+ output?: 'markdown' | 'html';
108
+ geo?: string;
109
+ locale?: string;
110
+ follow_redirects?: boolean;
111
+ JS_retry?: boolean;
112
+ max_duration?: number;
113
+ callback_url?: string;
114
+ passthrough?: string;
115
+ select_paths?: string | string[];
116
+ exclude_paths?: string | string[];
117
+ [k: string]: unknown;
118
+ }
119
+ export interface StartCrawlResponse {
120
+ target: 'crawl';
121
+ url: string;
122
+ created_at: string;
123
+ crawl_id: string;
124
+ status: 'pending' | 'done' | 'faulted' | 'cancelled' | 'truncated';
125
+ crawl_status: string;
126
+ results_tasks: string;
127
+ results_content: string;
128
+ }
129
+ export interface ListCrawlPagesQuery {
130
+ type?: string;
131
+ cursor?: string;
132
+ }
133
+ export interface ListCrawlPagesResponse {
134
+ crawl_id: string;
135
+ url: {
136
+ task_id?: string;
137
+ status: 'pending' | 'done' | 'faulted';
138
+ created_at: string;
139
+ updated_at: string;
140
+ url: string;
141
+ depth: number;
142
+ parent_url?: string;
143
+ result_key?: string;
144
+ content?: string;
145
+ content_error?: string;
146
+ crawl_status: string;
147
+ results_tasks: string;
148
+ results_content: string;
149
+ }[];
150
+ next?: string;
151
+ total?: number;
152
+ }
153
+ export interface GetCrawlStatusResponse {
154
+ target: 'crawl';
155
+ url: string;
156
+ created_at: string;
157
+ crawl_id: string;
158
+ status: 'pending' | 'done' | 'faulted' | 'cancelled' | 'truncated';
159
+ reason?: 'max_duration' | 'vendor_limit' | 'no_pages_found';
160
+ sitemap_status: 'include' | 'only' | 'skip';
161
+ output: 'markdown' | 'html';
162
+ discovered: number;
163
+ completed: number;
164
+ failed: number;
165
+ in_flight: number;
166
+ sitemaps: number;
167
+ requests: number;
168
+ }
169
+ export interface CancelCrawlResponse {
170
+ target: 'crawl';
171
+ url: string;
172
+ created_at: string;
173
+ crawl_id: string;
174
+ status: 'pending' | 'done' | 'faulted' | 'cancelled' | 'truncated';
175
+ crawl_status: string;
176
+ results_tasks: string;
177
+ results_content: string;
178
+ }
179
+ export declare const dataApiEndpoints: {
180
+ readonly runSearch: {
181
+ readonly method: "POST";
182
+ readonly path: "/v1/search";
183
+ };
184
+ readonly mapSite: {
185
+ readonly method: "POST";
186
+ readonly path: "/v1/map";
187
+ };
188
+ readonly listCrawls: {
189
+ readonly method: "GET";
190
+ readonly path: "/v1/crawl";
191
+ };
192
+ readonly startCrawl: {
193
+ readonly method: "POST";
194
+ readonly path: "/v1/crawl";
195
+ };
196
+ readonly listCrawlPages: {
197
+ readonly method: "GET";
198
+ readonly path: "/v1/crawl/{id}";
199
+ };
200
+ readonly getCrawlStatus: {
201
+ readonly method: "GET";
202
+ readonly path: "/v1/crawl/{id}/status";
203
+ };
204
+ readonly cancelCrawl: {
205
+ readonly method: "POST";
206
+ readonly path: "/v1/crawl/{id}/cancel";
207
+ };
208
+ };
209
+ export type DataApiEndpoint = keyof typeof dataApiEndpoints;
@@ -0,0 +1,10 @@
1
+ // Auto-generated by src/codegen/data-api/generate-data-api.ts — do not edit
2
+ export const dataApiEndpoints = {
3
+ runSearch: { method: "POST", path: "/v1/search" },
4
+ mapSite: { method: "POST", path: "/v1/map" },
5
+ listCrawls: { method: "GET", path: "/v1/crawl" },
6
+ startCrawl: { method: "POST", path: "/v1/crawl" },
7
+ listCrawlPages: { method: "GET", path: "/v1/crawl/{id}" },
8
+ getCrawlStatus: { method: "GET", path: "/v1/crawl/{id}/status" },
9
+ cancelCrawl: { method: "POST", path: "/v1/crawl/{id}/cancel" },
10
+ };
package/build/esm/http.js CHANGED
@@ -47,7 +47,7 @@ export class HttpClient {
47
47
  catch {
48
48
  // response body wasn't JSON
49
49
  }
50
- const message = errorBody?.message ?? `HTTP ${res.status}`;
50
+ const message = errorBody?.message ?? errorBody?.error ?? `HTTP ${res.status}`;
51
51
  if (res.status === 401 || res.status === 403) {
52
52
  throw new AuthenticationError(message);
53
53
  }
@@ -9,5 +9,7 @@ export type { TargetString, TargetParamsMap, TargetMeta, ScrapeRequest, BatchReq
9
9
  export { targets, targetMeta, Target, } from './generated/targets.js';
10
10
  export { parameterMeta } from './generated/parameters.js';
11
11
  export type { ParameterMeta } from './generated/parameters.js';
12
- export type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse, TaskStatus, ResultEntry, } from './types/responses.js';
12
+ export type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse, TaskStatus, ResultEntry, CrawlPage, CrawlPageList, CrawlResult, } from './types/responses.js';
13
+ export type { CrawlOptions, CrawlPagesOptions, CrawlResultsOptions, } from './types/requests.js';
14
+ export type { RunSearchRequest, RunSearchResponse, MapSiteRequest, MapSiteResponse, StartCrawlRequest, StartCrawlResponse, GetCrawlStatusResponse, ListCrawlPagesResponse, CancelCrawlResponse, } from './generated/data-api.js';
13
15
  export { DecodoError, AuthenticationError, RateLimitError, ValidationError, TimeoutError, } from './errors.js';
@@ -1 +1,11 @@
1
1
  export type { ScrapeRequest, BatchRequest } from '../generated/targets.js';
2
+ export type CrawlOptions = {
3
+ pollInterval?: number;
4
+ timeout?: number;
5
+ };
6
+ export type CrawlResultsOptions = {
7
+ content?: boolean;
8
+ };
9
+ export type CrawlPagesOptions = CrawlResultsOptions & {
10
+ cursor?: string;
11
+ };
@@ -1,3 +1,4 @@
1
+ import type { GetCrawlStatusResponse, ListCrawlPagesResponse } from '../generated/data-api.js';
1
2
  export type ResultEntry = {
2
3
  content: unknown;
3
4
  status_code: number;
@@ -56,7 +57,18 @@ export type TaskResultsResponse = {
56
57
  results: ResultEntry[];
57
58
  };
58
59
  export type ErrorResponse = {
59
- status: string;
60
- message: string;
60
+ status?: string;
61
+ message?: string;
62
+ error?: string;
61
63
  errors?: unknown[];
62
64
  };
65
+ export type CrawlPage = ListCrawlPagesResponse['url'][number];
66
+ export type CrawlPageList = {
67
+ pages: CrawlPage[];
68
+ next?: string;
69
+ total?: number;
70
+ };
71
+ export type CrawlResult = {
72
+ status: GetCrawlStatusResponse;
73
+ pages: CrawlPage[];
74
+ };
package/package.json CHANGED
@@ -1,7 +1,19 @@
1
1
  {
2
2
  "name": "@decodo/sdk-ts",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "description": "Official TypeScript SDK for the Decodo APIs",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Decodo/sdk-ts.git"
9
+ },
10
+ "homepage": "https://github.com/Decodo/sdk-ts#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/Decodo/sdk-ts/issues"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
5
17
  "type": "module",
6
18
  "main": "./build/cjs/index.cjs",
7
19
  "module": "./build/esm/index.js",
@@ -21,18 +33,6 @@
21
33
  "files": [
22
34
  "build"
23
35
  ],
24
- "scripts": {
25
- "codegen": "tsx src/codegen/codegen.ts",
26
- "build": "pnpm run codegen && pnpm run build:esm && pnpm run build:cjs",
27
- "build:esm": "tsc -p tsconfig.build.json",
28
- "build:cjs": "rm -rf build/cjs && mkdir -p build/cjs && esbuild src/index.ts --bundle --platform=node --format=cjs --external:zod --outfile=build/cjs/index.cjs && dts-bundle-generator -o build/cjs/index.d.cts --project tsconfig.build.json build/esm/index.d.ts",
29
- "typecheck": "tsc --noEmit",
30
- "lint": "eslint .",
31
- "lint:fix": "eslint . --fix",
32
- "test": "vitest run",
33
- "test:watch": "vitest",
34
- "prepack": "pnpm run build:esm && pnpm run build:cjs"
35
- },
36
36
  "engines": {
37
37
  "node": ">=18.0.0"
38
38
  },
@@ -52,5 +52,15 @@
52
52
  "dependencies": {
53
53
  "zod": "^4"
54
54
  },
55
- "license": "MIT"
56
- }
55
+ "scripts": {
56
+ "codegen": "tsx src/codegen/codegen.ts",
57
+ "build": "pnpm run codegen && pnpm run build:esm && pnpm run build:cjs",
58
+ "build:esm": "tsc -p tsconfig.build.json",
59
+ "build:cjs": "rm -rf build/cjs && mkdir -p build/cjs && esbuild src/index.ts --bundle --platform=node --format=cjs --external:zod --outfile=build/cjs/index.cjs && dts-bundle-generator -o build/cjs/index.d.cts --project tsconfig.build.json build/esm/index.d.ts",
60
+ "typecheck": "tsc --noEmit",
61
+ "lint": "eslint .",
62
+ "lint:fix": "eslint . --fix",
63
+ "test": "vitest run",
64
+ "test:watch": "vitest"
65
+ }
66
+ }