@decodo/sdk-ts 1.1.2 → 2.0.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 +136 -1
- package/build/cjs/api/web-scraping-api.d.ts +13 -0
- package/build/cjs/api/web-scraping-api.js +41 -0
- package/build/cjs/client.d.ts +11 -0
- package/build/cjs/client.js +34 -0
- package/build/cjs/errors.d.ts +18 -0
- package/build/cjs/errors.js +44 -0
- package/build/cjs/generated/parameters.d.ts +11 -0
- package/build/cjs/generated/parameters.js +85 -0
- package/build/cjs/generated/request-schemas.d.ts +2123 -0
- package/build/cjs/generated/request-schemas.js +2431 -0
- package/build/cjs/generated/targets.d.ts +735 -0
- package/build/cjs/generated/targets.js +343 -0
- package/build/cjs/http.d.ts +22 -0
- package/build/cjs/http.js +94 -0
- package/build/cjs/index.d.ts +9 -0
- package/build/cjs/index.js +19 -0
- package/build/cjs/types/requests.d.ts +1 -0
- package/build/cjs/types/requests.js +2 -0
- package/build/cjs/types/responses.d.ts +62 -0
- package/build/cjs/types/responses.js +2 -0
- package/build/esm/api/web-scraping-api.d.ts +13 -0
- package/build/esm/api/web-scraping-api.js +37 -0
- package/build/esm/client.d.ts +11 -0
- package/build/esm/client.js +30 -0
- package/build/esm/errors.d.ts +18 -0
- package/build/esm/errors.js +36 -0
- package/build/esm/generated/parameters.d.ts +11 -0
- package/build/esm/generated/parameters.js +82 -0
- package/build/esm/generated/request-schemas.d.ts +2123 -0
- package/build/esm/generated/request-schemas.js +2395 -0
- package/build/esm/generated/targets.d.ts +735 -0
- package/build/esm/generated/targets.js +340 -0
- package/build/esm/http.d.ts +22 -0
- package/build/esm/http.js +90 -0
- package/build/esm/index.d.ts +9 -0
- package/build/esm/index.js +5 -0
- package/build/esm/types/requests.d.ts +1 -0
- package/build/esm/types/requests.js +1 -0
- package/build/esm/types/responses.d.ts +62 -0
- package/build/esm/types/responses.js +1 -0
- package/package.json +37 -46
- package/CHANGELOG.md +0 -53
- package/lib/cjs/index.js +0 -1240
- package/lib/cjs/index.js.map +0 -1
- package/lib/esm/index.js +0 -1235
- package/lib/esm/index.js.map +0 -1
- package/lib/types/index.d.ts +0 -465
package/README.md
CHANGED
|
@@ -1,3 +1,138 @@
|
|
|
1
1
|
# @decodo/sdk-ts
|
|
2
2
|
|
|
3
|
-
Official
|
|
3
|
+
Official TypeScript SDK for the Decodo Web Scraping API.
|
|
4
|
+
|
|
5
|
+
Features:
|
|
6
|
+
|
|
7
|
+
- Strongly typed per-target parameters with full IDE autocomplete
|
|
8
|
+
- Sync, async, and batch scraping
|
|
9
|
+
- Zero runtime dependencies (uses native `fetch`, Node 18+)
|
|
10
|
+
- Typed error hierarchy
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @decodo/sdk-ts
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { DecodoClient, Target } from '@decodo/sdk-ts';
|
|
22
|
+
|
|
23
|
+
const client = new DecodoClient({
|
|
24
|
+
webScrapingApi: {
|
|
25
|
+
token: '<basic_auth_token>',
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const result = await client.webScrapingApi.scrape({
|
|
30
|
+
target: Target.GoogleSearch,
|
|
31
|
+
query: 'coffee shops',
|
|
32
|
+
geo: 'United States',
|
|
33
|
+
parse: true,
|
|
34
|
+
});
|
|
35
|
+
console.log(result.results[0].content);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const client = new DecodoClient({
|
|
42
|
+
webScrapingApi: {
|
|
43
|
+
token: '<basic_auth_token>',
|
|
44
|
+
},
|
|
45
|
+
timeoutMs: 120_000, // optional, request timeout in ms (default: 180s)
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Web Scraping API
|
|
50
|
+
|
|
51
|
+
Access via `client.webScrapingApi`.
|
|
52
|
+
|
|
53
|
+
The snippets below assume you have already imported `Target` (and `DecodoClient` where a client is constructed), for example:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { DecodoClient, Target } from '@decodo/sdk-ts';
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Sync scrape
|
|
60
|
+
|
|
61
|
+
Blocks until the scraping result is ready:
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
const result = await client.webScrapingApi.scrape({
|
|
65
|
+
target: Target.AmazonProduct,
|
|
66
|
+
query: 'B09H74FXNW',
|
|
67
|
+
parse: true,
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Async scrape
|
|
72
|
+
|
|
73
|
+
Creates a task and returns immediately. Poll separately for results:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const task = await client.webScrapingApi.scrapeAsync({
|
|
77
|
+
target: Target.GoogleSearch,
|
|
78
|
+
query: 'laptop reviews',
|
|
79
|
+
parse: true,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const meta = await client.webScrapingApi.getStatus(task.id);
|
|
83
|
+
console.log(meta.status); // 'pending' | 'done' | 'faulted'
|
|
84
|
+
|
|
85
|
+
const results = await client.webScrapingApi.getResults(task.id);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Batch scrape
|
|
89
|
+
|
|
90
|
+
Send multiple URLs or queries in a single request:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
const batch = await client.webScrapingApi.scrapeBatch({
|
|
94
|
+
target: Target.GoogleSearch,
|
|
95
|
+
query: ['coffee', 'tea', 'juice'],
|
|
96
|
+
parse: true,
|
|
97
|
+
});
|
|
98
|
+
console.log(batch.id);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Error handling
|
|
102
|
+
|
|
103
|
+
The SDK throws typed errors that map to API error codes:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import {
|
|
107
|
+
DecodoError,
|
|
108
|
+
AuthenticationError,
|
|
109
|
+
RateLimitError,
|
|
110
|
+
ValidationError,
|
|
111
|
+
TimeoutError,
|
|
112
|
+
Target,
|
|
113
|
+
} from '@decodo/sdk-ts';
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
await client.webScrapingApi.scrape({
|
|
117
|
+
target: Target.GoogleSearch,
|
|
118
|
+
query: 'test',
|
|
119
|
+
parse: true,
|
|
120
|
+
});
|
|
121
|
+
} catch (err) {
|
|
122
|
+
if (err instanceof AuthenticationError) {
|
|
123
|
+
// 401/403 — bad credentials
|
|
124
|
+
} else if (err instanceof RateLimitError) {
|
|
125
|
+
// 429 — too many requests
|
|
126
|
+
} else if (err instanceof ValidationError) {
|
|
127
|
+
// 422 — invalid parameters
|
|
128
|
+
console.log(err.errors);
|
|
129
|
+
} else if (err instanceof TimeoutError) {
|
|
130
|
+
// request timed out
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Requirements
|
|
136
|
+
|
|
137
|
+
- Node.js >= 18 (for native `fetch`)
|
|
138
|
+
- TypeScript >= 5.0 (recommended for best type inference)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { HttpClient } from '../http.js';
|
|
2
|
+
import type { ScrapeRequest, BatchRequest } from '../generated/targets.js';
|
|
3
|
+
import type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse } from '../types/responses.js';
|
|
4
|
+
export declare class WebScrapingApi {
|
|
5
|
+
private readonly http;
|
|
6
|
+
constructor(http: HttpClient);
|
|
7
|
+
private validate;
|
|
8
|
+
scrape(params: ScrapeRequest): Promise<SyncResponse>;
|
|
9
|
+
scrapeAsync(params: ScrapeRequest): Promise<AsyncTaskResponse>;
|
|
10
|
+
scrapeBatch(params: BatchRequest): Promise<BatchResponse>;
|
|
11
|
+
getStatus(taskId: string): Promise<TaskMetadata>;
|
|
12
|
+
getResults(taskId: string): Promise<TaskResultsResponse | null>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebScrapingApi = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const errors_js_1 = require("../errors.js");
|
|
6
|
+
const request_schemas_js_1 = require("../generated/request-schemas.js");
|
|
7
|
+
class WebScrapingApi {
|
|
8
|
+
http;
|
|
9
|
+
constructor(http) {
|
|
10
|
+
this.http = http;
|
|
11
|
+
}
|
|
12
|
+
validate(params) {
|
|
13
|
+
const schema = request_schemas_js_1.requestSchemas[params.target];
|
|
14
|
+
if (!schema) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const parsed = schema.safeParse(params);
|
|
18
|
+
if (!parsed.success) {
|
|
19
|
+
throw new errors_js_1.ValidationError((0, zod_1.prettifyError)(parsed.error));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async scrape(params) {
|
|
23
|
+
this.validate(params);
|
|
24
|
+
return this.http.post('/v2/scrape', params);
|
|
25
|
+
}
|
|
26
|
+
async scrapeAsync(params) {
|
|
27
|
+
this.validate(params);
|
|
28
|
+
return this.http.post('/v3/task', params);
|
|
29
|
+
}
|
|
30
|
+
async scrapeBatch(params) {
|
|
31
|
+
return this.http.post('/v3/task/batch', params);
|
|
32
|
+
}
|
|
33
|
+
async getStatus(taskId) {
|
|
34
|
+
return this.http.get(`/v3/task/${taskId}`);
|
|
35
|
+
}
|
|
36
|
+
async getResults(taskId) {
|
|
37
|
+
const res = await this.http.get(`/v3/task/${taskId}/results`);
|
|
38
|
+
return res ?? null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.WebScrapingApi = WebScrapingApi;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { WebScrapingApi } from './api/web-scraping-api.js';
|
|
2
|
+
export type DecodoConfig = {
|
|
3
|
+
webScrapingApi?: {
|
|
4
|
+
token: string;
|
|
5
|
+
};
|
|
6
|
+
timeoutMs?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare class DecodoClient {
|
|
9
|
+
readonly webScrapingApi: WebScrapingApi;
|
|
10
|
+
constructor(config: DecodoConfig);
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DecodoClient = void 0;
|
|
4
|
+
const http_js_1 = require("./http.js");
|
|
5
|
+
const web_scraping_api_js_1 = require("./api/web-scraping-api.js");
|
|
6
|
+
const WEB_API_BASE_URL = 'https://scraper-api.decodo.com';
|
|
7
|
+
const DEFAULT_TIMEOUT_MS = 180_000;
|
|
8
|
+
const notConfigured = (namespace, hint) => {
|
|
9
|
+
throw new Error(`${namespace} is not configured. ${hint}`);
|
|
10
|
+
};
|
|
11
|
+
class DecodoClient {
|
|
12
|
+
webScrapingApi;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
15
|
+
if (config.webScrapingApi) {
|
|
16
|
+
this.webScrapingApi = new web_scraping_api_js_1.WebScrapingApi(new http_js_1.HttpClient({
|
|
17
|
+
baseUrl: WEB_API_BASE_URL,
|
|
18
|
+
auth: {
|
|
19
|
+
type: 'basic',
|
|
20
|
+
token: config.webScrapingApi.token,
|
|
21
|
+
},
|
|
22
|
+
timeoutMs,
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
this.webScrapingApi = new Proxy({}, {
|
|
27
|
+
get() {
|
|
28
|
+
notConfigured('webScrapingApi', 'Provide webScrapingApi.token in DecodoConfig.');
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.DecodoClient = DecodoClient;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class DecodoError extends Error {
|
|
2
|
+
readonly statusCode: number;
|
|
3
|
+
readonly apiStatus: string | undefined;
|
|
4
|
+
constructor(message: string, statusCode: number, apiStatus?: string);
|
|
5
|
+
}
|
|
6
|
+
export declare class AuthenticationError extends DecodoError {
|
|
7
|
+
constructor(message?: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class RateLimitError extends DecodoError {
|
|
10
|
+
constructor(message?: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class ValidationError extends DecodoError {
|
|
13
|
+
readonly errors: unknown[] | undefined;
|
|
14
|
+
constructor(message: string, errors?: unknown[]);
|
|
15
|
+
}
|
|
16
|
+
export declare class TimeoutError extends Error {
|
|
17
|
+
constructor(message?: string);
|
|
18
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TimeoutError = exports.ValidationError = exports.RateLimitError = exports.AuthenticationError = exports.DecodoError = void 0;
|
|
4
|
+
class DecodoError extends Error {
|
|
5
|
+
statusCode;
|
|
6
|
+
apiStatus;
|
|
7
|
+
constructor(message, statusCode, apiStatus) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'DecodoError';
|
|
10
|
+
this.statusCode = statusCode;
|
|
11
|
+
this.apiStatus = apiStatus;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.DecodoError = DecodoError;
|
|
15
|
+
class AuthenticationError extends DecodoError {
|
|
16
|
+
constructor(message = 'Authentication failed. Check your credentials.') {
|
|
17
|
+
super(message, 401, 'failed');
|
|
18
|
+
this.name = 'AuthenticationError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.AuthenticationError = AuthenticationError;
|
|
22
|
+
class RateLimitError extends DecodoError {
|
|
23
|
+
constructor(message = 'Rate limit exceeded. Slow down your request rate.') {
|
|
24
|
+
super(message, 429, 'failed');
|
|
25
|
+
this.name = 'RateLimitError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.RateLimitError = RateLimitError;
|
|
29
|
+
class ValidationError extends DecodoError {
|
|
30
|
+
errors;
|
|
31
|
+
constructor(message, errors) {
|
|
32
|
+
super(message, 422, 'failed');
|
|
33
|
+
this.name = 'ValidationError';
|
|
34
|
+
this.errors = errors;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.ValidationError = ValidationError;
|
|
38
|
+
class TimeoutError extends Error {
|
|
39
|
+
constructor(message = 'The request timed out.') {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = 'TimeoutError';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.TimeoutError = TimeoutError;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Auto-generated by src/codegen/web-scraping-api/generate-parameters.ts — do not edit
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.parameterMeta = void 0;
|
|
5
|
+
exports.parameterMeta = {
|
|
6
|
+
"360": { type: "boolean" },
|
|
7
|
+
callback_url: { type: "string" },
|
|
8
|
+
query: { type: "string", maxLength: 2048 },
|
|
9
|
+
headless: { type: "string", enum: ["html", "png"] },
|
|
10
|
+
locale: { type: "string" },
|
|
11
|
+
geo: { type: "string" },
|
|
12
|
+
device_type: { type: "string" },
|
|
13
|
+
page_from: { type: "number", minimum: 0, maximum: 100 },
|
|
14
|
+
google_results_language: { type: "string" },
|
|
15
|
+
google_tbm: { type: "string" },
|
|
16
|
+
google_tbs: { type: "string" },
|
|
17
|
+
parse: { type: "boolean" },
|
|
18
|
+
google_nfpr: { type: "boolean" },
|
|
19
|
+
google_safe_search: { type: "boolean" },
|
|
20
|
+
session_id: { type: "string" },
|
|
21
|
+
xhr: { type: "boolean" },
|
|
22
|
+
markdown: { type: "boolean" },
|
|
23
|
+
page_count: { type: "number", minimum: 1, maximum: 10 },
|
|
24
|
+
date_range: { type: "string" },
|
|
25
|
+
stars: { type: "number" },
|
|
26
|
+
adults: { type: "number", minimum: 1, maximum: 3 },
|
|
27
|
+
children: { type: "number", minimum: 1, maximum: 3 },
|
|
28
|
+
search_type: { type: "string" },
|
|
29
|
+
date_start: { type: "string" },
|
|
30
|
+
date_end: { type: "string" },
|
|
31
|
+
url: { type: "string", maxLength: 2056 },
|
|
32
|
+
hotel_occupancy: { type: "string" },
|
|
33
|
+
domain: { type: "string" },
|
|
34
|
+
language_code: { type: "string" },
|
|
35
|
+
transcript_origin: { type: "string" },
|
|
36
|
+
autoselect_variant: { type: "boolean" },
|
|
37
|
+
currency: { type: "string" },
|
|
38
|
+
category: { type: "string" },
|
|
39
|
+
merchant: { type: "string" },
|
|
40
|
+
sort_by: { type: "string" },
|
|
41
|
+
parser_type: { type: "string" },
|
|
42
|
+
payload: { type: "string" },
|
|
43
|
+
http_method: { type: "string" },
|
|
44
|
+
successful_status_codes: { type: "array", items: { type: "number" } },
|
|
45
|
+
product_id: { type: "string" },
|
|
46
|
+
fulfillment_type: { type: "string" },
|
|
47
|
+
walmart_store_id: { type: "string" },
|
|
48
|
+
delivery_zip: { type: "string" },
|
|
49
|
+
store_id: { type: "string" },
|
|
50
|
+
delivery_type: { type: "string" },
|
|
51
|
+
target_store_id: { type: "string" },
|
|
52
|
+
lowes_store_id: { type: "string" },
|
|
53
|
+
user_agent_type: { type: "string" },
|
|
54
|
+
free_delivery: { type: "boolean" },
|
|
55
|
+
pickup_today: { type: "boolean" },
|
|
56
|
+
delivery_today_tomorrow: { type: "boolean" },
|
|
57
|
+
proxy_pool: { type: "string", enum: ["standard", "premium"] },
|
|
58
|
+
headers: { type: "object" },
|
|
59
|
+
cookies: { type: "object" },
|
|
60
|
+
force_headers: { type: "boolean" },
|
|
61
|
+
force_cookies: { type: "boolean" },
|
|
62
|
+
prompt: { type: "string", maxLength: 8192 },
|
|
63
|
+
search: { type: "boolean" },
|
|
64
|
+
country: { type: "string" },
|
|
65
|
+
sort: { type: "string" },
|
|
66
|
+
upload_date: { type: "string" },
|
|
67
|
+
type: { type: "string" },
|
|
68
|
+
duration: { type: "string" },
|
|
69
|
+
video_sort_by: { type: "string" },
|
|
70
|
+
"3d": { type: "boolean" },
|
|
71
|
+
"4k": { type: "boolean" },
|
|
72
|
+
creative_commons: { type: "boolean" },
|
|
73
|
+
hd: { type: "boolean" },
|
|
74
|
+
hdr: { type: "boolean" },
|
|
75
|
+
vr180: { type: "boolean" },
|
|
76
|
+
live: { type: "boolean" },
|
|
77
|
+
location: { type: "boolean" },
|
|
78
|
+
purchased: { type: "boolean" },
|
|
79
|
+
subtitles: { type: "boolean" },
|
|
80
|
+
subtitle_origin: { type: "string" },
|
|
81
|
+
limit: { type: "number", minimum: 0, maximum: 100 },
|
|
82
|
+
customStatusCode: { type: "string" },
|
|
83
|
+
requestBody: { type: "string" },
|
|
84
|
+
customCookies: { type: "string" },
|
|
85
|
+
};
|