@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
package/build/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export { DecodoClient } from './client.js';
|
|
2
2
|
export type { DecodoConfig } from './client.js';
|
|
3
3
|
export { WebScrapingApi } from './api/web-scraping-api.js';
|
|
4
|
+
export { BundledSchema } from './schema/bundled-schema.js';
|
|
5
|
+
export { RemoteSchema } from './schema/remote-schema.js';
|
|
6
|
+
export type { DecodoSchema, RemoteSchemaLoadOptions } from './schema/types.js';
|
|
4
7
|
export type { TargetString, TargetParamsMap, TargetMeta, ScrapeRequest, BatchRequest, } from './generated/targets.js';
|
|
5
8
|
export { targets, targetMeta, Target, } from './generated/targets.js';
|
|
6
9
|
export { parameterMeta } from './generated/parameters.js';
|
package/build/esm/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { DecodoClient } from './client.js';
|
|
2
2
|
export { WebScrapingApi } from './api/web-scraping-api.js';
|
|
3
|
+
export { BundledSchema } from './schema/bundled-schema.js';
|
|
4
|
+
export { RemoteSchema } from './schema/remote-schema.js';
|
|
3
5
|
export { targets, targetMeta, Target, } from './generated/targets.js';
|
|
4
6
|
export { parameterMeta } from './generated/parameters.js';
|
|
5
7
|
export { DecodoError, AuthenticationError, RateLimitError, ValidationError, TimeoutError, } from './errors.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
export const buildRequestSchemas = (api) => {
|
|
3
|
+
const schemas = new Map();
|
|
4
|
+
for (const [targetKey, target] of Object.entries(api.targets)) {
|
|
5
|
+
schemas.set(targetKey, z.fromJSONSchema(target.parameter_schema));
|
|
6
|
+
}
|
|
7
|
+
return schemas;
|
|
8
|
+
};
|
|
9
|
+
export const validateRequestSchemas = (api) => {
|
|
10
|
+
for (const [targetKey, target] of Object.entries(api.targets)) {
|
|
11
|
+
try {
|
|
12
|
+
z.fromJSONSchema(target.parameter_schema);
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
throw new Error(`Invalid parameter_schema for target "${targetKey}": ${err.message}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const getTargetParameterKeys = (parameterSchema) => {
|
|
2
|
+
const properties = parameterSchema.properties ?? {};
|
|
3
|
+
return Object.keys(properties).filter((key) => key !== 'target');
|
|
4
|
+
};
|
|
5
|
+
export const buildTargetMeta = (targets) => {
|
|
6
|
+
const meta = new Map();
|
|
7
|
+
for (const [targetKey, target] of Object.entries(targets)) {
|
|
8
|
+
meta.set(targetKey, {
|
|
9
|
+
group: target.group,
|
|
10
|
+
response_format: target.response_format,
|
|
11
|
+
parameters: getTargetParameterKeys(target.parameter_schema),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return meta;
|
|
15
|
+
};
|
|
@@ -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("../index.js").TargetMeta;
|
|
8
|
+
getTargetParameterSchema(target: string): JSONSchema4 | undefined;
|
|
9
|
+
getSharedParameters(): Record<string, unknown>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { requestJsonSchemas, requestSchemas, } from '../generated/request-schemas.js';
|
|
2
|
+
import { targetMeta, targets } from '../generated/targets.js';
|
|
3
|
+
export class BundledSchema {
|
|
4
|
+
static shared = new BundledSchema();
|
|
5
|
+
getRequestSchema(target) {
|
|
6
|
+
return requestSchemas[target];
|
|
7
|
+
}
|
|
8
|
+
listTargets() {
|
|
9
|
+
return [...targets];
|
|
10
|
+
}
|
|
11
|
+
getTargetMeta(target) {
|
|
12
|
+
return targetMeta[target];
|
|
13
|
+
}
|
|
14
|
+
getTargetParameterSchema(target) {
|
|
15
|
+
return requestJsonSchemas[target];
|
|
16
|
+
}
|
|
17
|
+
getSharedParameters() {
|
|
18
|
+
return {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -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,5 @@
|
|
|
1
|
+
export const DEFAULT_IR_BUCKET = 'decodo-sdk-config';
|
|
2
|
+
export const DEFAULT_IR_PREFIX = 'decodo-ir-v';
|
|
3
|
+
export const DEFAULT_IR_BASE = 'https://storage.googleapis.com/decodo-sdk-config';
|
|
4
|
+
export const DEFAULT_IR_LIST_URL = 'https://storage.googleapis.com/storage/v1/b/decodo-sdk-config/o?prefix=decodo-ir-v';
|
|
5
|
+
export const DEFAULT_IR_CACHE_PATH = '~/.decodo/decodo.ir.json';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const expandPath: (path: string) => string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
export const expandPath = (path) => {
|
|
4
|
+
if (path.startsWith('~/')) {
|
|
5
|
+
return resolve(homedir(), path.slice(2));
|
|
6
|
+
}
|
|
7
|
+
if (path === '~') {
|
|
8
|
+
return homedir();
|
|
9
|
+
}
|
|
10
|
+
return resolve(path);
|
|
11
|
+
};
|
|
@@ -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,83 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import * as z from 'zod';
|
|
4
|
+
import { DEFAULT_IR_CACHE_PATH } from './constants.js';
|
|
5
|
+
import { expandPath } from './expand-path.js';
|
|
6
|
+
import { resolveLatestIr } from './resolve-latest-ir.js';
|
|
7
|
+
const readCachedIr = (cachePath) => {
|
|
8
|
+
try {
|
|
9
|
+
const raw = readFileSync(cachePath, 'utf-8');
|
|
10
|
+
return JSON.parse(raw);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const isCacheFresh = (cached, ttlMs) => {
|
|
17
|
+
if (ttlMs === undefined) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return Date.now() - cached.fetchedAt < ttlMs;
|
|
21
|
+
};
|
|
22
|
+
const fetchIr = async (url) => {
|
|
23
|
+
const res = await fetch(url, {
|
|
24
|
+
headers: { Accept: 'application/json' },
|
|
25
|
+
});
|
|
26
|
+
if (!res.ok) {
|
|
27
|
+
throw new Error(`Failed to fetch IR from ${url}: HTTP ${res.status}`);
|
|
28
|
+
}
|
|
29
|
+
return (await res.json());
|
|
30
|
+
};
|
|
31
|
+
const writeCachedIr = (cachePath, cached) => {
|
|
32
|
+
mkdirSync(dirname(cachePath), { recursive: true });
|
|
33
|
+
writeFileSync(cachePath, JSON.stringify(cached, null, 2));
|
|
34
|
+
};
|
|
35
|
+
const resolveIrUrl = async (config) => {
|
|
36
|
+
if (config.url) {
|
|
37
|
+
return { url: config.url };
|
|
38
|
+
}
|
|
39
|
+
const latest = await resolveLatestIr();
|
|
40
|
+
return { url: latest.url, version: latest.version };
|
|
41
|
+
};
|
|
42
|
+
const loadIr = async (config) => {
|
|
43
|
+
const cachePath = expandPath(config.cachePath ?? DEFAULT_IR_CACHE_PATH);
|
|
44
|
+
const cached = readCachedIr(cachePath);
|
|
45
|
+
if (cached && isCacheFresh(cached, config.ttlMs)) {
|
|
46
|
+
return cached.ir;
|
|
47
|
+
}
|
|
48
|
+
const resolved = await resolveIrUrl(config);
|
|
49
|
+
if (cached) {
|
|
50
|
+
if (config.url) {
|
|
51
|
+
if (cached.resolvedUrl === config.url) {
|
|
52
|
+
return cached.ir;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (resolved.version && cached.ir.version === resolved.version) {
|
|
56
|
+
return cached.ir;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const ir = await fetchIr(resolved.url);
|
|
60
|
+
writeCachedIr(cachePath, {
|
|
61
|
+
fetchedAt: Date.now(),
|
|
62
|
+
resolvedUrl: resolved.url,
|
|
63
|
+
ir,
|
|
64
|
+
});
|
|
65
|
+
return ir;
|
|
66
|
+
};
|
|
67
|
+
const buildRequestSchemas = (ir) => {
|
|
68
|
+
const schemas = new Map();
|
|
69
|
+
const targets = ir.apis.webScrapingApi.targets;
|
|
70
|
+
for (const [targetKey, target] of Object.entries(targets)) {
|
|
71
|
+
schemas.set(targetKey, z.fromJSONSchema(target.parameter_schema));
|
|
72
|
+
}
|
|
73
|
+
return schemas;
|
|
74
|
+
};
|
|
75
|
+
export const createRemoteSchemaProvider = async (config = {}) => {
|
|
76
|
+
const ir = await loadIr(config);
|
|
77
|
+
const schemas = buildRequestSchemas(ir);
|
|
78
|
+
return {
|
|
79
|
+
getRequestSchema(target) {
|
|
80
|
+
return schemas.get(target);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
};
|
|
@@ -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,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,22 @@
|
|
|
1
|
+
import { bundledSchemaProvider } from './bundled-provider.js';
|
|
2
|
+
import { createRemoteSchemaProvider } from './remote-provider.js';
|
|
3
|
+
const REMOTE_VALIDATION_ERROR = 'Remote validation requires async initialization. Use DecodoClient.create() instead of new DecodoClient().';
|
|
4
|
+
export const resolveSchemaProvider = (validation) => {
|
|
5
|
+
if (validation?.source === undefined || validation.source === 'bundled') {
|
|
6
|
+
return bundledSchemaProvider;
|
|
7
|
+
}
|
|
8
|
+
throw new Error(REMOTE_VALIDATION_ERROR);
|
|
9
|
+
};
|
|
10
|
+
export const createSchemaProvider = async (validation) => {
|
|
11
|
+
if (validation?.source === undefined || validation.source === 'bundled') {
|
|
12
|
+
return bundledSchemaProvider;
|
|
13
|
+
}
|
|
14
|
+
if (validation.source === 'remote') {
|
|
15
|
+
return createRemoteSchemaProvider({
|
|
16
|
+
url: validation.url,
|
|
17
|
+
cachePath: validation.cachePath,
|
|
18
|
+
ttlMs: validation.ttlMs,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return bundledSchemaProvider;
|
|
22
|
+
};
|
|
@@ -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 {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decodo/sdk-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Official TypeScript SDK for the Decodo APIs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./build/cjs/index.cjs",
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
"build:cjs": "tsc -p tsconfig.build.cjs.json",
|
|
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"
|
|
32
34
|
},
|
|
33
35
|
"engines": {
|
|
34
36
|
"node": ">=18.0.0"
|
|
@@ -40,7 +42,8 @@
|
|
|
40
42
|
"eslint": "^9.39.1",
|
|
41
43
|
"tsx": "^4.19.0",
|
|
42
44
|
"typescript": "^5.3.3",
|
|
43
|
-
"typescript-eslint": "^8.40.0"
|
|
45
|
+
"typescript-eslint": "^8.40.0",
|
|
46
|
+
"vitest": "^4.1.7"
|
|
44
47
|
},
|
|
45
48
|
"dependencies": {
|
|
46
49
|
"json-schema-to-typescript": "^15.0.4",
|