@mindmakr/gs-websdk
Version:
Web SDK for Guru SaaS System - Complete JavaScript/TypeScript SDK for building applications with dynamic schema management
467 lines (466 loc) • 12.7 kB
TypeScript
/**
* Fluent API Template Builder
* Provides a developer-friendly way to build schema templates declaratively
*/
/**
* Field configuration interface for the builder
*/
export interface FieldConfig {
type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null';
title?: string;
description?: string;
format?: string;
placeholder?: string;
helpText?: string;
default?: any;
required?: boolean;
minLength?: number;
maxLength?: number;
minimum?: number;
maximum?: number;
pattern?: string;
enum?: any[];
enumNames?: string[];
items?: any;
minItems?: number;
maxItems?: number;
uniqueItems?: boolean;
layout?: {
width?: 'quarter' | 'half' | 'three-quarter' | 'full';
height?: 'small' | 'medium' | 'large';
group?: string;
conditional?: {
field: string;
operator: 'equals' | 'not_equals' | 'contains';
value: any;
};
};
enhancement?: {
icon?: string;
image?: string;
style?: {
variant?: 'default' | 'outline' | 'ghost' | 'destructive' | 'secondary';
size?: 'sm' | 'md' | 'lg' | 'xl';
className?: string;
};
validation?: Array<{
type: string;
value?: any;
message: Record<string, string>;
}>;
};
widget?: 'textarea' | 'select' | 'radio' | 'checkboxes' | 'file' | 'richtext' | 'section';
options?: Record<string, any>;
}
/**
* Section configuration for organizing fields
*/
export interface SectionConfig {
title: string;
description?: string;
collapsible?: boolean;
expanded?: boolean;
}
/**
* Fluent API Template Builder
*
* @example
* ```typescript
* const template = new TemplateBuilder('customer_profile', 'Customer Profile')
* .category('customer_management')
* .description('Comprehensive customer information form')
* .section('Basic Information', 'Essential customer details')
* .text('firstName', 'First Name')
* .required()
* .minLength(1)
* .maxLength(50)
* .placeholder('Enter first name')
* .layout({ width: 'half' })
* .email('email', 'Email Address')
* .required()
* .placeholder('customer@example.com')
* .layout({ width: 'half' })
* .section('Contact Information')
* .phone('phone', 'Phone Number')
* .layout({ width: 'half' })
* .select('country', 'Country')
* .options(['US', 'CA', 'UK'], ['United States', 'Canada', 'United Kingdom'])
* .default('US')
* .layout({ width: 'half' })
* .section('Preferences')
* .boolean('newsletter', 'Subscribe to Newsletter')
* .default(false)
* .multiSelect('interests', 'Areas of Interest')
* .options(['technology', 'sports', 'music', 'travel'])
* .minItems(1)
* .build();
* ```
*/
export declare class TemplateBuilder {
private templateData;
private currentField?;
private fieldOrder;
private requiredFields;
private enhancedProperties;
constructor(code: string, name: string);
/**
* Set template category
*/
category(category: string): this;
/**
* Set template description
*/
description(description: string): this;
/**
* Set template path for hierarchical organization
*/
path(path: string): this;
/**
* Set template version
*/
version(version: string): this;
/**
* Mark template as system template
*/
system(isSystem?: boolean): this;
/**
* Add a section divider
*/
section(title: string, description?: string): this;
/**
* Add a text field
*/
text(key: string, title?: string): FieldBuilder;
/**
* Add an email field
*/
email(key: string, title?: string): FieldBuilder;
/**
* Add a password field
*/
password(key: string, title?: string): FieldBuilder;
/**
* Add a phone field
*/
phone(key: string, title?: string): FieldBuilder;
/**
* Add a URL field
*/
url(key: string, title?: string): FieldBuilder;
/**
* Add a textarea field
*/
textarea(key: string, title?: string): FieldBuilder;
/**
* Add a rich text field
*/
richText(key: string, title?: string): FieldBuilder;
/**
* Add a number field
*/
number(key: string, title?: string): FieldBuilder;
/**
* Add an integer field
*/
integer(key: string, title?: string): FieldBuilder;
/**
* Add a boolean field
*/
boolean(key: string, title?: string): FieldBuilder;
/**
* Add a date field
*/
date(key: string, title?: string): FieldBuilder;
/**
* Add a datetime field
*/
datetime(key: string, title?: string): FieldBuilder;
/**
* Add a time field
*/
time(key: string, title?: string): FieldBuilder;
/**
* Add a color picker field
*/
color(key: string, title?: string): FieldBuilder;
/**
* Add a select dropdown field
*/
select(key: string, title?: string): FieldBuilder;
/**
* Add a radio button field
*/
radio(key: string, title?: string): FieldBuilder;
/**
* Add a multi-select field
*/
multiSelect(key: string, title?: string): FieldBuilder;
/**
* Add a checkbox array field
*/
checkboxes(key: string, title?: string): FieldBuilder;
/**
* Add a file upload field
*/
file(key: string, title?: string): FieldBuilder;
/**
* Add an image upload field
*/
image(key: string, title?: string): FieldBuilder;
/**
* Add a video upload field
*/
video(key: string, title?: string): FieldBuilder;
/**
* Add an audio upload field
*/
audio(key: string, title?: string): FieldBuilder;
/**
* Add a reference field (foreign key)
*/
reference(key: string, title?: string): FieldBuilder;
/**
* Add an object field (nested form)
*/
object(key: string, title?: string): ObjectFieldBuilder;
/**
* Add an array field (list of items)
*/
array(key: string, title?: string): ArrayFieldBuilder;
/**
* Internal method to add a field
*/
private addField;
/**
* Mark current field as required
*/
markRequired(key: string): void;
/**
* Set enhancement for current field
*/
setEnhancement(key: string, enhancement: any): void;
/**
* Get current field configuration
*/
getField(key: string): any;
/**
* Build the final template configuration
*/
build(): {
name: string;
code: string;
description?: string;
category: string;
schema_definition: Record<string, any>;
enhanced_properties?: Record<string, any>;
path?: string;
is_system?: boolean;
version?: string;
};
}
/**
* Field builder for fluent configuration of individual fields
*/
export declare class FieldBuilder {
private templateBuilder;
private fieldKey;
constructor(templateBuilder: TemplateBuilder, fieldKey: string);
/**
* Mark field as required
*/
required(): this;
/**
* Set field placeholder
*/
placeholder(placeholder: string): this;
/**
* Set field help text
*/
help(helpText: string): this;
/**
* Set field description
*/
description(description: string): this;
/**
* Set default value
*/
default(value: any): this;
/**
* Set minimum length (for strings)
*/
minLength(length: number): this;
/**
* Set maximum length (for strings)
*/
maxLength(length: number): this;
/**
* Set minimum value (for numbers)
*/
min(value: number): this;
/**
* Set maximum value (for numbers)
*/
max(value: number): this;
/**
* Set validation pattern (regex)
*/
pattern(pattern: string): this;
/**
* Set enum options for select/radio fields
*/
options(values: any[], labels?: string[]): this;
/**
* Set minimum items (for arrays)
*/
minItems(count: number): this;
/**
* Set maximum items (for arrays)
*/
maxItems(count: number): this;
/**
* Ensure unique items in array
*/
unique(): this;
/**
* Set layout configuration
*/
layout(config: {
width?: 'quarter' | 'half' | 'three-quarter' | 'full';
height?: 'small' | 'medium' | 'large';
group?: string;
conditional?: {
field: string;
operator: 'equals' | 'not_equals' | 'contains';
value: any;
};
}): this;
/**
* Set field styling
*/
style(config: {
variant?: 'default' | 'outline' | 'ghost' | 'destructive' | 'secondary';
size?: 'sm' | 'md' | 'lg' | 'xl';
className?: string;
}): this;
/**
* Set field icon
*/
icon(icon: string): this;
/**
* Add conditional display logic
*/
conditional(config: {
field: string;
operator: 'equals' | 'not_equals' | 'contains';
value: any;
}): this;
/**
* Add custom validation rules
*/
validation(rules: Array<{
type: string;
value?: any;
message: Record<string, string>;
}>): this;
/**
* Return to template builder
*/
end(): TemplateBuilder;
section(title: string, description?: string): TemplateBuilder;
text(key: string, title?: string): FieldBuilder;
email(key: string, title?: string): FieldBuilder;
password(key: string, title?: string): FieldBuilder;
phone(key: string, title?: string): FieldBuilder;
url(key: string, title?: string): FieldBuilder;
textarea(key: string, title?: string): FieldBuilder;
richText(key: string, title?: string): FieldBuilder;
number(key: string, title?: string): FieldBuilder;
integer(key: string, title?: string): FieldBuilder;
boolean(key: string, title?: string): FieldBuilder;
date(key: string, title?: string): FieldBuilder;
datetime(key: string, title?: string): FieldBuilder;
time(key: string, title?: string): FieldBuilder;
color(key: string, title?: string): FieldBuilder;
select(key: string, title?: string): FieldBuilder;
radio(key: string, title?: string): FieldBuilder;
multiSelect(key: string, title?: string): FieldBuilder;
checkboxes(key: string, title?: string): FieldBuilder;
file(key: string, title?: string): FieldBuilder;
image(key: string, title?: string): FieldBuilder;
video(key: string, title?: string): FieldBuilder;
audio(key: string, title?: string): FieldBuilder;
reference(key: string, title?: string): FieldBuilder;
build(): {
name: string;
code: string;
description?: string | undefined;
category: string;
schema_definition: Record<string, any>;
enhanced_properties?: Record<string, any> | undefined;
path?: string | undefined;
is_system?: boolean | undefined;
version?: string | undefined;
};
}
/**
* Object field builder for nested forms
*/
export declare class ObjectFieldBuilder {
private templateBuilder;
private fieldKey;
private objectField;
constructor(templateBuilder: TemplateBuilder, fieldKey: string, objectField: any);
/**
* Add property to object
*/
property(key: string, config: any): this;
/**
* Add text property
*/
text(key: string, title?: string): this;
/**
* Add number property
*/
number(key: string, title?: string): this;
/**
* Add boolean property
*/
boolean(key: string, title?: string): this;
/**
* Return to template builder
*/
end(): TemplateBuilder;
}
/**
* Array field builder for list items
*/
export declare class ArrayFieldBuilder {
private templateBuilder;
private fieldKey;
private arrayField;
constructor(templateBuilder: TemplateBuilder, fieldKey: string, arrayField: any);
/**
* Set item type for array
*/
itemType(type: 'string' | 'number' | 'boolean' | 'object'): this;
/**
* Set items as objects with properties
*/
itemsAsObject(properties: Record<string, any>): this;
/**
* Set minimum items
*/
minItems(count: number): this;
/**
* Set maximum items
*/
maxItems(count: number): this;
/**
* Ensure unique items
*/
unique(): this;
/**
* Return to template builder
*/
end(): TemplateBuilder;
}