@object-ui/core 0.5.0 → 3.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.
Files changed (96) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +28 -0
  3. package/dist/__benchmarks__/core.bench.d.ts +8 -0
  4. package/dist/__benchmarks__/core.bench.js +53 -0
  5. package/dist/actions/ActionRunner.d.ts +228 -4
  6. package/dist/actions/ActionRunner.js +397 -45
  7. package/dist/actions/TransactionManager.d.ts +193 -0
  8. package/dist/actions/TransactionManager.js +410 -0
  9. package/dist/actions/index.d.ts +1 -0
  10. package/dist/actions/index.js +1 -0
  11. package/dist/adapters/ApiDataSource.d.ts +69 -0
  12. package/dist/adapters/ApiDataSource.js +293 -0
  13. package/dist/adapters/ValueDataSource.d.ts +55 -0
  14. package/dist/adapters/ValueDataSource.js +287 -0
  15. package/dist/adapters/index.d.ts +3 -0
  16. package/dist/adapters/index.js +5 -2
  17. package/dist/adapters/resolveDataSource.d.ts +40 -0
  18. package/dist/adapters/resolveDataSource.js +59 -0
  19. package/dist/data-scope/DataScopeManager.d.ts +127 -0
  20. package/dist/data-scope/DataScopeManager.js +229 -0
  21. package/dist/data-scope/index.d.ts +10 -0
  22. package/dist/data-scope/index.js +10 -0
  23. package/dist/errors/index.d.ts +75 -0
  24. package/dist/errors/index.js +224 -0
  25. package/dist/evaluator/ExpressionEvaluator.d.ts +11 -1
  26. package/dist/evaluator/ExpressionEvaluator.js +32 -8
  27. package/dist/evaluator/FormulaFunctions.d.ts +58 -0
  28. package/dist/evaluator/FormulaFunctions.js +350 -0
  29. package/dist/evaluator/index.d.ts +1 -0
  30. package/dist/evaluator/index.js +1 -0
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +6 -2
  33. package/dist/query/query-ast.d.ts +2 -2
  34. package/dist/query/query-ast.js +3 -3
  35. package/dist/registry/Registry.d.ts +10 -0
  36. package/dist/registry/Registry.js +9 -2
  37. package/dist/registry/WidgetRegistry.d.ts +120 -0
  38. package/dist/registry/WidgetRegistry.js +275 -0
  39. package/dist/theme/ThemeEngine.d.ts +105 -0
  40. package/dist/theme/ThemeEngine.js +469 -0
  41. package/dist/theme/index.d.ts +8 -0
  42. package/dist/theme/index.js +8 -0
  43. package/dist/utils/debug.d.ts +31 -0
  44. package/dist/utils/debug.js +62 -0
  45. package/dist/validation/index.d.ts +1 -1
  46. package/dist/validation/index.js +1 -1
  47. package/dist/validation/validation-engine.d.ts +19 -1
  48. package/dist/validation/validation-engine.js +74 -3
  49. package/dist/validation/validators/index.d.ts +1 -1
  50. package/dist/validation/validators/index.js +1 -1
  51. package/dist/validation/validators/object-validation-engine.d.ts +2 -2
  52. package/dist/validation/validators/object-validation-engine.js +1 -1
  53. package/package.json +4 -3
  54. package/src/__benchmarks__/core.bench.ts +64 -0
  55. package/src/actions/ActionRunner.ts +577 -55
  56. package/src/actions/TransactionManager.ts +521 -0
  57. package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
  58. package/src/actions/__tests__/ActionRunner.test.ts +711 -0
  59. package/src/actions/__tests__/TransactionManager.test.ts +447 -0
  60. package/src/actions/index.ts +1 -0
  61. package/src/adapters/ApiDataSource.ts +349 -0
  62. package/src/adapters/ValueDataSource.ts +332 -0
  63. package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
  64. package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
  65. package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
  66. package/src/adapters/index.ts +6 -1
  67. package/src/adapters/resolveDataSource.ts +79 -0
  68. package/src/builder/__tests__/schema-builder.test.ts +235 -0
  69. package/src/data-scope/DataScopeManager.ts +269 -0
  70. package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
  71. package/src/data-scope/index.ts +16 -0
  72. package/src/errors/__tests__/errors.test.ts +292 -0
  73. package/src/errors/index.ts +270 -0
  74. package/src/evaluator/ExpressionEvaluator.ts +34 -8
  75. package/src/evaluator/FormulaFunctions.ts +398 -0
  76. package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
  77. package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
  78. package/src/evaluator/index.ts +1 -0
  79. package/src/index.ts +6 -3
  80. package/src/query/__tests__/window-functions.test.ts +1 -1
  81. package/src/query/query-ast.ts +3 -3
  82. package/src/registry/Registry.ts +19 -2
  83. package/src/registry/WidgetRegistry.ts +316 -0
  84. package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
  85. package/src/theme/ThemeEngine.ts +530 -0
  86. package/src/theme/__tests__/ThemeEngine.test.ts +668 -0
  87. package/src/theme/index.ts +24 -0
  88. package/src/utils/__tests__/debug.test.ts +83 -0
  89. package/src/utils/debug.ts +66 -0
  90. package/src/validation/__tests__/object-validation-engine.test.ts +1 -1
  91. package/src/validation/__tests__/schema-validator.test.ts +118 -0
  92. package/src/validation/index.ts +1 -1
  93. package/src/validation/validation-engine.ts +70 -3
  94. package/src/validation/validators/index.ts +1 -1
  95. package/src/validation/validators/object-validation-engine.ts +2 -2
  96. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,224 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ const BASE_DOC_URL = 'https://objectui.dev/docs/errors';
9
+ export const ERROR_CODES = {
10
+ 'OBJUI-001': {
11
+ code: 'OBJUI-001',
12
+ message: 'Unknown component type: "${type}"',
13
+ suggestion: 'Ensure the component is registered via registry.register() before rendering. Check for typos in the component type name.',
14
+ docUrl: `${BASE_DOC_URL}/OBJUI-001`,
15
+ },
16
+ 'OBJUI-002': {
17
+ code: 'OBJUI-002',
18
+ message: 'Schema validation failed: ${reason}',
19
+ suggestion: 'Verify the schema against the ObjectUI JSON spec. Run the built-in schema validator for details.',
20
+ docUrl: `${BASE_DOC_URL}/OBJUI-002`,
21
+ },
22
+ 'OBJUI-003': {
23
+ code: 'OBJUI-003',
24
+ message: 'Expression evaluation failed: ${expression}',
25
+ suggestion: 'Check the expression syntax and ensure all referenced variables exist in the current data scope.',
26
+ docUrl: `${BASE_DOC_URL}/OBJUI-003`,
27
+ },
28
+ 'OBJUI-004': {
29
+ code: 'OBJUI-004',
30
+ message: 'Plugin dependency missing: "${dependency}"',
31
+ suggestion: 'Install the required dependency and register it before loading this plugin.',
32
+ docUrl: `${BASE_DOC_URL}/OBJUI-004`,
33
+ },
34
+ 'OBJUI-005': {
35
+ code: 'OBJUI-005',
36
+ message: 'Plugin already loaded: "${plugin}"',
37
+ suggestion: 'A plugin with the same name is already registered. Remove the duplicate or use a unique plugin name.',
38
+ docUrl: `${BASE_DOC_URL}/OBJUI-005`,
39
+ },
40
+ 'OBJUI-006': {
41
+ code: 'OBJUI-006',
42
+ message: 'Plugin not found: "${plugin}"',
43
+ suggestion: 'Verify the plugin name and ensure it has been registered before access.',
44
+ docUrl: `${BASE_DOC_URL}/OBJUI-006`,
45
+ },
46
+ 'OBJUI-007': {
47
+ code: 'OBJUI-007',
48
+ message: 'Registry namespace deprecated: "${namespace}"',
49
+ suggestion: 'Migrate to the new namespace format. See the migration guide for details.',
50
+ docUrl: `${BASE_DOC_URL}/OBJUI-007`,
51
+ },
52
+ 'OBJUI-008': {
53
+ code: 'OBJUI-008',
54
+ message: 'Read-only scope violation: "${scope}"',
55
+ suggestion: 'You are trying to mutate a read-only data scope. Use a writable scope or clone the data before modifying.',
56
+ docUrl: `${BASE_DOC_URL}/OBJUI-008`,
57
+ },
58
+ 'OBJUI-009': {
59
+ code: 'OBJUI-009',
60
+ message: 'Invalid field configuration: "${field}"',
61
+ suggestion: 'Check the field schema for required properties (type, name). Ensure the field widget is registered.',
62
+ docUrl: `${BASE_DOC_URL}/OBJUI-009`,
63
+ },
64
+ 'OBJUI-010': {
65
+ code: 'OBJUI-010',
66
+ message: 'Action execution failed: "${action}"',
67
+ suggestion: 'Verify the action handler is registered and that all required parameters are provided.',
68
+ docUrl: `${BASE_DOC_URL}/OBJUI-010`,
69
+ },
70
+ };
71
+ // ---------------------------------------------------------------------------
72
+ // Type Guards
73
+ // ---------------------------------------------------------------------------
74
+ /**
75
+ * Check whether a string is a known ObjectUI error code.
76
+ */
77
+ export function isErrorCode(code) {
78
+ return typeof code === 'string' && code in ERROR_CODES;
79
+ }
80
+ // ---------------------------------------------------------------------------
81
+ // Base Error Class
82
+ // ---------------------------------------------------------------------------
83
+ /**
84
+ * Base error class for all ObjectUI errors.
85
+ */
86
+ export class ObjectUIError extends Error {
87
+ constructor(message, code, details) {
88
+ super(message);
89
+ Object.defineProperty(this, "code", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: code
94
+ });
95
+ Object.defineProperty(this, "details", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: details
100
+ });
101
+ this.name = 'ObjectUIError';
102
+ // Maintains proper stack trace for where error was thrown (only in V8)
103
+ if (Error.captureStackTrace) {
104
+ Error.captureStackTrace(this, this.constructor);
105
+ }
106
+ }
107
+ /**
108
+ * Convert error to JSON for logging / debugging.
109
+ */
110
+ toJSON() {
111
+ return {
112
+ name: this.name,
113
+ message: this.message,
114
+ code: this.code,
115
+ details: this.details,
116
+ stack: this.stack,
117
+ };
118
+ }
119
+ }
120
+ /**
121
+ * Type guard to check if an error is an ObjectUIError.
122
+ */
123
+ export function isObjectUIError(error) {
124
+ return error instanceof ObjectUIError;
125
+ }
126
+ // ---------------------------------------------------------------------------
127
+ // Specialized Error Classes
128
+ // ---------------------------------------------------------------------------
129
+ /** Thrown when a schema is invalid or fails validation. */
130
+ export class SchemaError extends ObjectUIError {
131
+ constructor(message, details) {
132
+ super(message, 'OBJUI-002', details);
133
+ this.name = 'SchemaError';
134
+ }
135
+ }
136
+ /** Thrown when a registry operation fails. */
137
+ export class RegistryError extends ObjectUIError {
138
+ constructor(message, code, details) {
139
+ super(message, code, details);
140
+ this.name = 'RegistryError';
141
+ }
142
+ }
143
+ /** Thrown when expression evaluation fails. */
144
+ export class ExpressionError extends ObjectUIError {
145
+ constructor(message, details) {
146
+ super(message, 'OBJUI-003', details);
147
+ this.name = 'ExpressionError';
148
+ }
149
+ }
150
+ /** Thrown when a plugin operation fails. */
151
+ export class PluginError extends ObjectUIError {
152
+ constructor(message, code, details) {
153
+ super(message, code, details);
154
+ this.name = 'PluginError';
155
+ }
156
+ }
157
+ /** Thrown when validation of user input / field config fails. */
158
+ export class FieldValidationError extends ObjectUIError {
159
+ constructor(message, details) {
160
+ super(message, 'OBJUI-009', details);
161
+ this.name = 'FieldValidationError';
162
+ }
163
+ }
164
+ // ---------------------------------------------------------------------------
165
+ // Factory & Formatting Helpers
166
+ // ---------------------------------------------------------------------------
167
+ /**
168
+ * Interpolate a template string with the given params.
169
+ * Template variables use the `${key}` syntax.
170
+ */
171
+ function interpolate(template, params) {
172
+ return template.replace(/\$\{(\w+)\}/g, (_match, key) => {
173
+ if (!(key in params) && typeof process !== 'undefined' && process.env?.NODE_ENV !== 'production') {
174
+ console.warn(`[ObjectUI] Missing interpolation parameter "${key}" in error message template.`);
175
+ }
176
+ return params[key] ?? `\${${key}}`;
177
+ });
178
+ }
179
+ /**
180
+ * Create an `ObjectUIError` (or subclass) from a known error code.
181
+ *
182
+ * @param code - A registered error code (e.g. `"OBJUI-001"`).
183
+ * @param params - Values to interpolate into the message template.
184
+ * @param details - Optional extra details attached to the error.
185
+ */
186
+ export function createError(code, params = {}, details) {
187
+ const entry = ERROR_CODES[code];
188
+ const message = entry
189
+ ? interpolate(entry.message, params)
190
+ : `Unknown error code: ${code}`;
191
+ switch (code) {
192
+ case 'OBJUI-002':
193
+ return new SchemaError(message, details);
194
+ case 'OBJUI-003':
195
+ return new ExpressionError(message, details);
196
+ case 'OBJUI-004':
197
+ case 'OBJUI-005':
198
+ case 'OBJUI-006':
199
+ return new PluginError(message, code, details);
200
+ case 'OBJUI-007':
201
+ case 'OBJUI-001':
202
+ return new RegistryError(message, code, details);
203
+ case 'OBJUI-009':
204
+ return new FieldValidationError(message, details);
205
+ default:
206
+ return new ObjectUIError(message, code, details);
207
+ }
208
+ }
209
+ /**
210
+ * Format an error message with actionable fix suggestions in development mode.
211
+ *
212
+ * @param error - The `ObjectUIError` to format.
213
+ * @param isDev - When `true`, appends the suggestion and documentation link.
214
+ */
215
+ export function formatErrorMessage(error, isDev = typeof process !== 'undefined' &&
216
+ process.env?.NODE_ENV !== 'production') {
217
+ const entry = ERROR_CODES[error.code];
218
+ let formatted = `[${error.code}] ${error.message}`;
219
+ if (isDev && entry) {
220
+ formatted += `\n\n💡 Suggestion: ${entry.suggestion}`;
221
+ formatted += `\n📖 Docs: ${entry.docUrl}`;
222
+ }
223
+ return formatted;
224
+ }
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import { ExpressionContext } from './ExpressionContext.js';
18
18
  import { ExpressionCache } from './ExpressionCache.js';
19
+ import { FormulaFunctions } from './FormulaFunctions.js';
19
20
  /**
20
21
  * Options for expression evaluation
21
22
  */
@@ -41,7 +42,8 @@ export interface EvaluationOptions {
41
42
  export declare class ExpressionEvaluator {
42
43
  private context;
43
44
  private cache;
44
- constructor(context?: ExpressionContext | Record<string, any>, cache?: ExpressionCache);
45
+ private formulas;
46
+ constructor(context?: ExpressionContext | Record<string, any>, cache?: ExpressionCache, formulas?: FormulaFunctions);
45
47
  /**
46
48
  * Evaluate a string that may contain template expressions like ${...}
47
49
  *
@@ -106,6 +108,14 @@ export declare class ExpressionEvaluator {
106
108
  * Clear the expression cache
107
109
  */
108
110
  clearCache(): void;
111
+ /**
112
+ * Get the formula functions registry
113
+ */
114
+ getFormulas(): FormulaFunctions;
115
+ /**
116
+ * Register a custom formula function
117
+ */
118
+ registerFunction(name: string, fn: (...args: any[]) => any): void;
109
119
  }
110
120
  /**
111
121
  * Convenience function to quickly evaluate an expression
@@ -16,11 +16,12 @@
16
16
  */
17
17
  import { ExpressionContext } from './ExpressionContext.js';
18
18
  import { ExpressionCache } from './ExpressionCache.js';
19
+ import { FormulaFunctions } from './FormulaFunctions.js';
19
20
  /**
20
21
  * Expression evaluator for dynamic UI expressions
21
22
  */
22
23
  export class ExpressionEvaluator {
23
- constructor(context, cache) {
24
+ constructor(context, cache, formulas) {
24
25
  Object.defineProperty(this, "context", {
25
26
  enumerable: true,
26
27
  configurable: true,
@@ -33,6 +34,12 @@ export class ExpressionEvaluator {
33
34
  writable: true,
34
35
  value: void 0
35
36
  });
37
+ Object.defineProperty(this, "formulas", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: void 0
42
+ });
36
43
  if (context instanceof ExpressionContext) {
37
44
  this.context = context;
38
45
  }
@@ -41,6 +48,7 @@ export class ExpressionEvaluator {
41
48
  }
42
49
  // Use provided cache or create a new one
43
50
  this.cache = cache || new ExpressionCache();
51
+ this.formulas = formulas || new FormulaFunctions();
44
52
  }
45
53
  /**
46
54
  * Evaluate a string that may contain template expressions like ${...}
@@ -114,9 +122,12 @@ export class ExpressionEvaluator {
114
122
  try {
115
123
  // Create a safe evaluation function
116
124
  const contextObj = this.context.toObject();
125
+ // Inject formula functions into the evaluation context
126
+ const formulaObj = this.formulas.toObject();
127
+ const mergedContext = { ...formulaObj, ...contextObj };
117
128
  // Build safe function with context variables
118
- const varNames = Object.keys(contextObj);
119
- const varValues = Object.values(contextObj);
129
+ const varNames = Object.keys(mergedContext);
130
+ const varValues = Object.values(mergedContext);
120
131
  // Use cached compilation
121
132
  const compiled = this.cache.compile(expression, varNames);
122
133
  // Execute with context values
@@ -184,8 +195,8 @@ export class ExpressionEvaluator {
184
195
  * Create a new evaluator with additional context data
185
196
  */
186
197
  withContext(data) {
187
- // Share the cache with the new evaluator for maximum efficiency
188
- return new ExpressionEvaluator(this.context.createChild(data), this.cache);
198
+ // Share the cache and formulas with the new evaluator for maximum efficiency
199
+ return new ExpressionEvaluator(this.context.createChild(data), this.cache, this.formulas);
189
200
  }
190
201
  /**
191
202
  * Get cache statistics (useful for debugging and optimization)
@@ -199,22 +210,35 @@ export class ExpressionEvaluator {
199
210
  clearCache() {
200
211
  this.cache.clear();
201
212
  }
213
+ /**
214
+ * Get the formula functions registry
215
+ */
216
+ getFormulas() {
217
+ return this.formulas;
218
+ }
219
+ /**
220
+ * Register a custom formula function
221
+ */
222
+ registerFunction(name, fn) {
223
+ this.formulas.register(name, fn);
224
+ }
202
225
  }
203
226
  /**
204
- * Shared global cache for convenience functions
227
+ * Shared global cache and formulas for convenience functions
205
228
  */
206
229
  const globalCache = new ExpressionCache();
230
+ const globalFormulas = new FormulaFunctions();
207
231
  /**
208
232
  * Convenience function to quickly evaluate an expression
209
233
  */
210
234
  export function evaluateExpression(expression, context = {}, options = {}) {
211
- const evaluator = new ExpressionEvaluator(context, globalCache);
235
+ const evaluator = new ExpressionEvaluator(context, globalCache, globalFormulas);
212
236
  return evaluator.evaluate(expression, options);
213
237
  }
214
238
  /**
215
239
  * Convenience function to evaluate a condition
216
240
  */
217
241
  export function evaluateCondition(condition, context = {}) {
218
- const evaluator = new ExpressionEvaluator(context, globalCache);
242
+ const evaluator = new ExpressionEvaluator(context, globalCache, globalFormulas);
219
243
  return evaluator.evaluateCondition(condition);
220
244
  }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ /**
9
+ * @object-ui/core - Formula Functions
10
+ *
11
+ * Built-in formula functions for the expression engine.
12
+ * Provides aggregation, date, logic, and string functions
13
+ * compatible with low-code platform expression evaluation.
14
+ *
15
+ * @module evaluator
16
+ * @packageDocumentation
17
+ */
18
+ /**
19
+ * A formula function that can be registered with the expression evaluator
20
+ */
21
+ export type FormulaFunction = (...args: any[]) => any;
22
+ /**
23
+ * Registry of built-in formula functions
24
+ */
25
+ export declare class FormulaFunctions {
26
+ private functions;
27
+ constructor();
28
+ /**
29
+ * Register a custom formula function
30
+ */
31
+ register(name: string, fn: FormulaFunction): void;
32
+ /**
33
+ * Get a formula function by name
34
+ */
35
+ get(name: string): FormulaFunction | undefined;
36
+ /**
37
+ * Check if a function is registered
38
+ */
39
+ has(name: string): boolean;
40
+ /**
41
+ * Get all registered function names
42
+ */
43
+ getNames(): string[];
44
+ /**
45
+ * Get all functions as a plain object (for injection into expression context)
46
+ */
47
+ toObject(): Record<string, FormulaFunction>;
48
+ /**
49
+ * Register all default built-in functions
50
+ */
51
+ private registerDefaults;
52
+ private registerAggregationFunctions;
53
+ private registerDateFunctions;
54
+ private registerLogicFunctions;
55
+ private registerStringFunctions;
56
+ private registerStringSearchFunctions;
57
+ private registerStatisticalFunctions;
58
+ }