@formspec/runtime 0.1.0-alpha.17 → 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.
Files changed (2) hide show
  1. package/README.md +19 -97
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,125 +1,47 @@
1
1
  # @formspec/runtime
2
2
 
3
- Runtime helpers for FormSpec - resolvers and data fetching.
3
+ Runtime helpers for dynamic FormSpec data sources.
4
4
 
5
- ## Installation
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
- > **Note:** Most users should install the `formspec` umbrella package instead, which re-exports everything from this package.
11
+ Or use:
14
12
 
15
- ## Requirements
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
- ## Usage
17
+ ## Main API
37
18
 
38
- ### Define Resolvers for Dynamic Enums
19
+ `defineResolvers(form, resolvers)` creates a typed resolver registry for the dynamic enum sources used by a form.
39
20
 
40
- ```typescript
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
- // Define a form with dynamic enum fields
45
- const OrderForm = formspec(
46
- field.dynamicEnum("country", "fetch_countries", { label: "Country" }),
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
- // Define type-safe resolvers
51
- const resolvers = defineResolvers(OrderForm, {
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
- fetch_states: async (params) => {
62
- // Params can include any context needed for the lookup (e.g. form data)
63
- const response = await fetch(`/api/states?country=${params?.country}`);
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
- // Use the resolver
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.17",
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.17"
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.17"
25
+ "@formspec/dsl": "0.1.0-alpha.19"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"