@decodo/sdk-ts 2.0.1 → 2.1.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/build/cjs/index.cjs +3104 -0
- package/build/cjs/index.d.cts +900 -0
- package/build/esm/api/web-scraping-api.d.ts +15 -0
- package/build/esm/api/web-scraping-api.js +39 -0
- package/build/esm/client.d.ts +13 -0
- package/build/esm/client.js +32 -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 +79 -0
- package/build/esm/generated/request-schemas.d.ts +1951 -0
- package/build/esm/generated/request-schemas.js +2205 -0
- package/build/esm/generated/targets.d.ts +675 -0
- package/build/esm/generated/targets.js +316 -0
- package/build/esm/http.d.ts +22 -0
- package/build/esm/http.js +91 -0
- package/build/esm/index.d.ts +12 -0
- package/build/esm/index.js +7 -0
- package/build/esm/schema/build-target-meta.d.ts +3 -0
- package/build/esm/schema/build-target-meta.js +15 -0
- package/build/esm/schema/bundled-schema.d.ts +10 -0
- package/build/esm/schema/bundled-schema.js +20 -0
- package/build/esm/schema/constants.d.ts +5 -0
- package/build/esm/schema/constants.js +5 -0
- package/build/esm/schema/expand-path.d.ts +1 -0
- package/build/esm/schema/expand-path.js +11 -0
- package/build/esm/schema/remote-schema.d.ts +19 -0
- package/build/esm/schema/remote-schema.js +112 -0
- package/build/esm/schema/resolve-latest-ir.d.ts +2 -0
- package/build/esm/schema/resolve-latest-ir.js +51 -0
- package/build/esm/schema/types.d.ts +44 -0
- package/build/esm/schema/types.js +1 -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 +13 -6
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import * as z from 'zod';
|
|
4
|
+
import { buildTargetMeta } from './build-target-meta.js';
|
|
5
|
+
import { DEFAULT_IR_CACHE_PATH } from './constants.js';
|
|
6
|
+
import { expandPath } from './expand-path.js';
|
|
7
|
+
import { resolveLatestIr } from './resolve-latest-ir.js';
|
|
8
|
+
const readCachedIr = (cachePath) => {
|
|
9
|
+
try {
|
|
10
|
+
const raw = readFileSync(cachePath, 'utf-8');
|
|
11
|
+
return JSON.parse(raw);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const isCacheFresh = (cached, ttlMs) => {
|
|
18
|
+
if (ttlMs === undefined) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
return Date.now() - cached.fetchedAt < ttlMs;
|
|
22
|
+
};
|
|
23
|
+
const fetchIr = async (url) => {
|
|
24
|
+
const res = await fetch(url, {
|
|
25
|
+
headers: { Accept: 'application/json' },
|
|
26
|
+
});
|
|
27
|
+
if (!res.ok) {
|
|
28
|
+
throw new Error(`Failed to fetch IR from ${url}: HTTP ${res.status}`);
|
|
29
|
+
}
|
|
30
|
+
return (await res.json());
|
|
31
|
+
};
|
|
32
|
+
const writeCachedIr = (cachePath, cached) => {
|
|
33
|
+
mkdirSync(dirname(cachePath), { recursive: true });
|
|
34
|
+
writeFileSync(cachePath, JSON.stringify(cached, null, 2));
|
|
35
|
+
};
|
|
36
|
+
const resolveIrUrl = async (config) => {
|
|
37
|
+
if (config.url) {
|
|
38
|
+
return { url: config.url };
|
|
39
|
+
}
|
|
40
|
+
const latest = await resolveLatestIr();
|
|
41
|
+
return { url: latest.url, version: latest.version };
|
|
42
|
+
};
|
|
43
|
+
const loadIr = async (config) => {
|
|
44
|
+
const cachePath = expandPath(config.cachePath ?? DEFAULT_IR_CACHE_PATH);
|
|
45
|
+
const cached = readCachedIr(cachePath);
|
|
46
|
+
const resolved = await resolveIrUrl(config);
|
|
47
|
+
if (cached?.resolvedUrl === resolved.url) {
|
|
48
|
+
if (isCacheFresh(cached, config.ttlMs)) {
|
|
49
|
+
return cached.ir;
|
|
50
|
+
}
|
|
51
|
+
if (config.url || cached.ir.version === resolved.version) {
|
|
52
|
+
return cached.ir;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const ir = await fetchIr(resolved.url);
|
|
56
|
+
writeCachedIr(cachePath, {
|
|
57
|
+
fetchedAt: Date.now(),
|
|
58
|
+
resolvedUrl: resolved.url,
|
|
59
|
+
ir,
|
|
60
|
+
});
|
|
61
|
+
return ir;
|
|
62
|
+
};
|
|
63
|
+
const buildRequestSchemas = (ir) => {
|
|
64
|
+
const schemas = new Map();
|
|
65
|
+
for (const [targetKey, target] of Object.entries(ir.apis.webScrapingApi.targets)) {
|
|
66
|
+
schemas.set(targetKey, z.fromJSONSchema(target.parameter_schema));
|
|
67
|
+
}
|
|
68
|
+
return schemas;
|
|
69
|
+
};
|
|
70
|
+
const buildParameterSchemas = (ir) => {
|
|
71
|
+
const schemas = new Map();
|
|
72
|
+
for (const [targetKey, target] of Object.entries(ir.apis.webScrapingApi.targets)) {
|
|
73
|
+
schemas.set(targetKey, target.parameter_schema);
|
|
74
|
+
}
|
|
75
|
+
return schemas;
|
|
76
|
+
};
|
|
77
|
+
export class RemoteSchema {
|
|
78
|
+
version;
|
|
79
|
+
schemas;
|
|
80
|
+
meta;
|
|
81
|
+
parameterSchemas;
|
|
82
|
+
sharedParameters;
|
|
83
|
+
targetKeys;
|
|
84
|
+
constructor(ir, schemas) {
|
|
85
|
+
this.version = ir.version;
|
|
86
|
+
this.schemas = schemas;
|
|
87
|
+
this.meta = buildTargetMeta(ir.apis.webScrapingApi.targets);
|
|
88
|
+
this.parameterSchemas = buildParameterSchemas(ir);
|
|
89
|
+
this.sharedParameters = ir.apis.webScrapingApi.parameters ?? {};
|
|
90
|
+
this.targetKeys = Object.keys(ir.apis.webScrapingApi.targets);
|
|
91
|
+
}
|
|
92
|
+
static async load(config = {}) {
|
|
93
|
+
const ir = await loadIr(config);
|
|
94
|
+
const schemas = buildRequestSchemas(ir);
|
|
95
|
+
return new RemoteSchema(ir, schemas);
|
|
96
|
+
}
|
|
97
|
+
getRequestSchema(target) {
|
|
98
|
+
return this.schemas.get(target);
|
|
99
|
+
}
|
|
100
|
+
listTargets() {
|
|
101
|
+
return this.targetKeys;
|
|
102
|
+
}
|
|
103
|
+
getTargetMeta(target) {
|
|
104
|
+
return this.meta.get(target);
|
|
105
|
+
}
|
|
106
|
+
getTargetParameterSchema(target) {
|
|
107
|
+
return this.parameterSchemas.get(target);
|
|
108
|
+
}
|
|
109
|
+
getSharedParameters() {
|
|
110
|
+
return this.sharedParameters;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { DEFAULT_IR_BASE, DEFAULT_IR_LIST_URL, DEFAULT_IR_PREFIX, } from './constants.js';
|
|
2
|
+
const IR_OBJECT_PATTERN = /^decodo-ir-v(.+)\.json$/;
|
|
3
|
+
const parseSemver = (version) => {
|
|
4
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version);
|
|
5
|
+
if (!match) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
9
|
+
};
|
|
10
|
+
const compareSemver = (left, right) => {
|
|
11
|
+
const parsedLeft = parseSemver(left);
|
|
12
|
+
const parsedRight = parseSemver(right);
|
|
13
|
+
if (!parsedLeft || !parsedRight) {
|
|
14
|
+
return left.localeCompare(right);
|
|
15
|
+
}
|
|
16
|
+
for (let i = 0; i < 3; i += 1) {
|
|
17
|
+
if (parsedLeft[i] !== parsedRight[i]) {
|
|
18
|
+
return parsedLeft[i] - parsedRight[i];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return 0;
|
|
22
|
+
};
|
|
23
|
+
const parseIrVersions = (names) => {
|
|
24
|
+
const versions = [];
|
|
25
|
+
for (const name of names) {
|
|
26
|
+
const match = IR_OBJECT_PATTERN.exec(name);
|
|
27
|
+
if (match && parseSemver(match[1])) {
|
|
28
|
+
versions.push(match[1]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return versions;
|
|
32
|
+
};
|
|
33
|
+
export const resolveLatestIr = async () => {
|
|
34
|
+
const res = await fetch(DEFAULT_IR_LIST_URL, {
|
|
35
|
+
headers: { Accept: 'application/json' },
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
throw new Error(`Failed to list IR versions from ${DEFAULT_IR_LIST_URL}: HTTP ${res.status}`);
|
|
39
|
+
}
|
|
40
|
+
const body = (await res.json());
|
|
41
|
+
const names = body.items?.map((item) => item.name) ?? [];
|
|
42
|
+
const versions = parseIrVersions(names);
|
|
43
|
+
if (versions.length === 0) {
|
|
44
|
+
throw new Error(`No versioned IR objects found in bucket with prefix "${DEFAULT_IR_PREFIX}".`);
|
|
45
|
+
}
|
|
46
|
+
const version = versions.reduce((latest, current) => compareSemver(current, latest) > 0 ? current : latest);
|
|
47
|
+
return {
|
|
48
|
+
version,
|
|
49
|
+
url: `${DEFAULT_IR_BASE}/${DEFAULT_IR_PREFIX}${version}.json`,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { JSONSchema4 } from 'json-schema';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
import type { TargetMeta } from '../generated/targets.js';
|
|
4
|
+
export type RemoteSchemaLoadOptions = {
|
|
5
|
+
url?: string;
|
|
6
|
+
cachePath?: string;
|
|
7
|
+
ttlMs?: number;
|
|
8
|
+
};
|
|
9
|
+
export type IrTarget = {
|
|
10
|
+
group: string;
|
|
11
|
+
response_format: string;
|
|
12
|
+
parameter_schema: JSONSchema4;
|
|
13
|
+
};
|
|
14
|
+
export type RemoteIr = {
|
|
15
|
+
version: string;
|
|
16
|
+
apis: {
|
|
17
|
+
webScrapingApi: {
|
|
18
|
+
parameters: Record<string, unknown>;
|
|
19
|
+
targets: Record<string, IrTarget>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export type CachedIr = {
|
|
24
|
+
fetchedAt: number;
|
|
25
|
+
resolvedUrl: string;
|
|
26
|
+
ir: RemoteIr;
|
|
27
|
+
};
|
|
28
|
+
export type GcsObjectList = {
|
|
29
|
+
items?: Array<{
|
|
30
|
+
name: string;
|
|
31
|
+
}>;
|
|
32
|
+
};
|
|
33
|
+
export type LatestIrLocation = {
|
|
34
|
+
version: string;
|
|
35
|
+
url: string;
|
|
36
|
+
};
|
|
37
|
+
export type DecodoSchema = {
|
|
38
|
+
getRequestSchema: (target: string) => z.ZodType | undefined;
|
|
39
|
+
listTargets: () => string[];
|
|
40
|
+
getTargetMeta: (target: string) => TargetMeta | undefined;
|
|
41
|
+
getTargetParameterSchema: (target: string) => JSONSchema4 | undefined;
|
|
42
|
+
getSharedParameters: () => Record<string, unknown>;
|
|
43
|
+
readonly version?: string;
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { ScrapeRequest, BatchRequest } from '../generated/targets.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export type ResultEntry = {
|
|
2
|
+
content: unknown;
|
|
3
|
+
status_code: number;
|
|
4
|
+
url?: string;
|
|
5
|
+
task_id: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
cookies?: Record<string, string>[];
|
|
8
|
+
created_at: string;
|
|
9
|
+
updated_at: string;
|
|
10
|
+
help?: string;
|
|
11
|
+
browser_actions_warnings?: Record<string, string>[];
|
|
12
|
+
browser_actions_error?: Record<string, string>[];
|
|
13
|
+
delivery_zip?: string;
|
|
14
|
+
};
|
|
15
|
+
export type SyncResponse = {
|
|
16
|
+
results: ResultEntry[];
|
|
17
|
+
};
|
|
18
|
+
export type AsyncTaskResponse = {
|
|
19
|
+
id: string;
|
|
20
|
+
target?: string;
|
|
21
|
+
url?: string;
|
|
22
|
+
query?: string;
|
|
23
|
+
status: string;
|
|
24
|
+
created_at?: string;
|
|
25
|
+
updated_at?: string;
|
|
26
|
+
page_from?: number;
|
|
27
|
+
limit?: number;
|
|
28
|
+
geo?: string | null;
|
|
29
|
+
device_type?: string;
|
|
30
|
+
headless?: string | null;
|
|
31
|
+
parse?: boolean;
|
|
32
|
+
locale?: string | null;
|
|
33
|
+
domain?: string;
|
|
34
|
+
output_schema?: string | null;
|
|
35
|
+
content_encoding?: string;
|
|
36
|
+
page_count?: number;
|
|
37
|
+
adults?: number;
|
|
38
|
+
children?: number;
|
|
39
|
+
callback_url?: string;
|
|
40
|
+
browser_actions_error?: Record<string, string>[];
|
|
41
|
+
browser_actions_warnings?: Record<string, string>[];
|
|
42
|
+
};
|
|
43
|
+
export type BatchResponse = {
|
|
44
|
+
id?: string;
|
|
45
|
+
queries?: AsyncTaskResponse[];
|
|
46
|
+
errors?: Array<{
|
|
47
|
+
message: string;
|
|
48
|
+
}>;
|
|
49
|
+
};
|
|
50
|
+
export type TaskStatus = 'pending' | 'faulted' | 'done';
|
|
51
|
+
export type TaskMetadata = {
|
|
52
|
+
status: TaskStatus;
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
};
|
|
55
|
+
export type TaskResultsResponse = {
|
|
56
|
+
results: ResultEntry[];
|
|
57
|
+
};
|
|
58
|
+
export type ErrorResponse = {
|
|
59
|
+
status: string;
|
|
60
|
+
message: string;
|
|
61
|
+
errors?: unknown[];
|
|
62
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decodo/sdk-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Official TypeScript SDK for the Decodo APIs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./build/cjs/index.cjs",
|
|
@@ -25,10 +25,13 @@
|
|
|
25
25
|
"codegen": "tsx src/codegen/codegen.ts",
|
|
26
26
|
"build": "pnpm run codegen && pnpm run build:esm && pnpm run build:cjs",
|
|
27
27
|
"build:esm": "tsc -p tsconfig.build.json",
|
|
28
|
-
"build:cjs": "
|
|
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
29
|
"typecheck": "tsc --noEmit",
|
|
30
30
|
"lint": "eslint .",
|
|
31
|
-
"lint:fix": "eslint . --fix"
|
|
31
|
+
"lint:fix": "eslint . --fix",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"test:watch": "vitest",
|
|
34
|
+
"prepack": "pnpm run build:esm && pnpm run build:cjs"
|
|
32
35
|
},
|
|
33
36
|
"engines": {
|
|
34
37
|
"node": ">=18.0.0"
|
|
@@ -37,13 +40,17 @@
|
|
|
37
40
|
"@eslint/js": "^9.39.1",
|
|
38
41
|
"@types/json-schema": "^7.0.15",
|
|
39
42
|
"@types/node": "^25.7.0",
|
|
43
|
+
"dts-bundle-generator": "^9.5.1",
|
|
44
|
+
"esbuild": "^0.28.0",
|
|
40
45
|
"eslint": "^9.39.1",
|
|
46
|
+
"json-schema-to-typescript": "^15.0.4",
|
|
41
47
|
"tsx": "^4.19.0",
|
|
42
48
|
"typescript": "^5.3.3",
|
|
43
|
-
"typescript-eslint": "^8.40.0"
|
|
49
|
+
"typescript-eslint": "^8.40.0",
|
|
50
|
+
"vitest": "^4.1.7"
|
|
44
51
|
},
|
|
45
52
|
"dependencies": {
|
|
46
|
-
"json-schema-to-typescript": "^15.0.4",
|
|
47
53
|
"zod": "^4"
|
|
48
|
-
}
|
|
54
|
+
},
|
|
55
|
+
"license": "MIT"
|
|
49
56
|
}
|