@decodo/sdk-ts 2.5.0 → 2.6.1
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 +48 -0
- package/build/cjs/index.cjs +92 -1
- package/build/cjs/index.d.cts +213 -0
- package/build/esm/api/web-scraping-api.d.ts +14 -1
- package/build/esm/api/web-scraping-api.js +56 -1
- package/build/esm/generated/data-api.d.ts +209 -0
- package/build/esm/generated/data-api.js +10 -0
- package/build/esm/index.d.ts +3 -1
- package/build/esm/types/requests.d.ts +7 -0
- package/build/esm/types/responses.d.ts +6 -0
- package/package.json +25 -15
package/README.md
CHANGED
|
@@ -436,6 +436,54 @@ 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
|
+
await client.webScrapingApi.cancelCrawl(job.crawl_id);
|
|
485
|
+
```
|
|
486
|
+
|
|
439
487
|
## Supported targets
|
|
440
488
|
|
|
441
489
|
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(...)`.
|
package/build/cjs/index.cjs
CHANGED
|
@@ -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,8 +2772,12 @@ 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
|
|
2765
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));
|
|
2766
2781
|
var WebScrapingApi = class {
|
|
2767
2782
|
http;
|
|
2768
2783
|
schemas;
|
|
@@ -2802,6 +2817,82 @@ var WebScrapingApi = class {
|
|
|
2802
2817
|
);
|
|
2803
2818
|
return res ?? null;
|
|
2804
2819
|
}
|
|
2820
|
+
dataApiPath(endpoint, caller) {
|
|
2821
|
+
if (!this.routes.dataApi) {
|
|
2822
|
+
throw new Error(
|
|
2823
|
+
`${caller}() requires webScrapingApi.apiKey. A Basic token cannot call the Data API.`
|
|
2824
|
+
);
|
|
2825
|
+
}
|
|
2826
|
+
return this.routes.dataApi[endpoint].path;
|
|
2827
|
+
}
|
|
2828
|
+
crawlPath(endpoint, crawlId, caller) {
|
|
2829
|
+
return this.dataApiPath(endpoint, caller).replace(
|
|
2830
|
+
"{id}",
|
|
2831
|
+
encodeURIComponent(crawlId)
|
|
2832
|
+
);
|
|
2833
|
+
}
|
|
2834
|
+
async search(params) {
|
|
2835
|
+
return this.http.post(
|
|
2836
|
+
this.dataApiPath("runSearch", "search"),
|
|
2837
|
+
params
|
|
2838
|
+
);
|
|
2839
|
+
}
|
|
2840
|
+
async map(params) {
|
|
2841
|
+
return this.http.post(
|
|
2842
|
+
this.dataApiPath("mapSite", "map"),
|
|
2843
|
+
params
|
|
2844
|
+
);
|
|
2845
|
+
}
|
|
2846
|
+
async startCrawl(params) {
|
|
2847
|
+
return this.http.post(
|
|
2848
|
+
this.dataApiPath("startCrawl", "startCrawl"),
|
|
2849
|
+
params
|
|
2850
|
+
);
|
|
2851
|
+
}
|
|
2852
|
+
async getCrawlStatus(crawlId) {
|
|
2853
|
+
return this.http.get(
|
|
2854
|
+
this.crawlPath("getCrawlStatus", crawlId, "getCrawlStatus")
|
|
2855
|
+
);
|
|
2856
|
+
}
|
|
2857
|
+
async getCrawlResults(crawlId, { content = true } = {}) {
|
|
2858
|
+
const path = this.crawlPath("listCrawlPages", crawlId, "getCrawlResults");
|
|
2859
|
+
const pages = [];
|
|
2860
|
+
let query = content ? "?type=content" : "";
|
|
2861
|
+
for (; ; ) {
|
|
2862
|
+
const page = await this.http.get(
|
|
2863
|
+
`${path}${query}`
|
|
2864
|
+
);
|
|
2865
|
+
pages.push(...page.url);
|
|
2866
|
+
if (!page.next) {
|
|
2867
|
+
return pages;
|
|
2868
|
+
}
|
|
2869
|
+
query = new URL(page.next).search;
|
|
2870
|
+
}
|
|
2871
|
+
}
|
|
2872
|
+
async cancelCrawl(crawlId) {
|
|
2873
|
+
return this.http.post(
|
|
2874
|
+
this.crawlPath("cancelCrawl", crawlId, "cancelCrawl"),
|
|
2875
|
+
void 0
|
|
2876
|
+
);
|
|
2877
|
+
}
|
|
2878
|
+
async crawl(params, {
|
|
2879
|
+
pollInterval = DEFAULT_CRAWL_POLL_INTERVAL_MS,
|
|
2880
|
+
timeout = DEFAULT_CRAWL_TIMEOUT_MS
|
|
2881
|
+
} = {}) {
|
|
2882
|
+
const { crawl_id: crawlId } = await this.startCrawl(params);
|
|
2883
|
+
const deadline = Date.now() + timeout;
|
|
2884
|
+
let status = await this.getCrawlStatus(crawlId);
|
|
2885
|
+
while (status.status === "pending") {
|
|
2886
|
+
if (Date.now() + pollInterval > deadline) {
|
|
2887
|
+
throw new TimeoutError(
|
|
2888
|
+
`Crawl ${crawlId} did not settle within ${timeout}ms. It is still running; call cancelCrawl('${crawlId}') to stop it.`
|
|
2889
|
+
);
|
|
2890
|
+
}
|
|
2891
|
+
await sleep(pollInterval);
|
|
2892
|
+
status = await this.getCrawlStatus(crawlId);
|
|
2893
|
+
}
|
|
2894
|
+
return { status, pages: await this.getCrawlResults(crawlId) };
|
|
2895
|
+
}
|
|
2805
2896
|
};
|
|
2806
2897
|
|
|
2807
2898
|
// src/client.ts
|
package/build/cjs/index.d.cts
CHANGED
|
@@ -777,6 +777,204 @@ 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
|
+
};
|
|
780
978
|
export type ResultEntry = {
|
|
781
979
|
content: unknown;
|
|
782
980
|
status_code: number;
|
|
@@ -834,9 +1032,15 @@ export type TaskMetadata = {
|
|
|
834
1032
|
export type TaskResultsResponse = {
|
|
835
1033
|
results: ResultEntry[];
|
|
836
1034
|
};
|
|
1035
|
+
export type CrawlPage = ListCrawlPagesResponse["url"][number];
|
|
1036
|
+
export type CrawlResult = {
|
|
1037
|
+
status: GetCrawlStatusResponse;
|
|
1038
|
+
pages: CrawlPage[];
|
|
1039
|
+
};
|
|
837
1040
|
export type WebScrapingApiRoutes = {
|
|
838
1041
|
scrape: string;
|
|
839
1042
|
task: string;
|
|
1043
|
+
dataApi?: typeof dataApiEndpoints;
|
|
840
1044
|
};
|
|
841
1045
|
export declare class WebScrapingApi {
|
|
842
1046
|
private readonly http;
|
|
@@ -849,6 +1053,15 @@ export declare class WebScrapingApi {
|
|
|
849
1053
|
scrapeBatch(params: BatchRequest): Promise<BatchResponse>;
|
|
850
1054
|
getStatus(taskId: string): Promise<TaskMetadata>;
|
|
851
1055
|
getResults(taskId: string): Promise<TaskResultsResponse | null>;
|
|
1056
|
+
private dataApiPath;
|
|
1057
|
+
private crawlPath;
|
|
1058
|
+
search(params: RunSearchRequest): Promise<RunSearchResponse>;
|
|
1059
|
+
map(params: MapSiteRequest): Promise<MapSiteResponse>;
|
|
1060
|
+
startCrawl(params: StartCrawlRequest): Promise<StartCrawlResponse>;
|
|
1061
|
+
getCrawlStatus(crawlId: string): Promise<GetCrawlStatusResponse>;
|
|
1062
|
+
getCrawlResults(crawlId: string, { content }?: CrawlResultsOptions): Promise<CrawlPage[]>;
|
|
1063
|
+
cancelCrawl(crawlId: string): Promise<CancelCrawlResponse>;
|
|
1064
|
+
crawl(params: StartCrawlRequest, { pollInterval, timeout, }?: CrawlOptions): Promise<CrawlResult>;
|
|
852
1065
|
}
|
|
853
1066
|
export type WebScrapingApiCredentials = {
|
|
854
1067
|
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 {
|
|
6
|
+
import type { CrawlOptions, CrawlResultsOptions } from '../types/requests.js';
|
|
7
|
+
import type { CrawlPage, 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,13 @@ 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
|
+
getCrawlResults(crawlId: string, { content }?: CrawlResultsOptions): Promise<CrawlPage[]>;
|
|
33
|
+
cancelCrawl(crawlId: string): Promise<CancelCrawlResponse>;
|
|
34
|
+
crawl(params: StartCrawlRequest, { pollInterval, timeout, }?: CrawlOptions): Promise<CrawlResult>;
|
|
22
35
|
}
|
|
@@ -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,7 +9,11 @@ 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,
|
|
11
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));
|
|
12
17
|
export class WebScrapingApi {
|
|
13
18
|
http;
|
|
14
19
|
schemas;
|
|
@@ -46,4 +51,54 @@ export class WebScrapingApi {
|
|
|
46
51
|
const res = await this.http.get(`${this.routes.task}/${taskId}/results`);
|
|
47
52
|
return res ?? null;
|
|
48
53
|
}
|
|
54
|
+
dataApiPath(endpoint, caller) {
|
|
55
|
+
if (!this.routes.dataApi) {
|
|
56
|
+
throw new Error(`${caller}() requires webScrapingApi.apiKey. A Basic token cannot call the Data API.`);
|
|
57
|
+
}
|
|
58
|
+
return this.routes.dataApi[endpoint].path;
|
|
59
|
+
}
|
|
60
|
+
crawlPath(endpoint, crawlId, caller) {
|
|
61
|
+
return this.dataApiPath(endpoint, caller).replace('{id}', encodeURIComponent(crawlId));
|
|
62
|
+
}
|
|
63
|
+
async search(params) {
|
|
64
|
+
return this.http.post(this.dataApiPath('runSearch', 'search'), params);
|
|
65
|
+
}
|
|
66
|
+
async map(params) {
|
|
67
|
+
return this.http.post(this.dataApiPath('mapSite', 'map'), params);
|
|
68
|
+
}
|
|
69
|
+
async startCrawl(params) {
|
|
70
|
+
return this.http.post(this.dataApiPath('startCrawl', 'startCrawl'), params);
|
|
71
|
+
}
|
|
72
|
+
async getCrawlStatus(crawlId) {
|
|
73
|
+
return this.http.get(this.crawlPath('getCrawlStatus', crawlId, 'getCrawlStatus'));
|
|
74
|
+
}
|
|
75
|
+
async getCrawlResults(crawlId, { content = true } = {}) {
|
|
76
|
+
const path = this.crawlPath('listCrawlPages', crawlId, 'getCrawlResults');
|
|
77
|
+
const pages = [];
|
|
78
|
+
let query = content ? '?type=content' : '';
|
|
79
|
+
for (;;) {
|
|
80
|
+
const page = await this.http.get(`${path}${query}`);
|
|
81
|
+
pages.push(...page.url);
|
|
82
|
+
if (!page.next) {
|
|
83
|
+
return pages;
|
|
84
|
+
}
|
|
85
|
+
query = new URL(page.next).search;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async cancelCrawl(crawlId) {
|
|
89
|
+
return this.http.post(this.crawlPath('cancelCrawl', crawlId, 'cancelCrawl'), undefined);
|
|
90
|
+
}
|
|
91
|
+
async crawl(params, { pollInterval = DEFAULT_CRAWL_POLL_INTERVAL_MS, timeout = DEFAULT_CRAWL_TIMEOUT_MS, } = {}) {
|
|
92
|
+
const { crawl_id: crawlId } = await this.startCrawl(params);
|
|
93
|
+
const deadline = Date.now() + timeout;
|
|
94
|
+
let status = await this.getCrawlStatus(crawlId);
|
|
95
|
+
while (status.status === 'pending') {
|
|
96
|
+
if (Date.now() + pollInterval > deadline) {
|
|
97
|
+
throw new TimeoutError(`Crawl ${crawlId} did not settle within ${timeout}ms. It is still running; call cancelCrawl('${crawlId}') to stop it.`);
|
|
98
|
+
}
|
|
99
|
+
await sleep(pollInterval);
|
|
100
|
+
status = await this.getCrawlStatus(crawlId);
|
|
101
|
+
}
|
|
102
|
+
return { status, pages: await this.getCrawlResults(crawlId) };
|
|
103
|
+
}
|
|
49
104
|
}
|
|
@@ -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/index.d.ts
CHANGED
|
@@ -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, CrawlResult, } from './types/responses.js';
|
|
13
|
+
export type { CrawlOptions, 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,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;
|
|
@@ -60,3 +61,8 @@ export type ErrorResponse = {
|
|
|
60
61
|
message: string;
|
|
61
62
|
errors?: unknown[];
|
|
62
63
|
};
|
|
64
|
+
export type CrawlPage = ListCrawlPagesResponse['url'][number];
|
|
65
|
+
export type CrawlResult = {
|
|
66
|
+
status: GetCrawlStatusResponse;
|
|
67
|
+
pages: CrawlPage[];
|
|
68
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decodo/sdk-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.1",
|
|
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
|
-
"
|
|
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
|
+
}
|