@punica/form-mcp 0.0.0-stage → 0.1.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 +73 -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 +9 -0
- package/bundled/form/define/FormItemProperty.d.ts +8 -0
- package/bundled/form/define/FormProperty.d.ts +7 -0
- package/bundled/form/define/index.d.ts +3 -0
- package/bundled/form/index.d.ts +7 -0
- package/bundled/form/model/command/index.d.ts +2 -0
- package/bundled/form/model/command/item.d.ts +7 -0
- package/bundled/form/model/command/service.d.ts +15 -0
- package/bundled/form/model/constants/index.d.ts +4 -0
- package/bundled/form/model/form/index.d.ts +8 -0
- package/bundled/form/model/index.d.ts +8 -0
- package/bundled/form/model/item/index.d.ts +25 -0
- package/bundled/form/model/reader/index.d.ts +4 -0
- package/bundled/form/model/service/index.d.ts +20 -0
- package/bundled/form/model/starter/index.d.ts +13 -0
- package/bundled/form/model/util/GetItem.d.ts +9 -0
- package/bundled/form/model/util/WriteItems.d.ts +8 -0
- package/bundled/form/model/util/index.d.ts +2 -0
- package/bundled/form/package.json +1 -0
- package/bundled/form/reader/decorators/base/index.d.ts +35 -0
- package/bundled/form/reader/decorators/readers/FormItemsReader.d.ts +17 -0
- package/bundled/form/reader/decorators/readers/ItemDataReader.d.ts +24 -0
- package/bundled/form/reader/decorators/readers/ItemInitializerReader.d.ts +16 -0
- package/bundled/form/reader/decorators/readers/index.d.ts +3 -0
- package/bundled/form/reader/index.d.ts +19 -0
- package/bundled/form/reader/reader/Reader.d.ts +13 -0
- package/bundled/form/reader/reader/index.d.ts +1 -0
- package/bundled/form/register/index.d.ts +43 -0
- package/bundled/form/utils/deepCopy.d.ts +8 -0
- package/bundled/form/utils/devtools.d.ts +77 -0
- package/bundled/form/utils/index.d.ts +2 -0
- package/bundled/form-service/clearError/index.d.ts +23 -0
- package/bundled/form-service/clearErrors/index.d.ts +22 -0
- package/bundled/form-service/getValues/index.d.ts +22 -0
- package/bundled/form-service/index.d.ts +9 -0
- package/bundled/form-service/package.json +1 -0
- package/bundled/form-service/reset/index.d.ts +22 -0
- package/bundled/form-service/resetItem/index.d.ts +23 -0
- package/bundled/form-service/setValue/index.d.ts +23 -0
- package/bundled/form-service/store/index.d.ts +30 -0
- package/bundled/form-service/trigger/index.d.ts +22 -0
- package/bundled/form-service/validate/index.d.ts +27 -0
- package/package.json +46 -3
- package/src/create.mjs +128 -0
- package/src/guide.md +160 -0
- package/src/knowledge.mjs +141 -0
- package/src/load.mjs +41 -0
- package/src/main.mjs +12 -0
- package/src/validate.mjs +98 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for clearing errors on a specific form item.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ClearError<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the service with a command.
|
|
14
|
+
* @param command - The command service to be used.
|
|
15
|
+
*/
|
|
16
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Clears errors on the specified form item.
|
|
19
|
+
* @param {keyof E} property - The name of the property to clear errors for.
|
|
20
|
+
* @returns {Promise<void>} - Resolves when the operation is complete.
|
|
21
|
+
*/
|
|
22
|
+
run(property: keyof E): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for clearing errors on form items.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ClearErrors<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl<boolean> {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the service with a command.
|
|
14
|
+
* @param command - The command service to be used.
|
|
15
|
+
*/
|
|
16
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Clears errors on form items.
|
|
19
|
+
* @returns {Promise<boolean>} - Resolves when the operation is complete.
|
|
20
|
+
*/
|
|
21
|
+
run(): Promise<boolean>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for retrieving form values as an entity.
|
|
4
|
+
*/
|
|
5
|
+
export declare class GetValues<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl<E> {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the service with a command.
|
|
14
|
+
* @param command - The command service to be used.
|
|
15
|
+
*/
|
|
16
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves form values and returns them as an entity.
|
|
19
|
+
* @returns {Promise<E>} - Resolves with the entity containing form values.
|
|
20
|
+
*/
|
|
21
|
+
run(): Promise<E>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './clearError';
|
|
2
|
+
export * from './clearErrors';
|
|
3
|
+
export * from './getValues';
|
|
4
|
+
export * from './reset';
|
|
5
|
+
export * from './resetItem';
|
|
6
|
+
export * from './setValue';
|
|
7
|
+
export * from './store';
|
|
8
|
+
export * from './trigger';
|
|
9
|
+
export * from './validate';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"1.2.0"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for resetting the form to its initial state.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Reset<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the service with a command.
|
|
14
|
+
* @param command - The command service to be used.
|
|
15
|
+
*/
|
|
16
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Resets the form to its initial state.
|
|
19
|
+
* @returns {Promise<void>} - Resolves when the operation is complete.
|
|
20
|
+
*/
|
|
21
|
+
run(): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for resetting form item values.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ResetItem<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the service with a command.
|
|
14
|
+
* @param command - The command service to be used.
|
|
15
|
+
*/
|
|
16
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Resets the value of the specified property.
|
|
19
|
+
* @param {keyof E} property - The name of the property to reset.
|
|
20
|
+
* @returns {Promise} - Resolves when the operation is complete, rejects on error.
|
|
21
|
+
*/
|
|
22
|
+
run(property: keyof E): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for setting values of form items.
|
|
4
|
+
*/
|
|
5
|
+
export declare class SetValue<E, F extends FormItem<E>> implements IServiceInitialize<E, F> {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Sets the value of a form item.
|
|
14
|
+
* @param {keyof E} property - Property of the form item
|
|
15
|
+
* @param value - Value to be set
|
|
16
|
+
*/
|
|
17
|
+
private setValue;
|
|
18
|
+
/**
|
|
19
|
+
* Initializes the service with a command.
|
|
20
|
+
* @param command - The command service to be used.
|
|
21
|
+
*/
|
|
22
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { IServiceCommand } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for storing data.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Store implements IServiceCommand {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Adds custom features for command items.
|
|
14
|
+
* This method provides a set of custom functions that can be used by command items.
|
|
15
|
+
* These functions allow for reading, writing, and deleting data from the internal store.
|
|
16
|
+
* @returns {{
|
|
17
|
+
* readStoreItem: (key: string) => any,
|
|
18
|
+
* writeStoreItem: (key: string, value: any) => void,
|
|
19
|
+
* deleteStoreItem: (key: string) => void
|
|
20
|
+
* }} An object containing custom functions for command items:
|
|
21
|
+
* - `readStoreItem`: A function that takes a `key` (string) and returns the corresponding value from the store.
|
|
22
|
+
* - `writeStoreItem`: A function that takes a `key` (string) and `value` (any) to store in the internal store.
|
|
23
|
+
* - `deleteStoreItem`: A function that takes a `key` (string) and deletes the corresponding item from the store.
|
|
24
|
+
*/
|
|
25
|
+
addCustomFeaturesForCommandItem(): {
|
|
26
|
+
readStoreItem: (key: string) => any;
|
|
27
|
+
writeStoreItem: (key: string, value: any) => void;
|
|
28
|
+
deleteStoreItem: (key: string) => void;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Trigger service is triggered when the value of a form item is changed.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Trigger<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Returns the name of the service.
|
|
9
|
+
* @returns {string} - Service name
|
|
10
|
+
*/
|
|
11
|
+
get name(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the service with a command.
|
|
14
|
+
* @param command - The command service to be used.
|
|
15
|
+
*/
|
|
16
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Called when the value of a form item is changed.
|
|
19
|
+
* @param property - Property of the changed item.
|
|
20
|
+
*/
|
|
21
|
+
run(property: keyof E): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CommandService, FormItem, IServiceControl, IServiceInitialize } from '@punica/form';
|
|
2
|
+
/**
|
|
3
|
+
* Service for validating form items.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Validate<E, F extends FormItem<E>> implements IServiceInitialize<E, F>, IServiceControl<boolean> {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Validates a form item.
|
|
9
|
+
* @param {F} item - The form item to be validated.
|
|
10
|
+
*/
|
|
11
|
+
private validateFormItem;
|
|
12
|
+
/**
|
|
13
|
+
* Returns the name of the service.
|
|
14
|
+
* @returns {string} - Service name
|
|
15
|
+
*/
|
|
16
|
+
get name(): string;
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the service with a command.
|
|
19
|
+
* @param command - The command service to be used.
|
|
20
|
+
*/
|
|
21
|
+
initialize(command: CommandService<E, F>): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Runs validation checks on all form items.
|
|
24
|
+
* @returns {Promise<boolean>} - Resolves to true if all validations pass, otherwise resolves to false.
|
|
25
|
+
*/
|
|
26
|
+
run(): Promise<boolean>;
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@punica/form-mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MCP server for Punica Form: look up the exact API, read the rules and check code that uses it.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mcp",
|
|
7
|
+
"model-context-protocol",
|
|
8
|
+
"form",
|
|
9
|
+
"punica"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://form.punicalab.com",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/punicalab/punica-form/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/punicalab/punica-form.git",
|
|
18
|
+
"directory": "packages/mcp"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "PunicaLab",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"bin": {
|
|
24
|
+
"punica-form-mcp": "src/main.mjs"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"bundled"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=20.12"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
38
|
+
"zod": "^4.6.7"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@punica/form": "^1.2.0",
|
|
42
|
+
"@punica/form-service": "^1.2.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "node build.mjs",
|
|
46
|
+
"clean": "rimraf bundled",
|
|
47
|
+
"test": "node --test test/*.test.mjs"
|
|
48
|
+
}
|
|
6
49
|
}
|
package/src/create.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// The MCP server: tools and resources over the public API of Punica Form,
|
|
2
|
+
// its rules and the code validator. Transport-free, so tests connect it to
|
|
3
|
+
// an in-memory client.
|
|
4
|
+
import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { createKnowledge } from './knowledge.mjs';
|
|
7
|
+
import { createValidator } from './validate.mjs';
|
|
8
|
+
|
|
9
|
+
const text = (value) => ({ content: [{ type: 'text', text: value }] });
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {{ packages: Array<Parameters<typeof createKnowledge>[0][number] & { installed?: boolean }>, guide: string }} options
|
|
13
|
+
* `installed` says whether the declarations come from the project or from
|
|
14
|
+
* the copy bundled with the server; the instructions tell the model.
|
|
15
|
+
*/
|
|
16
|
+
export function createPunicaFormServer({ packages, guide }) {
|
|
17
|
+
const knowledge = createKnowledge(packages);
|
|
18
|
+
const { validate } = createValidator(knowledge);
|
|
19
|
+
const versions = packages
|
|
20
|
+
.map((pkg) => `${pkg.name} ${pkg.version} (${pkg.installed ? 'installed in this project' : 'not installed, bundled copy'})`)
|
|
21
|
+
.join(', ');
|
|
22
|
+
|
|
23
|
+
const server = new McpServer(
|
|
24
|
+
{ name: 'punica-form', version: '0.1.0' },
|
|
25
|
+
{
|
|
26
|
+
instructions: `${versions}. Punica Form is a headless form engine and is rarely in training data: never write an import, a service name or a method from memory. Read get_guide first, look names up with search_api and get_api, and check the code with validate_code before handing it over.`
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
server.registerTool(
|
|
31
|
+
'get_guide',
|
|
32
|
+
{
|
|
33
|
+
title: 'How to use Punica Form',
|
|
34
|
+
description:
|
|
35
|
+
'The rules of Punica Form: what the packages provide and what the application builds itself, the lifecycle, the services and how to call them, events, and a complete working example.',
|
|
36
|
+
inputSchema: {},
|
|
37
|
+
annotations: { readOnlyHint: true }
|
|
38
|
+
},
|
|
39
|
+
async () => text(guide)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
server.registerTool(
|
|
43
|
+
'search_api',
|
|
44
|
+
{
|
|
45
|
+
title: 'Find Punica Form exports',
|
|
46
|
+
description:
|
|
47
|
+
'Finds exports of @punica/form and @punica/form-service by name or by words in their documentation ("validate", "reader", "event"). Without a query: every export.',
|
|
48
|
+
inputSchema: { query: z.string().optional().describe('Words to look for') },
|
|
49
|
+
annotations: { readOnlyHint: true }
|
|
50
|
+
},
|
|
51
|
+
async ({ query }) => {
|
|
52
|
+
const results = knowledge.search(query || '');
|
|
53
|
+
|
|
54
|
+
if (!results.length) {
|
|
55
|
+
return text(`Nothing matches "${query}". Exports: ${knowledge.names.join(', ')}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return text(
|
|
59
|
+
results
|
|
60
|
+
.map((item) => `${item.name} (${item.kind}, ${item.package})${item.summary ? `: ${item.summary}` : ''}`)
|
|
61
|
+
.join('\n')
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
server.registerTool(
|
|
67
|
+
'get_api',
|
|
68
|
+
{
|
|
69
|
+
title: 'The declaration of an export',
|
|
70
|
+
description:
|
|
71
|
+
'The exact TypeScript declaration and documentation of one or more exports, such as FormController, Validate, FormItem or IReader, read from the installed packages.',
|
|
72
|
+
inputSchema: { names: z.array(z.string()).min(1).max(12).describe('Export names') },
|
|
73
|
+
annotations: { readOnlyHint: true }
|
|
74
|
+
},
|
|
75
|
+
async ({ names }) => text(names.map((name) => knowledge.formatMarkdown(name)).join('\n\n'))
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
server.registerTool(
|
|
79
|
+
'validate_code',
|
|
80
|
+
{
|
|
81
|
+
title: 'Check code that uses Punica Form',
|
|
82
|
+
description:
|
|
83
|
+
'Checks TypeScript or JavaScript that uses Punica Form: imports the packages do not export, wrong service names and service calls that do not exist. Returns each problem with its line and a fix.',
|
|
84
|
+
inputSchema: { code: z.string().describe('The code to check') },
|
|
85
|
+
annotations: { readOnlyHint: true }
|
|
86
|
+
},
|
|
87
|
+
async ({ code }) => {
|
|
88
|
+
const findings = validate(code);
|
|
89
|
+
|
|
90
|
+
if (!findings.length) {
|
|
91
|
+
return text('No problems: every import exists and the services are called correctly.');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return text(
|
|
95
|
+
[
|
|
96
|
+
`${findings.length} problem${findings.length === 1 ? '' : 's'}:`,
|
|
97
|
+
...findings.map((f) => `line ${f.line} ${f.rule}: ${f.message} [fix: ${f.fix}]`)
|
|
98
|
+
].join('\n')
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
server.registerResource(
|
|
104
|
+
'guide',
|
|
105
|
+
'punica-form://guide',
|
|
106
|
+
{ title: 'The rules of Punica Form', mimeType: 'text/markdown' },
|
|
107
|
+
async (uri) => ({ contents: [{ uri: uri.href, mimeType: 'text/markdown', text: guide }] })
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
server.registerResource(
|
|
111
|
+
'api',
|
|
112
|
+
new ResourceTemplate('punica-form://api/{name}', {
|
|
113
|
+
list: async () => ({
|
|
114
|
+
resources: knowledge.names.map((name) => ({
|
|
115
|
+
uri: `punica-form://api/${name}`,
|
|
116
|
+
name,
|
|
117
|
+
mimeType: 'text/markdown'
|
|
118
|
+
}))
|
|
119
|
+
})
|
|
120
|
+
}),
|
|
121
|
+
{ title: 'The declaration of an export', mimeType: 'text/markdown' },
|
|
122
|
+
async (uri, { name }) => ({
|
|
123
|
+
contents: [{ uri: uri.href, mimeType: 'text/markdown', text: knowledge.formatMarkdown(String(name)) }]
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
return server;
|
|
128
|
+
}
|
package/src/guide.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Punica Form: the rules
|
|
2
|
+
|
|
3
|
+
Punica Form is a headless form engine. `@punica/form` holds the engine,
|
|
4
|
+
`@punica/form-service` the built-in services. There are no UI components.
|
|
5
|
+
|
|
6
|
+
## What the packages provide, and what they do not
|
|
7
|
+
|
|
8
|
+
- `@punica/form`: `FormController`, the class decorators `@Services`,
|
|
9
|
+
`@Readers` and `@Starters`, the helpers `defineFormItem`,
|
|
10
|
+
`defineFormItemProperty` and `defineFormProperty`, `FormItemRegister`,
|
|
11
|
+
`deepCopy`, the DevTools switches and the types. Use `search_api` for the
|
|
12
|
+
exact list.
|
|
13
|
+
- `@punica/form-service`: `Validate`, `GetValues`, `SetValue`, `Reset`,
|
|
14
|
+
`ResetItem`, `Trigger`, `ClearError`, `ClearErrors`, `Store`.
|
|
15
|
+
- NOT provided: `@Title`, `@Description`, `@Layout`, `@FormItemInputText` or
|
|
16
|
+
any other form item decorator, a `Reader` class, a `BaseReader`, a
|
|
17
|
+
`BaseFormItem`. These belong to the application's own form system. Build
|
|
18
|
+
them with the `define*` helpers and by implementing `IReader`; import them
|
|
19
|
+
from the application's own module, never from `@punica/form`.
|
|
20
|
+
- TypeScript needs `"experimentalDecorators": true`. `reflect-metadata` is
|
|
21
|
+
installed with the library and provides the global `Reflect.getMetadata`.
|
|
22
|
+
|
|
23
|
+
## Lifecycle
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
const controller = await FormController.fromEntity(new UserForm());
|
|
27
|
+
const form = await controller.start();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`fromEntity` creates the form items from the decorated properties of the
|
|
31
|
+
entity (only properties with an initializer, such as `name = ""`).
|
|
32
|
+
`start()` then fires `REGISTER_ITEMS`, runs the readers of `@Readers` in
|
|
33
|
+
order, runs the starters of `@Starters` (each returns the form, possibly a
|
|
34
|
+
new one), initializes the services of `@Services` and returns the form.
|
|
35
|
+
Both calls are asynchronous and reject when a reader, starter or service
|
|
36
|
+
throws.
|
|
37
|
+
|
|
38
|
+
## Services
|
|
39
|
+
|
|
40
|
+
Services are fetched by name. Names are camelCase strings and every service
|
|
41
|
+
is executed with `run`:
|
|
42
|
+
|
|
43
|
+
| Class | Name | Call |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| `Validate` | `'validate'` | `run(): Promise<boolean>`, validates every visible item |
|
|
46
|
+
| `GetValues` | `'getValues'` | `run(): Promise<E>`, the entity filled with the item values |
|
|
47
|
+
| `Reset` | `'reset'` | `run(): Promise<void>` |
|
|
48
|
+
| `ResetItem` | `'resetItem'` | `run(property): Promise<void>` |
|
|
49
|
+
| `Trigger` | `'trigger'` | `run(property): Promise<void>`, validates one item |
|
|
50
|
+
| `ClearError` | `'clearError'` | `run(property): Promise<void>` |
|
|
51
|
+
| `ClearErrors` | `'clearErrors'` | `run(): Promise<boolean>` |
|
|
52
|
+
| `SetValue` | `'setValue'` | no `run`: it adds `setValue(property, value)` to every form item |
|
|
53
|
+
| `Store` | `'store'` | no `run`: it adds `readStoreItem`, `writeStoreItem`, `deleteStoreItem` to the command passed to item functions |
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const isValid = await controller.getServices('validate').run();
|
|
57
|
+
const values = await controller.getServices('getValues').run();
|
|
58
|
+
await controller.getServices('trigger').run('name');
|
|
59
|
+
await item.setValue(item.property, value);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
- A service works only when it is listed in `@Services([...])`;
|
|
63
|
+
`getServices` returns `undefined` for a name that is not registered.
|
|
64
|
+
- There is no `validate(form)`, `getValues(form)`, `setValue(form, …)`,
|
|
65
|
+
`reset(form)` or `validateItem(…)`.
|
|
66
|
+
- `Validate.run()` returns `false` when the form is invalid. Other failures
|
|
67
|
+
(an unknown property, an error thrown by a validation function) reject.
|
|
68
|
+
- Setting a value does not validate it. Call `trigger` for that.
|
|
69
|
+
|
|
70
|
+
## Form items
|
|
71
|
+
|
|
72
|
+
A form item is a plain object: `property`, `label`, `type`, `value`,
|
|
73
|
+
`required`, `disabled`, `hidden`, `error`, `errorMessages`, plus anything the
|
|
74
|
+
application adds. Its functions receive a command `{ formItem, initialEntity,
|
|
75
|
+
getItem }`:
|
|
76
|
+
|
|
77
|
+
- `validation(command): Promise<{ error: boolean, errorMessages?: string[] }>`
|
|
78
|
+
- `control(command): Promise<FormItem[]>`, run after its value is set; the
|
|
79
|
+
returned items are written back to the form
|
|
80
|
+
- `initialize(command): Promise<FormItem[]>`, run once while the form is created
|
|
81
|
+
|
|
82
|
+
## Events
|
|
83
|
+
|
|
84
|
+
`controller.on(event, handler)` and `controller.off(event, handler)`. The
|
|
85
|
+
handler receives the payload as `unknown`; cast it.
|
|
86
|
+
|
|
87
|
+
| Event | Payload |
|
|
88
|
+
|---|---|
|
|
89
|
+
| `UPDATE_FORM` | the form; fired by every service that changes it |
|
|
90
|
+
| `UPDATE_ITEM` | an array: the changed item, then the items its `control` returned |
|
|
91
|
+
| `RESET` | the form; fired by `Reset`, followed by `UPDATE_FORM` |
|
|
92
|
+
| `REGISTER_ITEMS` | `string[]` of registered form item types; fired at the start of `start()` |
|
|
93
|
+
|
|
94
|
+
Services change the form in place. A UI library that compares references,
|
|
95
|
+
such as React, needs a new object: `setForm({ ...form })`.
|
|
96
|
+
|
|
97
|
+
## A complete example
|
|
98
|
+
|
|
99
|
+
The application's form system:
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
import { defineFormItem, defineFormProperty, type Form, type FormItem, type IReader } from "@punica/form";
|
|
103
|
+
|
|
104
|
+
export const Title = defineFormProperty<string>("app:title");
|
|
105
|
+
|
|
106
|
+
export class Reader<E extends object, F extends FormItem<E>> implements IReader<E, F> {
|
|
107
|
+
public async read(entity: E, form: Form<E, F>): Promise<void> {
|
|
108
|
+
form.title = Reflect.getMetadata("app:title", entity);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// The second argument is the renderer; FormItemRegister.getInstance().getItem(item.type) returns it
|
|
113
|
+
export const FormItemInputText = defineFormItem<Omit<FormItem<any>, "property">>(
|
|
114
|
+
"app:input-text",
|
|
115
|
+
InputText
|
|
116
|
+
);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
A business form:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { Readers, Services, type ErrorDetail } from "@punica/form";
|
|
123
|
+
import { GetValues, SetValue, Trigger, Validate } from "@punica/form-service";
|
|
124
|
+
import { FormItemInputText, Reader, Title } from "./formSystem";
|
|
125
|
+
|
|
126
|
+
@Title("Create User")
|
|
127
|
+
@Readers([new Reader()])
|
|
128
|
+
@Services([new GetValues(), new SetValue(), new Validate(), new Trigger()])
|
|
129
|
+
export class UserForm {
|
|
130
|
+
@FormItemInputText({
|
|
131
|
+
label: "Name",
|
|
132
|
+
required: true,
|
|
133
|
+
validation: async ({ formItem }): Promise<ErrorDetail> => ({
|
|
134
|
+
error: !formItem.value,
|
|
135
|
+
errorMessages: formItem.value ? [] : ["Name is required"],
|
|
136
|
+
}),
|
|
137
|
+
})
|
|
138
|
+
name = "";
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Using it:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const controller = await FormController.fromEntity<UserForm, FormItem<UserForm>>(new UserForm());
|
|
146
|
+
|
|
147
|
+
controller.on("UPDATE_FORM", (data) => render(data as Form<UserForm, FormItem<UserForm>>));
|
|
148
|
+
|
|
149
|
+
const form = await controller.start();
|
|
150
|
+
const [nameItem] = form.items;
|
|
151
|
+
|
|
152
|
+
await nameItem.setValue("name", "Jane");
|
|
153
|
+
await controller.getServices("trigger").run("name");
|
|
154
|
+
|
|
155
|
+
if (await controller.getServices("validate").run()) {
|
|
156
|
+
const values = await controller.getServices("getValues").run();
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Documentation: https://form.punicalab.com
|