@diagonal-hq/workflows 0.3.0 → 0.4.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 +47 -9
- package/dist/chunks/{chunk-RTCHH3JG.js → chunk-EU2ME5WR.js} +98 -1
- package/dist/chunks/chunk-QLQ4NOFV.js +433 -0
- package/dist/data.d.ts +7 -1
- package/dist/generated/capabilities.d.ts +70 -1
- package/dist/generated/profiles/test.d.ts +190 -0
- package/dist/index.js +45 -389
- package/dist/notifications.d.ts +4 -3
- package/dist/profiles/shared.d.ts +4 -0
- package/dist/profiles/test.d.ts +8 -2
- package/dist/profiles/test.js +282 -4
- package/dist/testing/context.d.ts +6 -0
- package/dist/testing/index.d.ts +8 -5
- package/dist/testing/index.js +37 -26
- package/dist/workflow-config.d.ts +7 -0
- package/dist/workflow.d.ts +23 -6
- package/package.json +1 -5
- package/dist/chunks/chunk-3EO3FSI5.js +0 -94
- package/dist/chunks/chunk-NUFRM6SI.js +0 -0
- package/dist/profiles/migration.d.ts +0 -3
- package/dist/profiles/migration.js +0 -21
package/dist/profiles/test.js
CHANGED
|
@@ -1,21 +1,299 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
ai,
|
|
3
|
+
calendar,
|
|
4
|
+
callback,
|
|
5
|
+
chart,
|
|
6
|
+
data,
|
|
7
|
+
date,
|
|
8
|
+
file,
|
|
9
|
+
guard,
|
|
10
|
+
http,
|
|
11
|
+
lifecycle,
|
|
12
|
+
mail,
|
|
13
|
+
markdown,
|
|
14
|
+
mediaLibrary,
|
|
15
|
+
money,
|
|
16
|
+
notification,
|
|
17
|
+
number,
|
|
18
|
+
package_,
|
|
19
|
+
page,
|
|
20
|
+
payables,
|
|
21
|
+
payment,
|
|
22
|
+
payments,
|
|
23
|
+
phone,
|
|
24
|
+
scheduling,
|
|
25
|
+
schema,
|
|
26
|
+
security,
|
|
27
|
+
sms,
|
|
28
|
+
subscriptions,
|
|
29
|
+
text,
|
|
30
|
+
time,
|
|
31
|
+
timezone,
|
|
32
|
+
tiptap,
|
|
33
|
+
transaction,
|
|
34
|
+
user,
|
|
35
|
+
web,
|
|
36
|
+
workflow,
|
|
37
|
+
workspace
|
|
38
|
+
} from "../chunks/chunk-QLQ4NOFV.js";
|
|
2
39
|
import {
|
|
3
40
|
InputField,
|
|
4
41
|
InputSchema,
|
|
5
|
-
|
|
6
|
-
|
|
42
|
+
defineWorkflow,
|
|
43
|
+
parseWorkflowInput,
|
|
44
|
+
redirect,
|
|
45
|
+
respond,
|
|
46
|
+
v,
|
|
47
|
+
validateWorkflowInput
|
|
48
|
+
} from "../chunks/chunk-EU2ME5WR.js";
|
|
7
49
|
import {
|
|
8
50
|
HostError,
|
|
9
51
|
WorkflowFailure,
|
|
10
52
|
fail,
|
|
11
53
|
validationError
|
|
12
54
|
} from "../chunks/chunk-OF5QITP7.js";
|
|
55
|
+
|
|
56
|
+
// packages/workflows/src/generated/profiles/test.ts
|
|
57
|
+
var ai2 = {
|
|
58
|
+
decide: ai.decide,
|
|
59
|
+
runPrompt: ai.runPrompt
|
|
60
|
+
};
|
|
61
|
+
var calendar2 = {
|
|
62
|
+
generateLink: calendar.generateLink,
|
|
63
|
+
searchEvents: calendar.searchEvents
|
|
64
|
+
};
|
|
65
|
+
var callback2 = {
|
|
66
|
+
get: callback.get,
|
|
67
|
+
remove: callback.remove,
|
|
68
|
+
set: callback.set
|
|
69
|
+
};
|
|
70
|
+
var chart2 = {
|
|
71
|
+
distribution: chart.distribution,
|
|
72
|
+
kpi: chart.kpi,
|
|
73
|
+
leaderboard: chart.leaderboard,
|
|
74
|
+
timeSeries: chart.timeSeries
|
|
75
|
+
};
|
|
76
|
+
var data2 = {
|
|
77
|
+
aggregateMetrics: data.aggregateMetrics,
|
|
78
|
+
count: data.count,
|
|
79
|
+
create: data.create,
|
|
80
|
+
createMany: data.createMany,
|
|
81
|
+
delete: data.delete,
|
|
82
|
+
deleteMany: data.deleteMany,
|
|
83
|
+
find: data.find,
|
|
84
|
+
findOrCreate: data.findOrCreate,
|
|
85
|
+
relation: {
|
|
86
|
+
attach: data.relation.attach,
|
|
87
|
+
attachMany: data.relation.attachMany,
|
|
88
|
+
detach: data.relation.detach,
|
|
89
|
+
detachMany: data.relation.detachMany,
|
|
90
|
+
sync: data.relation.sync
|
|
91
|
+
},
|
|
92
|
+
restore: data.restore,
|
|
93
|
+
search: data.search,
|
|
94
|
+
semanticSearch: data.semanticSearch,
|
|
95
|
+
sum: data.sum,
|
|
96
|
+
update: data.update,
|
|
97
|
+
updateOrCreate: data.updateOrCreate,
|
|
98
|
+
validate: data.validate
|
|
99
|
+
};
|
|
100
|
+
var date2 = {
|
|
101
|
+
add: date.add,
|
|
102
|
+
diff: date.diff,
|
|
103
|
+
endOf: date.endOf,
|
|
104
|
+
format: date.format,
|
|
105
|
+
startOf: date.startOf,
|
|
106
|
+
subtract: date.subtract
|
|
107
|
+
};
|
|
108
|
+
var file2 = {
|
|
109
|
+
changeVisibility: file.changeVisibility,
|
|
110
|
+
createFromUrl: file.createFromUrl,
|
|
111
|
+
createMediaLibraryItem: file.createMediaLibraryItem,
|
|
112
|
+
createQrCode: file.createQrCode
|
|
113
|
+
};
|
|
114
|
+
var guard2 = {
|
|
115
|
+
validate: guard.validate
|
|
116
|
+
};
|
|
117
|
+
var http2 = {
|
|
118
|
+
request: http.request
|
|
119
|
+
};
|
|
120
|
+
var lifecycle2 = {
|
|
121
|
+
output: lifecycle.output
|
|
122
|
+
};
|
|
123
|
+
var mail2 = {
|
|
124
|
+
search: mail.search,
|
|
125
|
+
send: mail.send,
|
|
126
|
+
sendNotification: mail.sendNotification
|
|
127
|
+
};
|
|
128
|
+
var markdown2 = {
|
|
129
|
+
fromContent: markdown.fromContent,
|
|
130
|
+
toContent: markdown.toContent
|
|
131
|
+
};
|
|
132
|
+
var mediaLibrary2 = {
|
|
133
|
+
createFolder: mediaLibrary.createFolder,
|
|
134
|
+
deleteItem: mediaLibrary.deleteItem,
|
|
135
|
+
moveItems: mediaLibrary.moveItems,
|
|
136
|
+
updateItem: mediaLibrary.updateItem
|
|
137
|
+
};
|
|
138
|
+
var money2 = {
|
|
139
|
+
format: money.format
|
|
140
|
+
};
|
|
141
|
+
var notification2 = {
|
|
142
|
+
send: notification.send,
|
|
143
|
+
sendBulkNotifications: notification.sendBulkNotifications,
|
|
144
|
+
sendNotification: notification.sendNotification,
|
|
145
|
+
setConsent: notification.setConsent
|
|
146
|
+
};
|
|
147
|
+
var number2 = {
|
|
148
|
+
format: number.format
|
|
149
|
+
};
|
|
150
|
+
var package_2 = {
|
|
151
|
+
installInput: package_.installInput
|
|
152
|
+
};
|
|
153
|
+
var page2 = {
|
|
154
|
+
url: page.url
|
|
155
|
+
};
|
|
156
|
+
var payables2 = {
|
|
157
|
+
void: payables.void,
|
|
158
|
+
writeOff: payables.writeOff
|
|
159
|
+
};
|
|
160
|
+
var payment2 = {
|
|
161
|
+
createManual: payment.createManual
|
|
162
|
+
};
|
|
163
|
+
var payments2 = {
|
|
164
|
+
charge: payments.charge,
|
|
165
|
+
checkout: payments.checkout,
|
|
166
|
+
confirm: payments.confirm,
|
|
167
|
+
refund: payments.refund
|
|
168
|
+
};
|
|
169
|
+
var phone2 = {
|
|
170
|
+
format: phone.format
|
|
171
|
+
};
|
|
172
|
+
var scheduling2 = {
|
|
173
|
+
bookSlot: scheduling.bookSlot,
|
|
174
|
+
findSlots: scheduling.findSlots
|
|
175
|
+
};
|
|
176
|
+
var schema2 = {
|
|
177
|
+
toJsonSchema: schema.toJsonSchema
|
|
178
|
+
};
|
|
179
|
+
var security2 = {
|
|
180
|
+
randomPassword: security.randomPassword
|
|
181
|
+
};
|
|
182
|
+
var sms2 = {
|
|
183
|
+
sendNotification: sms.sendNotification
|
|
184
|
+
};
|
|
185
|
+
var subscriptions2 = {
|
|
186
|
+
activate: subscriptions.activate,
|
|
187
|
+
cancel: subscriptions.cancel,
|
|
188
|
+
changeItems: subscriptions.changeItems,
|
|
189
|
+
create: subscriptions.create,
|
|
190
|
+
generateInvoice: subscriptions.generateInvoice,
|
|
191
|
+
pause: subscriptions.pause,
|
|
192
|
+
preview: subscriptions.preview,
|
|
193
|
+
previewChange: subscriptions.previewChange,
|
|
194
|
+
resume: subscriptions.resume,
|
|
195
|
+
setUpcomingItems: subscriptions.setUpcomingItems
|
|
196
|
+
};
|
|
197
|
+
var text2 = {
|
|
198
|
+
generate: text.generate,
|
|
199
|
+
hash: text.hash,
|
|
200
|
+
randomUuid: text.randomUuid,
|
|
201
|
+
toContent: text.toContent
|
|
202
|
+
};
|
|
203
|
+
var time2 = {
|
|
204
|
+
add: time.add,
|
|
205
|
+
diff: time.diff,
|
|
206
|
+
endOf: time.endOf,
|
|
207
|
+
format: time.format,
|
|
208
|
+
startOf: time.startOf,
|
|
209
|
+
subtract: time.subtract
|
|
210
|
+
};
|
|
211
|
+
var timezone2 = {
|
|
212
|
+
format: timezone.format,
|
|
213
|
+
interpret: timezone.interpret
|
|
214
|
+
};
|
|
215
|
+
var tiptap2 = {
|
|
216
|
+
parse: tiptap.parse
|
|
217
|
+
};
|
|
218
|
+
var user2 = {
|
|
219
|
+
authorize: user.authorize
|
|
220
|
+
};
|
|
221
|
+
var web2 = {
|
|
222
|
+
agent: web.agent,
|
|
223
|
+
batchScrape: web.batchScrape,
|
|
224
|
+
crawl: web.crawl,
|
|
225
|
+
extract: web.extract,
|
|
226
|
+
googleSearch: web.googleSearch,
|
|
227
|
+
map: web.map,
|
|
228
|
+
pdf: web.pdf,
|
|
229
|
+
scrape: web.scrape,
|
|
230
|
+
screenshot: web.screenshot,
|
|
231
|
+
searchRanked: web.searchRanked
|
|
232
|
+
};
|
|
233
|
+
var workflow2 = {
|
|
234
|
+
run: workflow.run,
|
|
235
|
+
url: workflow.url
|
|
236
|
+
};
|
|
237
|
+
var workspace2 = {
|
|
238
|
+
loginUrl: workspace.loginUrl,
|
|
239
|
+
setValue: workspace.setValue,
|
|
240
|
+
setValues: workspace.setValues,
|
|
241
|
+
value: workspace.value,
|
|
242
|
+
values: workspace.values
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// packages/workflows/src/profiles/test.ts
|
|
246
|
+
var data3 = {
|
|
247
|
+
...data2,
|
|
248
|
+
each: data.each
|
|
249
|
+
};
|
|
13
250
|
export {
|
|
14
251
|
HostError,
|
|
15
252
|
InputField,
|
|
16
253
|
InputSchema,
|
|
17
254
|
WorkflowFailure,
|
|
255
|
+
ai2 as ai,
|
|
256
|
+
calendar2 as calendar,
|
|
257
|
+
callback2 as callback,
|
|
258
|
+
chart2 as chart,
|
|
259
|
+
data3 as data,
|
|
260
|
+
date2 as date,
|
|
261
|
+
defineWorkflow,
|
|
18
262
|
fail,
|
|
263
|
+
file2 as file,
|
|
264
|
+
guard2 as guard,
|
|
265
|
+
http2 as http,
|
|
266
|
+
lifecycle2 as lifecycle,
|
|
267
|
+
mail2 as mail,
|
|
268
|
+
markdown2 as markdown,
|
|
269
|
+
mediaLibrary2 as mediaLibrary,
|
|
270
|
+
money2 as money,
|
|
271
|
+
notification2 as notification,
|
|
272
|
+
number2 as number,
|
|
273
|
+
package_2 as package,
|
|
274
|
+
page2 as page,
|
|
275
|
+
parseWorkflowInput,
|
|
276
|
+
payables2 as payables,
|
|
277
|
+
payment2 as payment,
|
|
278
|
+
payments2 as payments,
|
|
279
|
+
phone2 as phone,
|
|
280
|
+
redirect,
|
|
281
|
+
respond,
|
|
282
|
+
scheduling2 as scheduling,
|
|
283
|
+
schema2 as schema,
|
|
284
|
+
security2 as security,
|
|
285
|
+
sms2 as sms,
|
|
286
|
+
subscriptions2 as subscriptions,
|
|
287
|
+
text2 as text,
|
|
288
|
+
time2 as time,
|
|
289
|
+
timezone2 as timezone,
|
|
290
|
+
tiptap2 as tiptap,
|
|
291
|
+
transaction,
|
|
292
|
+
user2 as user,
|
|
19
293
|
v,
|
|
20
|
-
|
|
294
|
+
validateWorkflowInput,
|
|
295
|
+
validationError,
|
|
296
|
+
web2 as web,
|
|
297
|
+
workflow2 as workflow,
|
|
298
|
+
workspace2 as workspace
|
|
21
299
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { WorkflowContext } from "../workflow.js";
|
|
2
|
+
export type LocalContextOptions = Omit<Partial<WorkflowContext>, 'execution'> & {
|
|
3
|
+
execution?: Partial<WorkflowContext['execution']>;
|
|
4
|
+
};
|
|
5
|
+
/** The context of a local run: the supplied values over local defaults, with a fresh execution ID. */
|
|
6
|
+
export declare function localWorkflowContext(supplied: LocalContextOptions | undefined): WorkflowContext;
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type HostTransport } from "../transport.js";
|
|
2
|
-
import { type InputValidator, type
|
|
2
|
+
import { type InputValidator, type WorkflowDefinition } from "../workflow.js";
|
|
3
|
+
import { type LocalContextOptions } from "./context.js";
|
|
3
4
|
export declare class MockTransport implements HostTransport {
|
|
4
5
|
private readonly handlers;
|
|
5
6
|
readonly calls: Array<{
|
|
@@ -11,10 +12,12 @@ export declare class MockTransport implements HostTransport {
|
|
|
11
12
|
}
|
|
12
13
|
export declare function runWorkflow<I, O>(workflow: WorkflowDefinition<I, O>, input: I, options?: {
|
|
13
14
|
transport?: HostTransport;
|
|
14
|
-
context?:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
context?: LocalContextOptions;
|
|
16
|
+
/**
|
|
17
|
+
* The module's `validateInput` export; it runs before the handler, as in the sandbox. The
|
|
18
|
+
* `validate` option of the workflow config always runs, so a workflow with one takes no
|
|
19
|
+
* `validateInput`.
|
|
20
|
+
*/
|
|
18
21
|
validateInput?: InputValidator<I>;
|
|
19
22
|
}): Promise<O>;
|
|
20
23
|
export declare const fake: {
|
package/dist/testing/index.js
CHANGED
|
@@ -2,15 +2,13 @@ import {
|
|
|
2
2
|
parseWorkflowInput,
|
|
3
3
|
setTransport,
|
|
4
4
|
validateWorkflowInput
|
|
5
|
-
} from "../chunks/chunk-
|
|
6
|
-
import "../chunks/chunk-RTCHH3JG.js";
|
|
5
|
+
} from "../chunks/chunk-EU2ME5WR.js";
|
|
7
6
|
import {
|
|
8
7
|
HostError
|
|
9
8
|
} from "../chunks/chunk-OF5QITP7.js";
|
|
10
9
|
|
|
11
10
|
// packages/workflows/src/testing/index.ts
|
|
12
11
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
13
|
-
import { randomUUID } from "node:crypto";
|
|
14
12
|
|
|
15
13
|
// packages/workflows/src/serial-transaction-transport.ts
|
|
16
14
|
var PromiseConstructor = Promise;
|
|
@@ -92,6 +90,33 @@ var SerialTransactionTransport = class {
|
|
|
92
90
|
}
|
|
93
91
|
};
|
|
94
92
|
|
|
93
|
+
// packages/workflows/src/testing/context.ts
|
|
94
|
+
import { randomUUID } from "node:crypto";
|
|
95
|
+
var defaultContext = {
|
|
96
|
+
user: null,
|
|
97
|
+
callingUser: null,
|
|
98
|
+
trigger: {},
|
|
99
|
+
request: null,
|
|
100
|
+
workspace: { id: "local", timezone: "UTC" },
|
|
101
|
+
now: (/* @__PURE__ */ new Date(0)).toISOString()
|
|
102
|
+
};
|
|
103
|
+
function localWorkflowContext(supplied) {
|
|
104
|
+
const suppliedExecution = supplied?.execution;
|
|
105
|
+
const workflowId = suppliedExecution && "workflowId" in suppliedExecution && typeof suppliedExecution.workflowId === "string" ? suppliedExecution.workflowId : "local";
|
|
106
|
+
const execution = {
|
|
107
|
+
id: randomUUID(),
|
|
108
|
+
kind: "workflow",
|
|
109
|
+
workflowId,
|
|
110
|
+
owner: { type: "workflow", id: workflowId }
|
|
111
|
+
};
|
|
112
|
+
const context = {
|
|
113
|
+
...defaultContext,
|
|
114
|
+
...supplied,
|
|
115
|
+
execution: { ...execution, ...suppliedExecution }
|
|
116
|
+
};
|
|
117
|
+
return context;
|
|
118
|
+
}
|
|
119
|
+
|
|
95
120
|
// packages/workflows/src/testing/index.ts
|
|
96
121
|
var MockTransport = class {
|
|
97
122
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -109,14 +134,6 @@ var MockTransport = class {
|
|
|
109
134
|
return handler(input);
|
|
110
135
|
}
|
|
111
136
|
};
|
|
112
|
-
var defaultContext = {
|
|
113
|
-
user: null,
|
|
114
|
-
callingUser: null,
|
|
115
|
-
trigger: {},
|
|
116
|
-
request: null,
|
|
117
|
-
workspace: { id: "local", timezone: "UTC" },
|
|
118
|
-
now: (/* @__PURE__ */ new Date(0)).toISOString()
|
|
119
|
-
};
|
|
120
137
|
var runTransports = new AsyncLocalStorage();
|
|
121
138
|
var activeRuns = 0;
|
|
122
139
|
var runTransport = {
|
|
@@ -132,26 +149,20 @@ var runTransport = {
|
|
|
132
149
|
};
|
|
133
150
|
async function runWorkflow(workflow, input, options = {}) {
|
|
134
151
|
const transport = options.transport ?? new MockTransport({});
|
|
135
|
-
const
|
|
136
|
-
const workflowId = suppliedExecution && "workflowId" in suppliedExecution && typeof suppliedExecution.workflowId === "string" ? suppliedExecution.workflowId : "local";
|
|
137
|
-
const execution = {
|
|
138
|
-
id: randomUUID(),
|
|
139
|
-
kind: "workflow",
|
|
140
|
-
workflowId,
|
|
141
|
-
owner: { type: "workflow", id: workflowId }
|
|
142
|
-
};
|
|
143
|
-
const context = {
|
|
144
|
-
...defaultContext,
|
|
145
|
-
...options.context,
|
|
146
|
-
execution: { ...execution, ...suppliedExecution }
|
|
147
|
-
};
|
|
152
|
+
const context = localWorkflowContext(options.context);
|
|
148
153
|
activeRuns++;
|
|
149
154
|
setTransport(runTransport);
|
|
150
155
|
try {
|
|
151
156
|
return await runTransports.run(new SerialTransactionTransport(transport), async () => {
|
|
152
157
|
const parsed = parseWorkflowInput(workflow, input);
|
|
153
|
-
if (options.validateInput) {
|
|
154
|
-
|
|
158
|
+
if (workflow.validate !== void 0 && options.validateInput !== void 0) {
|
|
159
|
+
throw new TypeError(
|
|
160
|
+
"The workflow has a validate option, which always runs. Remove the validateInput option."
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
const validator = workflow.validate ?? options.validateInput;
|
|
164
|
+
if (validator !== void 0) {
|
|
165
|
+
await validateWorkflowInput(validator, parsed, context);
|
|
155
166
|
}
|
|
156
167
|
return await workflow.run(parsed, context);
|
|
157
168
|
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The options of `defineWorkflow(config, handler)` other than `input` and `validate`. The build
|
|
3
|
+
* reads them as static data, checks them, and removes them from the compiled script, so the
|
|
4
|
+
* compiler sees only `input` and `validate`. `@diagonal-hq/cli` replaces this declaration with its
|
|
5
|
+
* `WorkflowConfig`, so the editor checks each option.
|
|
6
|
+
*/
|
|
7
|
+
export type WorkflowConfig = Record<never, never>;
|
package/dist/workflow.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ResourceKeyWith } from "./data.js";
|
|
2
2
|
import type { ScriptHostContext } from "./index.js";
|
|
3
|
-
import { type InferInput, type InputFieldSpec, type
|
|
3
|
+
import { InputSchema, type InferInput, type InputFieldSpec, type InputShape } from "./input.js";
|
|
4
|
+
import type { WorkflowConfig } from "./workflow-config.js";
|
|
4
5
|
export interface Identity {
|
|
5
6
|
id: string;
|
|
6
7
|
/** The identity's resource has the Authenticatable capability. */
|
|
@@ -47,6 +48,11 @@ export interface WorkflowDefinition<I, O> {
|
|
|
47
48
|
readonly $diagonal: 'workflow';
|
|
48
49
|
/** The declared input schema, when the workflow has one. */
|
|
49
50
|
readonly input?: InputFieldSpec;
|
|
51
|
+
/**
|
|
52
|
+
* The `validate` option of the config, which runs before the handler with the same input. A
|
|
53
|
+
* method, like `run`, so a definition stays assignable to a narrower input type.
|
|
54
|
+
*/
|
|
55
|
+
validate?(input: I, ctx: WorkflowContext): ReturnType<InputValidator<I>>;
|
|
50
56
|
/** Runs the handler with input that {@link parseWorkflowInput} already converted. */
|
|
51
57
|
run(input: I, ctx: WorkflowContext): Promise<O>;
|
|
52
58
|
}
|
|
@@ -61,19 +67,30 @@ export declare function parseWorkflowInput<I>(workflow: WorkflowDefinition<I, un
|
|
|
61
67
|
*/
|
|
62
68
|
export type InputErrors<I> = Partial<Record<keyof I & string, string | string[]>> & Record<string, string | string[] | undefined>;
|
|
63
69
|
/**
|
|
64
|
-
* The `
|
|
65
|
-
* with the same input and context; when it
|
|
66
|
-
* errors, and the handler does not run.
|
|
70
|
+
* The `validate` option of a workflow config, or the `validateInput` export a script may define
|
|
71
|
+
* beside its workflow. It runs before the handler with the same input and context; when it
|
|
72
|
+
* reports any error, the run ends with a 422 and those errors, and the handler does not run.
|
|
67
73
|
*/
|
|
68
74
|
export type InputValidator<I> = (input: I, ctx: WorkflowContext) => InputErrors<I> | void | Promise<InputErrors<I> | void>;
|
|
69
75
|
/** Runs a script's input validator and ends the run with a validation error when it reports one. */
|
|
70
76
|
export declare function validateWorkflowInput<I>(validator: InputValidator<I>, input: I, ctx: WorkflowContext): Promise<void>;
|
|
71
77
|
type Handler<I, O> = (input: I, ctx: WorkflowContext) => Promise<O> | O;
|
|
72
78
|
/**
|
|
73
|
-
* Declares a workflow.
|
|
74
|
-
*
|
|
79
|
+
* Declares a workflow. A file-based workflow passes its config first, as `definePage` does. The
|
|
80
|
+
* build reads the config as static data. With an `input` schema in the config, or a schema first,
|
|
81
|
+
* the platform validates every run's input against it before the script starts, and the handler's
|
|
82
|
+
* `input` type comes from the schema. A `validate` function in the config runs after that check
|
|
83
|
+
* and before the handler, with the same input type.
|
|
75
84
|
*/
|
|
85
|
+
export declare function defineWorkflow<S extends InputShape, O = unknown>(config: WorkflowConfig & {
|
|
86
|
+
readonly input: InputSchema<S>;
|
|
87
|
+
readonly validate?: InputValidator<InferInput<S>>;
|
|
88
|
+
}, handler: Handler<InferInput<S>, O>): WorkflowDefinition<InferInput<S>, O>;
|
|
76
89
|
export declare function defineWorkflow<S extends InputShape, O = unknown>(input: InputSchema<S>, handler: Handler<InferInput<S>, O>): WorkflowDefinition<InferInput<S>, O>;
|
|
90
|
+
export declare function defineWorkflow<I = Record<string, unknown>, O = unknown>(config: WorkflowConfig & {
|
|
91
|
+
readonly input?: undefined;
|
|
92
|
+
readonly validate?: InputValidator<I>;
|
|
93
|
+
}, handler: Handler<I, O>): WorkflowDefinition<I, O>;
|
|
77
94
|
export declare function defineWorkflow<I = Record<string, unknown>, O = unknown>(handler: Handler<I, O>): WorkflowDefinition<I, O>;
|
|
78
95
|
export type ResponseOutput = {
|
|
79
96
|
$response_status: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diagonal-hq/workflows",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "The authoring API of Diagonal managed workflows, their capabilities, and the workflow test helpers.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"repository": {
|
|
@@ -19,10 +19,6 @@
|
|
|
19
19
|
"types": "./dist/profiles/test.d.ts",
|
|
20
20
|
"default": "./dist/profiles/test.js"
|
|
21
21
|
},
|
|
22
|
-
"./migration": {
|
|
23
|
-
"types": "./dist/profiles/migration.d.ts",
|
|
24
|
-
"default": "./dist/profiles/migration.js"
|
|
25
|
-
},
|
|
26
22
|
"./testing": {
|
|
27
23
|
"types": "./dist/testing/index.d.ts",
|
|
28
24
|
"default": "./dist/testing/index.js"
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
coerceInput
|
|
3
|
-
} from "./chunk-RTCHH3JG.js";
|
|
4
|
-
import {
|
|
5
|
-
validationError
|
|
6
|
-
} from "./chunk-OF5QITP7.js";
|
|
7
|
-
|
|
8
|
-
// packages/workflows/src/transport.ts
|
|
9
|
-
var active = null;
|
|
10
|
-
function setTransport(transport) {
|
|
11
|
-
active = transport;
|
|
12
|
-
}
|
|
13
|
-
function invoke(name, input) {
|
|
14
|
-
if (active === null) {
|
|
15
|
-
return Promise.reject(
|
|
16
|
-
new Error(
|
|
17
|
-
"No Diagonal host transport is set. In tests, call setTransport(new MockTransport(...))."
|
|
18
|
-
)
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
return active.call(name, input);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// packages/workflows/src/workflow.ts
|
|
25
|
-
function parseWorkflowInput(workflow, input) {
|
|
26
|
-
return workflow.input === void 0 ? input : coerceInput(workflow.input, input);
|
|
27
|
-
}
|
|
28
|
-
async function validateWorkflowInput(validator, input, ctx) {
|
|
29
|
-
const reported = await validator(input, ctx);
|
|
30
|
-
if (reported === void 0 || reported === null) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if (typeof reported !== "object" || Array.isArray(reported)) {
|
|
34
|
-
throw new TypeError(
|
|
35
|
-
"validateInput must return an object of errors keyed by field, or nothing."
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
const errors = {};
|
|
39
|
-
for (const [field, messages] of Object.entries(reported)) {
|
|
40
|
-
const list = (Array.isArray(messages) ? messages : [messages]).filter(
|
|
41
|
-
(message) => typeof message === "string" && message !== ""
|
|
42
|
-
);
|
|
43
|
-
if (list.length > 0) {
|
|
44
|
-
errors[field] = list;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (Object.keys(errors).length > 0) {
|
|
48
|
-
validationError(errors);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function defineWorkflow(inputOrHandler, maybeHandler) {
|
|
52
|
-
if (typeof inputOrHandler === "function") {
|
|
53
|
-
return { $diagonal: "workflow", run: async (input, ctx) => inputOrHandler(input, ctx) };
|
|
54
|
-
}
|
|
55
|
-
const spec = inputOrHandler.$spec;
|
|
56
|
-
const handler = maybeHandler;
|
|
57
|
-
if (handler === void 0) {
|
|
58
|
-
throw new TypeError("defineWorkflow(input, handler) needs a handler after the input schema.");
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
$diagonal: "workflow",
|
|
62
|
-
input: spec,
|
|
63
|
-
run: async (input, ctx) => handler(input, ctx)
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
function respond(body, options = {}) {
|
|
67
|
-
return {
|
|
68
|
-
$response_status: options.status ?? 200,
|
|
69
|
-
$response_body: body,
|
|
70
|
-
$response_content_type: options.contentType ?? "application/json"
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function redirect(url, options = {}) {
|
|
74
|
-
const type = options.type ?? (/^\/(?![/\\])/.test(url) ? "internal" : "external");
|
|
75
|
-
const output = {
|
|
76
|
-
redirect_url: url,
|
|
77
|
-
redirect_type: type,
|
|
78
|
-
permanent: type !== "back" && options.permanent === true
|
|
79
|
-
};
|
|
80
|
-
if (options.success !== void 0) {
|
|
81
|
-
output.success = options.success;
|
|
82
|
-
}
|
|
83
|
-
return output;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export {
|
|
87
|
-
setTransport,
|
|
88
|
-
invoke,
|
|
89
|
-
parseWorkflowInput,
|
|
90
|
-
validateWorkflowInput,
|
|
91
|
-
defineWorkflow,
|
|
92
|
-
respond,
|
|
93
|
-
redirect
|
|
94
|
-
};
|
|
File without changes
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import "../console.js";
|
|
2
|
-
export { HostError, WorkflowFailure, fail, validationError } from "../errors.js";
|
|
3
|
-
export { InputField, InputSchema, v, type ContentDocument, type ContentNode, type ContentOptions, type FileVisibility, type Infer, type InferInput, type InputFieldKind, type InputFieldSpec, type InputShape, type Money, type RecordScope } from "../input.js";
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import "../chunks/chunk-NUFRM6SI.js";
|
|
2
|
-
import {
|
|
3
|
-
InputField,
|
|
4
|
-
InputSchema,
|
|
5
|
-
v
|
|
6
|
-
} from "../chunks/chunk-RTCHH3JG.js";
|
|
7
|
-
import {
|
|
8
|
-
HostError,
|
|
9
|
-
WorkflowFailure,
|
|
10
|
-
fail,
|
|
11
|
-
validationError
|
|
12
|
-
} from "../chunks/chunk-OF5QITP7.js";
|
|
13
|
-
export {
|
|
14
|
-
HostError,
|
|
15
|
-
InputField,
|
|
16
|
-
InputSchema,
|
|
17
|
-
WorkflowFailure,
|
|
18
|
-
fail,
|
|
19
|
-
v,
|
|
20
|
-
validationError
|
|
21
|
-
};
|