@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.
- package/README.md +56 -4
- package/build/cjs/index.cjs +5557 -191
- package/build/cjs/index.d.cts +174 -2
- package/build/esm/api/flows.d.ts +52 -0
- package/build/esm/api/flows.js +301 -0
- package/build/esm/api/limits.d.ts +6 -0
- package/build/esm/api/limits.js +36 -0
- package/build/esm/api/results.d.ts +2 -0
- package/build/esm/api/results.js +87 -0
- package/build/esm/api/tools.d.ts +6 -0
- package/build/esm/api/tools.js +185 -0
- package/build/esm/api/web-scraping-api.d.ts +2 -0
- package/build/esm/api/web-scraping-api.js +3 -0
- package/build/esm/errors.d.ts +10 -0
- package/build/esm/errors.js +20 -0
- package/build/esm/generated/flows.d.ts +3 -0
- package/build/esm/generated/flows.js +4633 -0
- package/build/esm/index.d.ts +8 -2
- package/build/esm/index.js +5 -1
- package/build/esm/schema/bundled-schema.d.ts +7 -2
- package/build/esm/schema/bundled-schema.js +14 -0
- package/build/esm/schema/remote-schema.d.ts +7 -2
- package/build/esm/schema/remote-schema.js +75 -14
- package/build/esm/schema/types.d.ts +95 -1
- package/package.json +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ResultError } from '../errors.js';
|
|
2
|
+
const MISSING = Symbol('missing');
|
|
3
|
+
const RETRYABLE_STATUS = 429;
|
|
4
|
+
const SERVER_ERROR = 500;
|
|
5
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
6
|
+
const isNumber = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
7
|
+
const at = (value, path) => {
|
|
8
|
+
const [segment, ...restSegments] = path.split('.');
|
|
9
|
+
const rest = restSegments.join('.');
|
|
10
|
+
if (segment === '*') {
|
|
11
|
+
if (!Array.isArray(value)) {
|
|
12
|
+
return MISSING;
|
|
13
|
+
}
|
|
14
|
+
return value
|
|
15
|
+
.map((item) => (rest ? at(item, rest) : item))
|
|
16
|
+
.filter((item) => item !== MISSING);
|
|
17
|
+
}
|
|
18
|
+
let step = MISSING;
|
|
19
|
+
if (/^\d+$/.test(segment)) {
|
|
20
|
+
const index = Number(segment);
|
|
21
|
+
step =
|
|
22
|
+
Array.isArray(value) && index < value.length ? value[index] : MISSING;
|
|
23
|
+
}
|
|
24
|
+
else if (isRecord(value) && segment in value) {
|
|
25
|
+
step = value[segment];
|
|
26
|
+
}
|
|
27
|
+
return rest && step !== MISSING ? at(step, rest) : step;
|
|
28
|
+
};
|
|
29
|
+
const throwIfFailed = (failure, response) => {
|
|
30
|
+
const value = at(response, failure.path);
|
|
31
|
+
const failed = failure.equals !== undefined
|
|
32
|
+
? value !== MISSING && value === failure.equals
|
|
33
|
+
: isNumber(value) && value >= (failure.atLeast ?? Infinity);
|
|
34
|
+
if (!failed) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const reported = failure.code ? at(response, failure.code) : MISSING;
|
|
38
|
+
const code = reported === MISSING || reported === null ? value : reported;
|
|
39
|
+
const message = failure.message ? at(response, failure.message) : MISSING;
|
|
40
|
+
const retryable = Boolean(failure.retryable) ||
|
|
41
|
+
(isNumber(code) && (code === RETRYABLE_STATUS || code >= SERVER_ERROR));
|
|
42
|
+
const text = typeof message === 'string' && message
|
|
43
|
+
? message
|
|
44
|
+
: `Decodo reported a failure (${String(code)}).`;
|
|
45
|
+
throw new ResultError(text, String(code), retryable);
|
|
46
|
+
};
|
|
47
|
+
const isEmpty = (value) => value === undefined ||
|
|
48
|
+
value === null ||
|
|
49
|
+
value === '' ||
|
|
50
|
+
(Array.isArray(value) && value.length === 0) ||
|
|
51
|
+
(isRecord(value) && Object.keys(value).length === 0);
|
|
52
|
+
const pick = (item, rule) => {
|
|
53
|
+
if (rule.fields && Object.keys(rule.fields).length > 0) {
|
|
54
|
+
const picked = {};
|
|
55
|
+
for (const [name, path] of Object.entries(rule.fields)) {
|
|
56
|
+
const value = at(item, path);
|
|
57
|
+
if (value !== MISSING) {
|
|
58
|
+
picked[name] = value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const name of rule.required ?? []) {
|
|
62
|
+
if (isEmpty(picked[name])) {
|
|
63
|
+
throw new ResultError(`Decodo returned a result with no ${name}.`, `empty_${name}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return picked;
|
|
67
|
+
}
|
|
68
|
+
if (rule.omit && rule.omit.length > 0 && isRecord(item)) {
|
|
69
|
+
return Object.fromEntries(Object.entries(item).filter(([name]) => !rule.omit?.includes(name)));
|
|
70
|
+
}
|
|
71
|
+
return item;
|
|
72
|
+
};
|
|
73
|
+
export const readResult = (rule, response) => {
|
|
74
|
+
if (!rule) {
|
|
75
|
+
return response;
|
|
76
|
+
}
|
|
77
|
+
for (const failure of rule.failures ?? []) {
|
|
78
|
+
throwIfFailed(failure, response);
|
|
79
|
+
}
|
|
80
|
+
const result = rule.from ? at(response, rule.from) : response;
|
|
81
|
+
if (result === MISSING || result === null || result === undefined) {
|
|
82
|
+
throw new ResultError('Decodo returned no result.', 'empty_response');
|
|
83
|
+
}
|
|
84
|
+
return Array.isArray(result)
|
|
85
|
+
? result.map((item) => pick(item, rule))
|
|
86
|
+
: pick(result, rule);
|
|
87
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { JSONSchema4 } from 'json-schema';
|
|
2
|
+
import type { FlowSchema, IrSurfaceTool } from '../schema/types.js';
|
|
3
|
+
import type { Flows } from './flows.js';
|
|
4
|
+
export declare const buildToolInputSchema: (schema: FlowSchema, tool: IrSurfaceTool) => JSONSchema4;
|
|
5
|
+
export declare const buildToolRequest: (tool: IrSurfaceTool, args: Record<string, unknown>) => Record<string, unknown>;
|
|
6
|
+
export declare const runTool: (flows: Flows, tool: IrSurfaceTool, args: Record<string, unknown>) => Promise<unknown>;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { JobTimeoutError, ResultError } from '../errors.js';
|
|
2
|
+
import { readResult } from './results.js';
|
|
3
|
+
const isIdStep = (tool) => tool.step === 'status' || tool.step === 'results' || tool.step === 'cancel';
|
|
4
|
+
const CURSOR = 'cursor';
|
|
5
|
+
const flowOf = (schema, tool) => {
|
|
6
|
+
const flow = schema.getFlow(tool.flow);
|
|
7
|
+
if (!flow) {
|
|
8
|
+
throw new Error(`Tool "${tool.name}" names flow "${tool.flow}", which the schema lacks.`);
|
|
9
|
+
}
|
|
10
|
+
if (flow.pattern === 'job' && !tool.step) {
|
|
11
|
+
throw new Error(`Tool "${tool.name}" runs job flow "${tool.flow}" but names no step.`);
|
|
12
|
+
}
|
|
13
|
+
return flow;
|
|
14
|
+
};
|
|
15
|
+
const sourceSchema = (schema, tool, flow) => {
|
|
16
|
+
if (tool.target) {
|
|
17
|
+
const found = schema.getTargetParameterSchema(tool.target);
|
|
18
|
+
if (!found) {
|
|
19
|
+
throw new Error(`Tool "${tool.name}" names target "${tool.target}", which the schema lacks.`);
|
|
20
|
+
}
|
|
21
|
+
return found;
|
|
22
|
+
}
|
|
23
|
+
const operation = flow.pattern === 'call' ? flow.run : flow.start;
|
|
24
|
+
const found = schema.getOperation(operation)?.requestSchema;
|
|
25
|
+
if (!found) {
|
|
26
|
+
throw new Error(`Tool "${tool.name}" has no field list: operation "${operation}" has no request schema.`);
|
|
27
|
+
}
|
|
28
|
+
return found;
|
|
29
|
+
};
|
|
30
|
+
const shownProperty = (tool, name, api) => {
|
|
31
|
+
const property = { ...(api ?? {}) };
|
|
32
|
+
const description = tool.descriptions?.[name];
|
|
33
|
+
const choices = tool.choices?.[name];
|
|
34
|
+
if (description) {
|
|
35
|
+
property.description = description;
|
|
36
|
+
}
|
|
37
|
+
if (choices) {
|
|
38
|
+
property.enum = choices;
|
|
39
|
+
}
|
|
40
|
+
if (tool.defaults && name in tool.defaults) {
|
|
41
|
+
property.default = tool.defaults[name];
|
|
42
|
+
}
|
|
43
|
+
if (tool.minimums?.[name] !== undefined) {
|
|
44
|
+
property.minimum = tool.minimums[name];
|
|
45
|
+
}
|
|
46
|
+
if (tool.maximums?.[name] !== undefined) {
|
|
47
|
+
property.maximum = tool.maximums[name];
|
|
48
|
+
}
|
|
49
|
+
return property;
|
|
50
|
+
};
|
|
51
|
+
export const buildToolInputSchema = (schema, tool) => {
|
|
52
|
+
const flow = flowOf(schema, tool);
|
|
53
|
+
if (isIdStep(tool)) {
|
|
54
|
+
if (flow.pattern !== 'job') {
|
|
55
|
+
throw new Error(`Tool "${tool.name}" has step "${tool.step}", which only job flows have.`);
|
|
56
|
+
}
|
|
57
|
+
const paged = tool.step === 'results' && flow.pagination;
|
|
58
|
+
return {
|
|
59
|
+
type: 'object',
|
|
60
|
+
properties: {
|
|
61
|
+
[flow.idField]: {
|
|
62
|
+
type: 'string',
|
|
63
|
+
...(tool.descriptions?.[flow.idField] && {
|
|
64
|
+
description: tool.descriptions[flow.idField],
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
...(paged && {
|
|
68
|
+
[CURSOR]: {
|
|
69
|
+
type: 'string',
|
|
70
|
+
description: 'nextCursor from the previous call; leave it out for the first results',
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
},
|
|
74
|
+
required: [flow.idField],
|
|
75
|
+
additionalProperties: false,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const source = sourceSchema(schema, tool, flow);
|
|
79
|
+
const apiProperties = (source.properties ?? {});
|
|
80
|
+
const apiRequired = Array.isArray(source.required)
|
|
81
|
+
? source.required
|
|
82
|
+
: [];
|
|
83
|
+
const fields = tool.fields ?? [];
|
|
84
|
+
const properties = Object.fromEntries(fields.map((name) => [
|
|
85
|
+
name,
|
|
86
|
+
shownProperty(tool, name, apiProperties[name]),
|
|
87
|
+
]));
|
|
88
|
+
const required = [
|
|
89
|
+
...new Set([
|
|
90
|
+
...(tool.required ?? []),
|
|
91
|
+
...apiRequired.filter((name) => fields.includes(name)),
|
|
92
|
+
]),
|
|
93
|
+
];
|
|
94
|
+
return {
|
|
95
|
+
type: 'object',
|
|
96
|
+
properties,
|
|
97
|
+
...(required.length > 0 && { required }),
|
|
98
|
+
additionalProperties: false,
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
export const buildToolRequest = (tool, args) => {
|
|
102
|
+
const request = {};
|
|
103
|
+
for (const name of tool.fields ?? []) {
|
|
104
|
+
if (args[name] !== undefined) {
|
|
105
|
+
request[name] = args[name];
|
|
106
|
+
}
|
|
107
|
+
else if (tool.defaults?.[name] !== undefined) {
|
|
108
|
+
request[name] = tool.defaults[name];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { ...request, ...tool.fixed };
|
|
112
|
+
};
|
|
113
|
+
const jobIdOf = (flow, tool, args) => {
|
|
114
|
+
if (flow.pattern !== 'job') {
|
|
115
|
+
throw new Error(`Tool "${tool.name}" has step "${tool.step}", which only job flows have.`);
|
|
116
|
+
}
|
|
117
|
+
const id = args[flow.idField];
|
|
118
|
+
if (typeof id !== 'string' || !id) {
|
|
119
|
+
throw new Error(`Tool "${tool.name}" needs ${flow.idField}.`);
|
|
120
|
+
}
|
|
121
|
+
return id;
|
|
122
|
+
};
|
|
123
|
+
const runResults = async (flows, tool, flow, args) => {
|
|
124
|
+
const jobId = jobIdOf(flow, tool, args);
|
|
125
|
+
const waitSeconds = tool.limits?.waitSeconds;
|
|
126
|
+
let failedStatus;
|
|
127
|
+
if (waitSeconds) {
|
|
128
|
+
let status;
|
|
129
|
+
try {
|
|
130
|
+
status = await flows.wait(tool.flow, jobId, {
|
|
131
|
+
timeout: waitSeconds * 1000,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
if (err instanceof JobTimeoutError) {
|
|
136
|
+
return flows.status(tool.flow, jobId);
|
|
137
|
+
}
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
if (flows.hasFailed(tool.flow, status) && flow.pattern === 'job') {
|
|
141
|
+
failedStatus = String(status[flow.statusField]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const failed = () => new ResultError(`The ${tool.flow} job ${jobId} ended with status "${failedStatus}".`, failedStatus ?? 'failed');
|
|
145
|
+
if (flow.pattern !== 'job' || !flow.pagination) {
|
|
146
|
+
if (failedStatus) {
|
|
147
|
+
throw failed();
|
|
148
|
+
}
|
|
149
|
+
return readResult(tool.result, await flows.results(tool.flow, jobId, { maxItems: tool.limits?.maxItems }));
|
|
150
|
+
}
|
|
151
|
+
const cursor = args[CURSOR];
|
|
152
|
+
const page = await flows.resultsPage(tool.flow, jobId, {
|
|
153
|
+
cursor: typeof cursor === 'string' && cursor ? cursor : undefined,
|
|
154
|
+
maxItems: tool.limits?.maxItems,
|
|
155
|
+
});
|
|
156
|
+
// A cancelled or faulted job keeps the pages it finished; only a job with none is an error.
|
|
157
|
+
if (failedStatus && page.items.length === 0 && !cursor) {
|
|
158
|
+
throw failed();
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
items: readResult(tool.result, page.items),
|
|
162
|
+
...(page.nextCursor && { nextCursor: page.nextCursor }),
|
|
163
|
+
...(failedStatus && { status: failedStatus }),
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
export const runTool = async (flows, tool, args) => {
|
|
167
|
+
const flow = flowOf(flows.flowSchema, tool);
|
|
168
|
+
switch (tool.step) {
|
|
169
|
+
case 'status':
|
|
170
|
+
return flows.status(tool.flow, jobIdOf(flow, tool, args));
|
|
171
|
+
case 'cancel':
|
|
172
|
+
return flows.cancel(tool.flow, jobIdOf(flow, tool, args));
|
|
173
|
+
case 'results':
|
|
174
|
+
return runResults(flows, tool, flow, args);
|
|
175
|
+
case 'start':
|
|
176
|
+
return flows.start(tool.flow, buildToolRequest(tool, args));
|
|
177
|
+
default:
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
const request = buildToolRequest(tool, args);
|
|
181
|
+
if (flow.pattern === 'batch') {
|
|
182
|
+
return flows.startBatch(tool.flow, request);
|
|
183
|
+
}
|
|
184
|
+
return readResult(tool.result, await flows.call(tool.flow, request));
|
|
185
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpClient } from '../http.js';
|
|
2
|
+
import { Flows } from './flows.js';
|
|
2
3
|
import type { DecodoSchema } from '../schema/types.js';
|
|
3
4
|
import { dataApiEndpoints } from '../generated/data-api.js';
|
|
4
5
|
import type { CancelCrawlResponse, GetCrawlStatusResponse, MapSiteRequest, MapSiteResponse, RunSearchRequest, RunSearchResponse, StartCrawlRequest, StartCrawlResponse } from '../generated/data-api.js';
|
|
@@ -13,6 +14,7 @@ export type WebScrapingApiRoutes = {
|
|
|
13
14
|
export declare const SCRAPER_API_ROUTES: WebScrapingApiRoutes;
|
|
14
15
|
export declare const DATA_API_ROUTES: WebScrapingApiRoutes;
|
|
15
16
|
export declare class WebScrapingApi {
|
|
17
|
+
readonly flows: Flows;
|
|
16
18
|
private readonly http;
|
|
17
19
|
private readonly schemas;
|
|
18
20
|
private readonly routes;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { prettifyError } from 'zod';
|
|
2
2
|
import { TimeoutError, ValidationError } from '../errors.js';
|
|
3
3
|
import { BundledSchema } from '../schema/bundled-schema.js';
|
|
4
|
+
import { Flows } from './flows.js';
|
|
4
5
|
import { dataApiEndpoints } from '../generated/data-api.js';
|
|
5
6
|
export const SCRAPER_API_ROUTES = {
|
|
6
7
|
scrape: '/v2/scrape',
|
|
@@ -25,6 +26,7 @@ const nextCursor = (next) => {
|
|
|
25
26
|
return cursor;
|
|
26
27
|
};
|
|
27
28
|
export class WebScrapingApi {
|
|
29
|
+
flows;
|
|
28
30
|
http;
|
|
29
31
|
schemas;
|
|
30
32
|
routes;
|
|
@@ -32,6 +34,7 @@ export class WebScrapingApi {
|
|
|
32
34
|
this.http = http;
|
|
33
35
|
this.schemas = schemas;
|
|
34
36
|
this.routes = routes;
|
|
37
|
+
this.flows = new Flows(http, schemas, { available: Boolean(routes.dataApi) });
|
|
35
38
|
}
|
|
36
39
|
validate(params) {
|
|
37
40
|
const schema = this.schemas.getRequestSchema(params.target);
|
package/build/esm/errors.d.ts
CHANGED
|
@@ -16,3 +16,13 @@ export declare class ValidationError extends DecodoError {
|
|
|
16
16
|
export declare class TimeoutError extends Error {
|
|
17
17
|
constructor(message?: string);
|
|
18
18
|
}
|
|
19
|
+
export declare class ResultError extends Error {
|
|
20
|
+
readonly code: string;
|
|
21
|
+
readonly retryable: boolean;
|
|
22
|
+
constructor(message: string, code: string, retryable?: boolean);
|
|
23
|
+
}
|
|
24
|
+
export declare class JobTimeoutError extends TimeoutError {
|
|
25
|
+
readonly flow: string;
|
|
26
|
+
readonly jobId: string;
|
|
27
|
+
constructor(flow: string, jobId: string, timeoutMs: number);
|
|
28
|
+
}
|
package/build/esm/errors.js
CHANGED
|
@@ -34,3 +34,23 @@ export class TimeoutError extends Error {
|
|
|
34
34
|
this.name = 'TimeoutError';
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
+
export class ResultError extends Error {
|
|
38
|
+
code;
|
|
39
|
+
retryable;
|
|
40
|
+
constructor(message, code, retryable = false) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = 'ResultError';
|
|
43
|
+
this.code = code;
|
|
44
|
+
this.retryable = retryable;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export class JobTimeoutError extends TimeoutError {
|
|
48
|
+
flow;
|
|
49
|
+
jobId;
|
|
50
|
+
constructor(flow, jobId, timeoutMs) {
|
|
51
|
+
super(`The ${flow} job ${jobId} was still running after ${timeoutMs}ms.`);
|
|
52
|
+
this.name = 'JobTimeoutError';
|
|
53
|
+
this.flow = flow;
|
|
54
|
+
this.jobId = jobId;
|
|
55
|
+
}
|
|
56
|
+
}
|