@decodo/sdk-ts 2.0.0 → 2.1.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 +415 -17
- package/build/cjs/api/web-scraping-api.d.ts +3 -1
- package/build/cjs/api/web-scraping-api.js +5 -3
- package/build/cjs/client.d.ts +2 -0
- package/build/cjs/client.js +3 -1
- package/build/cjs/generated/parameters.js +3 -6
- package/build/cjs/generated/request-schemas.d.ts +0 -172
- package/build/cjs/generated/request-schemas.js +0 -190
- package/build/cjs/generated/targets.d.ts +2 -62
- package/build/cjs/generated/targets.js +1 -25
- package/build/cjs/http.js +1 -0
- package/build/cjs/index.d.ts +3 -0
- package/build/cjs/index.js +5 -1
- package/build/cjs/schema/build-request-schemas.d.ts +4 -0
- package/build/cjs/schema/build-request-schemas.js +56 -0
- package/build/cjs/schema/build-target-meta.d.ts +3 -0
- package/build/cjs/schema/build-target-meta.js +19 -0
- package/build/cjs/schema/bundled-provider.d.ts +2 -0
- package/build/cjs/schema/bundled-provider.js +9 -0
- package/build/cjs/schema/bundled-schema.d.ts +10 -0
- package/build/cjs/schema/bundled-schema.js +24 -0
- package/build/cjs/schema/constants.d.ts +5 -0
- package/build/cjs/schema/constants.js +8 -0
- package/build/cjs/schema/expand-path.d.ts +1 -0
- package/build/cjs/schema/expand-path.js +15 -0
- package/build/cjs/schema/remote-provider.d.ts +8 -0
- package/build/cjs/schema/remote-provider.js +120 -0
- package/build/cjs/schema/remote-schema.d.ts +19 -0
- package/build/cjs/schema/remote-schema.js +149 -0
- package/build/cjs/schema/resolve-latest-ir.d.ts +2 -0
- package/build/cjs/schema/resolve-latest-ir.js +55 -0
- package/build/cjs/schema/resolve-provider.d.ts +3 -0
- package/build/cjs/schema/resolve-provider.js +27 -0
- package/build/cjs/schema/types.d.ts +44 -0
- package/build/cjs/schema/types.js +2 -0
- package/build/esm/api/web-scraping-api.d.ts +3 -1
- package/build/esm/api/web-scraping-api.js +5 -3
- package/build/esm/client.d.ts +2 -0
- package/build/esm/client.js +3 -1
- package/build/esm/generated/parameters.js +3 -6
- package/build/esm/generated/request-schemas.d.ts +0 -172
- package/build/esm/generated/request-schemas.js +0 -190
- package/build/esm/generated/targets.d.ts +2 -62
- package/build/esm/generated/targets.js +1 -25
- package/build/esm/http.js +1 -0
- package/build/esm/index.d.ts +3 -0
- package/build/esm/index.js +2 -0
- package/build/esm/schema/build-request-schemas.d.ts +4 -0
- package/build/esm/schema/build-request-schemas.js +18 -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-provider.d.ts +2 -0
- package/build/esm/schema/bundled-provider.js +6 -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-provider.d.ts +8 -0
- package/build/esm/schema/remote-provider.js +83 -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/resolve-provider.d.ts +3 -0
- package/build/esm/schema/resolve-provider.js +22 -0
- package/build/esm/schema/types.d.ts +44 -0
- package/build/esm/schema/types.js +1 -0
- package/package.json +6 -3
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.validateRequestSchemas = exports.buildRequestSchemas = void 0;
|
|
37
|
+
const z = __importStar(require("zod"));
|
|
38
|
+
const buildRequestSchemas = (api) => {
|
|
39
|
+
const schemas = new Map();
|
|
40
|
+
for (const [targetKey, target] of Object.entries(api.targets)) {
|
|
41
|
+
schemas.set(targetKey, z.fromJSONSchema(target.parameter_schema));
|
|
42
|
+
}
|
|
43
|
+
return schemas;
|
|
44
|
+
};
|
|
45
|
+
exports.buildRequestSchemas = buildRequestSchemas;
|
|
46
|
+
const validateRequestSchemas = (api) => {
|
|
47
|
+
for (const [targetKey, target] of Object.entries(api.targets)) {
|
|
48
|
+
try {
|
|
49
|
+
z.fromJSONSchema(target.parameter_schema);
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
throw new Error(`Invalid parameter_schema for target "${targetKey}": ${err.message}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.validateRequestSchemas = validateRequestSchemas;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTargetMeta = void 0;
|
|
4
|
+
const getTargetParameterKeys = (parameterSchema) => {
|
|
5
|
+
const properties = parameterSchema.properties ?? {};
|
|
6
|
+
return Object.keys(properties).filter((key) => key !== 'target');
|
|
7
|
+
};
|
|
8
|
+
const buildTargetMeta = (targets) => {
|
|
9
|
+
const meta = new Map();
|
|
10
|
+
for (const [targetKey, target] of Object.entries(targets)) {
|
|
11
|
+
meta.set(targetKey, {
|
|
12
|
+
group: target.group,
|
|
13
|
+
response_format: target.response_format,
|
|
14
|
+
parameters: getTargetParameterKeys(target.parameter_schema),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return meta;
|
|
18
|
+
};
|
|
19
|
+
exports.buildTargetMeta = buildTargetMeta;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bundledSchemaProvider = void 0;
|
|
4
|
+
const request_schemas_js_1 = require("../generated/request-schemas.js");
|
|
5
|
+
exports.bundledSchemaProvider = {
|
|
6
|
+
getRequestSchema(target) {
|
|
7
|
+
return request_schemas_js_1.requestSchemas[target];
|
|
8
|
+
},
|
|
9
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { JSONSchema4 } from 'json-schema';
|
|
2
|
+
import type { DecodoSchema } from './types.js';
|
|
3
|
+
export declare class BundledSchema implements DecodoSchema {
|
|
4
|
+
static readonly shared: BundledSchema;
|
|
5
|
+
getRequestSchema(target: string): import("zod").ZodType<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>;
|
|
6
|
+
listTargets(): string[];
|
|
7
|
+
getTargetMeta(target: string): import("../generated/targets.js").TargetMeta;
|
|
8
|
+
getTargetParameterSchema(target: string): JSONSchema4 | undefined;
|
|
9
|
+
getSharedParameters(): Record<string, unknown>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BundledSchema = void 0;
|
|
4
|
+
const request_schemas_js_1 = require("../generated/request-schemas.js");
|
|
5
|
+
const targets_js_1 = require("../generated/targets.js");
|
|
6
|
+
class BundledSchema {
|
|
7
|
+
static shared = new BundledSchema();
|
|
8
|
+
getRequestSchema(target) {
|
|
9
|
+
return request_schemas_js_1.requestSchemas[target];
|
|
10
|
+
}
|
|
11
|
+
listTargets() {
|
|
12
|
+
return [...targets_js_1.targets];
|
|
13
|
+
}
|
|
14
|
+
getTargetMeta(target) {
|
|
15
|
+
return targets_js_1.targetMeta[target];
|
|
16
|
+
}
|
|
17
|
+
getTargetParameterSchema(target) {
|
|
18
|
+
return request_schemas_js_1.requestJsonSchemas[target];
|
|
19
|
+
}
|
|
20
|
+
getSharedParameters() {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.BundledSchema = BundledSchema;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const DEFAULT_IR_BUCKET = "decodo-sdk-config";
|
|
2
|
+
export declare const DEFAULT_IR_PREFIX = "decodo-ir-v";
|
|
3
|
+
export declare const DEFAULT_IR_BASE = "https://storage.googleapis.com/decodo-sdk-config";
|
|
4
|
+
export declare const DEFAULT_IR_LIST_URL = "https://storage.googleapis.com/storage/v1/b/decodo-sdk-config/o?prefix=decodo-ir-v";
|
|
5
|
+
export declare const DEFAULT_IR_CACHE_PATH = "~/.decodo/decodo.ir.json";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_IR_CACHE_PATH = exports.DEFAULT_IR_LIST_URL = exports.DEFAULT_IR_BASE = exports.DEFAULT_IR_PREFIX = exports.DEFAULT_IR_BUCKET = void 0;
|
|
4
|
+
exports.DEFAULT_IR_BUCKET = 'decodo-sdk-config';
|
|
5
|
+
exports.DEFAULT_IR_PREFIX = 'decodo-ir-v';
|
|
6
|
+
exports.DEFAULT_IR_BASE = 'https://storage.googleapis.com/decodo-sdk-config';
|
|
7
|
+
exports.DEFAULT_IR_LIST_URL = 'https://storage.googleapis.com/storage/v1/b/decodo-sdk-config/o?prefix=decodo-ir-v';
|
|
8
|
+
exports.DEFAULT_IR_CACHE_PATH = '~/.decodo/decodo.ir.json';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const expandPath: (path: string) => string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.expandPath = void 0;
|
|
4
|
+
const node_os_1 = require("node:os");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const expandPath = (path) => {
|
|
7
|
+
if (path.startsWith('~/')) {
|
|
8
|
+
return (0, node_path_1.resolve)((0, node_os_1.homedir)(), path.slice(2));
|
|
9
|
+
}
|
|
10
|
+
if (path === '~') {
|
|
11
|
+
return (0, node_os_1.homedir)();
|
|
12
|
+
}
|
|
13
|
+
return (0, node_path_1.resolve)(path);
|
|
14
|
+
};
|
|
15
|
+
exports.expandPath = expandPath;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { SchemaProvider } from './types.js';
|
|
2
|
+
type RemoteSchemaProviderConfig = {
|
|
3
|
+
url?: string;
|
|
4
|
+
cachePath?: string;
|
|
5
|
+
ttlMs?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const createRemoteSchemaProvider: (config?: RemoteSchemaProviderConfig) => Promise<SchemaProvider>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.createRemoteSchemaProvider = void 0;
|
|
37
|
+
const node_fs_1 = require("node:fs");
|
|
38
|
+
const node_path_1 = require("node:path");
|
|
39
|
+
const z = __importStar(require("zod"));
|
|
40
|
+
const constants_js_1 = require("./constants.js");
|
|
41
|
+
const expand_path_js_1 = require("./expand-path.js");
|
|
42
|
+
const resolve_latest_ir_js_1 = require("./resolve-latest-ir.js");
|
|
43
|
+
const readCachedIr = (cachePath) => {
|
|
44
|
+
try {
|
|
45
|
+
const raw = (0, node_fs_1.readFileSync)(cachePath, 'utf-8');
|
|
46
|
+
return JSON.parse(raw);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const isCacheFresh = (cached, ttlMs) => {
|
|
53
|
+
if (ttlMs === undefined) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return Date.now() - cached.fetchedAt < ttlMs;
|
|
57
|
+
};
|
|
58
|
+
const fetchIr = async (url) => {
|
|
59
|
+
const res = await fetch(url, {
|
|
60
|
+
headers: { Accept: 'application/json' },
|
|
61
|
+
});
|
|
62
|
+
if (!res.ok) {
|
|
63
|
+
throw new Error(`Failed to fetch IR from ${url}: HTTP ${res.status}`);
|
|
64
|
+
}
|
|
65
|
+
return (await res.json());
|
|
66
|
+
};
|
|
67
|
+
const writeCachedIr = (cachePath, cached) => {
|
|
68
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(cachePath), { recursive: true });
|
|
69
|
+
(0, node_fs_1.writeFileSync)(cachePath, JSON.stringify(cached, null, 2));
|
|
70
|
+
};
|
|
71
|
+
const resolveIrUrl = async (config) => {
|
|
72
|
+
if (config.url) {
|
|
73
|
+
return { url: config.url };
|
|
74
|
+
}
|
|
75
|
+
const latest = await (0, resolve_latest_ir_js_1.resolveLatestIr)();
|
|
76
|
+
return { url: latest.url, version: latest.version };
|
|
77
|
+
};
|
|
78
|
+
const loadIr = async (config) => {
|
|
79
|
+
const cachePath = (0, expand_path_js_1.expandPath)(config.cachePath ?? constants_js_1.DEFAULT_IR_CACHE_PATH);
|
|
80
|
+
const cached = readCachedIr(cachePath);
|
|
81
|
+
if (cached && isCacheFresh(cached, config.ttlMs)) {
|
|
82
|
+
return cached.ir;
|
|
83
|
+
}
|
|
84
|
+
const resolved = await resolveIrUrl(config);
|
|
85
|
+
if (cached) {
|
|
86
|
+
if (config.url) {
|
|
87
|
+
if (cached.resolvedUrl === config.url) {
|
|
88
|
+
return cached.ir;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (resolved.version && cached.ir.version === resolved.version) {
|
|
92
|
+
return cached.ir;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const ir = await fetchIr(resolved.url);
|
|
96
|
+
writeCachedIr(cachePath, {
|
|
97
|
+
fetchedAt: Date.now(),
|
|
98
|
+
resolvedUrl: resolved.url,
|
|
99
|
+
ir,
|
|
100
|
+
});
|
|
101
|
+
return ir;
|
|
102
|
+
};
|
|
103
|
+
const buildRequestSchemas = (ir) => {
|
|
104
|
+
const schemas = new Map();
|
|
105
|
+
const targets = ir.apis.webScrapingApi.targets;
|
|
106
|
+
for (const [targetKey, target] of Object.entries(targets)) {
|
|
107
|
+
schemas.set(targetKey, z.fromJSONSchema(target.parameter_schema));
|
|
108
|
+
}
|
|
109
|
+
return schemas;
|
|
110
|
+
};
|
|
111
|
+
const createRemoteSchemaProvider = async (config = {}) => {
|
|
112
|
+
const ir = await loadIr(config);
|
|
113
|
+
const schemas = buildRequestSchemas(ir);
|
|
114
|
+
return {
|
|
115
|
+
getRequestSchema(target) {
|
|
116
|
+
return schemas.get(target);
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
exports.createRemoteSchemaProvider = createRemoteSchemaProvider;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JSONSchema4 } from 'json-schema';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
import type { TargetMeta } from '../generated/targets.js';
|
|
4
|
+
import type { DecodoSchema, RemoteSchemaLoadOptions } from './types.js';
|
|
5
|
+
export declare class RemoteSchema implements DecodoSchema {
|
|
6
|
+
readonly version: string;
|
|
7
|
+
private readonly schemas;
|
|
8
|
+
private readonly meta;
|
|
9
|
+
private readonly parameterSchemas;
|
|
10
|
+
private readonly sharedParameters;
|
|
11
|
+
private readonly targetKeys;
|
|
12
|
+
private constructor();
|
|
13
|
+
static load(config?: RemoteSchemaLoadOptions): Promise<RemoteSchema>;
|
|
14
|
+
getRequestSchema(target: string): z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> | undefined;
|
|
15
|
+
listTargets(): string[];
|
|
16
|
+
getTargetMeta(target: string): TargetMeta | undefined;
|
|
17
|
+
getTargetParameterSchema(target: string): JSONSchema4 | undefined;
|
|
18
|
+
getSharedParameters(): Record<string, unknown>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.RemoteSchema = void 0;
|
|
37
|
+
const node_fs_1 = require("node:fs");
|
|
38
|
+
const node_path_1 = require("node:path");
|
|
39
|
+
const z = __importStar(require("zod"));
|
|
40
|
+
const build_target_meta_js_1 = require("./build-target-meta.js");
|
|
41
|
+
const constants_js_1 = require("./constants.js");
|
|
42
|
+
const expand_path_js_1 = require("./expand-path.js");
|
|
43
|
+
const resolve_latest_ir_js_1 = require("./resolve-latest-ir.js");
|
|
44
|
+
const readCachedIr = (cachePath) => {
|
|
45
|
+
try {
|
|
46
|
+
const raw = (0, node_fs_1.readFileSync)(cachePath, 'utf-8');
|
|
47
|
+
return JSON.parse(raw);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const isCacheFresh = (cached, ttlMs) => {
|
|
54
|
+
if (ttlMs === undefined) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return Date.now() - cached.fetchedAt < ttlMs;
|
|
58
|
+
};
|
|
59
|
+
const fetchIr = async (url) => {
|
|
60
|
+
const res = await fetch(url, {
|
|
61
|
+
headers: { Accept: 'application/json' },
|
|
62
|
+
});
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
throw new Error(`Failed to fetch IR from ${url}: HTTP ${res.status}`);
|
|
65
|
+
}
|
|
66
|
+
return (await res.json());
|
|
67
|
+
};
|
|
68
|
+
const writeCachedIr = (cachePath, cached) => {
|
|
69
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(cachePath), { recursive: true });
|
|
70
|
+
(0, node_fs_1.writeFileSync)(cachePath, JSON.stringify(cached, null, 2));
|
|
71
|
+
};
|
|
72
|
+
const resolveIrUrl = async (config) => {
|
|
73
|
+
if (config.url) {
|
|
74
|
+
return { url: config.url };
|
|
75
|
+
}
|
|
76
|
+
const latest = await (0, resolve_latest_ir_js_1.resolveLatestIr)();
|
|
77
|
+
return { url: latest.url, version: latest.version };
|
|
78
|
+
};
|
|
79
|
+
const loadIr = async (config) => {
|
|
80
|
+
const cachePath = (0, expand_path_js_1.expandPath)(config.cachePath ?? constants_js_1.DEFAULT_IR_CACHE_PATH);
|
|
81
|
+
const cached = readCachedIr(cachePath);
|
|
82
|
+
const resolved = await resolveIrUrl(config);
|
|
83
|
+
if (cached?.resolvedUrl === resolved.url) {
|
|
84
|
+
if (isCacheFresh(cached, config.ttlMs)) {
|
|
85
|
+
return cached.ir;
|
|
86
|
+
}
|
|
87
|
+
if (config.url || cached.ir.version === resolved.version) {
|
|
88
|
+
return cached.ir;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const ir = await fetchIr(resolved.url);
|
|
92
|
+
writeCachedIr(cachePath, {
|
|
93
|
+
fetchedAt: Date.now(),
|
|
94
|
+
resolvedUrl: resolved.url,
|
|
95
|
+
ir,
|
|
96
|
+
});
|
|
97
|
+
return ir;
|
|
98
|
+
};
|
|
99
|
+
const buildRequestSchemas = (ir) => {
|
|
100
|
+
const schemas = new Map();
|
|
101
|
+
for (const [targetKey, target] of Object.entries(ir.apis.webScrapingApi.targets)) {
|
|
102
|
+
schemas.set(targetKey, z.fromJSONSchema(target.parameter_schema));
|
|
103
|
+
}
|
|
104
|
+
return schemas;
|
|
105
|
+
};
|
|
106
|
+
const buildParameterSchemas = (ir) => {
|
|
107
|
+
const schemas = new Map();
|
|
108
|
+
for (const [targetKey, target] of Object.entries(ir.apis.webScrapingApi.targets)) {
|
|
109
|
+
schemas.set(targetKey, target.parameter_schema);
|
|
110
|
+
}
|
|
111
|
+
return schemas;
|
|
112
|
+
};
|
|
113
|
+
class RemoteSchema {
|
|
114
|
+
version;
|
|
115
|
+
schemas;
|
|
116
|
+
meta;
|
|
117
|
+
parameterSchemas;
|
|
118
|
+
sharedParameters;
|
|
119
|
+
targetKeys;
|
|
120
|
+
constructor(ir, schemas) {
|
|
121
|
+
this.version = ir.version;
|
|
122
|
+
this.schemas = schemas;
|
|
123
|
+
this.meta = (0, build_target_meta_js_1.buildTargetMeta)(ir.apis.webScrapingApi.targets);
|
|
124
|
+
this.parameterSchemas = buildParameterSchemas(ir);
|
|
125
|
+
this.sharedParameters = ir.apis.webScrapingApi.parameters ?? {};
|
|
126
|
+
this.targetKeys = Object.keys(ir.apis.webScrapingApi.targets);
|
|
127
|
+
}
|
|
128
|
+
static async load(config = {}) {
|
|
129
|
+
const ir = await loadIr(config);
|
|
130
|
+
const schemas = buildRequestSchemas(ir);
|
|
131
|
+
return new RemoteSchema(ir, schemas);
|
|
132
|
+
}
|
|
133
|
+
getRequestSchema(target) {
|
|
134
|
+
return this.schemas.get(target);
|
|
135
|
+
}
|
|
136
|
+
listTargets() {
|
|
137
|
+
return this.targetKeys;
|
|
138
|
+
}
|
|
139
|
+
getTargetMeta(target) {
|
|
140
|
+
return this.meta.get(target);
|
|
141
|
+
}
|
|
142
|
+
getTargetParameterSchema(target) {
|
|
143
|
+
return this.parameterSchemas.get(target);
|
|
144
|
+
}
|
|
145
|
+
getSharedParameters() {
|
|
146
|
+
return this.sharedParameters;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.RemoteSchema = RemoteSchema;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveLatestIr = void 0;
|
|
4
|
+
const constants_js_1 = require("./constants.js");
|
|
5
|
+
const IR_OBJECT_PATTERN = /^decodo-ir-v(.+)\.json$/;
|
|
6
|
+
const parseSemver = (version) => {
|
|
7
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version);
|
|
8
|
+
if (!match) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
12
|
+
};
|
|
13
|
+
const compareSemver = (left, right) => {
|
|
14
|
+
const parsedLeft = parseSemver(left);
|
|
15
|
+
const parsedRight = parseSemver(right);
|
|
16
|
+
if (!parsedLeft || !parsedRight) {
|
|
17
|
+
return left.localeCompare(right);
|
|
18
|
+
}
|
|
19
|
+
for (let i = 0; i < 3; i += 1) {
|
|
20
|
+
if (parsedLeft[i] !== parsedRight[i]) {
|
|
21
|
+
return parsedLeft[i] - parsedRight[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return 0;
|
|
25
|
+
};
|
|
26
|
+
const parseIrVersions = (names) => {
|
|
27
|
+
const versions = [];
|
|
28
|
+
for (const name of names) {
|
|
29
|
+
const match = IR_OBJECT_PATTERN.exec(name);
|
|
30
|
+
if (match && parseSemver(match[1])) {
|
|
31
|
+
versions.push(match[1]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return versions;
|
|
35
|
+
};
|
|
36
|
+
const resolveLatestIr = async () => {
|
|
37
|
+
const res = await fetch(constants_js_1.DEFAULT_IR_LIST_URL, {
|
|
38
|
+
headers: { Accept: 'application/json' },
|
|
39
|
+
});
|
|
40
|
+
if (!res.ok) {
|
|
41
|
+
throw new Error(`Failed to list IR versions from ${constants_js_1.DEFAULT_IR_LIST_URL}: HTTP ${res.status}`);
|
|
42
|
+
}
|
|
43
|
+
const body = (await res.json());
|
|
44
|
+
const names = body.items?.map((item) => item.name) ?? [];
|
|
45
|
+
const versions = parseIrVersions(names);
|
|
46
|
+
if (versions.length === 0) {
|
|
47
|
+
throw new Error(`No versioned IR objects found in bucket with prefix "${constants_js_1.DEFAULT_IR_PREFIX}".`);
|
|
48
|
+
}
|
|
49
|
+
const version = versions.reduce((latest, current) => compareSemver(current, latest) > 0 ? current : latest);
|
|
50
|
+
return {
|
|
51
|
+
version,
|
|
52
|
+
url: `${constants_js_1.DEFAULT_IR_BASE}/${constants_js_1.DEFAULT_IR_PREFIX}${version}.json`,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.resolveLatestIr = resolveLatestIr;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSchemaProvider = exports.resolveSchemaProvider = void 0;
|
|
4
|
+
const bundled_provider_js_1 = require("./bundled-provider.js");
|
|
5
|
+
const remote_provider_js_1 = require("./remote-provider.js");
|
|
6
|
+
const REMOTE_VALIDATION_ERROR = 'Remote validation requires async initialization. Use DecodoClient.create() instead of new DecodoClient().';
|
|
7
|
+
const resolveSchemaProvider = (validation) => {
|
|
8
|
+
if (validation?.source === undefined || validation.source === 'bundled') {
|
|
9
|
+
return bundled_provider_js_1.bundledSchemaProvider;
|
|
10
|
+
}
|
|
11
|
+
throw new Error(REMOTE_VALIDATION_ERROR);
|
|
12
|
+
};
|
|
13
|
+
exports.resolveSchemaProvider = resolveSchemaProvider;
|
|
14
|
+
const createSchemaProvider = async (validation) => {
|
|
15
|
+
if (validation?.source === undefined || validation.source === 'bundled') {
|
|
16
|
+
return bundled_provider_js_1.bundledSchemaProvider;
|
|
17
|
+
}
|
|
18
|
+
if (validation.source === 'remote') {
|
|
19
|
+
return (0, remote_provider_js_1.createRemoteSchemaProvider)({
|
|
20
|
+
url: validation.url,
|
|
21
|
+
cachePath: validation.cachePath,
|
|
22
|
+
ttlMs: validation.ttlMs,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return bundled_provider_js_1.bundledSchemaProvider;
|
|
26
|
+
};
|
|
27
|
+
exports.createSchemaProvider = createSchemaProvider;
|
|
@@ -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
|
+
};
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { HttpClient } from '../http.js';
|
|
2
|
+
import type { DecodoSchema } from '../schema/types.js';
|
|
2
3
|
import type { ScrapeRequest, BatchRequest } from '../generated/targets.js';
|
|
3
4
|
import type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse } from '../types/responses.js';
|
|
4
5
|
export declare class WebScrapingApi {
|
|
5
6
|
private readonly http;
|
|
6
|
-
|
|
7
|
+
private readonly schemas;
|
|
8
|
+
constructor(http: HttpClient, schemas?: DecodoSchema);
|
|
7
9
|
private validate;
|
|
8
10
|
scrape(params: ScrapeRequest): Promise<SyncResponse>;
|
|
9
11
|
scrapeAsync(params: ScrapeRequest): Promise<AsyncTaskResponse>;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { prettifyError } from 'zod';
|
|
2
2
|
import { ValidationError } from '../errors.js';
|
|
3
|
-
import {
|
|
3
|
+
import { BundledSchema } from '../schema/bundled-schema.js';
|
|
4
4
|
export class WebScrapingApi {
|
|
5
5
|
http;
|
|
6
|
-
|
|
6
|
+
schemas;
|
|
7
|
+
constructor(http, schemas = BundledSchema.shared) {
|
|
7
8
|
this.http = http;
|
|
9
|
+
this.schemas = schemas;
|
|
8
10
|
}
|
|
9
11
|
validate(params) {
|
|
10
|
-
const schema =
|
|
12
|
+
const schema = this.schemas.getRequestSchema(params.target);
|
|
11
13
|
if (!schema) {
|
|
12
14
|
return;
|
|
13
15
|
}
|
package/build/esm/client.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { WebScrapingApi } from './api/web-scraping-api.js';
|
|
2
|
+
import type { DecodoSchema } from './schema/types.js';
|
|
2
3
|
export type DecodoConfig = {
|
|
3
4
|
webScrapingApi?: {
|
|
4
5
|
token: string;
|
|
5
6
|
};
|
|
6
7
|
timeoutMs?: number;
|
|
8
|
+
schema?: DecodoSchema;
|
|
7
9
|
};
|
|
8
10
|
export declare class DecodoClient {
|
|
9
11
|
readonly webScrapingApi: WebScrapingApi;
|