@punica/form-mcp 0.1.0 → 0.2.1-next.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 (55) hide show
  1. package/README.md +6 -6
  2. package/bundled/form/controller/index.d.ts +46 -51
  3. package/bundled/form/decorator/Readers.d.ts +2 -2
  4. package/bundled/form/decorator/Services.d.ts +2 -2
  5. package/bundled/form/decorator/Starters.d.ts +2 -2
  6. package/bundled/form/define/FormItem.d.ts +12 -6
  7. package/bundled/form/define/FormItemProperty.d.ts +5 -5
  8. package/bundled/form/define/FormProperty.d.ts +5 -4
  9. package/bundled/form/define/decorator.d.ts +16 -0
  10. package/bundled/form/define/index.d.ts +1 -0
  11. package/bundled/form/index.d.ts +1 -0
  12. package/bundled/form/metadata/index.d.ts +16 -0
  13. package/bundled/form/model/command/item.d.ts +11 -5
  14. package/bundled/form/model/command/service.d.ts +25 -10
  15. package/bundled/form/model/constants/index.d.ts +13 -2
  16. package/bundled/form/model/form/index.d.ts +9 -4
  17. package/bundled/form/model/index.d.ts +0 -1
  18. package/bundled/form/model/item/index.d.ts +41 -12
  19. package/bundled/form/model/reader/index.d.ts +5 -1
  20. package/bundled/form/model/service/index.d.ts +28 -10
  21. package/bundled/form/package.json +1 -1
  22. package/bundled/form/reader/index.d.ts +10 -17
  23. package/bundled/form/register/index.d.ts +26 -39
  24. package/bundled/form/shared.d.ts +20 -0
  25. package/bundled/form/utils/deepCopy.d.ts +1 -1
  26. package/bundled/form/utils/devtools.d.ts +11 -70
  27. package/bundled/form/utils/index.d.ts +2 -1
  28. package/bundled/form/utils/path.d.ts +10 -0
  29. package/bundled/form-service/clearError/index.d.ts +4 -14
  30. package/bundled/form-service/clearErrors/index.d.ts +4 -16
  31. package/bundled/form-service/getValues/index.d.ts +5 -13
  32. package/bundled/form-service/index.d.ts +21 -0
  33. package/bundled/form-service/package.json +1 -1
  34. package/bundled/form-service/reset/index.d.ts +4 -13
  35. package/bundled/form-service/resetItem/index.d.ts +4 -14
  36. package/bundled/form-service/setValue/index.d.ts +9 -16
  37. package/bundled/form-service/store/index.d.ts +11 -23
  38. package/bundled/form-service/trigger/index.d.ts +4 -13
  39. package/bundled/form-service/validate/index.d.ts +4 -18
  40. package/bundled/form-service/validateItem.d.ts +13 -0
  41. package/package.json +5 -4
  42. package/src/create.mjs +12 -6
  43. package/src/guide.md +96 -58
  44. package/src/knowledge.mjs +2 -2
  45. package/src/validate.mjs +64 -47
  46. package/bundled/form/model/util/GetItem.d.ts +0 -9
  47. package/bundled/form/model/util/WriteItems.d.ts +0 -8
  48. package/bundled/form/model/util/index.d.ts +0 -2
  49. package/bundled/form/reader/decorators/base/index.d.ts +0 -35
  50. package/bundled/form/reader/decorators/readers/FormItemsReader.d.ts +0 -17
  51. package/bundled/form/reader/decorators/readers/ItemDataReader.d.ts +0 -24
  52. package/bundled/form/reader/decorators/readers/ItemInitializerReader.d.ts +0 -16
  53. package/bundled/form/reader/decorators/readers/index.d.ts +0 -3
  54. package/bundled/form/reader/reader/Reader.d.ts +0 -13
  55. package/bundled/form/reader/reader/index.d.ts +0 -1
package/README.md CHANGED
@@ -10,12 +10,12 @@ and validates its answer.
10
10
 
11
11
  ## Tools
12
12
 
13
- | Tool | What it does |
14
- | --- | --- |
15
- | `get_guide` | The rules: what the packages provide and what your application builds, the lifecycle, services, events and a complete example |
16
- | `search_api` | Finds exports of `@punica/form` and `@punica/form-service` by name or documentation |
17
- | `get_api` | The exact TypeScript declaration and documentation of one or more exports |
18
- | `validate_code` | Checks code for imports that do not exist, wrong service names and service calls that do not exist; each problem with a fix |
13
+ | Tool | What it does |
14
+ | --------------- | ----------------------------------------------------------------------------------------------------------------------------- |
15
+ | `get_guide` | The rules: what the packages provide and what your application builds, the lifecycle, services, events and a complete example |
16
+ | `search_api` | Finds exports of `@punica/form` and `@punica/form-service` by name or documentation |
17
+ | `get_api` | The exact TypeScript declaration and documentation of one or more exports |
18
+ | `validate_code` | Checks code for imports that do not exist, wrong service names and service calls that do not exist; each problem with a fix |
19
19
 
20
20
  Resources: `punica-form://guide` and `punica-form://api/<name>`.
21
21
 
@@ -1,73 +1,68 @@
1
- import { BaseListener } from '@punica/common';
2
- import { Form, FormEvents, FormItem, IServiceCommand, IServiceInitialize, IServiceControl } from '..';
1
+ import { Form, FormEventMap, FormItem, FormItemRegister, IService, ServiceRegistry } from '..';
2
+ export type FormControllerOptions = {
3
+ /** Identifies the form in developer tools. Defaults to the name of its class. */
4
+ name?: string;
5
+ /** The register whose form item types the form reports. */
6
+ register?: FormItemRegister;
7
+ };
8
+ /** A class whose instances are services. */
9
+ type ServiceClass<S extends IService> = new (...args: any[]) => S;
10
+ /** The keys of `ServiceRegistry` a service class is registered under. */
11
+ type RegistryKey<S> = {
12
+ [K in keyof ServiceRegistry<any, any>]: ServiceRegistry<any, any>[K] extends S ? S extends ServiceRegistry<any, any>[K] ? K : never : never;
13
+ }[keyof ServiceRegistry<any, any>];
3
14
  /**
4
- * Controller for managing a form and its associated services.
15
+ * A service bound to the entity and the item type of a form, when its class
16
+ * is in `ServiceRegistry`; the service as it is otherwise.
5
17
  */
6
- export declare class FormController<E, F extends FormItem<E>> extends BaseListener<FormEvents> {
18
+ type BoundService<S, E, F extends FormItem<E>> = [RegistryKey<S>] extends [
19
+ never
20
+ ] ? S : ServiceRegistry<E, F>[RegistryKey<S>];
21
+ /**
22
+ * Holds the state of a form and runs its readers, starters and services.
23
+ * The form is immutable: every change produces a new form object.
24
+ */
25
+ export declare class FormController<E extends object, F extends FormItem<E> = FormItem<E>> {
7
26
  #private;
8
- /**
9
- * Private constructor to prevent direct instantiation.
10
- */
11
27
  private constructor();
12
28
  /**
13
- * Static method to initialize the FormController with form data.
14
- * @param form The initial form data
15
- */
16
- static fromForm<E, F extends FormItem<E>>(form: Form<E, F>): Promise<FormController<E, F>>;
17
- /**
18
- * Static method to initialize the FormController with an entity.
19
- * @param entity The entity for the form
20
- */
21
- static fromEntity<E, F extends FormItem<E>>(entity: E): Promise<FormController<E, F>>;
22
- /**
23
- * Trigger a form event.
24
- * @param eventType The type of the event
25
- * @param data Data associated with the event
26
- */
27
- private fireEvent;
28
- /**
29
- * Send entity data to DevTools
30
- */
31
- private sendEntityToDevTools;
32
- /**
33
- * Create a command service for the form.
34
- * @returns A CommandService for the form
35
- */
36
- private createCommandService;
37
- /**
38
- * Registers form items by triggering the REGISTER_ITEMS event.
29
+ * Creates a controller for a form that is described as data.
39
30
  */
40
- private registerFormItems;
31
+ static fromForm<E extends object, F extends FormItem<E> = FormItem<E>>(form: Form<E, F>, options?: FormControllerOptions): Promise<FormController<E, F>>;
41
32
  /**
42
- * Maps form items to their properties.
33
+ * Creates a controller for an instance of a decorated form class.
43
34
  */
44
- private mapFormItems;
35
+ static fromEntity<E extends object, F extends FormItem<E> = FormItem<E>>(entity: E, options?: FormControllerOptions): Promise<FormController<E, F>>;
45
36
  /**
46
- * Initializes the services defined in the form data.
37
+ * The current form. A form object is never changed once it is returned:
38
+ * after `start()` and after every change this returns a new object.
47
39
  */
48
- private initializeServices;
40
+ getForm: () => Form<E, F>;
49
41
  /**
50
- * Executes the readers defined in the form data.
42
+ * Calls the listener after every change of the form.
43
+ * @returns A function that removes the listener
51
44
  */
52
- private runReaders;
45
+ subscribe: (listener: () => void) => (() => void);
53
46
  /**
54
- * Executes the starters defined in the form data.
47
+ * Listens to an event of the form.
48
+ * @returns A function that removes the handler
55
49
  */
56
- private runStarters;
50
+ on<K extends keyof FormEventMap<E, F>>(event: K, handler: (payload: FormEventMap<E, F>[K]) => void): () => void;
57
51
  /**
58
- * Keeps a deep copy of the form items for the initial state.
59
- * Services, readers and starters are shared, not copied.
52
+ * The service of a class listed in `@Services`, or of a subclass of it.
53
+ * @throws When the form is not started yet or has no service of the class
60
54
  */
61
- private deepCopyForm;
55
+ getService<S extends IService>(service: ServiceClass<S>): BoundService<S, E, F>;
62
56
  /**
63
- * Get services by their names.
64
- * Overloads to return a single item or an array based on provided names.
57
+ * Runs changes as one: subscribers and UPDATE_FORM listeners are notified
58
+ * once, after the function returned or the promise it returned settled.
65
59
  */
66
- getServices<T = IServiceInitialize<E, F> & IServiceControl & IServiceCommand>(serviceName: string): T;
67
- getServices<T = IServiceInitialize<E, F> & IServiceControl & IServiceCommand>(serviceName: string, ...additionalServiceNames: string[]): T[];
60
+ batch: <T>(change: () => T) => T;
68
61
  /**
69
- * Starts the form controller by executing all necessary steps to initialize the form.
70
- * @returns A promise that resolves when the form is fully initialized.
62
+ * Starts the form: runs its readers and starters and initializes its
63
+ * services.
64
+ * @returns The form
71
65
  */
72
66
  start(): Promise<Form<E, F>>;
73
67
  }
68
+ export {};
@@ -1,4 +1,4 @@
1
- import { FormItem, IReader } from '..';
1
+ import { FormClassDecorator, FormItem, IReader } from '..';
2
2
  /**
3
3
  * Constant for identifying form readers decorator.
4
4
  */
@@ -6,4 +6,4 @@ export declare const DECORATOR_READERS = "form-readers";
6
6
  /**
7
7
  * Decorator for adding readers to a form class.
8
8
  */
9
- export declare const Readers: <E, F extends FormItem<E>>(data: IReader<E, F>[]) => ClassDecorator;
9
+ export declare const Readers: <E, F extends FormItem<E>>(data: Array<IReader<E, F>>) => FormClassDecorator;
@@ -1,4 +1,4 @@
1
- import { IService } from '..';
1
+ import { FormClassDecorator, IService } from '..';
2
2
  /**
3
3
  * Constant for identifying form services decorator.
4
4
  */
@@ -6,4 +6,4 @@ export declare const DECORATOR_SERVICES = "form-services";
6
6
  /**
7
7
  * Decorator for adding services to a form class.
8
8
  */
9
- export declare const Services: (data: Array<IService>) => ClassDecorator;
9
+ export declare const Services: (data: Array<IService | (new () => IService)>) => FormClassDecorator;
@@ -1,4 +1,4 @@
1
- import { FormItem, IStarter } from '..';
1
+ import { FormClassDecorator, FormItem, IStarter } from '..';
2
2
  /**
3
3
  * Constant for identifying form starters decorator.
4
4
  */
@@ -6,4 +6,4 @@ export declare const DECORATOR_STARTERS = "form-starters";
6
6
  /**
7
7
  * Decorator for adding starters to a form class.
8
8
  */
9
- export declare const Starters: <E, F extends FormItem<E>>(data: IStarter<E, F>[]) => ClassDecorator;
9
+ export declare const Starters: <E, F extends FormItem<E>>(data: Array<IStarter<E, F>>) => FormClassDecorator;
@@ -1,9 +1,15 @@
1
- import { IPropertyDecorator } from '@punica/common';
1
+ import { FormItemRegister, Renderer } from '../register';
2
+ import { FormPropertyDecorator } from './decorator';
3
+ /** The key the form items of a class are stored under. */
4
+ export declare const FORM_ITEMS = "form-items";
2
5
  /**
3
- * Defines a form item with the specified type and renderer.
4
- * @param {string} type - Type of the form item.
5
- * @param {Function} renderer - Renderer function for the form item.
6
- * @returns {IPropertyDecorator<T>} - Decorator function.
6
+ * Defines a form item type: registers its renderer and returns the property
7
+ * decorator that turns a property of a form class into an item of that type.
8
+ * @param type - The form item type
9
+ * @param renderer - What renders items of the type; Punica Form only stores it
10
+ * @param options.register - The register to use instead of the default one
7
11
  */
8
- declare const defineFormItem: <T>(type: string, renderer: (props: T) => any) => IPropertyDecorator<T>;
12
+ declare const defineFormItem: <T extends object>(type: string, renderer: Renderer, options?: {
13
+ register?: FormItemRegister;
14
+ }) => ((data: T) => FormPropertyDecorator);
9
15
  export default defineFormItem;
@@ -1,8 +1,8 @@
1
- import { IPropertyDecorator } from '@punica/common';
1
+ import { FormPropertyDecorator } from './decorator';
2
2
  /**
3
- * Defines a form item with the specified type and renderer.
4
- * @param {string} type - Type of the form item.
5
- * @returns {IPropertyDecorator<T>} - Decorator function.
3
+ * Defines a property decorator that stores data for a property of a form
4
+ * class. Read it back in a reader with `getFormItemProperties(entity, key)`.
5
+ * @param key - Identifies the data
6
6
  */
7
- declare const defineFormItemProperty: <T>(type: string) => IPropertyDecorator<T>;
7
+ declare const defineFormItemProperty: <T>(key: string) => ((data: T) => FormPropertyDecorator);
8
8
  export default defineFormItemProperty;
@@ -1,7 +1,8 @@
1
+ import { FormClassDecorator } from './decorator';
1
2
  /**
2
- * Defines a form property with the specified type.
3
- * @param {string} type - Type of the form property.
4
- * @returns {ClassDecorator<T>} - Decorator function.
3
+ * Defines a class decorator that stores data on a form class. Read it back
4
+ * in a reader with `getFormProperty(entity, key)`.
5
+ * @param key - Identifies the data
5
6
  */
6
- declare const defineFormProperty: <T>(type: string) => (data: T) => ClassDecorator;
7
+ declare const defineFormProperty: <T>(key: string) => ((data: T) => FormClassDecorator);
7
8
  export default defineFormProperty;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Decorators that work with both decorator implementations of TypeScript:
3
+ * the legacy one (`experimentalDecorators`) and the standard one.
4
+ */
5
+ /** A class decorator. Both implementations pass the class first. */
6
+ export type FormClassDecorator = (target: object, context?: ClassDecoratorContext) => void;
7
+ /**
8
+ * A property decorator: `(prototype, propertyKey)` for the legacy
9
+ * implementation, `(undefined, context)` for the standard one.
10
+ */
11
+ export type FormPropertyDecorator = (target: object | undefined, context: string | symbol | ClassFieldDecoratorContext) => void;
12
+ /**
13
+ * Creates a property decorator.
14
+ * @param register - Called with the class and the name of the decorated property
15
+ */
16
+ export declare const propertyDecorator: (register: (target: object, property: string) => void) => FormPropertyDecorator;
@@ -1,3 +1,4 @@
1
+ export type { FormClassDecorator, FormPropertyDecorator } from './decorator';
1
2
  export { default as defineFormItem } from './FormItem';
2
3
  export { default as defineFormItemProperty } from './FormItemProperty';
3
4
  export { default as defineFormProperty } from './FormProperty';
@@ -1,4 +1,5 @@
1
1
  export * from './model';
2
+ export { getFormItemProperties, getFormProperty } from './metadata';
2
3
  export * from './define';
3
4
  export * from './decorator';
4
5
  export * from './register';
@@ -0,0 +1,16 @@
1
+ export declare const setFormProperty: (target: object, key: string, data: unknown) => void;
2
+ export declare const setFormItemProperty: (target: object, key: string, property: string, data: unknown) => void;
3
+ /**
4
+ * Reads what a decorator created with `defineFormProperty` stored on the
5
+ * class of the entity. A class overrides its base classes.
6
+ * @param entity - An instance of the form class
7
+ * @param key - The key the decorator was defined with
8
+ */
9
+ export declare const getFormProperty: <T>(entity: object, key: string) => T | undefined;
10
+ /**
11
+ * Reads what a decorator created with `defineFormItemProperty` stored for
12
+ * the properties of the entity, keyed by property name.
13
+ * @param entity - An instance of the form class
14
+ * @param key - The key the decorator was defined with
15
+ */
16
+ export declare const getFormItemProperties: <T>(entity: object, key: string) => Record<string, T>;
@@ -1,7 +1,13 @@
1
1
  import { FormItem } from '..';
2
- export interface CommandItem<E, F extends FormItem<E>> {
2
+ /**
3
+ * What the functions of a form item (initialize, control, validation) receive.
4
+ * `C` holds the features services add, such as the functions of `Store`.
5
+ */
6
+ export type CommandItem<E, F extends FormItem<E>, C = Record<string, unknown>> = {
7
+ /** A copy of the item the function belongs to. */
3
8
  formItem: F;
4
- initialEntity?: E;
5
- getItem?: (property: keyof E) => F;
6
- [key: string]: any;
7
- }
9
+ /** The entity the form was created from. */
10
+ initialEntity: E;
11
+ /** A copy of another item of the form. */
12
+ getItem: (property: keyof E) => F | undefined;
13
+ } & C;
@@ -1,15 +1,30 @@
1
- import { Form, FormEvents, FormItem } from '..';
2
- import { CommandItem } from '.';
1
+ import { CommandItem, Form, FormEventMap, FormItem } from '..';
3
2
  /**
4
- * Defines the structure of the CommandService.
3
+ * What a service receives when it is initialized: the only way to read and
4
+ * change the state of the form.
5
5
  */
6
6
  export type CommandService<E, F extends FormItem<E>> = {
7
- initialForm: Form<E, F>;
7
+ /** The entity the form was created from. */
8
8
  initialEntity: E;
9
- form: Form<E, F>;
10
- itemsMap: Record<keyof E, number>;
11
- createCommandItem: (item: F) => Promise<CommandItem<E, F>>;
12
- fireEvent: (eventType: FormEvents, data: any) => void;
13
- getItem: (property: keyof E) => F;
14
- writeItems: (items: Array<FormItem<E>>) => Promise<void>;
9
+ /** The current form. */
10
+ getForm: () => Form<E, F>;
11
+ /** The items as they were when the form was started. */
12
+ getInitialItems: () => F[];
13
+ /** The current item of a property. */
14
+ getItem: (property: keyof E) => F | undefined;
15
+ /**
16
+ * Replaces the items with the same properties. The form gets a new
17
+ * reference and UPDATE_FORM is fired. Unknown properties are ignored.
18
+ */
19
+ setItems: (items: Array<FormItem<E>>) => void;
20
+ /**
21
+ * Marks the pending asynchronous writes of a property, or of the whole
22
+ * form when no property is given, as superseded by a newer one.
23
+ * @returns A function that tells whether this write is still the newest
24
+ */
25
+ supersede: (property?: keyof E) => () => boolean;
26
+ /** Fires an event of the controller. */
27
+ emit: <K extends keyof FormEventMap<E, F>>(event: K, payload: FormEventMap<E, F>[K]) => void;
28
+ /** Creates what the functions of a form item receive. */
29
+ createCommandItem: (item: F) => CommandItem<E, F>;
15
30
  };
@@ -1,4 +1,15 @@
1
+ import { Form, FormItem } from '..';
1
2
  /**
2
- * Type representing various events that can occur in a form.
3
+ * The events of a form controller, mapped to the payload of each event.
3
4
  */
4
- export type FormEvents = 'UPDATE_FORM' | 'RESET' | 'REGISTER_ITEMS' | 'UPDATE_ITEM';
5
+ export type FormEventMap<E, F extends FormItem<E>> = {
6
+ /** The form changed. Fired after every change of the state. */
7
+ UPDATE_FORM: Form<E, F>;
8
+ /** The form was reset to its initial state. Fired after UPDATE_FORM. */
9
+ RESET: Form<E, F>;
10
+ /** A value was set: the changed item, then the items its control returned. */
11
+ UPDATE_ITEM: F[];
12
+ /** The registered form item types. Fired at the beginning of start(). */
13
+ REGISTER_ITEMS: string[];
14
+ };
15
+ export type FormEvents = keyof FormEventMap<unknown, FormItem<unknown>>;
@@ -1,8 +1,13 @@
1
1
  import { FormItem, IReader, IService, IStarter } from '../..';
2
- export type Form<E = any, F extends FormItem<E> = any> = {
3
- items: Array<F>;
4
- services?: Array<IService>;
2
+ /**
3
+ * The state of a form. Readers and starters may attach their own properties
4
+ * (a title, a layout, …), which are read back as `unknown`.
5
+ */
6
+ export type Form<E, F extends FormItem<E> = FormItem<E>> = {
7
+ items: F[];
8
+ /** Service classes (one instance per form) or instances. */
9
+ services?: Array<IService | (new () => IService)>;
5
10
  readers?: Array<IReader<E, F>>;
6
11
  starters?: Array<IStarter<E, F>>;
7
- [key: string]: any;
12
+ [key: string]: unknown;
8
13
  };
@@ -5,4 +5,3 @@ export * from './reader';
5
5
  export * from './form';
6
6
  export * from './starter';
7
7
  export * from './service';
8
- export * from './util';
@@ -1,25 +1,54 @@
1
1
  import { CommandItem } from '../command';
2
+ /**
3
+ * A field of a form. Extend it with the properties of your own form system:
4
+ * `type AppItem<E> = FormItem<E> & { layout?: Layout }`.
5
+ */
2
6
  export type FormItem<E> = {
7
+ /** The property of the entity this item belongs to. */
3
8
  property: keyof E;
9
+ /** Path of the value inside the entity, for nested values (`address/city`). */
4
10
  path?: string;
11
+ /** The label or display text of the item. */
5
12
  label: string;
6
- required?: boolean;
13
+ /** The form item type the item was created with. */
7
14
  type?: string;
15
+ /**
16
+ * The value of the item. Deliberately `any`: its type depends on the
17
+ * property and on the form item type.
18
+ */
8
19
  value?: any;
9
- error?: boolean;
10
- errorMessages?: Array<string>;
20
+ required?: boolean;
11
21
  disabled?: boolean;
12
22
  hidden?: boolean;
13
- getData?: (params?: any) => Promise<any>;
14
- setValue?: (property: keyof E, value: any) => Promise<void>;
15
- control?: (command: CommandItem<E, any>) => Promise<Array<FormItem<E>>>;
16
- submitted?: (command: CommandItem<E, any>) => Promise<void>;
17
- initialize?: (command: CommandItem<E, any>) => Promise<Array<FormItem<E>>>;
18
- validation?: (command: CommandItem<E, any>) => Promise<ErrorDetail>;
19
- [key: string]: any;
23
+ /** Whether the last validation of the item failed. */
24
+ error?: boolean;
25
+ /** The messages of the last failed validation. */
26
+ errorMessages?: string[];
27
+ /** Runs once while the form is created; the returned items are written to the form. */
28
+ initialize?(command: CommandItem<E, FormItem<E>>): Promise<Array<FormItem<E>>>;
29
+ /** Runs after the value of the item was set; the returned items are written to the form. */
30
+ control?(command: CommandItem<E, FormItem<E>>): Promise<Array<FormItem<E>>>;
31
+ /** Validates the item. */
32
+ validation?(command: CommandItem<E, FormItem<E>>): Promise<ErrorDetail>;
20
33
  };
34
+ /**
35
+ * A form item whose `value` has the type of its property. Use it as the item
36
+ * type of a form to get checked values after narrowing by `property`:
37
+ *
38
+ * const controller = await FormController.fromEntity<User, TypedFormItem<User>>(user);
39
+ * const item = controller.getForm().items[0];
40
+ * if (item.property === 'age') item.value; // number | undefined
41
+ */
42
+ export type TypedFormItem<E, F extends FormItem<E> = FormItem<E>> = {
43
+ [K in keyof E]: Omit<F, 'property' | 'value'> & {
44
+ property: K;
45
+ value?: E[K];
46
+ };
47
+ }[keyof E];
48
+ /**
49
+ * The result of a validation.
50
+ */
21
51
  export type ErrorDetail = {
22
52
  error: boolean;
23
- errorMessages?: Array<string>;
24
- [key: string]: any;
53
+ errorMessages?: string[];
25
54
  };
@@ -1,4 +1,8 @@
1
1
  import { Form, FormItem } from '..';
2
+ /**
3
+ * Reads metadata of the entity into the form while the form is started.
4
+ * A reader changes the form it is given.
5
+ */
2
6
  export interface IReader<E, F extends FormItem<E>> {
3
- read(entity: E, form: Form<E, F>, itemsMap?: Record<keyof E, number>): Promise<void>;
7
+ read(entity: E, form: Form<E, F>): void | Promise<void>;
4
8
  }
@@ -1,20 +1,38 @@
1
- import { CommandService, Form, FormItem } from '..';
1
+ import { CommandService, FormItem } from '..';
2
+ /**
3
+ * A service of a form. Register it with `@Services` and read it back with
4
+ * `controller.getService(Class)`.
5
+ */
2
6
  export interface IService {
7
+ /** A label for the service, shown by developer tools. */
3
8
  get name(): string;
4
9
  }
10
+ /**
11
+ * A service that reads or changes the form.
12
+ */
5
13
  export interface IServiceInitialize<E, F extends FormItem<E>> extends IService {
6
- initialize: (command: CommandService<E, F>) => Promise<void>;
14
+ /** Called once when the form is started. */
15
+ initialize: (command: CommandService<E, F>) => void | Promise<void>;
7
16
  }
8
- export interface IServiceControl<T = any> extends IService {
17
+ /**
18
+ * A service that is executed.
19
+ */
20
+ export interface IServiceControl<T = unknown> extends IService {
9
21
  run: (...args: any[]) => Promise<T>;
10
22
  }
23
+ /**
24
+ * A service that adds features to what the functions of form items receive.
25
+ */
11
26
  export interface IServiceCommand extends IService {
12
- addCustomFeaturesForCommandItem: () => {
13
- [key: string]: any;
14
- };
27
+ addCustomFeaturesForCommandItem: () => Record<string, unknown>;
15
28
  }
16
- export interface IServiceStartup<E, F extends FormItem<E>> extends IService {
17
- start: (entity: E, form: Form<E, F>) => {
18
- [key: string]: any;
19
- };
29
+ /**
30
+ * The services `controller.getService` knows the types of. Packages add
31
+ * theirs by declaration merging, which binds them to the entity of the form:
32
+ *
33
+ * declare module '@punica/form' {
34
+ * interface ServiceRegistry<E, F> { myService: MyService<E, F> }
35
+ * }
36
+ */
37
+ export interface ServiceRegistry<E, F extends FormItem<E>> {
20
38
  }
@@ -1 +1 @@
1
- {"version":"1.2.0"}
1
+ {"version":"2.1.0-next.0"}
@@ -1,19 +1,12 @@
1
- import { IReader } from '..';
1
+ import { FormItem, IReader } from '..';
2
2
  /**
3
- * Decorator Design Pattern for a Reader System
4
- *
5
- * - The `IReader` interface defines the contract for all types of readers.
6
- * - The `Reader` class serves as the base reader.
7
- * - Decorators are implemented as classes (e.g., `FormItemsReader`, `ItemDataReader`, `ItemInitializerReader`) that wrap existing readers and enhance their behavior.
8
- * - The `createInitialReader` function creates an initial reader object with a chain of decorators applied.
9
- *
10
- * - `FormItemsReader` adds functionality related to reading form items.
11
- * - `ItemDataReader` adds functionality related to reading item data.
12
- * - `ItemInitializerReader` adds functionality related to initializing items.
13
- *
14
- * - The final `itemInitializerReader` returned by `createInitialReader` has all the decorators chained together,
15
- * creating a powerful reader object that can perform a series of operations in a coordinated way.
16
- *
17
- * @returns {IReader<any, any>} - Initial reader object with a chain of decorators.
3
+ * Creates the reader that turns an entity into a form:
4
+ *
5
+ * 1. the services, readers and starters of the class decorators
6
+ * 2. one item for every decorated property, in the order of the properties
7
+ * of the entity; decorated properties the entity does not hold (declared
8
+ * without a value) follow
9
+ * 3. the values of the entity
10
+ * 4. the `initialize` function of every item that has one
18
11
  */
19
- export declare const createInitialReader: () => IReader<any, any>;
12
+ export declare const createInitialReader: <E extends object, F extends FormItem<E>>() => IReader<E, F>;
@@ -1,43 +1,30 @@
1
- import { FormItem } from '..';
2
- type Render<F> = (props: F) => any;
3
- export declare class FormItemRegister<E, F extends FormItem<E>> {
1
+ /**
2
+ * What renders the items of a form item type. Punica Form is headless: it
3
+ * stores renderers for the application and never calls them.
4
+ */
5
+ export type Renderer = (props: any) => any;
6
+ /**
7
+ * The renderers of the form item types, by type.
8
+ */
9
+ export declare class FormItemRegister {
4
10
  #private;
5
- private static _instance;
6
11
  /**
7
- * Private constructor to prevent external instantiation.
12
+ * Registers the renderer of a type. The first registration of a type wins.
8
13
  */
9
- private constructor();
10
- /**
11
- * Singleton pattern: Get the instance of FormItemRegister.
12
- * @returns The instance of FormItemRegister.
13
- */
14
- static getInstance(): FormItemRegister<any, any>;
15
- /**
16
- * Register a form item property with its corresponding renderer.
17
- * @param property - The form item property.
18
- * @param renderer - The rendering function for the form item.
19
- */
20
- register(property: string, renderer: (props: F) => any): void;
21
- /**
22
- * Unregister a form item property.
23
- * @param property - The form item property to unregister.
24
- */
25
- unRegister(property: string): void;
26
- /**
27
- * Get the rendering function for a specific form item property.
28
- * @param property - The form item property.
29
- * @returns The rendering function for the specified property.
30
- */
31
- getItem(property: string): Render<F>;
32
- /**
33
- * Get all registered form item properties and their corresponding renderers.
34
- * @returns A map containing form item properties and their renderers.
35
- */
36
- getItems(): Map<string, Render<F>>;
37
- /**
38
- * Get an iterable of all registered form item properties.
39
- * @returns An iterable of form item properties.
40
- */
41
- getItemKeys(): IterableIterator<string>;
14
+ register(type: string, renderer: Renderer): void;
15
+ unregister(type: string): void;
16
+ /** The renderer of a type. */
17
+ getItem(type: string): Renderer | undefined;
18
+ /** The registered types. */
19
+ getItemKeys(): string[];
42
20
  }
43
- export {};
21
+ /**
22
+ * Creates a register of its own, for tests or for an application that hosts
23
+ * more than one form system.
24
+ */
25
+ export declare const createFormItemRegister: () => FormItemRegister;
26
+ /**
27
+ * The register `defineFormItem` and `FormController` use unless they are
28
+ * given another one.
29
+ */
30
+ export declare const defaultFormItemRegister: FormItemRegister;