@object-ui/core 0.5.0 → 2.0.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +11 -0
- package/dist/actions/ActionRunner.d.ts +228 -4
- package/dist/actions/ActionRunner.js +397 -45
- package/dist/actions/TransactionManager.d.ts +193 -0
- package/dist/actions/TransactionManager.js +410 -0
- package/dist/actions/index.d.ts +1 -0
- package/dist/actions/index.js +1 -0
- package/dist/adapters/ApiDataSource.d.ts +69 -0
- package/dist/adapters/ApiDataSource.js +293 -0
- package/dist/adapters/ValueDataSource.d.ts +55 -0
- package/dist/adapters/ValueDataSource.js +287 -0
- package/dist/adapters/index.d.ts +3 -0
- package/dist/adapters/index.js +5 -2
- package/dist/adapters/resolveDataSource.d.ts +40 -0
- package/dist/adapters/resolveDataSource.js +59 -0
- package/dist/data-scope/DataScopeManager.d.ts +127 -0
- package/dist/data-scope/DataScopeManager.js +229 -0
- package/dist/data-scope/index.d.ts +10 -0
- package/dist/data-scope/index.js +10 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +11 -1
- package/dist/evaluator/ExpressionEvaluator.js +32 -8
- package/dist/evaluator/FormulaFunctions.d.ts +58 -0
- package/dist/evaluator/FormulaFunctions.js +350 -0
- package/dist/evaluator/index.d.ts +1 -0
- package/dist/evaluator/index.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -2
- package/dist/query/query-ast.d.ts +2 -2
- package/dist/query/query-ast.js +3 -3
- package/dist/registry/Registry.d.ts +10 -0
- package/dist/registry/Registry.js +2 -1
- package/dist/registry/WidgetRegistry.d.ts +120 -0
- package/dist/registry/WidgetRegistry.js +275 -0
- package/dist/theme/ThemeEngine.d.ts +82 -0
- package/dist/theme/ThemeEngine.js +400 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/index.js +8 -0
- package/dist/validation/index.d.ts +1 -1
- package/dist/validation/index.js +1 -1
- package/dist/validation/validation-engine.d.ts +19 -1
- package/dist/validation/validation-engine.js +67 -2
- package/dist/validation/validators/index.d.ts +1 -1
- package/dist/validation/validators/index.js +1 -1
- package/dist/validation/validators/object-validation-engine.d.ts +2 -2
- package/dist/validation/validators/object-validation-engine.js +1 -1
- package/package.json +4 -3
- package/src/actions/ActionRunner.ts +577 -55
- package/src/actions/TransactionManager.ts +521 -0
- package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
- package/src/actions/__tests__/ActionRunner.test.ts +711 -0
- package/src/actions/__tests__/TransactionManager.test.ts +447 -0
- package/src/actions/index.ts +1 -0
- package/src/adapters/ApiDataSource.ts +349 -0
- package/src/adapters/ValueDataSource.ts +332 -0
- package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
- package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
- package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
- package/src/adapters/index.ts +6 -1
- package/src/adapters/resolveDataSource.ts +79 -0
- package/src/builder/__tests__/schema-builder.test.ts +235 -0
- package/src/data-scope/DataScopeManager.ts +269 -0
- package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
- package/src/data-scope/index.ts +16 -0
- package/src/evaluator/ExpressionEvaluator.ts +34 -8
- package/src/evaluator/FormulaFunctions.ts +398 -0
- package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
- package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
- package/src/evaluator/index.ts +1 -0
- package/src/index.ts +4 -3
- package/src/query/__tests__/window-functions.test.ts +1 -1
- package/src/query/query-ast.ts +3 -3
- package/src/registry/Registry.ts +12 -1
- package/src/registry/WidgetRegistry.ts +316 -0
- package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
- package/src/theme/ThemeEngine.ts +452 -0
- package/src/theme/__tests__/ThemeEngine.test.ts +606 -0
- package/src/theme/index.ts +22 -0
- package/src/validation/__tests__/object-validation-engine.test.ts +1 -1
- package/src/validation/__tests__/schema-validator.test.ts +118 -0
- package/src/validation/index.ts +1 -1
- package/src/validation/validation-engine.ts +61 -2
- package/src/validation/validators/index.ts +1 -1
- package/src/validation/validators/object-validation-engine.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
* @object-ui/core - Action Runner
|
|
10
10
|
*
|
|
11
11
|
* Executes actions defined in ActionSchema and EventHandler.
|
|
12
|
+
* Supports all spec v2.0.1 action types: script, url, modal, flow, api.
|
|
13
|
+
* Features: conditional execution, confirmation, toast notifications,
|
|
14
|
+
* redirect handling, action chaining, custom handler registration.
|
|
12
15
|
*/
|
|
13
16
|
import { ExpressionEvaluator } from '../evaluator/ExpressionEvaluator';
|
|
14
17
|
export class ActionRunner {
|
|
@@ -31,14 +34,87 @@ export class ActionRunner {
|
|
|
31
34
|
writable: true,
|
|
32
35
|
value: void 0
|
|
33
36
|
});
|
|
37
|
+
Object.defineProperty(this, "confirmHandler", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: void 0
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(this, "toastHandler", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: void 0
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(this, "modalHandler", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: void 0
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "navigationHandler", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: void 0
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(this, "paramCollectionHandler", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
34
67
|
this.context = context;
|
|
35
68
|
this.evaluator = new ExpressionEvaluator(context);
|
|
69
|
+
// Default confirmation: window.confirm (can be overridden)
|
|
70
|
+
this.confirmHandler = async (message) => window.confirm(message);
|
|
71
|
+
this.toastHandler = null;
|
|
72
|
+
this.modalHandler = null;
|
|
73
|
+
this.navigationHandler = null;
|
|
74
|
+
this.paramCollectionHandler = null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Set a custom confirmation handler (e.g., Shadcn AlertDialog).
|
|
78
|
+
*/
|
|
79
|
+
setConfirmHandler(handler) {
|
|
80
|
+
this.confirmHandler = handler;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Set a custom toast handler (e.g., Sonner).
|
|
84
|
+
*/
|
|
85
|
+
setToastHandler(handler) {
|
|
86
|
+
this.toastHandler = handler;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Set a modal handler (e.g., render a Dialog via React state).
|
|
90
|
+
*/
|
|
91
|
+
setModalHandler(handler) {
|
|
92
|
+
this.modalHandler = handler;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Set a navigation handler (e.g., React Router navigate).
|
|
96
|
+
*/
|
|
97
|
+
setNavigationHandler(handler) {
|
|
98
|
+
this.navigationHandler = handler;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Set a param collection handler — shows a dialog to collect
|
|
102
|
+
* ActionParam values before action execution.
|
|
103
|
+
*/
|
|
104
|
+
setParamCollectionHandler(handler) {
|
|
105
|
+
this.paramCollectionHandler = handler;
|
|
36
106
|
}
|
|
37
107
|
registerHandler(actionName, handler) {
|
|
38
108
|
this.handlers.set(actionName, handler);
|
|
39
109
|
}
|
|
110
|
+
unregisterHandler(actionName) {
|
|
111
|
+
this.handlers.delete(actionName);
|
|
112
|
+
}
|
|
40
113
|
async execute(action) {
|
|
41
114
|
try {
|
|
115
|
+
// Resolve the action type
|
|
116
|
+
const actionType = action.type || action.actionType || action.name || '';
|
|
117
|
+
// Conditional execution
|
|
42
118
|
if (action.condition) {
|
|
43
119
|
const shouldExecute = this.evaluator.evaluateCondition(action.condition);
|
|
44
120
|
if (!shouldExecute) {
|
|
@@ -53,34 +129,245 @@ export class ActionRunner {
|
|
|
53
129
|
return { success: false, error: 'Action is disabled' };
|
|
54
130
|
}
|
|
55
131
|
}
|
|
56
|
-
|
|
57
|
-
|
|
132
|
+
// Confirmation (structured or simple)
|
|
133
|
+
const confirmMessage = action.confirm?.message || action.confirmText;
|
|
134
|
+
if (confirmMessage) {
|
|
135
|
+
const confirmed = await this.confirmHandler(this.evaluator.evaluate(confirmMessage), action.confirm ? {
|
|
136
|
+
title: action.confirm.title,
|
|
137
|
+
confirmText: action.confirm.confirmText,
|
|
138
|
+
cancelText: action.confirm.cancelText,
|
|
139
|
+
} : undefined);
|
|
140
|
+
if (!confirmed) {
|
|
141
|
+
return { success: false, error: 'Action cancelled by user' };
|
|
142
|
+
}
|
|
58
143
|
}
|
|
59
|
-
|
|
60
|
-
|
|
144
|
+
// Param collection: if the action defines ActionParam[] to collect,
|
|
145
|
+
// show a dialog to gather user input before executing.
|
|
146
|
+
if (action.actionParams && Array.isArray(action.actionParams) && action.actionParams.length > 0) {
|
|
147
|
+
if (this.paramCollectionHandler) {
|
|
148
|
+
const collected = await this.paramCollectionHandler(action.actionParams);
|
|
149
|
+
if (collected === null) {
|
|
150
|
+
return { success: false, error: 'Action cancelled by user (params)' };
|
|
151
|
+
}
|
|
152
|
+
// Merge collected params into action.params
|
|
153
|
+
action.params = { ...(action.params || {}), ...collected };
|
|
154
|
+
}
|
|
61
155
|
}
|
|
62
|
-
|
|
63
|
-
|
|
156
|
+
// Check for a registered custom handler first
|
|
157
|
+
if (actionType && this.handlers.has(actionType)) {
|
|
158
|
+
const handler = this.handlers.get(actionType);
|
|
159
|
+
const result = await handler(action, this.context);
|
|
160
|
+
await this.handlePostExecution(action, result);
|
|
161
|
+
return result;
|
|
64
162
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
163
|
+
// Built-in action execution by type
|
|
164
|
+
let result;
|
|
165
|
+
switch (actionType) {
|
|
166
|
+
case 'script':
|
|
167
|
+
result = await this.executeScript(action);
|
|
168
|
+
break;
|
|
169
|
+
case 'url':
|
|
170
|
+
result = await this.executeUrl(action);
|
|
171
|
+
break;
|
|
172
|
+
case 'modal':
|
|
173
|
+
result = await this.executeModal(action);
|
|
174
|
+
break;
|
|
175
|
+
case 'flow':
|
|
176
|
+
result = await this.executeFlow(action);
|
|
177
|
+
break;
|
|
178
|
+
case 'api':
|
|
179
|
+
result = await this.executeAPI(action);
|
|
180
|
+
break;
|
|
181
|
+
case 'navigation':
|
|
182
|
+
result = await this.executeNavigation(action);
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
// Legacy fallback: check for navigate, api, or onClick
|
|
186
|
+
if (action.navigate) {
|
|
187
|
+
result = await this.executeNavigation(action);
|
|
188
|
+
}
|
|
189
|
+
else if (action.api || action.endpoint) {
|
|
190
|
+
result = await this.executeAPI(action);
|
|
191
|
+
}
|
|
192
|
+
else if (action.onClick) {
|
|
193
|
+
await action.onClick();
|
|
194
|
+
result = { success: true };
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
result = await this.executeActionSchema(action);
|
|
198
|
+
}
|
|
68
199
|
}
|
|
69
|
-
|
|
200
|
+
await this.handlePostExecution(action, result);
|
|
201
|
+
return result;
|
|
70
202
|
}
|
|
71
203
|
catch (error) {
|
|
72
|
-
|
|
204
|
+
const result = { success: false, error: error.message };
|
|
205
|
+
await this.handlePostExecution(action, result);
|
|
206
|
+
return result;
|
|
73
207
|
}
|
|
74
208
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Execute multiple actions in sequence or parallel.
|
|
211
|
+
*/
|
|
212
|
+
async executeChain(actions, mode = 'sequential') {
|
|
213
|
+
if (actions.length === 0) {
|
|
214
|
+
return { success: true };
|
|
215
|
+
}
|
|
216
|
+
if (mode === 'parallel') {
|
|
217
|
+
const results = await Promise.allSettled(actions.map(a => this.execute(a)));
|
|
218
|
+
const failures = results.filter(r => r.status === 'rejected' || (r.status === 'fulfilled' && !r.value.success));
|
|
219
|
+
if (failures.length > 0) {
|
|
220
|
+
const firstFail = results.find(r => r.status === 'fulfilled' && !r.value.success);
|
|
221
|
+
return {
|
|
222
|
+
success: false,
|
|
223
|
+
error: firstFail?.value?.error || 'One or more parallel actions failed',
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const lastResult = results[results.length - 1];
|
|
227
|
+
return lastResult.status === 'fulfilled'
|
|
228
|
+
? lastResult.value
|
|
229
|
+
: { success: false, error: 'Action failed' };
|
|
230
|
+
}
|
|
231
|
+
// Sequential execution — stop on first failure
|
|
232
|
+
let lastResult = { success: true };
|
|
233
|
+
for (const action of actions) {
|
|
234
|
+
lastResult = await this.execute(action);
|
|
235
|
+
if (!lastResult.success) {
|
|
236
|
+
return lastResult;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return lastResult;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Post-execution: emit toast notifications, handle chaining, callbacks.
|
|
243
|
+
*/
|
|
244
|
+
async handlePostExecution(action, result) {
|
|
245
|
+
// Toast notifications
|
|
246
|
+
if (this.toastHandler) {
|
|
247
|
+
const showToast = action.toast ?? { showOnSuccess: true, showOnError: true };
|
|
248
|
+
const duration = action.toast?.duration;
|
|
249
|
+
if (result.success && showToast.showOnSuccess !== false) {
|
|
250
|
+
const message = action.successMessage || 'Action completed successfully';
|
|
251
|
+
this.toastHandler(message, { type: 'success', duration });
|
|
252
|
+
}
|
|
253
|
+
if (!result.success && showToast.showOnError !== false && result.error) {
|
|
254
|
+
const message = action.errorMessage || result.error;
|
|
255
|
+
this.toastHandler(message, { type: 'error', duration });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// Apply refreshAfter from UIActionSchema
|
|
259
|
+
if (action.refreshAfter && result.success) {
|
|
260
|
+
result.reload = true;
|
|
261
|
+
}
|
|
262
|
+
// Execute chained actions
|
|
263
|
+
if (action.chain && action.chain.length > 0 && result.success) {
|
|
264
|
+
const chainResult = await this.executeChain(action.chain, action.chainMode || 'sequential');
|
|
265
|
+
// Merge chain result
|
|
266
|
+
if (!chainResult.success) {
|
|
267
|
+
result.success = false;
|
|
268
|
+
result.error = chainResult.error;
|
|
81
269
|
}
|
|
270
|
+
if (chainResult.data)
|
|
271
|
+
result.data = chainResult.data;
|
|
272
|
+
if (chainResult.redirect)
|
|
273
|
+
result.redirect = chainResult.redirect;
|
|
274
|
+
if (chainResult.reload)
|
|
275
|
+
result.reload = true;
|
|
276
|
+
}
|
|
277
|
+
// Execute onSuccess/onFailure callbacks
|
|
278
|
+
if (result.success && action.onSuccess) {
|
|
279
|
+
const callbacks = Array.isArray(action.onSuccess) ? action.onSuccess : [action.onSuccess];
|
|
280
|
+
await this.executeChain(callbacks, 'sequential');
|
|
281
|
+
}
|
|
282
|
+
if (!result.success && action.onFailure) {
|
|
283
|
+
const callbacks = Array.isArray(action.onFailure) ? action.onFailure : [action.onFailure];
|
|
284
|
+
await this.executeChain(callbacks, 'sequential');
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Execute script action — evaluates client-side expression via ExpressionEvaluator.
|
|
289
|
+
* Supports ${} template expressions referencing data, record, user context.
|
|
290
|
+
*/
|
|
291
|
+
async executeScript(action) {
|
|
292
|
+
const script = action.execute || action.target;
|
|
293
|
+
if (!script) {
|
|
294
|
+
return { success: false, error: 'No script provided for script action' };
|
|
295
|
+
}
|
|
296
|
+
try {
|
|
297
|
+
const result = this.evaluator.evaluate(`\${${script}}`);
|
|
298
|
+
return { success: true, data: result };
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
return { success: false, error: `Script execution failed: ${error.message}` };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Execute URL action — navigate to a URL.
|
|
306
|
+
* Uses navigationHandler for SPA routing, falls back to window.location.
|
|
307
|
+
*/
|
|
308
|
+
async executeUrl(action) {
|
|
309
|
+
const rawUrl = action.target || action.redirect;
|
|
310
|
+
if (!rawUrl) {
|
|
311
|
+
return { success: false, error: 'No URL provided for url action' };
|
|
312
|
+
}
|
|
313
|
+
const url = this.evaluator.evaluate(rawUrl);
|
|
314
|
+
if (!this.isValidUrl(url)) {
|
|
315
|
+
return {
|
|
316
|
+
success: false,
|
|
317
|
+
error: 'Invalid URL scheme. Only http://, https://, and relative URLs are allowed.',
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
const isExternal = url.startsWith('http://') || url.startsWith('https://');
|
|
321
|
+
const newTab = action.params?.newTab ?? isExternal;
|
|
322
|
+
if (this.navigationHandler) {
|
|
323
|
+
this.navigationHandler(url, { external: isExternal, newTab });
|
|
324
|
+
return { success: true };
|
|
325
|
+
}
|
|
326
|
+
if (newTab) {
|
|
327
|
+
window.open(url, '_blank', 'noopener,noreferrer');
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
return { success: true, redirect: url };
|
|
331
|
+
}
|
|
332
|
+
return { success: true };
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Execute modal action — open a dialog.
|
|
336
|
+
* Delegates to the registered modalHandler; returns modal schema if no handler.
|
|
337
|
+
*/
|
|
338
|
+
async executeModal(action) {
|
|
339
|
+
const modalSchema = action.modal || action.target || action.params?.schema;
|
|
340
|
+
if (!modalSchema) {
|
|
341
|
+
return { success: false, error: 'No modal schema or target provided for modal action' };
|
|
82
342
|
}
|
|
83
|
-
if (
|
|
343
|
+
if (this.modalHandler) {
|
|
344
|
+
return await this.modalHandler(modalSchema, this.context);
|
|
345
|
+
}
|
|
346
|
+
// Return the modal schema for the consumer to render
|
|
347
|
+
return { success: true, modal: modalSchema };
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Execute flow action — trigger a workflow/automation.
|
|
351
|
+
* Delegates to a registered 'flow' handler; otherwise returns not-implemented.
|
|
352
|
+
*/
|
|
353
|
+
async executeFlow(action) {
|
|
354
|
+
const flowName = action.target || action.name;
|
|
355
|
+
if (!flowName) {
|
|
356
|
+
return { success: false, error: 'No flow target provided for flow action' };
|
|
357
|
+
}
|
|
358
|
+
// Check for a registered flow handler (consumers register via registerHandler)
|
|
359
|
+
if (this.handlers.has('flow')) {
|
|
360
|
+
const handler = this.handlers.get('flow');
|
|
361
|
+
return await handler(action, this.context);
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
success: false,
|
|
365
|
+
error: `Flow handler not registered. Cannot execute flow: ${flowName}`,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
async executeActionSchema(action) {
|
|
369
|
+
const result = { success: true };
|
|
370
|
+
if (action.api || action.endpoint) {
|
|
84
371
|
const apiResult = await this.executeAPI(action);
|
|
85
372
|
if (!apiResult.success)
|
|
86
373
|
return apiResult;
|
|
@@ -101,19 +388,23 @@ export class ActionRunner {
|
|
|
101
388
|
*/
|
|
102
389
|
async executeNavigation(action) {
|
|
103
390
|
const nav = action.navigate || action;
|
|
104
|
-
const to = this.evaluator.evaluate(nav.to);
|
|
105
|
-
|
|
106
|
-
const isValidUrl = typeof to === 'string' && (to.startsWith('http://') ||
|
|
107
|
-
to.startsWith('https://') ||
|
|
108
|
-
to.startsWith('/') ||
|
|
109
|
-
to.startsWith('./'));
|
|
110
|
-
if (!isValidUrl) {
|
|
391
|
+
const to = this.evaluator.evaluate(nav.to || nav.target);
|
|
392
|
+
if (!this.isValidUrl(to)) {
|
|
111
393
|
return {
|
|
112
394
|
success: false,
|
|
113
|
-
error: 'Invalid URL scheme. Only http://, https://, and relative URLs are allowed.'
|
|
395
|
+
error: 'Invalid URL scheme. Only http://, https://, and relative URLs are allowed.',
|
|
114
396
|
};
|
|
115
397
|
}
|
|
116
|
-
|
|
398
|
+
const isExternal = nav.external || (typeof to === 'string' && (to.startsWith('http://') || to.startsWith('https://')));
|
|
399
|
+
if (this.navigationHandler) {
|
|
400
|
+
this.navigationHandler(to, {
|
|
401
|
+
external: isExternal,
|
|
402
|
+
newTab: nav.newTab ?? isExternal,
|
|
403
|
+
replace: nav.replace,
|
|
404
|
+
});
|
|
405
|
+
return { success: true };
|
|
406
|
+
}
|
|
407
|
+
if (isExternal) {
|
|
117
408
|
window.open(to, '_blank', 'noopener,noreferrer');
|
|
118
409
|
}
|
|
119
410
|
else {
|
|
@@ -121,30 +412,82 @@ export class ActionRunner {
|
|
|
121
412
|
}
|
|
122
413
|
return { success: true };
|
|
123
414
|
}
|
|
415
|
+
/**
|
|
416
|
+
* Execute API action — supports both simple string endpoint and complex ApiConfig.
|
|
417
|
+
*/
|
|
124
418
|
async executeAPI(action) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
419
|
+
// Resolve the endpoint: api (string/object), endpoint, or target
|
|
420
|
+
const apiConfig = action.api || action.endpoint || action.target;
|
|
421
|
+
if (!apiConfig) {
|
|
422
|
+
return { success: false, error: 'No API endpoint provided' };
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
let url;
|
|
426
|
+
let method;
|
|
427
|
+
let headers = { 'Content-Type': 'application/json' };
|
|
428
|
+
let body = undefined;
|
|
429
|
+
let responseType = 'json';
|
|
430
|
+
if (typeof apiConfig === 'string') {
|
|
431
|
+
// Simple string endpoint
|
|
432
|
+
url = apiConfig;
|
|
433
|
+
method = action.method || 'POST';
|
|
434
|
+
body = JSON.stringify(action.params || this.context.data || {});
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
// Complex ApiConfig
|
|
438
|
+
const config = apiConfig;
|
|
439
|
+
url = config.url;
|
|
440
|
+
method = config.method || action.method || 'POST';
|
|
441
|
+
headers = { ...headers, ...config.headers };
|
|
442
|
+
responseType = config.responseType || 'json';
|
|
443
|
+
// Build query params
|
|
444
|
+
if (config.queryParams) {
|
|
445
|
+
const searchParams = new URLSearchParams(config.queryParams);
|
|
446
|
+
url = `${url}${url.includes('?') ? '&' : '?'}${searchParams.toString()}`;
|
|
135
447
|
}
|
|
136
|
-
|
|
137
|
-
|
|
448
|
+
// Build body
|
|
449
|
+
if (config.body) {
|
|
450
|
+
body = typeof config.body === 'string'
|
|
451
|
+
? config.body
|
|
452
|
+
: JSON.stringify(config.body);
|
|
453
|
+
}
|
|
454
|
+
else if (method !== 'GET' && method !== 'HEAD') {
|
|
455
|
+
body = JSON.stringify(action.params || this.context.data || {});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
const fetchInit = { method, headers };
|
|
459
|
+
if (body && method !== 'GET' && method !== 'HEAD') {
|
|
460
|
+
fetchInit.body = body;
|
|
461
|
+
}
|
|
462
|
+
const response = await fetch(url, fetchInit);
|
|
463
|
+
if (!response.ok) {
|
|
464
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
138
465
|
}
|
|
139
|
-
|
|
140
|
-
|
|
466
|
+
let data;
|
|
467
|
+
switch (responseType) {
|
|
468
|
+
case 'text':
|
|
469
|
+
data = await response.text();
|
|
470
|
+
break;
|
|
471
|
+
case 'blob':
|
|
472
|
+
data = await response.blob();
|
|
473
|
+
break;
|
|
474
|
+
default:
|
|
475
|
+
data = await response.json();
|
|
141
476
|
}
|
|
477
|
+
return { success: true, data };
|
|
478
|
+
}
|
|
479
|
+
catch (error) {
|
|
480
|
+
return { success: false, error: error.message };
|
|
142
481
|
}
|
|
143
|
-
return { success: false, error: 'Complex API configuration not yet implemented' };
|
|
144
482
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
483
|
+
/**
|
|
484
|
+
* Validate URL to prevent javascript: or data: protocol injection.
|
|
485
|
+
*/
|
|
486
|
+
isValidUrl(url) {
|
|
487
|
+
return typeof url === 'string' && (url.startsWith('http://') ||
|
|
488
|
+
url.startsWith('https://') ||
|
|
489
|
+
url.startsWith('/') ||
|
|
490
|
+
url.startsWith('./'));
|
|
148
491
|
}
|
|
149
492
|
updateContext(newContext) {
|
|
150
493
|
this.context = { ...this.context, ...newContext };
|
|
@@ -153,7 +496,16 @@ export class ActionRunner {
|
|
|
153
496
|
getContext() {
|
|
154
497
|
return this.context;
|
|
155
498
|
}
|
|
499
|
+
/**
|
|
500
|
+
* Get the expression evaluator (for components that need to evaluate visibility, etc.)
|
|
501
|
+
*/
|
|
502
|
+
getEvaluator() {
|
|
503
|
+
return this.evaluator;
|
|
504
|
+
}
|
|
156
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
* Convenience function to execute a single action with a one-off runner.
|
|
508
|
+
*/
|
|
157
509
|
export async function executeAction(action, context = {}) {
|
|
158
510
|
const runner = new ActionRunner(context);
|
|
159
511
|
return await runner.execute(action);
|