@object-ui/core 0.3.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.
@@ -0,0 +1,576 @@
1
+ /**
2
+ * @object-ui/core - Schema Builder
3
+ *
4
+ * Fluent API for building schemas programmatically.
5
+ * Provides type-safe builder functions for common schema patterns.
6
+ *
7
+ * @module builder
8
+ * @packageDocumentation
9
+ */
10
+
11
+ import type {
12
+ BaseSchema,
13
+ FormSchema,
14
+ FormField,
15
+ CRUDSchema,
16
+ TableColumn,
17
+ ActionSchema,
18
+ ButtonSchema,
19
+ InputSchema,
20
+ CardSchema,
21
+ GridSchema,
22
+ FlexSchema
23
+ } from '@object-ui/types';
24
+
25
+ /**
26
+ * Base builder class
27
+ */
28
+ class SchemaBuilder<T extends BaseSchema> {
29
+ protected schema: any;
30
+
31
+ constructor(type: string) {
32
+ this.schema = { type };
33
+ }
34
+
35
+ /**
36
+ * Set the ID
37
+ */
38
+ id(id: string): this {
39
+ this.schema.id = id;
40
+ return this;
41
+ }
42
+
43
+ /**
44
+ * Set the className
45
+ */
46
+ className(className: string): this {
47
+ this.schema.className = className;
48
+ return this;
49
+ }
50
+
51
+ /**
52
+ * Set visibility
53
+ */
54
+ visible(visible: boolean): this {
55
+ this.schema.visible = visible;
56
+ return this;
57
+ }
58
+
59
+ /**
60
+ * Set conditional visibility
61
+ */
62
+ visibleOn(expression: string): this {
63
+ this.schema.visibleOn = expression;
64
+ return this;
65
+ }
66
+
67
+ /**
68
+ * Set disabled state
69
+ */
70
+ disabled(disabled: boolean): this {
71
+ this.schema.disabled = disabled;
72
+ return this;
73
+ }
74
+
75
+ /**
76
+ * Set test ID
77
+ */
78
+ testId(testId: string): this {
79
+ this.schema.testId = testId;
80
+ return this;
81
+ }
82
+
83
+ /**
84
+ * Build the final schema
85
+ */
86
+ build(): T {
87
+ return this.schema as T;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Form builder
93
+ */
94
+ export class FormBuilder extends SchemaBuilder<FormSchema> {
95
+ constructor() {
96
+ super('form');
97
+ this.schema.fields = [];
98
+ }
99
+
100
+ /**
101
+ * Add a field to the form
102
+ */
103
+ field(field: FormField): this {
104
+ this.schema.fields = [...(this.schema.fields || []), field];
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * Add multiple fields
110
+ */
111
+ fields(fields: FormField[]): this {
112
+ this.schema.fields = fields;
113
+ return this;
114
+ }
115
+
116
+ /**
117
+ * Set default values
118
+ */
119
+ defaultValues(values: Record<string, any>): this {
120
+ this.schema.defaultValues = values;
121
+ return this;
122
+ }
123
+
124
+ /**
125
+ * Set submit label
126
+ */
127
+ submitLabel(label: string): this {
128
+ this.schema.submitLabel = label;
129
+ return this;
130
+ }
131
+
132
+ /**
133
+ * Set form layout
134
+ */
135
+ layout(layout: 'vertical' | 'horizontal'): this {
136
+ this.schema.layout = layout;
137
+ return this;
138
+ }
139
+
140
+ /**
141
+ * Set number of columns
142
+ */
143
+ columns(columns: number): this {
144
+ this.schema.columns = columns;
145
+ return this;
146
+ }
147
+
148
+ /**
149
+ * Set submit handler
150
+ */
151
+ onSubmit(handler: (data: Record<string, any>) => void | Promise<void>): this {
152
+ this.schema.onSubmit = handler;
153
+ return this;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * CRUD builder
159
+ */
160
+ export class CRUDBuilder extends SchemaBuilder<CRUDSchema> {
161
+ constructor() {
162
+ super('crud');
163
+ this.schema.columns = [];
164
+ }
165
+
166
+ /**
167
+ * Set resource name
168
+ */
169
+ resource(resource: string): this {
170
+ this.schema.resource = resource;
171
+ return this;
172
+ }
173
+
174
+ /**
175
+ * Set API endpoint
176
+ */
177
+ api(api: string): this {
178
+ this.schema.api = api;
179
+ return this;
180
+ }
181
+
182
+ /**
183
+ * Set title
184
+ */
185
+ title(title: string): this {
186
+ this.schema.title = title;
187
+ return this;
188
+ }
189
+
190
+ /**
191
+ * Set description
192
+ */
193
+ description(description: string): this {
194
+ this.schema.description = description;
195
+ return this;
196
+ }
197
+
198
+ /**
199
+ * Add a column
200
+ */
201
+ column(column: TableColumn): this {
202
+ this.schema.columns = [...(this.schema.columns || []), column];
203
+ return this;
204
+ }
205
+
206
+ /**
207
+ * Set all columns
208
+ */
209
+ columns(columns: TableColumn[]): this {
210
+ this.schema.columns = columns;
211
+ return this;
212
+ }
213
+
214
+ /**
215
+ * Set form fields
216
+ */
217
+ fields(fields: FormField[]): this {
218
+ this.schema.fields = fields;
219
+ return this;
220
+ }
221
+
222
+ /**
223
+ * Enable create operation
224
+ */
225
+ enableCreate(label?: string): this {
226
+ if (!this.schema.operations) this.schema.operations = {};
227
+ this.schema.operations.create = {
228
+ enabled: true,
229
+ label: label || 'Create',
230
+ api: this.schema.api,
231
+ method: 'POST'
232
+ };
233
+ return this;
234
+ }
235
+
236
+ /**
237
+ * Enable update operation
238
+ */
239
+ enableUpdate(label?: string): this {
240
+ if (!this.schema.operations) this.schema.operations = {};
241
+ this.schema.operations.update = {
242
+ enabled: true,
243
+ label: label || 'Update',
244
+ api: `${this.schema.api}/\${id}`,
245
+ method: 'PUT'
246
+ };
247
+ return this;
248
+ }
249
+
250
+ /**
251
+ * Enable delete operation
252
+ */
253
+ enableDelete(label?: string, confirmText?: string): this {
254
+ if (!this.schema.operations) this.schema.operations = {};
255
+ this.schema.operations.delete = {
256
+ enabled: true,
257
+ label: label || 'Delete',
258
+ api: `${this.schema.api}/\${id}`,
259
+ method: 'DELETE',
260
+ confirmText: confirmText || 'Are you sure?'
261
+ };
262
+ return this;
263
+ }
264
+
265
+ /**
266
+ * Set pagination
267
+ */
268
+ pagination(pageSize: number = 20): this {
269
+ this.schema.pagination = {
270
+ enabled: true,
271
+ pageSize,
272
+ pageSizeOptions: [10, 20, 50, 100],
273
+ showTotal: true,
274
+ showSizeChanger: true
275
+ };
276
+ return this;
277
+ }
278
+
279
+ /**
280
+ * Enable row selection
281
+ */
282
+ selectable(mode: 'single' | 'multiple' = 'multiple'): this {
283
+ this.schema.selectable = mode;
284
+ return this;
285
+ }
286
+
287
+ /**
288
+ * Add a batch action
289
+ */
290
+ batchAction(action: ActionSchema): this {
291
+ this.schema.batchActions = [...(this.schema.batchActions || []), action];
292
+ return this;
293
+ }
294
+
295
+ /**
296
+ * Add a row action
297
+ */
298
+ rowAction(action: ActionSchema): this {
299
+ this.schema.rowActions = [...(this.schema.rowActions || []), action];
300
+ return this;
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Button builder
306
+ */
307
+ export class ButtonBuilder extends SchemaBuilder<ButtonSchema> {
308
+ constructor() {
309
+ super('button');
310
+ }
311
+
312
+ /**
313
+ * Set button label
314
+ */
315
+ label(label: string): this {
316
+ this.schema.label = label;
317
+ return this;
318
+ }
319
+
320
+ /**
321
+ * Set button variant
322
+ */
323
+ variant(variant: 'default' | 'secondary' | 'destructive' | 'outline' | 'ghost' | 'link'): this {
324
+ this.schema.variant = variant;
325
+ return this;
326
+ }
327
+
328
+ /**
329
+ * Set button size
330
+ */
331
+ size(size: 'default' | 'sm' | 'lg' | 'icon'): this {
332
+ this.schema.size = size;
333
+ return this;
334
+ }
335
+
336
+ /**
337
+ * Set button icon
338
+ */
339
+ icon(icon: string): this {
340
+ this.schema.icon = icon;
341
+ return this;
342
+ }
343
+
344
+ /**
345
+ * Set click handler
346
+ */
347
+ onClick(handler: () => void | Promise<void>): this {
348
+ this.schema.onClick = handler;
349
+ return this;
350
+ }
351
+
352
+ /**
353
+ * Set loading state
354
+ */
355
+ loading(loading: boolean): this {
356
+ this.schema.loading = loading;
357
+ return this;
358
+ }
359
+ }
360
+
361
+ /**
362
+ * Input builder
363
+ */
364
+ export class InputBuilder extends SchemaBuilder<InputSchema> {
365
+ constructor() {
366
+ super('input');
367
+ }
368
+
369
+ /**
370
+ * Set field name
371
+ */
372
+ name(name: string): this {
373
+ this.schema.name = name;
374
+ return this;
375
+ }
376
+
377
+ /**
378
+ * Set label
379
+ */
380
+ label(label: string): this {
381
+ this.schema.label = label;
382
+ return this;
383
+ }
384
+
385
+ /**
386
+ * Set placeholder
387
+ */
388
+ placeholder(placeholder: string): this {
389
+ this.schema.placeholder = placeholder;
390
+ return this;
391
+ }
392
+
393
+ /**
394
+ * Set input type
395
+ */
396
+ inputType(type: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url'): this {
397
+ this.schema.inputType = type;
398
+ return this;
399
+ }
400
+
401
+ /**
402
+ * Mark as required
403
+ */
404
+ required(required: boolean = true): this {
405
+ this.schema.required = required;
406
+ return this;
407
+ }
408
+
409
+ /**
410
+ * Set default value
411
+ */
412
+ defaultValue(value: string | number): this {
413
+ this.schema.defaultValue = value;
414
+ return this;
415
+ }
416
+ }
417
+
418
+ /**
419
+ * Card builder
420
+ */
421
+ export class CardBuilder extends SchemaBuilder<CardSchema> {
422
+ constructor() {
423
+ super('card');
424
+ }
425
+
426
+ /**
427
+ * Set card title
428
+ */
429
+ title(title: string): this {
430
+ this.schema.title = title;
431
+ return this;
432
+ }
433
+
434
+ /**
435
+ * Set card description
436
+ */
437
+ description(description: string): this {
438
+ this.schema.description = description;
439
+ return this;
440
+ }
441
+
442
+ /**
443
+ * Set card content
444
+ */
445
+ content(content: BaseSchema | BaseSchema[]): this {
446
+ this.schema.content = content;
447
+ return this;
448
+ }
449
+
450
+ /**
451
+ * Set card variant
452
+ */
453
+ variant(variant: 'default' | 'outline' | 'ghost'): this {
454
+ this.schema.variant = variant;
455
+ return this;
456
+ }
457
+
458
+ /**
459
+ * Make card hoverable
460
+ */
461
+ hoverable(hoverable: boolean = true): this {
462
+ this.schema.hoverable = hoverable;
463
+ return this;
464
+ }
465
+ }
466
+
467
+ /**
468
+ * Grid builder
469
+ */
470
+ export class GridBuilder extends SchemaBuilder<GridSchema> {
471
+ constructor() {
472
+ super('grid');
473
+ this.schema.children = [];
474
+ }
475
+
476
+ /**
477
+ * Set number of columns
478
+ */
479
+ columns(columns: number): this {
480
+ this.schema.columns = columns;
481
+ return this;
482
+ }
483
+
484
+ /**
485
+ * Set gap
486
+ */
487
+ gap(gap: number): this {
488
+ this.schema.gap = gap;
489
+ return this;
490
+ }
491
+
492
+ /**
493
+ * Add a child
494
+ */
495
+ child(child: BaseSchema): this {
496
+ const children = Array.isArray(this.schema.children) ? this.schema.children : [];
497
+ this.schema.children = [...children, child];
498
+ return this;
499
+ }
500
+
501
+ /**
502
+ * Set all children
503
+ */
504
+ children(children: BaseSchema[]): this {
505
+ this.schema.children = children;
506
+ return this;
507
+ }
508
+ }
509
+
510
+ /**
511
+ * Flex builder
512
+ */
513
+ export class FlexBuilder extends SchemaBuilder<FlexSchema> {
514
+ constructor() {
515
+ super('flex');
516
+ this.schema.children = [];
517
+ }
518
+
519
+ /**
520
+ * Set flex direction
521
+ */
522
+ direction(direction: 'row' | 'col' | 'row-reverse' | 'col-reverse'): this {
523
+ this.schema.direction = direction;
524
+ return this;
525
+ }
526
+
527
+ /**
528
+ * Set justify content
529
+ */
530
+ justify(justify: 'start' | 'end' | 'center' | 'between' | 'around' | 'evenly'): this {
531
+ this.schema.justify = justify;
532
+ return this;
533
+ }
534
+
535
+ /**
536
+ * Set align items
537
+ */
538
+ align(align: 'start' | 'end' | 'center' | 'baseline' | 'stretch'): this {
539
+ this.schema.align = align;
540
+ return this;
541
+ }
542
+
543
+ /**
544
+ * Set gap
545
+ */
546
+ gap(gap: number): this {
547
+ this.schema.gap = gap;
548
+ return this;
549
+ }
550
+
551
+ /**
552
+ * Add a child
553
+ */
554
+ child(child: BaseSchema): this {
555
+ const children = Array.isArray(this.schema.children) ? this.schema.children : [];
556
+ this.schema.children = [...children, child];
557
+ return this;
558
+ }
559
+
560
+ /**
561
+ * Set all children
562
+ */
563
+ children(children: BaseSchema[]): this {
564
+ this.schema.children = children;
565
+ return this;
566
+ }
567
+ }
568
+
569
+ // Export factory functions
570
+ export const form = () => new FormBuilder();
571
+ export const crud = () => new CRUDBuilder();
572
+ export const button = () => new ButtonBuilder();
573
+ export const input = () => new InputBuilder();
574
+ export const card = () => new CardBuilder();
575
+ export const grid = () => new GridBuilder();
576
+ export const flex = () => new FlexBuilder();
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './types';
2
+ export * from './registry/Registry';
3
+ export * from './validation/schema-validator';
4
+ export * from './builder/schema-builder';
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from './types';
2
+ export * from './registry/Registry';
3
+ export * from './validation/schema-validator';
4
+ export * from './builder/schema-builder';
5
+ // export * from './data-scope'; // TODO
6
+ // export * from './evaluator'; // TODO
7
+ // export * from './validators'; // TODO
@@ -0,0 +1,7 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ describe('core', () => {
4
+ it('should work', () => {
5
+ expect(true).toBe(true);
6
+ });
7
+ });
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './types';
2
+ export * from './registry/Registry';
3
+ export * from './validation/schema-validator';
4
+ export * from './builder/schema-builder';
5
+ // export * from './data-scope'; // TODO
6
+ // export * from './evaluator'; // TODO
7
+ // export * from './validators'; // TODO
8
+
@@ -0,0 +1,49 @@
1
+ import type { SchemaNode } from '../types';
2
+ export type ComponentRenderer<T = any> = T;
3
+ export type ComponentInput = {
4
+ name: string;
5
+ type: 'string' | 'number' | 'boolean' | 'enum' | 'array' | 'object' | 'color' | 'date' | 'code' | 'file' | 'slot';
6
+ label?: string;
7
+ defaultValue?: any;
8
+ required?: boolean;
9
+ enum?: string[] | {
10
+ label: string;
11
+ value: any;
12
+ }[];
13
+ description?: string;
14
+ advanced?: boolean;
15
+ inputType?: string;
16
+ };
17
+ export type ComponentMeta = {
18
+ label?: string;
19
+ icon?: string;
20
+ category?: string;
21
+ inputs?: ComponentInput[];
22
+ defaultProps?: Record<string, any>;
23
+ defaultChildren?: SchemaNode[];
24
+ examples?: Record<string, any>;
25
+ isContainer?: boolean;
26
+ resizable?: boolean;
27
+ resizeConstraints?: {
28
+ width?: boolean;
29
+ height?: boolean;
30
+ minWidth?: number;
31
+ maxWidth?: number;
32
+ minHeight?: number;
33
+ maxHeight?: number;
34
+ };
35
+ };
36
+ export type ComponentConfig<T = any> = ComponentMeta & {
37
+ type: string;
38
+ component: ComponentRenderer<T>;
39
+ };
40
+ export declare class Registry<T = any> {
41
+ private components;
42
+ register(type: string, component: ComponentRenderer<T>, meta?: ComponentMeta): void;
43
+ get(type: string): ComponentRenderer<T> | undefined;
44
+ getConfig(type: string): ComponentConfig<T> | undefined;
45
+ has(type: string): boolean;
46
+ getAllTypes(): string[];
47
+ getAllConfigs(): ComponentConfig<T>[];
48
+ }
49
+ export declare const ComponentRegistry: Registry<any>;
@@ -0,0 +1,36 @@
1
+ export class Registry {
2
+ constructor() {
3
+ Object.defineProperty(this, "components", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: new Map()
8
+ });
9
+ }
10
+ register(type, component, meta) {
11
+ if (this.components.has(type)) {
12
+ console.warn(`Component type "${type}" is already registered. Overwriting.`);
13
+ }
14
+ this.components.set(type, {
15
+ type,
16
+ component,
17
+ ...meta
18
+ });
19
+ }
20
+ get(type) {
21
+ return this.components.get(type)?.component;
22
+ }
23
+ getConfig(type) {
24
+ return this.components.get(type);
25
+ }
26
+ has(type) {
27
+ return this.components.has(type);
28
+ }
29
+ getAllTypes() {
30
+ return Array.from(this.components.keys());
31
+ }
32
+ getAllConfigs() {
33
+ return Array.from(this.components.values());
34
+ }
35
+ }
36
+ export const ComponentRegistry = new Registry();