@zerolith/forms 0.0.0-canary.20261102.g8292c77
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 +201 -0
- package/README.md +32 -0
- package/dist/form.d.ts +91 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +246 -0
- package/dist/form.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/problem.d.ts +11 -0
- package/dist/problem.d.ts.map +1 -0
- package/dist/problem.js +17 -0
- package/dist/problem.js.map +1 -0
- package/dist/schema.d.ts +62 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +55 -0
- package/dist/schema.js.map +1 -0
- package/package.json +50 -0
- package/src/form.ts +397 -0
- package/src/index.ts +11 -0
- package/src/problem.ts +18 -0
- package/src/schema.ts +122 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @zerolith/forms
|
|
2
|
+
|
|
3
|
+
Forms for zerolith applications. A form stays a native `<form>` that posts `FormData` to its action and works with JavaScript disabled; `createForm` enhances it when JavaScript runs: the submission goes through `fetch` with the form's own method, action, and encoding, pending state and the outcome are signals, and a failure arrives as an RFC 9457 problem details document whose `fields` map carries the messages for each input.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { html } from "zerolith";
|
|
7
|
+
import { createForm } from "@zerolith/forms";
|
|
8
|
+
|
|
9
|
+
const form = createForm({ onSuccess: () => router.navigate("/notes") });
|
|
10
|
+
|
|
11
|
+
html`<form method="post" action="/notes" @submit=${form.submit}>
|
|
12
|
+
<input name="title" aria-invalid=${form.field("title").invalid}>
|
|
13
|
+
<output>${form.field("title").error}</output>
|
|
14
|
+
<button ?disabled=${form.pending}>Save</button>
|
|
15
|
+
</form>`;
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Validation is written once as a Standard Schema and runs on both sides: `validateForm` turns the schema's issues into the same problem document the server returns, and `problemResponse` writes that document as a WinterTC `Response`, so the no-JavaScript path and the enhanced path share one handler and one wire shape. Any backend that can answer with `application/problem+json` fits.
|
|
19
|
+
|
|
20
|
+
The guide is on the [docs site](https://zerolith.dev/guide/forms/).
|
|
21
|
+
|
|
22
|
+
Status: alpha. Install with `npm install @zerolith/forms@alpha`; the API may change before 1.0.
|
|
23
|
+
|
|
24
|
+
## Documentation
|
|
25
|
+
|
|
26
|
+
- The [forms guide](https://zerolith.dev/guide/forms/) explains it with examples that run as tests.
|
|
27
|
+
- The [reference](https://zerolith.dev/reference/zerolith-forms/) lists every export, generated from the source.
|
|
28
|
+
- New to zerolith? The [tutorial](https://zerolith.dev/learn/) builds an app step by step, and the [playground](https://zerolith.dev/playground/) runs code in the browser.
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
Apache-2.0. See `LICENSE`.
|
package/dist/form.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The enhancer. A form stays a native `<form>` that posts to its action and
|
|
3
|
+
* works without JavaScript; `createForm` takes over its submit event when
|
|
4
|
+
* JavaScript runs, sends the same request with `fetch`, and exposes the
|
|
5
|
+
* outcome as signals: pending state, a parsed result, or a problem whose
|
|
6
|
+
* `fields` feed the inputs that failed.
|
|
7
|
+
*/
|
|
8
|
+
import { type Problem, type ReadableSignal } from "zerolith";
|
|
9
|
+
import { type StandardSchema } from "./schema.js";
|
|
10
|
+
/** Where a form is in its current submission. */
|
|
11
|
+
export type FormStatus = "idle" | "pending" | "success" | "failure" | "error";
|
|
12
|
+
/** What the default sender, or a custom `send`, receives. */
|
|
13
|
+
export interface Submission {
|
|
14
|
+
/** The form that was submitted. */
|
|
15
|
+
readonly element: HTMLFormElement;
|
|
16
|
+
/** The button or input that submitted it, when there was one. */
|
|
17
|
+
readonly submitter: HTMLElement | null;
|
|
18
|
+
/** The form's data, including the submitter's name and value. */
|
|
19
|
+
readonly data: FormData;
|
|
20
|
+
/** The lowercase method, after the submitter's `formmethod` override. */
|
|
21
|
+
readonly method: string;
|
|
22
|
+
/** The resolved action URL, after the submitter's `formaction` override. */
|
|
23
|
+
readonly action: URL;
|
|
24
|
+
/** Aborted when the form is submitted again or reset. */
|
|
25
|
+
readonly signal: AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
/** The outcome of one submission. */
|
|
28
|
+
export interface SubmissionOutcome {
|
|
29
|
+
/** The parsed body: JSON when the response says so, text otherwise, undefined for an empty body. */
|
|
30
|
+
readonly result: unknown;
|
|
31
|
+
/** The successful response, as `fetch` or a custom `send` returned it; its body has already been read into `result` unless it was redirected. */
|
|
32
|
+
readonly response: Response;
|
|
33
|
+
/** The form that was submitted, already reset when the `reset` option is set. */
|
|
34
|
+
readonly element: HTMLFormElement;
|
|
35
|
+
}
|
|
36
|
+
/** Options for `createForm`. */
|
|
37
|
+
export interface FormOptions {
|
|
38
|
+
/** Validated on the client before anything is sent; failing issues become the problem's `fields`. */
|
|
39
|
+
schema?: StandardSchema;
|
|
40
|
+
/** Sends the submission. Defaults to a `fetch` of the form's method, action, and encoding that asks for JSON. */
|
|
41
|
+
send?: (submission: Submission) => Promise<Response>;
|
|
42
|
+
/** Headers added to the default request. */
|
|
43
|
+
headers?: HeadersInit;
|
|
44
|
+
/** The `fetch` the default sender uses; defaults to the global one. */
|
|
45
|
+
fetch?: typeof fetch;
|
|
46
|
+
/** Where a redirected response takes the page. Defaults to `location.assign`; pass the router's `navigate` to stay in the application. */
|
|
47
|
+
navigate?: (url: string) => void;
|
|
48
|
+
/** Resets the form element after a successful response. */
|
|
49
|
+
reset?: boolean;
|
|
50
|
+
/** Called after a successful response. */
|
|
51
|
+
onSuccess?: (outcome: SubmissionOutcome) => void;
|
|
52
|
+
/** Called with the problem from client-side validation or from the server. */
|
|
53
|
+
onFailure?: (problem: Problem, element: HTMLFormElement) => void;
|
|
54
|
+
/** Called when the request itself fails. */
|
|
55
|
+
onError?: (error: unknown, element: HTMLFormElement) => void;
|
|
56
|
+
}
|
|
57
|
+
/** One input's slice of the current problem. */
|
|
58
|
+
export interface Field {
|
|
59
|
+
/** Every message for the field, oldest first. */
|
|
60
|
+
readonly errors: ReadableSignal<readonly string[]>;
|
|
61
|
+
/** The first message, or undefined. */
|
|
62
|
+
readonly error: ReadableSignal<string | undefined>;
|
|
63
|
+
/** True while the field has a message; bind it to `aria-invalid`. */
|
|
64
|
+
readonly invalid: ReadableSignal<boolean>;
|
|
65
|
+
}
|
|
66
|
+
/** The enhanced form. */
|
|
67
|
+
export interface Form {
|
|
68
|
+
/** The current submission's stage: `pending` while in flight, then `success`, `failure` with a problem, or `error` when it did not complete; `idle` before the first submission and after `reset()`. */
|
|
69
|
+
readonly status: ReadableSignal<FormStatus>;
|
|
70
|
+
/** True while a submission is in flight. */
|
|
71
|
+
readonly pending: ReadableSignal<boolean>;
|
|
72
|
+
/** The problem from the last failed submission, or undefined. */
|
|
73
|
+
readonly problem: ReadableSignal<Problem | undefined>;
|
|
74
|
+
/** The parsed body of the last successful submission. */
|
|
75
|
+
readonly result: ReadableSignal<unknown>;
|
|
76
|
+
/** The error of the last submission that did not complete. */
|
|
77
|
+
readonly error: ReadableSignal<unknown>;
|
|
78
|
+
/** A field's view of the problem; the same object for the same name. */
|
|
79
|
+
field(name: string): Field;
|
|
80
|
+
/** The submit handler; bind it with `@submit`. */
|
|
81
|
+
submit(event: SubmitEvent): void;
|
|
82
|
+
/** Aborts any submission in flight and returns the form to idle. */
|
|
83
|
+
reset(): void;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Creates the enhancer for one form. Bind `submit` to the form's `@submit`;
|
|
87
|
+
* the method, action, and encoding stay on the form element, so the same
|
|
88
|
+
* markup submits natively when the script has not run.
|
|
89
|
+
*/
|
|
90
|
+
export declare function createForm(options?: FormOptions): Form;
|
|
91
|
+
//# sourceMappingURL=form.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../src/form.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAsB,KAAK,OAAO,EAAE,KAAK,cAAc,EAAsC,MAAM,UAAU,CAAC;AACrH,OAAO,EAAE,KAAK,cAAc,EAAgB,MAAM,aAAa,CAAC;AAEhE,iDAAiD;AACjD,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;AAE9E,6DAA6D;AAC7D,MAAM,WAAW,UAAU;IAEvB,mCAAmC;IACnC,QAAQ,CAAC,OAAO,EAAE,eAAe,CAAC;IAClC,iEAAiE;IACjE,QAAQ,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI,CAAC;IACvC,iEAAiE;IACjE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,yEAAyE;IACzE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,4EAA4E;IAC5E,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC;IACrB,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;CAChC;AAED,qCAAqC;AACrC,MAAM,WAAW,iBAAiB;IAE9B,oGAAoG;IACpG,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,iJAAiJ;IACjJ,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,iFAAiF;IACjF,QAAQ,CAAC,OA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AAO,CAAC,CAAC;IAC1C,iEAAiE;IACjE,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC,OAAO,GAAG,SAAS,CAAC,CAAC;IACtD,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IACzC,8DAA8D;IAC9D,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;IACxC,wEAAwE;IACxE,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;IAC3B,kDAAkD;IAClD,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;IACjC,oEAAoE;IACpE,KAAK,IAAI,IAAI,CAAC;CACjB;AAuJD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,GAAE,WAAgB,GAAG,IAAI,CAkJ1D"}
|
package/dist/form.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The enhancer. A form stays a native `<form>` that posts to its action and
|
|
3
|
+
* works without JavaScript; `createForm` takes over its submit event when
|
|
4
|
+
* JavaScript runs, sends the same request with `fetch`, and exposes the
|
|
5
|
+
* outcome as signals: pending state, a parsed result, or a problem whose
|
|
6
|
+
* `fields` feed the inputs that failed.
|
|
7
|
+
*/
|
|
8
|
+
import { PROBLEM_MEDIA_TYPE, batch, computed, isProblem, signal } from "zerolith";
|
|
9
|
+
import { validateForm } from "./schema.js";
|
|
10
|
+
const EMPTY = [];
|
|
11
|
+
/** Calls an application callback, reporting what it throws as an uncaught error would be, without changing the form's state. */
|
|
12
|
+
function guarded(callback) {
|
|
13
|
+
try {
|
|
14
|
+
callback();
|
|
15
|
+
}
|
|
16
|
+
catch (thrown) {
|
|
17
|
+
if (typeof reportError === "function") {
|
|
18
|
+
reportError(thrown);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
queueMicrotask(() => {
|
|
22
|
+
throw thrown;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** The `FormData` implementation probed, and whether its constructor adds the submitter's entry, which browsers before Chrome 112 and Firefox 111 skip. */
|
|
28
|
+
let probed;
|
|
29
|
+
/** The form's data as the browser submits it, with the submitter's entry added where `new FormData(form, submitter)` leaves it out. */
|
|
30
|
+
function submittedData(element, submitter) {
|
|
31
|
+
const data = new FormData(element, submitter ?? undefined);
|
|
32
|
+
if (submitter === null) {
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
if (probed?.implementation !== FormData) {
|
|
36
|
+
const form = document.createElement("form");
|
|
37
|
+
const button = document.createElement("button");
|
|
38
|
+
button.name = "probe";
|
|
39
|
+
button.value = "kept";
|
|
40
|
+
form.append(button);
|
|
41
|
+
probed = { implementation: FormData, keepsSubmitter: new FormData(form, button).get("probe") === "kept" };
|
|
42
|
+
}
|
|
43
|
+
const name = submitter.getAttribute("name");
|
|
44
|
+
if (probed.keepsSubmitter || name === null || name === "") {
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
if (submitter instanceof HTMLInputElement && submitter.type === "image") {
|
|
48
|
+
data.append(`${name}.x`, "0");
|
|
49
|
+
data.append(`${name}.y`, "0");
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
data.append(name, submitter.value);
|
|
53
|
+
}
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The problem a failed response describes: its body when that is a problem
|
|
58
|
+
* document, or one built from the status. The server is a trust boundary,
|
|
59
|
+
* so `fields` keeps only own keys whose value is a message or a list of
|
|
60
|
+
* messages, a lone message becoming a list of one.
|
|
61
|
+
*/
|
|
62
|
+
function problemOf(body, response) {
|
|
63
|
+
if (!isProblem(body)) {
|
|
64
|
+
return { status: response.status, title: response.statusText === "" ? `HTTP ${response.status}` : response.statusText };
|
|
65
|
+
}
|
|
66
|
+
if (body.fields === undefined) {
|
|
67
|
+
return body;
|
|
68
|
+
}
|
|
69
|
+
const fields = Object.create(null);
|
|
70
|
+
for (const [name, value] of Object.entries(body.fields)) {
|
|
71
|
+
if (typeof value === "string") {
|
|
72
|
+
fields[name] = [value];
|
|
73
|
+
}
|
|
74
|
+
else if (Array.isArray(value)) {
|
|
75
|
+
const messages = value.filter((message) => typeof message === "string");
|
|
76
|
+
if (messages.length > 0) {
|
|
77
|
+
fields[name] = messages;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { ...body, fields };
|
|
82
|
+
}
|
|
83
|
+
function entries(data) {
|
|
84
|
+
return [...data].map(([name, value]) => [name, typeof value === "string" ? value : value.name]);
|
|
85
|
+
}
|
|
86
|
+
async function defaultSend(submission, headers, fetchImpl) {
|
|
87
|
+
const { element, submitter, data, method, action, signal: abort } = submission;
|
|
88
|
+
const requestHeaders = new Headers(headers);
|
|
89
|
+
if (!requestHeaders.has("accept")) {
|
|
90
|
+
requestHeaders.set("accept", `application/json, ${PROBLEM_MEDIA_TYPE}`);
|
|
91
|
+
}
|
|
92
|
+
const init = { method: method.toUpperCase(), headers: requestHeaders, signal: abort, credentials: "same-origin" };
|
|
93
|
+
if (method === "get") {
|
|
94
|
+
const url = new URL(action);
|
|
95
|
+
url.search = new URLSearchParams(entries(data)).toString();
|
|
96
|
+
return fetchImpl(url, init);
|
|
97
|
+
}
|
|
98
|
+
const enctype = submitter !== null && submitter.hasAttribute("formenctype") ? submitter.formEnctype : element.enctype;
|
|
99
|
+
if (enctype === "multipart/form-data") {
|
|
100
|
+
init.body = data;
|
|
101
|
+
}
|
|
102
|
+
else if (enctype === "text/plain") {
|
|
103
|
+
requestHeaders.set("content-type", "text/plain; charset=utf-8");
|
|
104
|
+
init.body = entries(data).map(([name, value]) => `${name}=${value}\r\n`).join("");
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
init.body = new URLSearchParams(entries(data));
|
|
108
|
+
}
|
|
109
|
+
return fetchImpl(action, init);
|
|
110
|
+
}
|
|
111
|
+
async function parseBody(response) {
|
|
112
|
+
if (response.status === 204 || response.status === 205) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
const type = response.headers.get("content-type") ?? "";
|
|
116
|
+
if (type.includes("json")) {
|
|
117
|
+
return response.json();
|
|
118
|
+
}
|
|
119
|
+
const text = await response.text();
|
|
120
|
+
return text === "" ? undefined : text;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates the enhancer for one form. Bind `submit` to the form's `@submit`;
|
|
124
|
+
* the method, action, and encoding stay on the form element, so the same
|
|
125
|
+
* markup submits natively when the script has not run.
|
|
126
|
+
*/
|
|
127
|
+
export function createForm(options = {}) {
|
|
128
|
+
const status = signal("idle");
|
|
129
|
+
const problem = signal(undefined);
|
|
130
|
+
const result = signal(undefined);
|
|
131
|
+
const error = signal(undefined);
|
|
132
|
+
const pending = computed(() => status.get() === "pending");
|
|
133
|
+
const fields = new Map();
|
|
134
|
+
let controller;
|
|
135
|
+
const fail = (found, element) => {
|
|
136
|
+
batch(() => {
|
|
137
|
+
problem.set(found);
|
|
138
|
+
status.set("failure");
|
|
139
|
+
});
|
|
140
|
+
guarded(() => options.onFailure?.(found, element));
|
|
141
|
+
};
|
|
142
|
+
const run = async (element, submitter, method, action, current) => {
|
|
143
|
+
const data = submittedData(element, submitter);
|
|
144
|
+
batch(() => {
|
|
145
|
+
status.set("pending");
|
|
146
|
+
problem.set(undefined);
|
|
147
|
+
error.set(undefined);
|
|
148
|
+
});
|
|
149
|
+
if (options.schema !== undefined) {
|
|
150
|
+
const validation = await validateForm(options.schema, data);
|
|
151
|
+
if (current.signal.aborted) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (validation.problem !== undefined) {
|
|
155
|
+
fail(validation.problem, element);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const submission = { element, submitter, data, method, action, signal: current.signal };
|
|
160
|
+
const response = await (options.send ?? ((request) => defaultSend(request, options.headers, options.fetch ?? fetch)))(submission);
|
|
161
|
+
if (current.signal.aborted) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const type = (response.headers.get("content-type") ?? "").toLowerCase();
|
|
165
|
+
if (type.includes(PROBLEM_MEDIA_TYPE) || !response.ok) {
|
|
166
|
+
const body = type.includes("json") ? await response.json().catch(() => undefined) : undefined;
|
|
167
|
+
if (current.signal.aborted) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
fail(problemOf(body, response), element);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const parsed = response.redirected ? undefined : await parseBody(response);
|
|
174
|
+
if (current.signal.aborted) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
batch(() => {
|
|
178
|
+
result.set(parsed);
|
|
179
|
+
status.set("success");
|
|
180
|
+
});
|
|
181
|
+
if (options.reset === true) {
|
|
182
|
+
element.reset();
|
|
183
|
+
}
|
|
184
|
+
guarded(() => options.onSuccess?.({ result: parsed, response, element }));
|
|
185
|
+
if (response.redirected) {
|
|
186
|
+
(options.navigate ?? ((url) => location.assign(url)))(response.url);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
return {
|
|
190
|
+
status,
|
|
191
|
+
pending,
|
|
192
|
+
problem,
|
|
193
|
+
result,
|
|
194
|
+
error,
|
|
195
|
+
field(name) {
|
|
196
|
+
let field = fields.get(name);
|
|
197
|
+
if (field === undefined) {
|
|
198
|
+
const errors = computed(() => {
|
|
199
|
+
const found = problem.get()?.fields;
|
|
200
|
+
return found !== undefined && Object.hasOwn(found, name) ? found[name] ?? EMPTY : EMPTY;
|
|
201
|
+
});
|
|
202
|
+
field = {
|
|
203
|
+
errors,
|
|
204
|
+
error: computed(() => errors.get()[0]),
|
|
205
|
+
invalid: computed(() => errors.get().length > 0)
|
|
206
|
+
};
|
|
207
|
+
fields.set(name, field);
|
|
208
|
+
}
|
|
209
|
+
return field;
|
|
210
|
+
},
|
|
211
|
+
submit(event) {
|
|
212
|
+
const element = event.currentTarget;
|
|
213
|
+
const submitter = event.submitter;
|
|
214
|
+
const method = submitter !== null && submitter.hasAttribute("formmethod") ? submitter.formMethod : element.method;
|
|
215
|
+
if (method === "dialog") {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
const action = new URL(submitter?.hasAttribute("formaction") === true ? submitter.formAction : element.action);
|
|
220
|
+
controller?.abort();
|
|
221
|
+
const current = new AbortController();
|
|
222
|
+
controller = current;
|
|
223
|
+
run(element, submitter, method, action, current).catch((thrown) => {
|
|
224
|
+
if (current.signal.aborted) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
batch(() => {
|
|
228
|
+
error.set(thrown);
|
|
229
|
+
status.set("error");
|
|
230
|
+
});
|
|
231
|
+
guarded(() => options.onError?.(thrown, element));
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
reset() {
|
|
235
|
+
controller?.abort();
|
|
236
|
+
controller = undefined;
|
|
237
|
+
batch(() => {
|
|
238
|
+
status.set("idle");
|
|
239
|
+
problem.set(undefined);
|
|
240
|
+
result.set(undefined);
|
|
241
|
+
error.set(undefined);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=form.js.map
|
package/dist/form.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form.js","sourceRoot":"","sources":["../src/form.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,kBAAkB,EAAqC,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACrH,OAAO,EAAuB,YAAY,EAAE,MAAM,aAAa,CAAC;AAwFhE,MAAM,KAAK,GAAsB,EAAE,CAAC;AAEpC,gIAAgI;AAChI,SAAS,OAAO,CAAC,QAAoB;IAEjC,IACA,CAAC;QACG,QAAQ,EAAE,CA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AU,EAAE,KAAK,EAAE,CAAC;YACpB,MAAM,OAAO,GAAG,IAAI,eAAe,EAAE,CAAC;YACtC,UAAU,GAAG,OAAO,CAAC;YACrB,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,MAAe,EAAE,EAAE;gBAEvE,IAAI,OAAO,CAAC,MAAM,CAAC,OAAO,EAC1B,CAAC;oBACG,OAAO;gBACX,CAAC;gBACD,KAAK,CAAC,GAAG,EAAE;oBAEP,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;oBAClB,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;gBACxB,CAAC,CAAC,CAAC;gBACH,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;YACtD,CAAC,CAAC,CAAC;QACP,CAAC;QACD,KAAK;YAED,UAAU,EAAE,KAAK,EAAE,CAAC;YACpB,UAAU,GAAG,SAAS,CAAC;YACvB,KAAK,CAAC,GAAG,EAAE;gBAEP,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;gBACnB,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;gBACvB,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;gBACtB,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YACzB,CAAC,CAAC,CAAC;QACP,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zerolith/forms: native forms, enhanced. The public surface is assembled here.
|
|
3
|
+
* The `Problem` type, `isProblem`, and `PROBLEM_MEDIA_TYPE` come from `zerolith`.
|
|
4
|
+
*/
|
|
5
|
+
export { problemResponse } from "./problem.js";
|
|
6
|
+
export type { FormValues, InferOutput, StandardIssue, StandardPathSegment, StandardResult, StandardSchema, Validation } from "./schema.js";
|
|
7
|
+
export { formValues, problemFromIssues, validateForm } from "./schema.js";
|
|
8
|
+
export type { Field, Form, FormOptions, FormStatus, Submission, SubmissionOutcome } from "./form.js";
|
|
9
|
+
export { createForm } from "./form.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,mBAAmB,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC3I,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE1E,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AACrG,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zerolith/forms: native forms, enhanced. The public surface is assembled here.
|
|
3
|
+
* The `Problem` type, `isProblem`, and `PROBLEM_MEDIA_TYPE` come from `zerolith`.
|
|
4
|
+
*/
|
|
5
|
+
export { problemResponse } from "./problem.js";
|
|
6
|
+
export { formValues, problemFromIssues, validateForm } from "./schema.js";
|
|
7
|
+
export { createForm } from "./form.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG1E,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server side of the problem document: writing a `Problem` as a
|
|
3
|
+
* WinterTC `Response` with the problem details media type.
|
|
4
|
+
*/
|
|
5
|
+
import { type Problem } from "zerolith";
|
|
6
|
+
/**
|
|
7
|
+
* Writes a problem as a `Response` with the problem details media type.
|
|
8
|
+
* The status comes from `init`, or the problem, or defaults to 400.
|
|
9
|
+
*/
|
|
10
|
+
export declare function problemResponse(problem: Problem, init?: ResponseInit): Response;
|
|
11
|
+
//# sourceMappingURL=problem.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"problem.d.ts","sourceRoot":"","sources":["../src/problem.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAsB,KAAK,OAAO,EAAE,MAAM,UAAU,CAAC;AAE5D;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,GAAE,YAAiB,GAAG,QAAQ,CAOnF"}
|