@decodo/sdk-ts 4.0.0 → 4.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.
@@ -2,9 +2,15 @@ export { DecodoClient } from './client.js';
2
2
  export type { DecodoConfig, WebScrapingApiConfig } from './client.js';
3
3
  export { WebScrapingApi } from './api/web-scraping-api.js';
4
4
  export type { WebScrapingApiRoutes } from './api/web-scraping-api.js';
5
+ export { Flows } from './api/flows.js';
6
+ export type { JobResultsOptions, JobResultsPage, JobResultsPageOptions, WaitOptions, } from './api/flows.js';
7
+ export { limitResult } from './api/limits.js';
8
+ export type { LimitedResult } from './api/limits.js';
9
+ export { readResult } from './api/results.js';
10
+ export { buildToolInputSchema, buildToolRequest, runTool } from './api/tools.js';
5
11
  export { BundledSchema } from './schema/bundled-schema.js';
6
12
  export { RemoteSchema } from './schema/remote-schema.js';
7
- export type { DecodoSchema, RemoteSchemaLoadOptions } from './schema/types.js';
13
+ export type { DecodoSchema, FlowSchema, IrBatchFlow, IrCallFlow, IrFlow, IrFlowSections, IrJobFlow, IrKeyCheck, IrOperation, IrResult, IrResultFailure, IrSurface, IrSurfaceLimits, IrSurfaceTool, RemoteSchemaLoadOptions, } from './schema/types.js';
8
14
  export type { TargetString, TargetParamsMap, TargetMeta, ScrapeRequest, BatchRequest, } from './generated/targets.js';
9
15
  export { targets, targetMeta, Target, } from './generated/targets.js';
10
16
  export { requestSchemas } from './generated/request-schemas.js';
@@ -14,4 +20,4 @@ export type { ParameterMeta } from './generated/parameters.js';
14
20
  export type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse, TaskStatus, ResultEntry, CrawlPage, CrawlPageList, CrawlResult, } from './types/responses.js';
15
21
  export type { CrawlOptions, CrawlPagesOptions, CrawlResultsOptions, } from './types/requests.js';
16
22
  export type { RunSearchRequest, RunSearchResponse, MapSiteRequest, MapSiteResponse, StartCrawlRequest, StartCrawlResponse, GetCrawlStatusResponse, ListCrawlPagesResponse, CancelCrawlResponse, } from './generated/data-api.js';
17
- export { DecodoError, AuthenticationError, RateLimitError, ValidationError, TimeoutError, } from './errors.js';
23
+ export { DecodoError, AuthenticationError, RateLimitError, ValidationError, TimeoutError, ResultError, JobTimeoutError, } from './errors.js';
@@ -1,9 +1,13 @@
1
1
  export { DecodoClient } from './client.js';
2
2
  export { WebScrapingApi } from './api/web-scraping-api.js';
3
+ export { Flows } from './api/flows.js';
4
+ export { limitResult } from './api/limits.js';
5
+ export { readResult } from './api/results.js';
6
+ export { buildToolInputSchema, buildToolRequest, runTool } from './api/tools.js';
3
7
  export { BundledSchema } from './schema/bundled-schema.js';
4
8
  export { RemoteSchema } from './schema/remote-schema.js';
5
9
  export { targets, targetMeta, Target, } from './generated/targets.js';
6
10
  export { requestSchemas } from './generated/request-schemas.js';
7
11
  export { dataApiRequestSchemas } from './generated/data-api.js';
8
12
  export { parameterMeta } from './generated/parameters.js';
9
- export { DecodoError, AuthenticationError, RateLimitError, ValidationError, TimeoutError, } from './errors.js';
13
+ export { DecodoError, AuthenticationError, RateLimitError, ValidationError, TimeoutError, ResultError, JobTimeoutError, } from './errors.js';
@@ -1,10 +1,15 @@
1
1
  import type { JSONSchema4 } from 'json-schema';
2
- import type { DecodoSchema } from './types.js';
3
- export declare class BundledSchema implements DecodoSchema {
2
+ import type { FlowSchema, IrFlow, IrKeyCheck, IrOperation, IrSurface } from './types.js';
3
+ export declare class BundledSchema implements FlowSchema {
4
4
  static readonly shared: BundledSchema;
5
+ readonly version = "2.7.0";
5
6
  getRequestSchema(target: string): import("zod").ZodObject<import("zod/v4/core").$ZodLooseShape, import("zod/v4/core").$strip>;
6
7
  listTargets(): string[];
7
8
  getTargetMeta(target: string): import("../index.js").TargetMeta;
8
9
  getTargetParameterSchema(target: string): JSONSchema4 | undefined;
9
10
  getSharedParameters(): Record<string, unknown>;
11
+ getOperation(operation: string): IrOperation | undefined;
12
+ getFlow(flow: string): IrFlow | undefined;
13
+ getSurface(client: string): IrSurface | undefined;
14
+ getKeyCheck(): IrKeyCheck | undefined;
10
15
  }
@@ -1,7 +1,9 @@
1
1
  import { requestJsonSchemas, requestSchemas, } from '../generated/request-schemas.js';
2
2
  import { targetMeta, targets } from '../generated/targets.js';
3
+ import { flowSections, flowSectionsVersion } from '../generated/flows.js';
3
4
  export class BundledSchema {
4
5
  static shared = new BundledSchema();
6
+ version = flowSectionsVersion;
5
7
  getRequestSchema(target) {
6
8
  return requestSchemas[target];
7
9
  }
@@ -17,4 +19,16 @@ export class BundledSchema {
17
19
  getSharedParameters() {
18
20
  return {};
19
21
  }
22
+ getOperation(operation) {
23
+ return flowSections.operations[operation];
24
+ }
25
+ getFlow(flow) {
26
+ return flowSections.flows[flow];
27
+ }
28
+ getSurface(client) {
29
+ return flowSections.surfaces[client];
30
+ }
31
+ getKeyCheck() {
32
+ return flowSections.keyCheck;
33
+ }
20
34
  }
@@ -1,14 +1,15 @@
1
1
  import type { JSONSchema4 } from 'json-schema';
2
2
  import * as z from 'zod';
3
3
  import type { TargetMeta } from '../generated/targets.js';
4
- import type { DecodoSchema, RemoteSchemaLoadOptions } from './types.js';
5
- export declare class RemoteSchema implements DecodoSchema {
4
+ import type { FlowSchema, IrFlow, IrKeyCheck, IrOperation, IrSurface, RemoteSchemaLoadOptions } from './types.js';
5
+ export declare class RemoteSchema implements FlowSchema {
6
6
  readonly version: string;
7
7
  private readonly schemas;
8
8
  private readonly meta;
9
9
  private readonly parameterSchemas;
10
10
  private readonly sharedParameters;
11
11
  private readonly targetKeys;
12
+ private readonly flowSections;
12
13
  private constructor();
13
14
  static load(config?: RemoteSchemaLoadOptions): Promise<RemoteSchema>;
14
15
  getRequestSchema(target: string): z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> | undefined;
@@ -16,4 +17,8 @@ export declare class RemoteSchema implements DecodoSchema {
16
17
  getTargetMeta(target: string): TargetMeta | undefined;
17
18
  getTargetParameterSchema(target: string): JSONSchema4 | undefined;
18
19
  getSharedParameters(): Record<string, unknown>;
20
+ getOperation(operation: string): IrOperation | undefined;
21
+ getFlow(flow: string): IrFlow | undefined;
22
+ getSurface(client: string): IrSurface | undefined;
23
+ getKeyCheck(): IrKeyCheck | undefined;
19
24
  }
@@ -1,14 +1,21 @@
1
1
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { dirname } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
3
4
  import * as z from 'zod';
4
5
  import { buildTargetMeta } from './build-target-meta.js';
5
6
  import { DEFAULT_IR_CACHE_PATH } from './constants.js';
6
7
  import { expandPath } from './expand-path.js';
7
8
  import { resolveLatestIr } from './resolve-latest-ir.js';
9
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
10
+ const isCachedIr = (value) => isRecord(value) &&
11
+ typeof value.fetchedAt === 'number' &&
12
+ typeof value.resolvedUrl === 'string' &&
13
+ isRecord(value.ir) &&
14
+ typeof value.ir.version === 'string';
8
15
  const readCachedIr = (cachePath) => {
9
16
  try {
10
- const raw = readFileSync(cachePath, 'utf-8');
11
- return JSON.parse(raw);
17
+ const parsed = JSON.parse(readFileSync(cachePath, 'utf-8'));
18
+ return isCachedIr(parsed) ? parsed : null;
12
19
  }
13
20
  catch {
14
21
  return null;
@@ -20,7 +27,11 @@ const isCacheFresh = (cached, ttlMs) => {
20
27
  }
21
28
  return Date.now() - cached.fetchedAt < ttlMs;
22
29
  };
30
+ const isFileUrl = (url) => url.startsWith('file:');
23
31
  const fetchIr = async (url) => {
32
+ if (isFileUrl(url)) {
33
+ return JSON.parse(readFileSync(fileURLToPath(url), 'utf-8'));
34
+ }
24
35
  const res = await fetch(url, {
25
36
  headers: { Accept: 'application/json' },
26
37
  });
@@ -30,8 +41,13 @@ const fetchIr = async (url) => {
30
41
  return (await res.json());
31
42
  };
32
43
  const writeCachedIr = (cachePath, cached) => {
33
- mkdirSync(dirname(cachePath), { recursive: true });
34
- writeFileSync(cachePath, JSON.stringify(cached, null, 2));
44
+ try {
45
+ mkdirSync(dirname(cachePath), { recursive: true });
46
+ writeFileSync(cachePath, JSON.stringify(cached, null, 2));
47
+ }
48
+ catch {
49
+ return;
50
+ }
35
51
  };
36
52
  const resolveIrUrl = async (config) => {
37
53
  if (config.url) {
@@ -40,26 +56,57 @@ const resolveIrUrl = async (config) => {
40
56
  const latest = await resolveLatestIr();
41
57
  return { url: latest.url, version: latest.version };
42
58
  };
43
- const loadIr = async (config) => {
44
- const cachePath = expandPath(config.cachePath ?? DEFAULT_IR_CACHE_PATH);
45
- const cached = readCachedIr(cachePath);
59
+ const isCacheOf = (cached, url) => url ? cached.resolvedUrl === url : cached.latest === true;
60
+ const refreshIr = async (config, cachePath, cached) => {
46
61
  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
- }
62
+ if (cached?.resolvedUrl === resolved.url &&
63
+ cached.ir.version === resolved.version) {
64
+ writeCachedIr(cachePath, {
65
+ ...cached,
66
+ fetchedAt: Date.now(),
67
+ latest: !config.url,
68
+ });
69
+ return cached.ir;
54
70
  }
55
71
  const ir = await fetchIr(resolved.url);
56
72
  writeCachedIr(cachePath, {
57
73
  fetchedAt: Date.now(),
58
74
  resolvedUrl: resolved.url,
75
+ latest: !config.url,
59
76
  ir,
60
77
  });
61
78
  return ir;
62
79
  };
80
+ const loadIr = async (config) => {
81
+ if (config.url && isFileUrl(config.url)) {
82
+ return fetchIr(config.url);
83
+ }
84
+ const cachePath = expandPath(config.cachePath ?? DEFAULT_IR_CACHE_PATH);
85
+ const cached = readCachedIr(cachePath);
86
+ const usable = cached && isCacheOf(cached, config.url) ? cached : null;
87
+ if (usable &&
88
+ (config.url ||
89
+ (config.ttlMs !== undefined && isCacheFresh(usable, config.ttlMs)))) {
90
+ return usable.ir;
91
+ }
92
+ try {
93
+ return await refreshIr(config, cachePath, cached);
94
+ }
95
+ catch (error) {
96
+ if (!usable) {
97
+ throw error;
98
+ }
99
+ const message = error instanceof Error ? error.message : String(error);
100
+ process.emitWarning(`Could not refresh the Decodo IR (${message}); using the cached copy.`);
101
+ return usable.ir;
102
+ }
103
+ };
104
+ const flowSectionsOf = (ir) => ({
105
+ operations: ir.operations ?? {},
106
+ flows: ir.flows ?? {},
107
+ surfaces: ir.surfaces ?? {},
108
+ keyCheck: ir.keyCheck,
109
+ });
63
110
  const buildRequestSchemas = (ir) => {
64
111
  const schemas = new Map();
65
112
  for (const [targetKey, target] of Object.entries(ir.apis.webScrapingApi.targets)) {
@@ -81,6 +128,7 @@ export class RemoteSchema {
81
128
  parameterSchemas;
82
129
  sharedParameters;
83
130
  targetKeys;
131
+ flowSections;
84
132
  constructor(ir, schemas) {
85
133
  this.version = ir.version;
86
134
  this.schemas = schemas;
@@ -88,6 +136,7 @@ export class RemoteSchema {
88
136
  this.parameterSchemas = buildParameterSchemas(ir);
89
137
  this.sharedParameters = ir.apis.webScrapingApi.parameters ?? {};
90
138
  this.targetKeys = Object.keys(ir.apis.webScrapingApi.targets);
139
+ this.flowSections = flowSectionsOf(ir);
91
140
  }
92
141
  static async load(config = {}) {
93
142
  const ir = await loadIr(config);
@@ -109,4 +158,16 @@ export class RemoteSchema {
109
158
  getSharedParameters() {
110
159
  return this.sharedParameters;
111
160
  }
161
+ getOperation(operation) {
162
+ return this.flowSections.operations[operation];
163
+ }
164
+ getFlow(flow) {
165
+ return this.flowSections.flows[flow];
166
+ }
167
+ getSurface(client) {
168
+ return this.flowSections.surfaces[client];
169
+ }
170
+ getKeyCheck() {
171
+ return this.flowSections.keyCheck;
172
+ }
112
173
  }
@@ -11,6 +11,93 @@ export type IrTarget = {
11
11
  response_format: string;
12
12
  parameter_schema: JSONSchema4;
13
13
  };
14
+ export type IrOperation = {
15
+ method: string;
16
+ path: string;
17
+ requestSchema?: JSONSchema4;
18
+ };
19
+ export type IrCallFlow = {
20
+ pattern: 'call';
21
+ run: string;
22
+ };
23
+ export type IrJobFlow = {
24
+ pattern: 'job';
25
+ start: string;
26
+ status: string;
27
+ results: string;
28
+ cancel?: string;
29
+ idField: string;
30
+ statusField: string;
31
+ pendingStatuses: string[];
32
+ failedStatuses: string[];
33
+ resultsQuery?: Record<string, string>;
34
+ pagination?: {
35
+ itemsField: string;
36
+ nextUrlField: string;
37
+ };
38
+ pollSeconds?: number;
39
+ };
40
+ export type IrBatchFlow = {
41
+ pattern: 'batch';
42
+ start: string;
43
+ itemsField: string;
44
+ job: string;
45
+ };
46
+ export type IrFlow = IrCallFlow | IrJobFlow | IrBatchFlow;
47
+ export type IrKeyCheck = {
48
+ flow: string;
49
+ params: Record<string, unknown>;
50
+ };
51
+ export type IrResultFailure = {
52
+ path: string;
53
+ equals?: string | number | boolean;
54
+ atLeast?: number;
55
+ message?: string;
56
+ code?: string;
57
+ retryable?: true;
58
+ };
59
+ export type IrResult = {
60
+ failures?: IrResultFailure[];
61
+ from?: string;
62
+ fields?: Record<string, string>;
63
+ omit?: string[];
64
+ required?: string[];
65
+ };
66
+ export type IrSurfaceLimits = {
67
+ maxResultChars?: number;
68
+ maxTextChars?: number;
69
+ maxItems?: number;
70
+ maxListValues?: number;
71
+ waitSeconds?: number;
72
+ };
73
+ export type IrSurfaceTool = {
74
+ name: string;
75
+ title?: string;
76
+ description: string;
77
+ group?: string;
78
+ flow: string;
79
+ step?: 'start' | 'status' | 'results' | 'cancel';
80
+ result?: IrResult;
81
+ limits?: IrSurfaceLimits;
82
+ target?: string;
83
+ fields?: string[];
84
+ required?: string[];
85
+ fixed?: Record<string, unknown>;
86
+ defaults?: Record<string, unknown>;
87
+ choices?: Record<string, Array<string | number | boolean>>;
88
+ maximums?: Record<string, number>;
89
+ minimums?: Record<string, number>;
90
+ descriptions?: Record<string, string>;
91
+ };
92
+ export type IrSurface = {
93
+ tools: IrSurfaceTool[];
94
+ };
95
+ export type IrFlowSections = {
96
+ operations: Record<string, IrOperation>;
97
+ flows: Record<string, IrFlow>;
98
+ surfaces: Record<string, IrSurface>;
99
+ keyCheck?: IrKeyCheck;
100
+ };
14
101
  export type RemoteIr = {
15
102
  version: string;
16
103
  apis: {
@@ -19,10 +106,11 @@ export type RemoteIr = {
19
106
  targets: Record<string, IrTarget>;
20
107
  };
21
108
  };
22
- };
109
+ } & Partial<IrFlowSections>;
23
110
  export type CachedIr = {
24
111
  fetchedAt: number;
25
112
  resolvedUrl: string;
113
+ latest?: boolean;
26
114
  ir: RemoteIr;
27
115
  };
28
116
  export type GcsObjectList = {
@@ -42,3 +130,9 @@ export type DecodoSchema = {
42
130
  getSharedParameters: () => Record<string, unknown>;
43
131
  readonly version?: string;
44
132
  };
133
+ export type FlowSchema = DecodoSchema & {
134
+ getOperation: (operation: string) => IrOperation | undefined;
135
+ getFlow: (flow: string) => IrFlow | undefined;
136
+ getSurface: (client: string) => IrSurface | undefined;
137
+ getKeyCheck: () => IrKeyCheck | undefined;
138
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decodo/sdk-ts",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "Official TypeScript SDK for the Decodo APIs",
5
5
  "license": "MIT",
6
6
  "repository": {