keits 0.0.3 → 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.
Files changed (75) hide show
  1. package/README.md +93 -79
  2. package/dist/dev/api.js +275 -0
  3. package/dist/dev/forms.js +330 -0
  4. package/dist/dev/index.js +26 -326
  5. package/dist/dev/router.js +24 -5
  6. package/dist/dev/shared/computed-DrF8O8b4.js +40 -0
  7. package/dist/dev/shared/{element-C4DfKtZW.js → element-DXwmUAnz.js} +32 -111
  8. package/dist/dev/shared/{graph-D6-x_CWC.js → graph-SIvYLXSv.js} +62 -3
  9. package/dist/dev/shared/hydrate-C8cOsDDH.js +588 -0
  10. package/dist/dev/shared/mount-CDB2HgN5.js +23 -0
  11. package/dist/dev/shared/namespaces-Cz82B0Pr.js +84 -0
  12. package/dist/dev/shared/owner-BuwIoI4j.js +72 -0
  13. package/dist/dev/shared/render-context-k2uu_dM_.js +34 -0
  14. package/dist/dev/shared/render-effect-Uk9dDmuU.js +32 -0
  15. package/dist/dev/shared/resource-DlgzdtcG.js +145 -0
  16. package/dist/dev/shared/signal-B3xOKbVm.js +24 -0
  17. package/dist/dev/shared/store-0vEg2bQV.js +152 -0
  18. package/dist/dev/shared/unsafe-keys-CB1P22No.js +8 -0
  19. package/dist/dev/static.js +76 -0
  20. package/dist/dev/svg.js +2 -1
  21. package/dist/dev/testing.js +216 -0
  22. package/dist/dev/vite.js +141 -0
  23. package/dist/prod/api.js +215 -0
  24. package/dist/prod/forms.js +240 -0
  25. package/dist/prod/index.js +110 -327
  26. package/dist/prod/router.js +83 -68
  27. package/dist/prod/shared/computed-BjqJ6Pps.js +33 -0
  28. package/dist/prod/shared/element-DCit4-3e.js +129 -0
  29. package/dist/prod/shared/{graph-C_aA46c6.js → graph-CR2WKbwr.js} +107 -63
  30. package/dist/prod/shared/hydrate-kKnJbkV7.js +480 -0
  31. package/dist/prod/shared/mount-DbeKIoyD.js +18 -0
  32. package/dist/prod/shared/namespaces-BSRrBJIU.js +71 -0
  33. package/dist/prod/shared/owner-DBi3odT4.js +67 -0
  34. package/dist/prod/shared/render-context-DF8lr1uu.js +30 -0
  35. package/dist/prod/shared/render-effect-UFDi2Pfh.js +25 -0
  36. package/dist/prod/shared/resource-Ch5tVseZ.js +91 -0
  37. package/dist/prod/shared/signal-BDMlfa0Z.js +16 -0
  38. package/dist/prod/shared/store-CvW0DbmK.js +110 -0
  39. package/dist/prod/shared/unsafe-keys-C5PMlerl.js +7 -0
  40. package/dist/prod/static.js +48 -0
  41. package/dist/prod/svg.js +3 -2
  42. package/dist/prod/testing.js +144 -0
  43. package/dist/prod/vite.js +106 -0
  44. package/dist/types/api/cache.d.ts +16 -0
  45. package/dist/types/api/client.d.ts +42 -0
  46. package/dist/types/api/error.d.ts +16 -0
  47. package/dist/types/api/index.d.ts +3 -0
  48. package/dist/types/api/url.d.ts +5 -0
  49. package/dist/types/async/render-context.d.ts +10 -0
  50. package/dist/types/core/effect.d.ts +1 -0
  51. package/dist/types/core/graph.d.ts +11 -0
  52. package/dist/types/core/owner.d.ts +1 -0
  53. package/dist/types/core/unsafe-keys.d.ts +2 -0
  54. package/dist/types/dom/hooks.d.ts +4 -0
  55. package/dist/types/dom/types.d.ts +5 -0
  56. package/dist/types/forms/form.d.ts +55 -0
  57. package/dist/types/forms/index.d.ts +2 -0
  58. package/dist/types/forms/paths.d.ts +6 -0
  59. package/dist/types/forms/types.d.ts +38 -0
  60. package/dist/types/router/index.d.ts +1 -0
  61. package/dist/types/router/match.d.ts +1 -0
  62. package/dist/types/router/types.d.ts +1 -0
  63. package/dist/types/static/head.d.ts +36 -0
  64. package/dist/types/static/hydrate.d.ts +3 -0
  65. package/dist/types/static/index.d.ts +4 -0
  66. package/dist/types/static/island.d.ts +14 -0
  67. package/dist/types/static/json.d.ts +1 -0
  68. package/dist/types/static/render.d.ts +11 -0
  69. package/dist/types/static/server-dom.d.ts +100 -0
  70. package/dist/types/testing/index.d.ts +62 -0
  71. package/dist/types/vite/index.d.ts +24 -0
  72. package/package.json +46 -5
  73. package/dist/dev/shared/owner-DlUsXKXE.js +0 -122
  74. package/dist/prod/shared/element-DUtXb3px.js +0 -187
  75. package/dist/prod/shared/owner-DgJNPM0P.js +0 -104
@@ -0,0 +1,240 @@
1
+ import { t as e } from "./shared/computed-BjqJ6Pps.js";
2
+ import { i as t, x as n } from "./shared/graph-CR2WKbwr.js";
3
+ import { t as r } from "./shared/signal-BDMlfa0Z.js";
4
+ import { n as i, r as a } from "./shared/store-CvW0DbmK.js";
5
+ import { t as o } from "./shared/unsafe-keys-C5PMlerl.js";
6
+ // src/forms/paths.ts
7
+ function s(e, t) {
8
+ if (t === "") return e;
9
+ let n = e;
10
+ for (let e of t.split(".")) {
11
+ if (typeof n != "object" || !n) return;
12
+ n = n[e];
13
+ }
14
+ return n;
15
+ }
16
+ function c(e, t, n) {
17
+ let r = t.split(".");
18
+ if (r.some((e) => o.has(e))) throw Error(`[keits/forms] "${t}" is not a field path: "__proto__", "constructor" and "prototype" aren't allowed.`);
19
+ let i = r.pop(), a = e;
20
+ r.forEach((e, t) => {
21
+ let n = a[e];
22
+ typeof n == "object" && n || (n = /^\d+$/.test(r[t + 1] ?? i) ? [] : {}, a[e] = n, n = a[e]), a = n;
23
+ }), a[i] = n;
24
+ }
25
+ function l(e, t) {
26
+ if (Object.is(e, t)) return !0;
27
+ if (e instanceof Date && t instanceof Date) return e.getTime() === t.getTime();
28
+ if (e === null || t === null || typeof e != "object" || typeof t != "object" || Array.isArray(e) !== Array.isArray(t)) return !1;
29
+ let n = Object.keys(e);
30
+ return n.length === Object.keys(t).length && n.every((n) => l(e[n], t[n]));
31
+ }
32
+ function u(e, t) {
33
+ return t === "" || e === t || e.startsWith(`${t}.`);
34
+ }
35
+ function d(e, t, n) {
36
+ let r = {}, i = `${t}.`;
37
+ for (let [t, a] of Object.entries(e)) {
38
+ if (!t.startsWith(i)) {
39
+ r[t] = a;
40
+ continue;
41
+ }
42
+ let e = t.slice(i.length), o = e.indexOf("."), s = n(Number(o < 0 ? e : e.slice(0, o)));
43
+ s >= 0 && (r[`${i}${s}${o < 0 ? "" : e.slice(o)}`] = a);
44
+ }
45
+ return r;
46
+ }
47
+ function f(e) {
48
+ let t = {}, n = (e, n) => {
49
+ let r = Array.isArray(n) ? n.find((e) => typeof e == "string") : n;
50
+ typeof r == "string" && !(e in t) && (t[e] = r);
51
+ }, r = (e, t) => {
52
+ for (let [i, a] of Object.entries(e)) {
53
+ let e = t === "" ? i : `${t}.${i}`;
54
+ typeof a == "object" && a && !Array.isArray(a) ? r(a, e) : n(e, a);
55
+ }
56
+ };
57
+ if (typeof e != "object" || !e) return t;
58
+ let { errors: i, detail: a, message: o } = e;
59
+ if (Array.isArray(i)) for (let e of i) {
60
+ let t = e.path ?? e.field ?? e.name;
61
+ n(Array.isArray(t) ? t.join(".") : String(t ?? ""), e.message);
62
+ }
63
+ else typeof i == "object" && i && r(i, "");
64
+ if (Array.isArray(a)) for (let e of a) {
65
+ let t = Array.isArray(e.loc) ? e.loc : [], r = +(t[0] === "body" || t[0] === "query");
66
+ n(t.slice(r).join("."), e.msg);
67
+ }
68
+ return Object.keys(t).length === 0 && n("", o), t;
69
+ }
70
+ // src/forms/form.ts
71
+ function p(o) {
72
+ let [p, v] = i(structuredClone(o.initial)), [y, b] = i({}), [x, S] = i({}), [C, w] = i({}), T = r(structuredClone(o.initial), { equals: !1 }), E = r(0), D = r(!1), O = r(!1), k = 0, A = (e) => typeof e == "function" ? e() : e, j = () => structuredClone(a(p)), M = () => {
73
+ let e = j(), t = [
74
+ {},
75
+ {},
76
+ {}
77
+ ], n = {
78
+ errors: {},
79
+ output: void 0
80
+ }, r = [], i = (e, t) => {
81
+ _(e) ? r.push(e.then(t)) : t(e);
82
+ };
83
+ for (let [n, r] of Object.entries(o.fields ?? {})) i(r(s(e, n), e), (e) => {
84
+ e && (t[0][n] = e);
85
+ });
86
+ o.validate && i(o.validate(e), (e) => {
87
+ for (let [n, r] of Object.entries(e ?? {})) typeof r == "string" && (t[1][n] = r);
88
+ }), o.schema && i(o.schema["~standard"].validate(e), (e) => {
89
+ if (e.issues === void 0) n.output = e.value;
90
+ else for (let n of e.issues) {
91
+ let e = (n.path ?? []).map((e) => String(typeof e == "object" ? e.key : e)).join(".");
92
+ t[2][e] ??= n.message;
93
+ }
94
+ });
95
+ let a = () => {
96
+ for (let e of t) for (let [t, r] of Object.entries(e)) n.errors[t] ??= r;
97
+ return n;
98
+ };
99
+ return r.length === 0 ? a() : Promise.all(r).then(a);
100
+ }, N = () => {
101
+ let e = ++k, n = (n) => (e === k && t(() => {
102
+ h(b, n.errors), O.set(!1);
103
+ }), n), r = M();
104
+ return _(r) ? (O.set(!0), r.then(n)) : n(r);
105
+ }, P = (e) => {
106
+ S((t) => {
107
+ for (let n of Object.keys(t)) (u(n, e) || u(e, n)) && delete t[n];
108
+ }), N();
109
+ }, F = (e, n) => t(() => {
110
+ v((t) => c(t, e, n)), P(e);
111
+ }), I = (e) => w((t) => void (t[e] = !0)), L = (e, n, r) => t(() => {
112
+ v((t) => {
113
+ Array.isArray(s(t, e)) || c(t, e, []), n(s(t, e));
114
+ }), w((t) => m(t, {
115
+ ...d(a(t), e, r),
116
+ [e]: !0
117
+ })), S((t) => m(t, d(a(t), e, r))), N();
118
+ }), R = () => E() > 0, z = (e) => e === "" ? R() : C[e] === !0 || R(), B = e(() => Object.keys(y).length === 0), V = e(() => !l(p, T())), H = {
119
+ values: p,
120
+ value: (e) => s(p, A(e)),
121
+ set: (e, t) => F(e, t),
122
+ update: (e) => t(() => {
123
+ v(e), P("");
124
+ }),
125
+ bind(e, t = {}) {
126
+ let r = () => A(e), i = () => s(p, r()), a = {
127
+ name: r,
128
+ onblur: () => I(r()),
129
+ "aria-invalid": () => H.error(r()) ? "true" : void 0
130
+ }, o = n(i), c = t.value;
131
+ return c !== void 0 && Array.isArray(o) ? {
132
+ ...a,
133
+ type: "checkbox",
134
+ value: () => c,
135
+ checked: () => i().includes(c),
136
+ onchange: (e) => {
137
+ let t = n(i).filter((e) => e !== c);
138
+ F(r(), e.currentTarget.checked ? [...t, c] : t);
139
+ }
140
+ } : c === void 0 ? typeof o == "boolean" ? {
141
+ ...a,
142
+ type: "checkbox",
143
+ checked: () => i() === !0,
144
+ onchange: (e) => F(r(), e.currentTarget.checked)
145
+ } : typeof o == "number" ? {
146
+ ...a,
147
+ value: () => {
148
+ let e = i();
149
+ return Number.isNaN(e) ? "" : String(e);
150
+ },
151
+ oninput: (e) => {
152
+ let t = e.currentTarget.value;
153
+ F(r(), t.trim() === "" ? NaN : Number(t));
154
+ }
155
+ } : {
156
+ ...a,
157
+ value: () => String(i() ?? ""),
158
+ oninput: (e) => F(r(), e.currentTarget.value)
159
+ } : {
160
+ ...a,
161
+ type: "radio",
162
+ value: () => c,
163
+ checked: () => i() === c,
164
+ onchange: () => F(r(), c)
165
+ };
166
+ },
167
+ error(e = "") {
168
+ let t = A(e);
169
+ return (z(t) ? y[t] : void 0) ?? x[t];
170
+ },
171
+ touched: (e) => C[A(e)] === !0,
172
+ touch: (e) => I(e),
173
+ dirty: (e) => e === void 0 ? V() : !l(s(p, A(e)), s(T(), A(e))),
174
+ valid: () => B(),
175
+ validating: () => O(),
176
+ submitting: () => D(),
177
+ submitted: R,
178
+ array(e) {
179
+ return {
180
+ items: () => s(p, e) ?? [],
181
+ add: (t, n) => L(e, (e) => n === void 0 ? e.push(t) : e.splice(n, 0, t), (e) => n === void 0 || e < n ? e : e + 1),
182
+ remove: (t) => L(e, (e) => e.splice(t, 1), (e) => e === t ? -1 : e > t ? e - 1 : e),
183
+ move: (t, n) => L(e, (e) => e.splice(n, 0, ...e.splice(t, 1)), (e) => e === t ? n : t < n ? e > t && e <= n ? e - 1 : e : e >= n && e < t ? e + 1 : e)
184
+ };
185
+ },
186
+ setErrors(e) {
187
+ let t;
188
+ if (g(e)) {
189
+ if (e.status !== 400 && e.status !== 422) return !1;
190
+ t = f(e.body);
191
+ } else {
192
+ t = {};
193
+ for (let [n, r] of Object.entries(e)) typeof r == "string" && (t[n] = r);
194
+ }
195
+ return Object.keys(t).length !== 0 && (S((e) => m(e, t)), !0);
196
+ },
197
+ submit(e) {
198
+ return async (r) => {
199
+ if (r?.preventDefault(), n(D)) return !1;
200
+ let i = r?.currentTarget instanceof Element ? r.currentTarget : null, a = () => i?.querySelector("[aria-invalid=\"true\"]")?.focus();
201
+ t(() => {
202
+ E.set(n(E) + 1), D.set(!0), S((e) => m(e, {}));
203
+ });
204
+ try {
205
+ let t = j(), n = await N();
206
+ return Object.keys(n.errors).length > 0 ? (a(), !1) : (await e(o.schema ? n.output : t), T.set(() => t), !0);
207
+ } catch (e) {
208
+ if (H.setErrors(e)) return a(), !1;
209
+ throw e;
210
+ } finally {
211
+ D.set(!1);
212
+ }
213
+ };
214
+ },
215
+ reset(e) {
216
+ let r = structuredClone(e ?? n(T));
217
+ t(() => {
218
+ v((e) => m(e, structuredClone(r))), T.set(() => r), w((e) => m(e, {})), S((e) => m(e, {})), E.set(0), N();
219
+ });
220
+ }
221
+ };
222
+ return N(), H;
223
+ }
224
+ function m(e, t) {
225
+ for (let n of Object.keys(e)) n in t || delete e[n];
226
+ Object.assign(e, t);
227
+ }
228
+ function h(e, t) {
229
+ e((e) => {
230
+ for (let n of Object.keys(e)) n in t || delete e[n];
231
+ for (let [n, r] of Object.entries(t)) e[n] !== r && (e[n] = r);
232
+ });
233
+ }
234
+ function g(e) {
235
+ return typeof e == "object" && !!e && typeof e.status == "number" && "body" in e;
236
+ }
237
+ function _(e) {
238
+ return typeof e?.then == "function";
239
+ }
240
+ export { p as form };