@punica/form-mcp 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +49 -2
- package/bundled/form/controller/index.d.ts +63 -0
- package/bundled/form/decorator/Readers.d.ts +9 -0
- package/bundled/form/decorator/Services.d.ts +9 -0
- package/bundled/form/decorator/Starters.d.ts +9 -0
- package/bundled/form/decorator/index.d.ts +3 -0
- package/bundled/form/define/FormItem.d.ts +15 -0
- package/bundled/form/define/FormItemProperty.d.ts +8 -0
- package/bundled/form/define/FormProperty.d.ts +8 -0
- package/bundled/form/define/decorator.d.ts +16 -0
- package/bundled/form/define/index.d.ts +4 -0
- package/bundled/form/index.d.ts +8 -0
- package/bundled/form/metadata/index.d.ts +20 -0
- package/bundled/form/model/command/index.d.ts +2 -0
- package/bundled/form/model/command/item.d.ts +13 -0
- package/bundled/form/model/command/service.d.ts +24 -0
- package/bundled/form/model/constants/index.d.ts +15 -0
- package/bundled/form/model/form/index.d.ts +12 -0
- package/bundled/form/model/index.d.ts +7 -0
- package/bundled/form/model/item/index.d.ts +40 -0
- package/bundled/form/model/reader/index.d.ts +8 -0
- package/bundled/form/model/service/index.d.ts +38 -0
- package/bundled/form/model/starter/index.d.ts +13 -0
- package/bundled/form/package.json +1 -0
- package/bundled/form/reader/index.d.ts +11 -0
- package/bundled/form/register/index.d.ts +30 -0
- package/bundled/form/utils/deepCopy.d.ts +8 -0
- package/bundled/form/utils/devtools.d.ts +18 -0
- package/bundled/form/utils/index.d.ts +2 -0
- package/bundled/form-service/clearError/index.d.ts +13 -0
- package/bundled/form-service/clearErrors/index.d.ts +10 -0
- package/bundled/form-service/getValues/index.d.ts +14 -0
- package/bundled/form-service/index.d.ts +30 -0
- package/bundled/form-service/package.json +1 -0
- package/bundled/form-service/reset/index.d.ts +13 -0
- package/bundled/form-service/resetItem/index.d.ts +13 -0
- package/bundled/form-service/setValue/index.d.ts +14 -0
- package/bundled/form-service/store/index.d.ts +18 -0
- package/bundled/form-service/trigger/index.d.ts +13 -0
- package/bundled/form-service/validate/index.d.ts +13 -0
- package/bundled/form-service/validateItem.d.ts +7 -0
- package/package.json +46 -3
- package/src/create.mjs +128 -0
- package/src/guide.md +184 -0
- package/src/knowledge.mjs +141 -0
- package/src/load.mjs +41 -0
- package/src/main.mjs +12 -0
- package/src/validate.mjs +111 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Uryan Sezgin TEKE
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @punica/form-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io) server that lets an AI assistant use
|
|
4
|
+
[Punica Form](https://form.punicalab.com) correctly: look up the exact API,
|
|
5
|
+
read the rules and check the code it wrote.
|
|
6
|
+
|
|
7
|
+
Punica Form is rarely in a model's training data, so without help a model
|
|
8
|
+
guesses imports, service names and methods. With this server it looks them up
|
|
9
|
+
and validates its answer.
|
|
10
|
+
|
|
11
|
+
## Tools
|
|
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 |
|
|
19
|
+
|
|
20
|
+
Resources: `punica-form://guide` and `punica-form://api/<name>`.
|
|
21
|
+
|
|
22
|
+
The API is read from the type declarations of the `@punica/form` and
|
|
23
|
+
`@punica/form-service` installed in the project the server is started in, so
|
|
24
|
+
the answers match that version. Without them it uses the versions it was
|
|
25
|
+
built with.
|
|
26
|
+
|
|
27
|
+
## Set up
|
|
28
|
+
|
|
29
|
+
**Claude Code**
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
claude mcp add punica-form -- npx -y @punica/form-mcp
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Cursor** (`.cursor/mcp.json`), **VS Code** (`.vscode/mcp.json`, key `servers`), **Claude Desktop** and others:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"mcpServers": {
|
|
40
|
+
"punica-form": { "command": "npx", "args": ["-y", "@punica/form-mcp"] }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Development
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
pnpm build # in the repository root: builds the packages, then bundled/
|
|
49
|
+
pnpm --filter @punica/form-mcp test
|
|
50
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
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>];
|
|
14
|
+
/**
|
|
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.
|
|
17
|
+
*/
|
|
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>> {
|
|
26
|
+
#private;
|
|
27
|
+
private constructor();
|
|
28
|
+
/**
|
|
29
|
+
* Creates a controller for a form that is described as data.
|
|
30
|
+
*/
|
|
31
|
+
static fromForm<E extends object, F extends FormItem<E> = FormItem<E>>(form: Form<E, F>, options?: FormControllerOptions): Promise<FormController<E, F>>;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a controller for an instance of a decorated form class.
|
|
34
|
+
*/
|
|
35
|
+
static fromEntity<E extends object, F extends FormItem<E> = FormItem<E>>(entity: E, options?: FormControllerOptions): Promise<FormController<E, F>>;
|
|
36
|
+
/**
|
|
37
|
+
* The current form. It is never changed: after a change this returns a
|
|
38
|
+
* new object.
|
|
39
|
+
*/
|
|
40
|
+
getForm: () => Form<E, F>;
|
|
41
|
+
/**
|
|
42
|
+
* Calls the listener after every change of the form.
|
|
43
|
+
* @returns A function that removes the listener
|
|
44
|
+
*/
|
|
45
|
+
subscribe: (listener: () => void) => (() => void);
|
|
46
|
+
/**
|
|
47
|
+
* Listens to an event of the form.
|
|
48
|
+
* @returns A function that removes the handler
|
|
49
|
+
*/
|
|
50
|
+
on<K extends keyof FormEventMap<E, F>>(event: K, handler: (payload: FormEventMap<E, F>[K]) => void): () => void;
|
|
51
|
+
/**
|
|
52
|
+
* The service of a class listed in `@Services`.
|
|
53
|
+
* @throws When the form has no service of the class
|
|
54
|
+
*/
|
|
55
|
+
getService<S extends IService>(service: ServiceClass<S>): BoundService<S, E, F>;
|
|
56
|
+
/**
|
|
57
|
+
* Starts the form: runs its readers and starters and initializes its
|
|
58
|
+
* services.
|
|
59
|
+
* @returns The form
|
|
60
|
+
*/
|
|
61
|
+
start(): Promise<Form<E, F>>;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FormClassDecorator, FormItem, IReader } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* Constant for identifying form readers decorator.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DECORATOR_READERS = "form-readers";
|
|
6
|
+
/**
|
|
7
|
+
* Decorator for adding readers to a form class.
|
|
8
|
+
*/
|
|
9
|
+
export declare const Readers: <E, F extends FormItem<E>>(data: IReader<E, F>[]) => FormClassDecorator;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FormClassDecorator, IService } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* Constant for identifying form services decorator.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DECORATOR_SERVICES = "form-services";
|
|
6
|
+
/**
|
|
7
|
+
* Decorator for adding services to a form class.
|
|
8
|
+
*/
|
|
9
|
+
export declare const Services: (data: Array<IService>) => FormClassDecorator;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FormClassDecorator, FormItem, IStarter } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* Constant for identifying form starters decorator.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DECORATOR_STARTERS = "form-starters";
|
|
6
|
+
/**
|
|
7
|
+
* Decorator for adding starters to a form class.
|
|
8
|
+
*/
|
|
9
|
+
export declare const Starters: <E, F extends FormItem<E>>(data: IStarter<E, F>[]) => FormClassDecorator;
|
|
@@ -0,0 +1,15 @@
|
|
|
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";
|
|
5
|
+
/**
|
|
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
|
|
11
|
+
*/
|
|
12
|
+
declare const defineFormItem: <T extends object>(type: string, renderer: Renderer, options?: {
|
|
13
|
+
register?: FormItemRegister;
|
|
14
|
+
}) => (data: T) => FormPropertyDecorator;
|
|
15
|
+
export default defineFormItem;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FormPropertyDecorator } from './decorator';
|
|
2
|
+
/**
|
|
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
|
+
*/
|
|
7
|
+
declare const defineFormItemProperty: <T>(key: string) => (data: T) => FormPropertyDecorator;
|
|
8
|
+
export default defineFormItemProperty;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FormClassDecorator } from './decorator';
|
|
2
|
+
/**
|
|
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
|
|
6
|
+
*/
|
|
7
|
+
declare const defineFormProperty: <T>(key: string) => (data: T) => FormClassDecorator;
|
|
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: Function, 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;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { FormClassDecorator, FormPropertyDecorator } from './decorator';
|
|
2
|
+
export { default as defineFormItem } from './FormItem';
|
|
3
|
+
export { default as defineFormItemProperty } from './FormItemProperty';
|
|
4
|
+
export { default as defineFormProperty } from './FormProperty';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './model';
|
|
2
|
+
export { getFormItemProperties, getFormProperty } from './metadata';
|
|
3
|
+
export * from './define';
|
|
4
|
+
export * from './decorator';
|
|
5
|
+
export * from './register';
|
|
6
|
+
export * from './reader';
|
|
7
|
+
export * from './controller';
|
|
8
|
+
export * from './utils';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The metadata decorators attach to form classes. It is kept per class, so
|
|
3
|
+
* no reflection polyfill is needed.
|
|
4
|
+
*/
|
|
5
|
+
export declare const setFormProperty: (target: object, key: string, data: unknown) => void;
|
|
6
|
+
export declare const setFormItemProperty: (target: object, key: string, property: string, data: unknown) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Reads what a decorator created with `defineFormProperty` stored on the
|
|
9
|
+
* class of the entity. A class overrides its base classes.
|
|
10
|
+
* @param entity - An instance of the form class
|
|
11
|
+
* @param key - The key the decorator was defined with
|
|
12
|
+
*/
|
|
13
|
+
export declare const getFormProperty: <T>(entity: object, key: string) => T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Reads what a decorator created with `defineFormItemProperty` stored for
|
|
16
|
+
* the properties of the entity, keyed by property name.
|
|
17
|
+
* @param entity - An instance of the form class
|
|
18
|
+
* @param key - The key the decorator was defined with
|
|
19
|
+
*/
|
|
20
|
+
export declare const getFormItemProperties: <T>(entity: object, key: string) => Record<string, T>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FormItem } from '..';
|
|
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. */
|
|
8
|
+
formItem: F;
|
|
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;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CommandItem, Form, FormEventMap, FormItem } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* What a service receives when it is initialized: the only way to read and
|
|
4
|
+
* change the state of the form.
|
|
5
|
+
*/
|
|
6
|
+
export type CommandService<E, F extends FormItem<E>> = {
|
|
7
|
+
/** The entity the form was created from. */
|
|
8
|
+
initialEntity: E;
|
|
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
|
+
/** Fires an event of the controller. */
|
|
21
|
+
emit: <K extends keyof FormEventMap<E, F>>(event: K, payload: FormEventMap<E, F>[K]) => void;
|
|
22
|
+
/** Creates what the functions of a form item receive. */
|
|
23
|
+
createCommandItem: (item: F) => CommandItem<E, F>;
|
|
24
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Form, FormItem } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* The events of a form controller, mapped to the payload of each event.
|
|
4
|
+
*/
|
|
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>>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormItem, IReader, IService, IStarter } from '../..';
|
|
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
|
+
services?: IService[];
|
|
9
|
+
readers?: Array<IReader<E, F>>;
|
|
10
|
+
starters?: Array<IStarter<E, F>>;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
*/
|
|
6
|
+
export type FormItem<E> = {
|
|
7
|
+
/** The property of the entity this item belongs to. */
|
|
8
|
+
property: keyof E;
|
|
9
|
+
/** Path of the value inside the entity, for nested values (`address/city`). */
|
|
10
|
+
path?: string;
|
|
11
|
+
/** The label or display text of the item. */
|
|
12
|
+
label: string;
|
|
13
|
+
/** The form item type the item was created with. */
|
|
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
|
+
*/
|
|
19
|
+
value?: any;
|
|
20
|
+
required?: boolean;
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
hidden?: boolean;
|
|
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>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The result of a validation.
|
|
36
|
+
*/
|
|
37
|
+
export type ErrorDetail = {
|
|
38
|
+
error: boolean;
|
|
39
|
+
errorMessages?: string[];
|
|
40
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
*/
|
|
6
|
+
export interface IReader<E, F extends FormItem<E>> {
|
|
7
|
+
read(entity: E, form: Form<E, F>): void | Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
*/
|
|
6
|
+
export interface IService {
|
|
7
|
+
/** A label for the service, shown by developer tools. */
|
|
8
|
+
get name(): string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A service that reads or changes the form.
|
|
12
|
+
*/
|
|
13
|
+
export interface IServiceInitialize<E, F extends FormItem<E>> extends IService {
|
|
14
|
+
/** Called once when the form is started. */
|
|
15
|
+
initialize: (command: CommandService<E, F>) => void | Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A service that is executed.
|
|
19
|
+
*/
|
|
20
|
+
export interface IServiceControl<T = unknown> extends IService {
|
|
21
|
+
run: (...args: any[]) => Promise<T>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A service that adds features to what the functions of form items receive.
|
|
25
|
+
*/
|
|
26
|
+
export interface IServiceCommand extends IService {
|
|
27
|
+
addCustomFeaturesForCommandItem: () => Record<string, unknown>;
|
|
28
|
+
}
|
|
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>> {
|
|
38
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Form, FormItem } from '..';
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing a Starter for initializing form data.
|
|
4
|
+
*/
|
|
5
|
+
export interface IStarter<E, F extends FormItem<E>> {
|
|
6
|
+
/**
|
|
7
|
+
* Function to run the starter logic.
|
|
8
|
+
* @param form - The current form data.
|
|
9
|
+
* @param entity - The entity associated with the form.
|
|
10
|
+
* @returns A Promise that resolves to the updated form data after initialization.
|
|
11
|
+
*/
|
|
12
|
+
run: (form: Form<E, F>, entity: E) => Promise<Form<E, F>>;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"2.0.0"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FormItem, IReader } from '..';
|
|
2
|
+
/**
|
|
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 the entity holds, in the order
|
|
7
|
+
* of the properties
|
|
8
|
+
* 3. the values of the entity
|
|
9
|
+
* 4. the `initialize` function of every item that has one
|
|
10
|
+
*/
|
|
11
|
+
export declare const createInitialReader: <E extends object, F extends FormItem<E>>() => IReader<E, F>;
|
|
@@ -0,0 +1,30 @@
|
|
|
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 {
|
|
10
|
+
#private;
|
|
11
|
+
/**
|
|
12
|
+
* Registers the renderer of a type. The first registration of a type wins.
|
|
13
|
+
*/
|
|
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[];
|
|
20
|
+
}
|
|
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;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a deep copy of the given value.
|
|
3
|
+
* Functions are kept by reference, prototypes are preserved and
|
|
4
|
+
* circular references are supported.
|
|
5
|
+
* @param value - The value to copy
|
|
6
|
+
* @returns The copied value
|
|
7
|
+
*/
|
|
8
|
+
export declare const deepCopy: <T>(value: T, seen?: WeakMap<object, unknown>) => T;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Marker the DevTools extension recognizes bridge messages by. */
|
|
2
|
+
export declare const DEVTOOLS_MESSAGE_SOURCE = "punica-form-devtools";
|
|
3
|
+
/** Version of the message format. */
|
|
4
|
+
export declare const DEVTOOLS_PROTOCOL_VERSION = 2;
|
|
5
|
+
/**
|
|
6
|
+
* Makes the forms of the page report to the Punica Form DevTools extension.
|
|
7
|
+
* Call it once, in development only.
|
|
8
|
+
*/
|
|
9
|
+
export declare const enableDevTools: () => void;
|
|
10
|
+
export declare const disableDevTools: () => void;
|
|
11
|
+
export declare const isDevToolsEnabled: () => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Sends a message to the DevTools extension, to the page's own origin only.
|
|
14
|
+
* @param formId - Identifies the form among the forms of the page
|
|
15
|
+
* @param type - `@@INIT`, `ENTITY` or the name of a form event
|
|
16
|
+
* @param formItemKey - The property the message is about
|
|
17
|
+
*/
|
|
18
|
+
export declare const sendToDevTools: (formId: string, type: string, data: unknown, formItemKey?: string) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Clears the validation error of one item.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ClearError<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
get name(): string;
|
|
8
|
+
initialize(command: CommandService<E, F>): void;
|
|
9
|
+
/**
|
|
10
|
+
* @throws When the form has no item for the property
|
|
11
|
+
*/
|
|
12
|
+
run(property: keyof E): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Clears the validation errors of every item.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ClearErrors<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
get name(): string;
|
|
8
|
+
initialize(command: CommandService<E, F>): void;
|
|
9
|
+
run(): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the entity with the values of the form.
|
|
4
|
+
*/
|
|
5
|
+
export declare class GetValues<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
get name(): string;
|
|
8
|
+
initialize(command: CommandService<E, F>): void;
|
|
9
|
+
/**
|
|
10
|
+
* @returns A copy of the entity the form was created from, holding the
|
|
11
|
+
* current values. The entity itself is never changed.
|
|
12
|
+
*/
|
|
13
|
+
run(): Promise<E>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FormItem } from '@punica/form';
|
|
2
|
+
import type { ClearError } from './clearError';
|
|
3
|
+
import type { ClearErrors } from './clearErrors';
|
|
4
|
+
import type { GetValues } from './getValues';
|
|
5
|
+
import type { Reset } from './reset';
|
|
6
|
+
import type { ResetItem } from './resetItem';
|
|
7
|
+
import type { SetValue } from './setValue';
|
|
8
|
+
import type { Trigger } from './trigger';
|
|
9
|
+
import type { Validate } from './validate';
|
|
10
|
+
export * from './clearError';
|
|
11
|
+
export * from './clearErrors';
|
|
12
|
+
export * from './getValues';
|
|
13
|
+
export * from './reset';
|
|
14
|
+
export * from './resetItem';
|
|
15
|
+
export * from './setValue';
|
|
16
|
+
export * from './store';
|
|
17
|
+
export * from './trigger';
|
|
18
|
+
export * from './validate';
|
|
19
|
+
declare module '@punica/form' {
|
|
20
|
+
interface ServiceRegistry<E, F extends FormItem<E>> {
|
|
21
|
+
clearError: ClearError<E, F>;
|
|
22
|
+
clearErrors: ClearErrors<E, F>;
|
|
23
|
+
getValues: GetValues<E, F>;
|
|
24
|
+
reset: Reset<E, F>;
|
|
25
|
+
resetItem: ResetItem<E, F>;
|
|
26
|
+
setValue: SetValue<E, F>;
|
|
27
|
+
trigger: Trigger<E, F>;
|
|
28
|
+
validate: Validate<E, F>;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"2.0.0"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Resets the form to the state it was started with.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Reset<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
get name(): string;
|
|
8
|
+
initialize(command: CommandService<E, F>): void;
|
|
9
|
+
/**
|
|
10
|
+
* Fires RESET after the form was updated.
|
|
11
|
+
*/
|
|
12
|
+
run(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Resets one item to the state it was started with.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ResetItem<E, F extends FormItem<E> = FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
get name(): string;
|
|
8
|
+
initialize(command: CommandService<E, F>): void;
|
|
9
|
+
/**
|
|
10
|
+
* @throws When the form has no item for the property
|
|
11
|
+
*/
|
|
12
|
+
run(property: keyof E): Promise<void>;
|
|
13
|
+
}
|