@punica/form-mcp 0.5.0 → 0.6.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.2.
|
|
1
|
+
{"version":"0.2.1"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { FormController } from '@punica/form';
|
|
2
|
+
import { type JsonFormItem, type Rule, type JsonFormValues, type RuleDefinition } from '@punica/form-json';
|
|
3
|
+
/**
|
|
4
|
+
* A form you draw yourself (your own markup, @punica/components or native
|
|
5
|
+
* controls) whose values, rules and submit are Punica Form's: the engine
|
|
6
|
+
* holds the value of each named control as the person types or chooses; a
|
|
7
|
+
* submit checks the engine's rules and each control's own constraints (in the
|
|
8
|
+
* words the control already uses), shows what is wrong on the control's
|
|
9
|
+
* `punica-field`, focuses the first wrong control, holds the submit buttons
|
|
10
|
+
* while it works, and hands on the values. A control of the form the engine
|
|
11
|
+
* was not given still holds to its own constraints.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { bindForm } from '@punica/form-kit/dom';
|
|
15
|
+
*
|
|
16
|
+
* const form = bindForm(element, {
|
|
17
|
+
* fields: { name: nameInput, amount: amountInput },
|
|
18
|
+
* rules: { amount: [{ rule: 'pattern', value: '^[0-9]+$', message: 'A whole number.' }] },
|
|
19
|
+
* onSubmit: (values) => save(values),
|
|
20
|
+
* alert: errorLine
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
/** A control of a hand-drawn form: a @punica/components field control (or a native one) with a value. */
|
|
25
|
+
export type BoundControl = HTMLElement & {
|
|
26
|
+
value?: unknown;
|
|
27
|
+
checked?: boolean;
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
validity?: ValidityState;
|
|
30
|
+
validationMessage?: string;
|
|
31
|
+
checkValidity?: () => boolean;
|
|
32
|
+
setCustomValidity?: (message: string) => void;
|
|
33
|
+
updateComplete?: Promise<unknown>;
|
|
34
|
+
};
|
|
35
|
+
export interface BindFormOptions {
|
|
36
|
+
/** The form's controls, by the name each value is handed on under. */
|
|
37
|
+
fields: Record<string, BoundControl>;
|
|
38
|
+
/** Rules of the engine beyond what each control says itself: `{ rule, value?, message? }`. */
|
|
39
|
+
rules?: Record<string, RuleDefinition[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Rules of your own, by the names `rules` uses them with, beside the built-in ones (`required`, `min`,
|
|
42
|
+
* `pattern`, …): `{ vatNumber: (value) => (valid(value) ? undefined : 'Not a VAT number.') }`.
|
|
43
|
+
*/
|
|
44
|
+
customRules?: Record<string, Rule>;
|
|
45
|
+
/** Called with the values when every rule and every control holds. What it throws is said in `alert` (or `failed`). */
|
|
46
|
+
onSubmit: (values: Record<string, unknown>) => unknown;
|
|
47
|
+
/**
|
|
48
|
+
* The fields a failed `onSubmit` names, read from what it threw (a server that refused some fields):
|
|
49
|
+
* `{ email: 'Already registered.' }`. They are shown on their fields, the first one takes the focus, and
|
|
50
|
+
* `alert` says only what named no field of the form. Nothing named: `undefined` or `{}`.
|
|
51
|
+
*/
|
|
52
|
+
refusal?: (err: unknown) => Record<string, string | string[]> | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* The words for a control that is wrong and has no message of its own (a control without
|
|
55
|
+
* `validationMessage`): `required` for a missing value, `invalid` for any other.
|
|
56
|
+
*/
|
|
57
|
+
problemMessage?: (problem: 'required' | 'invalid') => string;
|
|
58
|
+
/**
|
|
59
|
+
* Called when a submit failed, instead of saying it in `alert`: with what was thrown and how many fields
|
|
60
|
+
* of the form `refusal` marked.
|
|
61
|
+
*/
|
|
62
|
+
failed?: (err: unknown, named: number) => void;
|
|
63
|
+
/** Where a failure is said. */
|
|
64
|
+
alert?: HTMLElement;
|
|
65
|
+
/** After a submit that went through, put every field back to where the form started. */
|
|
66
|
+
reset?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* `false` for a form whose buttons decide how it goes (several ways through, Ctrl+Enter): the browser's and the
|
|
69
|
+
* controls' implicit submit (Enter in a field) is let go, and only `submit()` sends it. `true` by default.
|
|
70
|
+
*/
|
|
71
|
+
submitOnEnter?: boolean;
|
|
72
|
+
}
|
|
73
|
+
export interface BoundForm {
|
|
74
|
+
ready: Promise<FormController<JsonFormValues, JsonFormItem>>;
|
|
75
|
+
submit: () => Promise<boolean>;
|
|
76
|
+
values: () => Promise<Record<string, unknown>>;
|
|
77
|
+
set: (name: string, value: unknown) => Promise<void>;
|
|
78
|
+
dispose: () => void;
|
|
79
|
+
}
|
|
80
|
+
export declare function bindForm(form: HTMLFormElement, opts: BindFormOptions): BoundForm;
|
|
81
|
+
/** `bindForm`, handing the form back: for a form built inside an expression. */
|
|
82
|
+
export declare function boundForm(form: HTMLFormElement, opts: BindFormOptions): HTMLFormElement;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.
|
|
1
|
+
{"version":"0.6.0"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@punica/form-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "MCP server for Punica Form: look up the exact API of its packages, read the rules and check code that uses them.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|
|
@@ -36,14 +36,14 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
39
|
-
"@punica/form-json": "^0.2.
|
|
39
|
+
"@punica/form-json": "^0.2.1",
|
|
40
40
|
"zod": "^4.6.7"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@punica/form": "^2.3.0",
|
|
44
44
|
"@punica/form-service": "^2.3.0",
|
|
45
45
|
"@punica/form-react": "^0.2.0",
|
|
46
|
-
"@punica/form-kit": "^0.
|
|
46
|
+
"@punica/form-kit": "^0.6.0",
|
|
47
47
|
"@punica/form-schema": "^0.1.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
package/src/guide.md
CHANGED
|
@@ -91,6 +91,38 @@ and runs it.
|
|
|
91
91
|
- `fromJsonSchema(schema)` turns a flat JSON Schema (string, number, integer,
|
|
92
92
|
boolean and enum properties) into a definition.
|
|
93
93
|
|
|
94
|
+
## A form drawn by hand, without React
|
|
95
|
+
|
|
96
|
+
When the markup is your own (a page drawn with the DOM, a framework other
|
|
97
|
+
than React, server-rendered HTML), bind the form to the engine with
|
|
98
|
+
`bindForm` from `@punica/form-kit/dom` instead of rewriting it as a class.
|
|
99
|
+
It takes the controls by name (@punica/components elements or native ones);
|
|
100
|
+
the engine holds their values, checks its rules and each control's own
|
|
101
|
+
constraints on submit, shows errors on each control's `punica-field`,
|
|
102
|
+
focuses the first wrong control and holds the submit buttons while it works.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { bindForm } from '@punica/form-kit/dom';
|
|
106
|
+
|
|
107
|
+
const form = bindForm(document.querySelector('form')!, {
|
|
108
|
+
fields: { email: emailInput, seats: seatsInput },
|
|
109
|
+
rules: { seats: [{ rule: 'min', value: 1, message: 'At least one seat.' }] },
|
|
110
|
+
onSubmit: (values) => api.post('orders', { json: values }),
|
|
111
|
+
alert: document.querySelector<HTMLElement>('.form-error')!
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
- `onSubmit` runs only when everything holds; what it throws is shown in
|
|
116
|
+
`alert` (or given to `failed`).
|
|
117
|
+
- `submitOnEnter: false` for a form whose buttons decide (several actions):
|
|
118
|
+
call `form.submit()` from each button.
|
|
119
|
+
- `form.set(name, value)` writes into the engine and the control;
|
|
120
|
+
`form.dispose()` before the form is drawn again.
|
|
121
|
+
- `refusal: (err) => ({ field: 'message' })` reads the fields a failed
|
|
122
|
+
`onSubmit` names (a server's refusal) and shows them on those fields;
|
|
123
|
+
`customRules` adds named rules of your own; `problemMessage` gives the
|
|
124
|
+
words for a control without a message of its own.
|
|
125
|
+
|
|
94
126
|
## Lifecycle
|
|
95
127
|
|
|
96
128
|
```ts
|
package/src/main.mjs
CHANGED
|
@@ -12,6 +12,7 @@ const packages = [
|
|
|
12
12
|
loadPackage('@punica/form-kit'),
|
|
13
13
|
loadPackage('@punica/form-kit', process.cwd(), 'react'),
|
|
14
14
|
loadPackage('@punica/form-kit', process.cwd(), 'json'),
|
|
15
|
+
loadPackage('@punica/form-kit', process.cwd(), 'dom'),
|
|
15
16
|
loadPackage('@punica/form-json'),
|
|
16
17
|
loadPackage('@punica/form-schema')
|
|
17
18
|
].filter(Boolean);
|