@formspec/runtime 0.1.0-alpha.16 → 0.1.0-alpha.19
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/README.md +19 -97
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,125 +1,47 @@
|
|
|
1
1
|
# @formspec/runtime
|
|
2
2
|
|
|
3
|
-
Runtime helpers for FormSpec
|
|
3
|
+
Runtime helpers for dynamic FormSpec data sources.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @formspec/runtime
|
|
9
|
-
# or
|
|
10
8
|
pnpm add @formspec/runtime
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
Or use:
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
This package is ESM-only and requires:
|
|
18
|
-
|
|
19
|
-
```json
|
|
20
|
-
// package.json
|
|
21
|
-
{
|
|
22
|
-
"type": "module"
|
|
23
|
-
}
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
```json
|
|
27
|
-
// tsconfig.json
|
|
28
|
-
{
|
|
29
|
-
"compilerOptions": {
|
|
30
|
-
"module": "NodeNext",
|
|
31
|
-
"moduleResolution": "NodeNext"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add formspec
|
|
34
15
|
```
|
|
35
16
|
|
|
36
|
-
##
|
|
17
|
+
## Main API
|
|
37
18
|
|
|
38
|
-
|
|
19
|
+
`defineResolvers(form, resolvers)` creates a typed resolver registry for the dynamic enum sources used by a form.
|
|
39
20
|
|
|
40
|
-
```
|
|
21
|
+
```ts
|
|
22
|
+
import { field, formspec } from "@formspec/dsl";
|
|
41
23
|
import { defineResolvers } from "@formspec/runtime";
|
|
42
|
-
import { formspec, field } from "@formspec/dsl";
|
|
43
24
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
field.dynamicEnum("
|
|
47
|
-
field.dynamicEnum("state", "fetch_states", { label: "State" })
|
|
25
|
+
const Form = formspec(
|
|
26
|
+
field.dynamicEnum("country", "countries"),
|
|
27
|
+
field.dynamicEnum("state", "states")
|
|
48
28
|
);
|
|
49
29
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
fetch_countries: async () => ({
|
|
30
|
+
const resolvers = defineResolvers(Form, {
|
|
31
|
+
countries: async () => ({
|
|
53
32
|
options: [
|
|
54
33
|
{ value: "us", label: "United States" },
|
|
55
34
|
{ value: "ca", label: "Canada" },
|
|
56
|
-
{ value: "uk", label: "United Kingdom" },
|
|
57
35
|
],
|
|
58
36
|
validity: "valid",
|
|
59
37
|
}),
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const states = await response.json();
|
|
65
|
-
return {
|
|
66
|
-
options: states.map((s: { code: string; name: string }) => ({
|
|
67
|
-
value: s.code,
|
|
68
|
-
label: s.name,
|
|
69
|
-
})),
|
|
70
|
-
validity: "valid",
|
|
71
|
-
};
|
|
72
|
-
},
|
|
38
|
+
states: async () => ({
|
|
39
|
+
options: [],
|
|
40
|
+
validity: "unknown",
|
|
41
|
+
}),
|
|
73
42
|
});
|
|
74
43
|
|
|
75
|
-
|
|
76
|
-
const countries = await resolvers.get("fetch_countries")();
|
|
77
|
-
console.log(countries.options);
|
|
78
|
-
// [{ value: "us", label: "United States" }, ...]
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Resolver Response Format
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
interface FetchOptionsResponse {
|
|
85
|
-
options: Array<{
|
|
86
|
-
value: string;
|
|
87
|
-
label: string;
|
|
88
|
-
}>;
|
|
89
|
-
validity: "valid" | "invalid" | "unknown";
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
## API Reference
|
|
94
|
-
|
|
95
|
-
### Functions
|
|
96
|
-
|
|
97
|
-
| Function | Description |
|
|
98
|
-
| ---------------------------------- | ------------------------------------ |
|
|
99
|
-
| `defineResolvers(form, resolvers)` | Create a type-safe resolver registry |
|
|
100
|
-
|
|
101
|
-
### Types
|
|
102
|
-
|
|
103
|
-
| Type | Description |
|
|
104
|
-
| --------------------- | ------------------------------------- |
|
|
105
|
-
| `Resolver` | Async function that fetches options |
|
|
106
|
-
| `ResolverMap<F>` | Map of data source names to resolvers |
|
|
107
|
-
| `ResolverRegistry<F>` | Registry with `get()` method |
|
|
108
|
-
|
|
109
|
-
## Type Safety
|
|
110
|
-
|
|
111
|
-
The `defineResolvers` function enforces that:
|
|
112
|
-
|
|
113
|
-
1. All data sources referenced in the form have corresponding resolvers
|
|
114
|
-
2. Resolver names match the data source IDs in the form
|
|
115
|
-
3. Return types match the expected `FetchOptionsResponse` format
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
// TypeScript error: Missing resolver for "fetch_states"
|
|
119
|
-
const resolvers = defineResolvers(OrderForm, {
|
|
120
|
-
fetch_countries: async () => ({ ... }),
|
|
121
|
-
// Error: 'fetch_states' is required
|
|
122
|
-
});
|
|
44
|
+
const countries = await resolvers.get("countries")();
|
|
123
45
|
```
|
|
124
46
|
|
|
125
47
|
## License
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formspec/runtime",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.19",
|
|
4
4
|
"description": "Runtime helpers for FormSpec - resolvers and data fetching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@formspec/core": "0.1.0-alpha.
|
|
21
|
+
"@formspec/core": "0.1.0-alpha.19"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"vitest": "^3.0.0",
|
|
25
|
-
"@formspec/dsl": "0.1.0-alpha.
|
|
25
|
+
"@formspec/dsl": "0.1.0-alpha.19"
|
|
26
26
|
},
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|