@embeddables/forms 0.0.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.
package/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # @embeddables/forms
2
+
3
+ Schema-driven form state for Embeddables funnels. Composes on
4
+ [`@embeddables/core`](https://www.npmjs.com/package/@embeddables/core) for
5
+ identity; optionally accepts an analytics client on `initForms` to emit events
6
+ on `.set()` and `.submit()`. When core resolves a publishable key, project id,
7
+ and app user id, durable R2 writes run automatically on successful `.set()` and
8
+ `.submit()` — best-effort and independent of analytics.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install @embeddables/forms @embeddables/core
14
+ ```
15
+
16
+ ## Quick start
17
+
18
+ ```typescript
19
+ import { initEmbeddables } from '@embeddables/core'
20
+ import { initForms, type FormSchema } from '@embeddables/forms'
21
+
22
+ const schema = {
23
+ id: 'signup',
24
+ name: 'Signup',
25
+ fields: [
26
+ {
27
+ key: 'email',
28
+ label: 'Email',
29
+ type: 'email',
30
+ validations: { required: true },
31
+ },
32
+ ],
33
+ } as const satisfies FormSchema
34
+
35
+ const core = initEmbeddables({
36
+ projectId: 'proj_…',
37
+ forms: [],
38
+ experiments: [],
39
+ })
40
+
41
+ const { initForm } = initForms({ core })
42
+ const form = initForm({ schema })
43
+
44
+ const result = await form.set({ email: 'maria@gmail.com' })
45
+ if (!result.ok) console.log(result.errors)
46
+
47
+ const email = form.get('email')
48
+ await form.submit()
49
+ ```
50
+
51
+ Use `as const satisfies FormSchema` on hand-authored schemas so field keys and
52
+ value types flow into `.set()`, `.get()`, and `.getAll()`. CLI-generated schemas
53
+ from `embeddables/_dist` are already typed.
54
+
55
+ ## Schema
56
+
57
+ Each form is one object: `id`, optional `name`, and `fields`. Supported field
58
+ types: `text`, `email`, `number`, `boolean`, `select`, `multiselect`, `json`.
59
+
60
+ Declarative rules: `required`, `minLength`, `maxLength`, `min`, `max`,
61
+ `pattern`, `patternFlags`, `oneOf`, and optional synchronous `validations.custom`.
62
+ Use string patterns, not `RegExp` objects. For YAML/JSON schemas without inline
63
+ functions, pass validators via `customValidations` on `initForm`.
64
+
65
+ ## Form API
66
+
67
+ | Method | Purpose |
68
+ | ------ | ------- |
69
+ | `set({ … })` | Validate and persist a patch atomically; optional analytics; best-effort durable R2 write when configured |
70
+ | `get(key)` / `getAll()` | Read declared fields from storage |
71
+ | `validate({ … })` | Check values without writing or tracking |
72
+ | `submit()` | Validate all fields, best-effort durable R2 write, and emit `form:submitted` when analytics is configured |
73
+ | `errors()` | Current validation messages |
74
+ | `clear()` | Remove this form's stored values |
75
+
76
+ `.set()` applies all keys in one call or writes nothing. Bind it to `change`,
77
+ `blur`, or a step button — not per-keystroke `input`. `.submit()` is not
78
+ idempotent; guard against double-clicks in your UI.
79
+
80
+ Returned promises from `set`, `submit`, and `validate` never reject for
81
+ validation failures — check `result.ok` and `result.errors` instead. Durable
82
+ backend writes are also best-effort: a failing persistence request never rejects
83
+ `.set()` or `.submit()`.
84
+
85
+ ## Options
86
+
87
+ **`initForms`**
88
+
89
+ | Option | Purpose |
90
+ | ------ | ------- |
91
+ | `core` | Initialized `@embeddables/core` instance (required) |
92
+ | `analyticsInstance` | Optional analytics client for event tracking only |
93
+ | `baseUrl` | Optional backend URL for durable R2 persistence writes |
94
+
95
+ **`initForm`**
96
+
97
+ | Option | Purpose |
98
+ | ------ | ------- |
99
+ | `schema` | Form definition (required) |
100
+ | `customValidations` | Per-field validators; overrides inline `validations.custom` |
101
+
102
+ ## Analytics
103
+
104
+ To track form events, install [`@embeddables/analytics`](https://www.npmjs.com/package/@embeddables/analytics) and pass `initAnalytics({ core })` as `analyticsInstance` on `initForms`.
105
+
106
+ When `analyticsInstance` is provided, a successful `.set()` emits `data:updated`
107
+ and one `field:updated` per changed field; `.submit()` emits `form:submitted`.
108
+ Analytics failures surface as `trackError` on the result — local field values
109
+ are still persisted. Omitting analytics disables event tracking only; durable
110
+ R2 writes still occur when core resolves persistence configuration.
111
+
112
+ ## Errors
113
+
114
+ | When | Error |
115
+ | ---- | ----- |
116
+ | Bad core at init | `FormsError` (thrown) |
117
+ | Bad schema at init | `SchemaError` (thrown) |
118
+ | Validation / bad patch | `{ ok: false, errors }` on the result |
119
+ | Analytics failure | `trackError` on the result |
120
+
121
+ All SDK errors extend `FormsError`. Custom validators that throw propagate
122
+ synchronously from `.set()` and `.submit()`.