@taggor/component 0.0.0-stage → 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 (169) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -2
  3. package/assets.d.ts +35 -0
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/assets.d.ts +26 -0
  6. package/dist/assets.d.ts.map +1 -0
  7. package/dist/assets.js +14 -0
  8. package/dist/assets.js.map +1 -0
  9. package/dist/bounded-json.d.ts +21 -0
  10. package/dist/bounded-json.d.ts.map +1 -0
  11. package/dist/bounded-json.js +55 -0
  12. package/dist/bounded-json.js.map +1 -0
  13. package/dist/build/check-messages.d.ts +41 -0
  14. package/dist/build/check-messages.d.ts.map +1 -0
  15. package/dist/build/check-messages.js +142 -0
  16. package/dist/build/check-messages.js.map +1 -0
  17. package/dist/build/extract-manifest.d.ts +48 -0
  18. package/dist/build/extract-manifest.d.ts.map +1 -0
  19. package/dist/build/extract-manifest.js +81 -0
  20. package/dist/build/extract-manifest.js.map +1 -0
  21. package/dist/build/extract-styles.d.ts +36 -0
  22. package/dist/build/extract-styles.d.ts.map +1 -0
  23. package/dist/build/extract-styles.js +48 -0
  24. package/dist/build/extract-styles.js.map +1 -0
  25. package/dist/build/find-definition.d.ts +14 -0
  26. package/dist/build/find-definition.d.ts.map +1 -0
  27. package/dist/build/find-definition.js +23 -0
  28. package/dist/build/find-definition.js.map +1 -0
  29. package/dist/build/index.d.ts +10 -0
  30. package/dist/build/index.d.ts.map +1 -0
  31. package/dist/build/index.js +10 -0
  32. package/dist/build/index.js.map +1 -0
  33. package/dist/consent.d.ts +100 -0
  34. package/dist/consent.d.ts.map +1 -0
  35. package/dist/consent.js +147 -0
  36. package/dist/consent.js.map +1 -0
  37. package/dist/data-client.d.ts +34 -0
  38. package/dist/data-client.d.ts.map +1 -0
  39. package/dist/data-client.js +78 -0
  40. package/dist/data-client.js.map +1 -0
  41. package/dist/data-declaration.d.ts +59 -0
  42. package/dist/data-declaration.d.ts.map +1 -0
  43. package/dist/data-declaration.js +2 -0
  44. package/dist/data-declaration.js.map +1 -0
  45. package/dist/data-errors.d.ts +33 -0
  46. package/dist/data-errors.d.ts.map +1 -0
  47. package/dist/data-errors.js +73 -0
  48. package/dist/data-errors.js.map +1 -0
  49. package/dist/data-transport.d.ts +73 -0
  50. package/dist/data-transport.d.ts.map +1 -0
  51. package/dist/data-transport.js +2 -0
  52. package/dist/data-transport.js.map +1 -0
  53. package/dist/debug.d.ts +14 -0
  54. package/dist/debug.d.ts.map +1 -0
  55. package/dist/debug.js +11 -0
  56. package/dist/debug.js.map +1 -0
  57. package/dist/define-component.d.ts +162 -0
  58. package/dist/define-component.d.ts.map +1 -0
  59. package/dist/define-component.js +79 -0
  60. package/dist/define-component.js.map +1 -0
  61. package/dist/define-library.d.ts +24 -0
  62. package/dist/define-library.d.ts.map +1 -0
  63. package/dist/define-library.js +24 -0
  64. package/dist/define-library.js.map +1 -0
  65. package/dist/edge-transport.d.ts +25 -0
  66. package/dist/edge-transport.d.ts.map +1 -0
  67. package/dist/edge-transport.js +94 -0
  68. package/dist/edge-transport.js.map +1 -0
  69. package/dist/events.d.ts +50 -0
  70. package/dist/events.d.ts.map +1 -0
  71. package/dist/events.js +2 -0
  72. package/dist/events.js.map +1 -0
  73. package/dist/form-field.d.ts +59 -0
  74. package/dist/form-field.d.ts.map +1 -0
  75. package/dist/form-field.js +114 -0
  76. package/dist/form-field.js.map +1 -0
  77. package/dist/host.d.ts +45 -0
  78. package/dist/host.d.ts.map +1 -0
  79. package/dist/host.js +30 -0
  80. package/dist/host.js.map +1 -0
  81. package/dist/index.d.ts +46 -0
  82. package/dist/index.d.ts.map +1 -0
  83. package/dist/index.js +38 -0
  84. package/dist/index.js.map +1 -0
  85. package/dist/intl-format.d.ts +37 -0
  86. package/dist/intl-format.d.ts.map +1 -0
  87. package/dist/intl-format.js +56 -0
  88. package/dist/intl-format.js.map +1 -0
  89. package/dist/issued-proofs.d.ts +19 -0
  90. package/dist/issued-proofs.d.ts.map +1 -0
  91. package/dist/issued-proofs.js +19 -0
  92. package/dist/issued-proofs.js.map +1 -0
  93. package/dist/locale.d.ts +28 -0
  94. package/dist/locale.d.ts.map +1 -0
  95. package/dist/locale.js +62 -0
  96. package/dist/locale.js.map +1 -0
  97. package/dist/memory-transport.d.ts +39 -0
  98. package/dist/memory-transport.d.ts.map +1 -0
  99. package/dist/memory-transport.js +34 -0
  100. package/dist/memory-transport.js.map +1 -0
  101. package/dist/messages.d.ts +34 -0
  102. package/dist/messages.d.ts.map +1 -0
  103. package/dist/messages.js +54 -0
  104. package/dist/messages.js.map +1 -0
  105. package/dist/none.d.ts +7 -0
  106. package/dist/none.d.ts.map +1 -0
  107. package/dist/none.js +6 -0
  108. package/dist/none.js.map +1 -0
  109. package/dist/props.d.ts +89 -0
  110. package/dist/props.d.ts.map +1 -0
  111. package/dist/props.js +25 -0
  112. package/dist/props.js.map +1 -0
  113. package/dist/slots.d.ts +26 -0
  114. package/dist/slots.d.ts.map +1 -0
  115. package/dist/slots.js +70 -0
  116. package/dist/slots.js.map +1 -0
  117. package/dist/submit-result.d.ts +19 -0
  118. package/dist/submit-result.d.ts.map +1 -0
  119. package/dist/submit-result.js +58 -0
  120. package/dist/submit-result.js.map +1 -0
  121. package/dist/testing/form-internals.d.ts +59 -0
  122. package/dist/testing/form-internals.d.ts.map +1 -0
  123. package/dist/testing/form-internals.js +150 -0
  124. package/dist/testing/form-internals.js.map +1 -0
  125. package/dist/testing/index.d.ts +15 -0
  126. package/dist/testing/index.d.ts.map +1 -0
  127. package/dist/testing/index.js +14 -0
  128. package/dist/testing/index.js.map +1 -0
  129. package/dist/testing/mount-component.d.ts +105 -0
  130. package/dist/testing/mount-component.d.ts.map +1 -0
  131. package/dist/testing/mount-component.js +270 -0
  132. package/dist/testing/mount-component.js.map +1 -0
  133. package/dist/testing/visitor.d.ts +23 -0
  134. package/dist/testing/visitor.d.ts.map +1 -0
  135. package/dist/testing/visitor.js +34 -0
  136. package/dist/testing/visitor.js.map +1 -0
  137. package/dist/theming.d.ts +42 -0
  138. package/dist/theming.d.ts.map +1 -0
  139. package/dist/theming.js +19 -0
  140. package/dist/theming.js.map +1 -0
  141. package/dist/turnstile.d.ts +60 -0
  142. package/dist/turnstile.d.ts.map +1 -0
  143. package/dist/turnstile.js +179 -0
  144. package/dist/turnstile.js.map +1 -0
  145. package/dist/use-font.d.ts +65 -0
  146. package/dist/use-font.d.ts.map +1 -0
  147. package/dist/use-font.js +145 -0
  148. package/dist/use-font.js.map +1 -0
  149. package/dist/use-locale.d.ts +46 -0
  150. package/dist/use-locale.d.ts.map +1 -0
  151. package/dist/use-locale.js +57 -0
  152. package/dist/use-locale.js.map +1 -0
  153. package/dist/use-taggor-data.d.ts +72 -0
  154. package/dist/use-taggor-data.d.ts.map +1 -0
  155. package/dist/use-taggor-data.js +179 -0
  156. package/dist/use-taggor-data.js.map +1 -0
  157. package/dist/use-track.d.ts +17 -0
  158. package/dist/use-track.d.ts.map +1 -0
  159. package/dist/use-track.js +64 -0
  160. package/dist/use-track.js.map +1 -0
  161. package/dist/value-types.d.ts +69 -0
  162. package/dist/value-types.d.ts.map +1 -0
  163. package/dist/value-types.js +15 -0
  164. package/dist/value-types.js.map +1 -0
  165. package/dist/visitor-action.d.ts +49 -0
  166. package/dist/visitor-action.d.ts.map +1 -0
  167. package/dist/visitor-action.js +77 -0
  168. package/dist/visitor-action.js.map +1 -0
  169. package/package.json +71 -3
@@ -0,0 +1,179 @@
1
+ /**
2
+ * `useTaggorData` (TAG-203): the hook a component uses to submit a form to one of its declared
3
+ * collections. It builds the contract's `SubmitRequest` (an idempotency key, the time since
4
+ * render for the bot filter, the optional Turnstile token and honeypot), sends it through the
5
+ * host's `DataTransport` and exposes the status, a friendly message (pt-BR or en) and the
6
+ * per-field errors the server returned.
7
+ *
8
+ * Guarantees: one submission at a time (a double click while one is in flight gets the same
9
+ * promise, nothing is sent twice); `submit` never rejects and nothing throws to the page (golden
10
+ * rule 2); nothing is written to cookies or storage, and the idempotency key is a random value of
11
+ * one submission, never a visitor identifier (golden rule 6). No Zod: the server validates.
12
+ * TAG-212: a component whose manifest declares `data.personal` sends nothing without the visitor's
13
+ * consent (`useConsent`), and sends the consent proof with each submission. TAG-237: only a
14
+ * proof `useConsent` issued counts (`issued-proofs.ts`); one built by hand is refused.
15
+ */
16
+ import { useContext, useRef, useState } from "preact/hooks";
17
+ import { errorMessage, messageLocale } from "./data-errors.js";
18
+ import { debugWarn } from "./debug.js";
19
+ import { HostContext } from "./host.js";
20
+ import { issuedProofs } from "./issued-proofs.js";
21
+ import { normalizeSubmitResult } from "./submit-result.js";
22
+ const NO_FIELD_ERRORS = Object.freeze({});
23
+ const IDLE = Object.freeze({
24
+ status: "idle",
25
+ error: undefined,
26
+ fieldErrors: NO_FIELD_ERRORS,
27
+ });
28
+ const SUBMITTING = Object.freeze({ ...IDLE, status: "submitting" });
29
+ const SUCCESS = Object.freeze({ ...IDLE, status: "success" });
30
+ /**
31
+ * A fresh idempotency key: 16 random bytes as 32 hex characters (within the contract's 16 to 64
32
+ * `[A-Za-z0-9_-]`). Random per submission, so it identifies a submission, never a visitor.
33
+ */
34
+ function newIdempotencyKey() {
35
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
36
+ return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
37
+ }
38
+ /**
39
+ * A stable fingerprint of submitted values (keys sorted, so `{a, b}` and `{b, a}` match), used to
40
+ * reuse an idempotency key only for a retry of the same values. `undefined` when the values cannot
41
+ * be serialized (a cycle from untyped code), which never matches, so such a retry gets a new key.
42
+ */
43
+ function fingerprintOf(values) {
44
+ try {
45
+ if (typeof values !== "object" || values === null)
46
+ return JSON.stringify(values);
47
+ const record = values;
48
+ return JSON.stringify(Object.keys(record)
49
+ .sort()
50
+ .map((key) => [key, record[key]]));
51
+ }
52
+ catch {
53
+ return undefined;
54
+ }
55
+ }
56
+ /** A failed result the SDK decides locally (nothing sent). */
57
+ function localFailure(code) {
58
+ return { ok: false, code, fields: [] };
59
+ }
60
+ /**
61
+ * Submits to one declared collection of the component.
62
+ *
63
+ * Inputs: the render context (`render`'s second argument; its type carries the component's `data`
64
+ * declaration) and the name of a collection declared in `data.collections` — an undeclared name
65
+ * is a compile error. Output: `TaggorData` with `status`, `error`, `fieldErrors`, `submit(values,
66
+ * { turnstileToken, honeypot })` and `reset()`; `values` are typed from the collection's fields
67
+ * (required ones mandatory). Errors: none; every failure is an outcome: `unavailable` without a
68
+ * transport, `collection_not_found` (sent nowhere, debug warning) for a collection the manifest
69
+ * does not declare, `consent_required` (sent nowhere) for a component with `data.personal` when
70
+ * `options.consent` is not a proof `useConsent` issued for this component (TAG-212; a proof built by
71
+ * hand is refused, TAG-237), `network_error`, or the server's code with
72
+ * its field messages; a given `consent` travels in the body. A `consent_required` answer that blames
73
+ * a `consent.*` field (the site has no privacy policy URL saved, or the page's differs from it) comes
74
+ * out as `consent_unverifiable` (TAG-728): the visitor is told the form is not available, the debug
75
+ * console says how to fix it. Side effects:
76
+ * one transport `submit` per accepted call; the idempotency key is kept only to retry the same
77
+ * values after a `network_error` (the server may have received them) and dropped once the server
78
+ * answers or the values change. TAG-317: the host's `signal` goes to the transport, so unmounting
79
+ * the component cancels a submission in flight; it resolves to `network_error` and, the component
80
+ * being gone, no state is shown. Whatever the transport resolves to is normalized (unknown codes
81
+ * and malformed results become `network_error`).
82
+ */
83
+ export function useTaggorData(context, collection) {
84
+ const host = useContext(HostContext);
85
+ const [state, setState] = useState(IDLE);
86
+ // The first render's time: `_t` is measured from when the visitor could first see the form, and
87
+ // starts over when the Preact component mounts again. The server only marks a submission with a
88
+ // small `_t` as suspect (TAG-730), so a re-mounted multi-step form loses nothing by it.
89
+ const [renderedAt] = useState(Date.now);
90
+ const inFlight = useRef(undefined);
91
+ // The key of a submission that ended in network_error, and the values it carried.
92
+ const retry = useRef(undefined);
93
+ const send = async (values, options) => {
94
+ const data = context.manifest.data;
95
+ const declared = data?.collections;
96
+ if (declared === undefined || !Object.hasOwn(declared, collection)) {
97
+ debugWarn(host, `submit to "${collection}" ignored: not declared in data.collections`);
98
+ return localFailure("collection_not_found");
99
+ }
100
+ const { turnstileToken, honeypot, consent } = options;
101
+ // Personal data never leaves the page without the visitor's consent (TAG-212), proven by a
102
+ // proof `useConsent` issued, never one the component wrote itself (TAG-237).
103
+ if (data?.personal === true && !issuedProofs.has(consent))
104
+ return localFailure("consent_required");
105
+ const transport = host?.transport;
106
+ if (transport === undefined)
107
+ return localFailure("unavailable");
108
+ // Reuse the key only to retry the very same values: the server may have stored them already.
109
+ // Changed values are a new submission, which an old key would make the server drop as a replay.
110
+ const fingerprint = fingerprintOf(values);
111
+ const previous = retry.current;
112
+ const key = previous !== undefined &&
113
+ previous.fingerprint !== undefined &&
114
+ previous.fingerprint === fingerprint
115
+ ? previous.key
116
+ : newIdempotencyKey();
117
+ retry.current = undefined;
118
+ // The transport is untrusted to keep its contract: normalize whatever it resolves to.
119
+ const result = normalizeSubmitResult(await transport.submit(collection, {
120
+ data: values,
121
+ idempotency_key: key,
122
+ _t: Math.max(0, Math.round(Date.now() - renderedAt)),
123
+ ...(turnstileToken ? { turnstile_token: turnstileToken } : {}),
124
+ ...(honeypot === undefined ? {} : { _hp: honeypot }),
125
+ ...(consent ? { consent } : {}),
126
+ }, host?.signal));
127
+ if (!result.ok && result.code === "network_error")
128
+ retry.current = { key, fingerprint };
129
+ return result;
130
+ };
131
+ const run = async (values, options) => {
132
+ setState(SUBMITTING);
133
+ let result;
134
+ try {
135
+ result = await send(values, options);
136
+ }
137
+ catch {
138
+ // A transport that breaks its contract (throws or rejects) counts as a failed request.
139
+ result = localFailure("network_error");
140
+ }
141
+ if (result.ok) {
142
+ setState(SUCCESS);
143
+ return { ok: true };
144
+ }
145
+ // `consent_required` with a `consent.*` field is the site's setup (no privacy policy URL saved,
146
+ // or one that differs from the page's), not the visitor's box: say "not available" (TAG-728).
147
+ const code = result.code === "consent_required" &&
148
+ result.fields.some((field) => field.path.startsWith("consent."))
149
+ ? "consent_unverifiable"
150
+ : result.code;
151
+ if (code === "consent_unverifiable")
152
+ debugWarn(host, "consent: run taggor sites props set");
153
+ const error = { code, message: errorMessage(code, messageLocale(host?.element)) };
154
+ const firstPerField = new Map();
155
+ for (const { path, message } of result.fields) {
156
+ if (!firstPerField.has(path))
157
+ firstPerField.set(path, message);
158
+ }
159
+ // fromEntries defines own properties, so a `__proto__` path cannot reach a prototype.
160
+ const fieldErrors = Object.freeze(Object.fromEntries(firstPerField));
161
+ setState({ status: "error", error, fieldErrors });
162
+ return { ok: false, error, fieldErrors };
163
+ };
164
+ const submit = (values, options = {}) => {
165
+ if (inFlight.current !== undefined)
166
+ return inFlight.current;
167
+ const promise = run(values, options).finally(() => {
168
+ inFlight.current = undefined;
169
+ });
170
+ inFlight.current = promise;
171
+ return promise;
172
+ };
173
+ const reset = () => {
174
+ if (inFlight.current === undefined)
175
+ setState(IDLE);
176
+ };
177
+ return { ...state, submit, reset };
178
+ }
179
+ //# sourceMappingURL=use-taggor-data.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-taggor-data.js","sourceRoot":"","sources":["../src/use-taggor-data.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI5D,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAsD3D,MAAM,eAAe,GAAgB,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AACvD,MAAM,IAAI,GAAU,MAAM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ACD,sFAAsF;QACtF,MAAM,WAAW,GAAgB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC,CAAC;QAClF,QAAQ,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;QAClD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC;IAC3C,CAAC,CAAC;IAEF,MAAM,MAAM,GAAG,CAAC,MAA0B,EAAE,UAAyB,EAAE,EAAE,EAAE;QACzE,IAAI,QAAQ,CAAC,OAAO,KAAK,SAAS;YAAE,OAAO,QAAQ,CAAC,OAAO,CAAC;QAC5D,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE;YAChD,QAAQ,CAAC,OAAO,GAAG,SAAS,CAAC;QAC/B,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC;QAC3B,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAEF,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,IAAI,QAAQ,CAAC,OAAO,KAAK,SAAS;YAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;IACrD,CAAC,CAAC;IAEF,OAAO,EAAE,GAAG,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AACrC,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { DataAnchor, TrackName, TrackProps } from "./data-declaration.js";
2
+ /** Tracks one declared interaction; `props` must be declared props of that interaction. */
3
+ export type Track<D> = <K extends TrackName<D>>(name: K, props?: TrackProps<D, K>) => void;
4
+ /**
5
+ * Gives the component its `track` function.
6
+ *
7
+ * Inputs: the render context (`render`'s second argument; its type carries the component's `data`
8
+ * declaration). Output: `track(name, props?)`, typed so only names in `data.track` and their
9
+ * declared props compile; the same function for the same host and manifest across renders.
10
+ * Errors: none, ever: an undeclared name or an undeclared or invalid prop is dropped (warned in
11
+ * debug mode), and a component without a transport tracks nothing. Side effects: at most one
12
+ * transport `track` call per accepted call (TAG-820: the transport samples it by
13
+ * `eventSampleRate` and caps it per page; the beacon holds only the kind, the version hash and the
14
+ * declared name, props stay in the page).
15
+ */
16
+ export declare function useTrack<D>(context: DataAnchor<D>): Track<D>;
17
+ //# sourceMappingURL=use-track.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-track.d.ts","sourceRoot":"","sources":["../src/use-track.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAI/E,2FAA2F;AAC3F,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC;AA0C3F;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAqB5D"}
@@ -0,0 +1,64 @@
1
+ import { checkValue } from "@taggor/contract/zod-free";
2
+ import { useContext, useMemo } from "preact/hooks";
3
+ import { debugWarn } from "./debug.js";
4
+ import { HostContext } from "./host.js";
5
+ /**
6
+ * Builds the request for `name`, keeping only declared, valid props; warns about the rest.
7
+ * Returns `undefined` when `name` is not declared.
8
+ */
9
+ function trackRequest(host, manifest, name, props) {
10
+ const declared = manifest.data?.track;
11
+ const declaration = declared !== undefined && Object.hasOwn(declared, name) ? declared[name] : undefined;
12
+ if (declaration === undefined) {
13
+ debugWarn(host, `track("${name}") ignored: not declared in data.track`);
14
+ return undefined;
15
+ }
16
+ const schemas = declaration.props ?? {};
17
+ const kept = [];
18
+ if (typeof props === "object" && props !== null) {
19
+ for (const [key, value] of Object.entries(props)) {
20
+ const schema = Object.hasOwn(schemas, key) ? schemas[key] : undefined;
21
+ const outcome = schema === undefined ? undefined : checkValue(schema, value);
22
+ if (outcome?.ok === true)
23
+ kept.push([key, outcome.value]);
24
+ else
25
+ debugWarn(host, `track("${name}"): prop "${key}" dropped (not declared or invalid)`);
26
+ }
27
+ }
28
+ const request = { component: manifest.tag, name };
29
+ // fromEntries defines own properties, so no key can reach a prototype.
30
+ return kept.length === 0 ? request : { ...request, props: Object.fromEntries(kept) };
31
+ }
32
+ /**
33
+ * Gives the component its `track` function.
34
+ *
35
+ * Inputs: the render context (`render`'s second argument; its type carries the component's `data`
36
+ * declaration). Output: `track(name, props?)`, typed so only names in `data.track` and their
37
+ * declared props compile; the same function for the same host and manifest across renders.
38
+ * Errors: none, ever: an undeclared name or an undeclared or invalid prop is dropped (warned in
39
+ * debug mode), and a component without a transport tracks nothing. Side effects: at most one
40
+ * transport `track` call per accepted call (TAG-820: the transport samples it by
41
+ * `eventSampleRate` and caps it per page; the beacon holds only the kind, the version hash and the
42
+ * declared name, props stay in the page).
43
+ */
44
+ export function useTrack(context) {
45
+ const host = useContext(HostContext);
46
+ const { manifest } = context;
47
+ return useMemo(() => (name, props) => {
48
+ try {
49
+ const request = trackRequest(host, manifest, name, props);
50
+ if (request !== undefined) {
51
+ host?.transport?.track({
52
+ ...request,
53
+ kind: "custom",
54
+ manifest,
55
+ host,
56
+ });
57
+ }
58
+ }
59
+ catch {
60
+ // Tracking is best-effort: a broken transport must never break the component or the page.
61
+ }
62
+ }, [host, manifest]);
63
+ }
64
+ //# sourceMappingURL=use-track.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-track.js","sourceRoot":"","sources":["../src/use-track.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGnD,OAAO,EAAE,SAAS,EAAE,M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,UAAU,IAAI,aAAa,GAAG,qCAAqC,CAAC,CAAC;QAC5F,CAAC;IACH,CAAC;IACD,MAAM,OAAO,GAAiB,EAAE,SAAS,EAAE,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;IAChE,uEAAuE;IACvE,OAAO,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;AACvF,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CAAI,OAAsB;IAChD,MAAM,IAAI,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IACrC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC7B,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;QACpB,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;YAC1D,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;gBAC1B,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC;oBACrB,GAAG,OAAO;oBACV,IAAI,EAAE,QAAQ;oBACd,QAAQ;oBACR,IAAI;iBACL,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,0FAA0F;QAC5F,CAAC;IACH,CAAC,EACD,CAAC,IAAI,EAAE,QAAQ,CAAC,CACjB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Type-level reading of declarative value schemas (TAG-202): `ValueOf<S>` is the TypeScript type
3
+ * of the values the contract's `checkValue` accepts for the schema `S` (`@taggor/contract`,
4
+ * value-check.ts). Props of type `array`/`object` and event `detail`s are typed with it, so the
5
+ * types an author sees are derived from the same declaration the runtime validates against,
6
+ * never from a separate analysis of the author's code (the old prototype's defect).
7
+ *
8
+ * TAG-227: `DeepReadonly` is what `PropsOf` wraps every prop value in, because the runtime hands
9
+ * props to `render` frozen in depth (TAG-304); `ValueOf` itself stays mutable, since event
10
+ * `detail`s and submitted values are objects the component builds.
11
+ *
12
+ * Types only: nothing here exists at runtime.
13
+ */
14
+ /** An object value schema reduced to what typing needs. */
15
+ interface ObjectShape {
16
+ readonly type: "object";
17
+ readonly fields: Readonly<Record<string, unknown>>;
18
+ readonly required?: readonly string[] | undefined;
19
+ }
20
+ /** Flattens an intersection so editors show one object type. */
21
+ type Simplify<T> = {
22
+ [K in keyof T]: T[K];
23
+ } & {};
24
+ /** Names listed in `required`, or `never` when the schema has none. */
25
+ type RequiredNames<S> = S extends {
26
+ readonly required: readonly (infer Name)[];
27
+ } ? Name & string : never;
28
+ /**
29
+ * The value of an object schema: fields named in `required` are always present, the others may be
30
+ * absent (never `undefined`: `checkValue` only copies own keys that hold an accepted value).
31
+ */
32
+ type ObjectValue<S extends ObjectShape> = Simplify<{
33
+ -readonly [K in keyof S["fields"] & RequiredNames<S>]: ValueOf<S["fields"][K]>;
34
+ } & {
35
+ -readonly [K in Exclude<keyof S["fields"], RequiredNames<S>>]?: ValueOf<S["fields"][K]>;
36
+ }>;
37
+ /**
38
+ * The type of a value described by the value schema `S` (any nesting level).
39
+ *
40
+ * `string` → `string`, `number` → `number`, `boolean` → `boolean`, `enum` → the union of its
41
+ * `values` (or `string` when they are not literal), `array` → `ValueOf<items>[]`, `object` →
42
+ * required and optional fields as above. Anything else is `unknown`.
43
+ */
44
+ export type ValueOf<S> = S extends {
45
+ readonly type: "string";
46
+ } ? string : S extends {
47
+ readonly type: "number";
48
+ } ? number : S extends {
49
+ readonly type: "boolean";
50
+ } ? boolean : S extends {
51
+ readonly type: "enum";
52
+ readonly values: readonly (infer V)[];
53
+ } ? V : S extends {
54
+ readonly type: "array";
55
+ readonly items: infer Items;
56
+ } ? ValueOf<Items>[] : S extends ObjectShape ? ObjectValue<S> : unknown;
57
+ /**
58
+ * `T` read-only at every level: arrays become `readonly` arrays of read-only items and every
59
+ * object field becomes `readonly` (optional fields stay optional). Primitives are unchanged.
60
+ *
61
+ * It mirrors the contract's `deepFreeze`, which the runtime applies to the props it renders with
62
+ * (TAG-304): writing to a frozen prop (`props.items.push(…)`, `props.link.label = …`) throws a
63
+ * `TypeError` in the browser and sends the component to its fallback, so the compiler refuses it.
64
+ */
65
+ export type DeepReadonly<T> = T extends readonly (infer Item)[] ? readonly DeepReadonly<Item>[] : T extends object ? {
66
+ readonly [K in keyof T]: DeepReadonly<T[K]>;
67
+ } : T;
68
+ export {};
69
+ //# sourceMappingURL=value-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-types.d.ts","sourceRoot":"","sources":["../src/value-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,2DAA2D;AAC3D,UAAU,WAAW;IACnB,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC;CACnD;AAED,gEAAgE;AAChE,KAAK,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAEjD,uEAAuE;AACvE,KAAK,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,MAAM,IAAI,CAAC,EAAE,CAAA;CAAE,GAC5E,IAAI,GAAG,MAAM,GACb,KAAK,CAAC;AAEV;;;GAGG;AACH,KAAK,WAAW,CAAC,CAAC,SAAS,WAAW,IAAI,QAAQ,CAChD;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,C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,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAC/C,CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Type-level reading of declarative value schemas (TAG-202): `ValueOf<S>` is the TypeScript type
3
+ * of the values the contract's `checkValue` accepts for the schema `S` (`@taggor/contract`,
4
+ * value-check.ts). Props of type `array`/`object` and event `detail`s are typed with it, so the
5
+ * types an author sees are derived from the same declaration the runtime validates against,
6
+ * never from a separate analysis of the author's code (the old prototype's defect).
7
+ *
8
+ * TAG-227: `DeepReadonly` is what `PropsOf` wraps every prop value in, because the runtime hands
9
+ * props to `render` frozen in depth (TAG-304); `ValueOf` itself stays mutable, since event
10
+ * `detail`s and submitted values are objects the component builds.
11
+ *
12
+ * Types only: nothing here exists at runtime.
13
+ */
14
+ export {};
15
+ //# sourceMappingURL=value-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-types.js","sourceRoot":"","sources":["../src/value-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Whether code runs inside a visitor's own action in the component (TAG-237): `useConsent` records
3
+ * the consent only then, so a component cannot tick it by itself with `el.click()`, `dispatchEvent`,
4
+ * a timer or an effect (threat model T-62 and §6, the synthetic click the static gate of TAG-1706
5
+ * lets through).
6
+ *
7
+ * `window.event` is deprecated and not set inside a shadow root in every engine, so the component's
8
+ * shadow root records, in its capture phase (before any listener of the component's own elements),
9
+ * the last event of the types a visitor acts with. A call is inside an action when that event is
10
+ * trusted and still being dispatched (`eventPhase` not `NONE`): synchronously in its listeners.
11
+ *
12
+ * Two kinds of type, because the browser marks some events trusted even when a script started them
13
+ * (Chromium, Firefox and WebKit alike): `el.click()` on a checkbox fires an untrusted `click`
14
+ * followed by a trusted `input` and `change`, and `form.requestSubmit()` a trusted `submit`.
15
+ * - a cause (`click`, `keydown`, `keyup`, `pointerup`) counts when it is trusted; an untrusted one
16
+ * also disarms the consequences that follow it;
17
+ * - a consequence (`input`, `change`, `submit`) counts when it is trusted and a trusted cause was
18
+ * seen in this root less than `CONSEQUENCE_WINDOW_MS` before, with no untrusted cause since. The
19
+ * window only has to cover one task (a click and the `change` it fires, a key and the text it
20
+ * types); a later trusted consequence is no stronger than calling `setGiven` in that cause.
21
+ *
22
+ * Scope (re-review of TAG-237): this stops the naive use (a synthetic click, a timer, an
23
+ * effect), not a component that subverts its own realm on purpose. It runs inside the untrusted
24
+ * bundle: replacing `Function.prototype.call` at any time, `EventTarget.prototype.addEventListener`
25
+ * before the first render, or the `eventPhase` getter and `Date.now` before this module loads, or
26
+ * reaching Preact's hook state, gets past it. Against that, the gate, the AUP and the server-side
27
+ * check of the proof (TAG-703) apply; threat model §6, residual 3.
28
+ *
29
+ * Accepted residual (security review B3 of TAG-237): the listeners end with the host's signal,
30
+ * when the component unmounts, not with the component. A component that calls `useConsent` and is
31
+ * unmounted and mounted again while the component stays on the page (a conditional consent step)
32
+ * adds seven more listeners each time; the old ones only keep recording into their own closure.
33
+ * Ending them per component (an `AbortController` aborted in an effect's cleanup) measured +38 B
34
+ * gzip in every consent component, more than the ~30 B the review allowed.
35
+ */
36
+ /** How long after a trusted cause a trusted consequence still belongs to it, in milliseconds. */
37
+ export declare const CONSEQUENCE_WINDOW_MS = 1000;
38
+ /**
39
+ * Starts recording the visitor's actions in `root` and returns the check.
40
+ *
41
+ * Inputs: the component's shadow root (any event target; `undefined` outside a mounted component) and
42
+ * the signal that ends the recording (the host's, aborted when the component unmounts; absent, it
43
+ * lasts as long as the root). Output: `inVisitorAction()`, true only when called synchronously
44
+ * inside the dispatch of a trusted visitor action in `root`, as the module header defines; always
45
+ * false without a root. Errors: none. Side effects: seven capture listeners on `root`; reads
46
+ * the clock (`Date.now` as it was when the module loaded) for each recorded event.
47
+ */
48
+ export declare function watchVisitorActions(root: EventTarget | undefined, signal: AbortSignal | undefined): () => boolean;
49
+ //# sourceMappingURL=visitor-action.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor-action.d.ts","sourceRoot":"","sources":["../src/visitor-action.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAwBH,iGAAiG;AACjG,eAAO,MAAM,qBAAqB,OAAO,CAAC;AAE1C;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,MAAM,OAAO,CAYf"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Whether code runs inside a visitor's own action in the component (TAG-237): `useConsent` records
3
+ * the consent only then, so a component cannot tick it by itself with `el.click()`, `dispatchEvent`,
4
+ * a timer or an effect (threat model T-62 and §6, the synthetic click the static gate of TAG-1706
5
+ * lets through).
6
+ *
7
+ * `window.event` is deprecated and not set inside a shadow root in every engine, so the component's
8
+ * shadow root records, in its capture phase (before any listener of the component's own elements),
9
+ * the last event of the types a visitor acts with. A call is inside an action when that event is
10
+ * trusted and still being dispatched (`eventPhase` not `NONE`): synchronously in its listeners.
11
+ *
12
+ * Two kinds of type, because the browser marks some events trusted even when a script started them
13
+ * (Chromium, Firefox and WebKit alike): `el.click()` on a checkbox fires an untrusted `click`
14
+ * followed by a trusted `input` and `change`, and `form.requestSubmit()` a trusted `submit`.
15
+ * - a cause (`click`, `keydown`, `keyup`, `pointerup`) counts when it is trusted; an untrusted one
16
+ * also disarms the consequences that follow it;
17
+ * - a consequence (`input`, `change`, `submit`) counts when it is trusted and a trusted cause was
18
+ * seen in this root less than `CONSEQUENCE_WINDOW_MS` before, with no untrusted cause since. The
19
+ * window only has to cover one task (a click and the `change` it fires, a key and the text it
20
+ * types); a later trusted consequence is no stronger than calling `setGiven` in that cause.
21
+ *
22
+ * Scope (re-review of TAG-237): this stops the naive use (a synthetic click, a timer, an
23
+ * effect), not a component that subverts its own realm on purpose. It runs inside the untrusted
24
+ * bundle: replacing `Function.prototype.call` at any time, `EventTarget.prototype.addEventListener`
25
+ * before the first render, or the `eventPhase` getter and `Date.now` before this module loads, or
26
+ * reaching Preact's hook state, gets past it. Against that, the gate, the AUP and the server-side
27
+ * check of the proof (TAG-703) apply; threat model §6, residual 3.
28
+ *
29
+ * Accepted residual (security review B3 of TAG-237): the listeners end with the host's signal,
30
+ * when the component unmounts, not with the component. A component that calls `useConsent` and is
31
+ * unmounted and mounted again while the component stays on the page (a conditional consent step)
32
+ * adds seven more listeners each time; the old ones only keep recording into their own closure.
33
+ * Ending them per component (an `AbortController` aborted in an effect's cleanup) measured +38 B
34
+ * gzip in every consent component, more than the ~30 B the review allowed.
35
+ */
36
+ /** The event types a visitor acts with: the causes first, then the consequences. */
37
+ const VISITOR_ACTIONS = ["click", "keydown", "keyup", "pointerup", "input", "change", "submit"];
38
+ /** How many of `VISITOR_ACTIONS`, from the start, are causes. */
39
+ const CAUSES = 4;
40
+ /**
41
+ * The browser's own `eventPhase` getter, read when the module loads (security review B1 of
42
+ * TAG-237): a getter put on `Event.prototype` later cannot make an old event look dispatched.
43
+ * Missing (a DOM without it): every check is false.
44
+ */
45
+ const eventPhase = Object.getOwnPropertyDescriptor(Event.prototype, "eventPhase")?.get;
46
+ /**
47
+ * The clock, read when the module loads for the same reason: a `Date.now` replaced later cannot
48
+ * stretch or fake the window.
49
+ */
50
+ const now = Date.now.bind(Date);
51
+ /** How long after a trusted cause a trusted consequence still belongs to it, in milliseconds. */
52
+ export const CONSEQUENCE_WINDOW_MS = 1000;
53
+ /**
54
+ * Starts recording the visitor's actions in `root` and returns the check.
55
+ *
56
+ * Inputs: the component's shadow root (any event target; `undefined` outside a mounted component) and
57
+ * the signal that ends the recording (the host's, aborted when the component unmounts; absent, it
58
+ * lasts as long as the root). Output: `inVisitorAction()`, true only when called synchronously
59
+ * inside the dispatch of a trusted visitor action in `root`, as the module header defines; always
60
+ * false without a root. Errors: none. Side effects: seven capture listeners on `root`; reads
61
+ * the clock (`Date.now` as it was when the module loaded) for each recorded event.
62
+ */
63
+ export function watchVisitorActions(root, signal) {
64
+ // -Infinity is "no trusted cause": no clock value is ever within the window after it.
65
+ let causeAt = -Infinity;
66
+ let current;
67
+ for (const [index, type] of VISITOR_ACTIONS.entries()) {
68
+ const record = (event) => {
69
+ if (index < CAUSES)
70
+ causeAt = event.isTrusted ? now() : -Infinity;
71
+ current = event.isTrusted && now() - causeAt < CONSEQUENCE_WINDOW_MS ? event : undefined;
72
+ };
73
+ root?.addEventListener(type, record, { capture: true, signal });
74
+ }
75
+ return () => !!(current && eventPhase?.call(current));
76
+ }
77
+ //# sourceMappingURL=visitor-action.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor-action.js","sourceRoot":"","sources":["../src/visitor-action.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,oFAAoF;AACpF,MAAM,eAAe,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;AAEhG,iEAAiE;AACjE,MAAM,MAAM,GAAG,CAAC,CAAC;AAEjB;;;;GAIG;AACH,MAAM,UAAU,GACd,MAAM,CAAC,wBAAwB,CAAC,KAAK,CAAC,SAAS,EAAE,YAAY,CAE9D,EAAE,GAAG,CAAC;AAEP;;;GAGG;AACH,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEhC,iGAAiG;AACjG,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAE1C;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CACjC,IAA6B,EAC7B,MAA+B;IAE/B,sFAAsF;IACtF,IAAI,OAAO,GAAG,CAAC,QAAQ,CAAC;IACxB,IAAI,OAA0B,CAAC;IAC/B,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,eAAe,CAAC,OAAO,EAAE,EAAE,CAAC;QACtD,MAAM,MAAM,GAAG,CAAC,KAAY,EAAE,EAAE;YAC9B,IAAI,KAAK,GAAG,MAAM;gBAAE,OAAO,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;YAClE,OAAO,GAAG,KAAK,CAAC,SAAS,IAAI,GAAG,EAAE,GAAG,OAAO,GAAG,qBAAqB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;QAC3F,CAAC,CAAC;QACF,IAAI,EAAE,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAA6B,CAAC,CAAC;IAC7F,CAAC;IACD,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;AACxD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@taggor/component",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Authoring SDK: defineTaggorComponent, defineLibrary, typed props and events, useHost, useSlot, the data hooks useTaggorData and useTrack with their transports, useConsent and <Consent>, useLocale with message catalogs and Intl helpers, useFont and the asset import types (@taggor/component/assets); extractManifest for the builder; mountComponent for authors' tests (@taggor/component/testing).",
5
+ "keywords": [
6
+ "taggor",
7
+ "sdk",
8
+ "web-components",
9
+ "preact",
10
+ "shadow-dom",
11
+ "components"
12
+ ],
13
+ "license": "MIT",
14
+ "homepage": "https://taggor.dev/docs/",
15
+ "engines": {
16
+ "node": ">=20"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ },
28
+ "./build": {
29
+ "types": "./dist/build/index.d.ts",
30
+ "default": "./dist/build/index.js"
31
+ },
32
+ "./testing": {
33
+ "types": "./dist/testing/index.d.ts",
34
+ "default": "./dist/testing/index.js"
35
+ },
36
+ "./data-module": {
37
+ "types": "./dist/data-client.d.ts",
38
+ "default": "./dist/data-client.js"
39
+ },
40
+ "./assets": {
41
+ "types": "./assets.d.ts"
42
+ }
43
+ },
44
+ "files": [
45
+ "dist",
46
+ "assets.d.ts"
47
+ ],
48
+ "dependencies": {
49
+ "@taggor/contract": "0.1.0",
50
+ "@taggor/runtime": "0.1.0"
51
+ },
52
+ "peerDependencies": {
53
+ "preact": "10.29.8"
54
+ },
55
+ "devDependencies": {
56
+ "@size-limit/esbuild": "14.2.0",
57
+ "@size-limit/file": "14.2.0",
58
+ "@taggor/config": "0.0.0",
59
+ "@types/node": "26.6.5",
60
+ "ajv": "8.20.0",
61
+ "esbuild": "0.28.2",
62
+ "happy-dom": "21.0.4",
63
+ "preact": "10.29.8",
64
+ "size-limit": "14.2.0"
65
+ },
66
+ "scripts": {
67
+ "build": "tsc -b tsconfig.build.json",
68
+ "typecheck": "tsc -p tsconfig.json",
69
+ "lint": "eslint . --max-warnings 0",
70
+ "test": "vitest run && size-limit --config size-limit.config.js",
71
+ "test:coverage": "vitest run --coverage && size-limit --config size-limit.config.js",
72
+ "size": "size-limit --config size-limit.config.js"
73
+ }
6
74
  }