@vllnt/convex-analytics 0.1.0 → 0.1.1-canary.879bde1

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 (46) hide show
  1. package/CHANGELOG.md +125 -0
  2. package/README.md +99 -38
  3. package/dist/component/crons.js +1 -1
  4. package/dist/component/crons.js.map +1 -1
  5. package/dist/component/http.d.ts +5 -0
  6. package/dist/component/http.d.ts.map +1 -1
  7. package/dist/component/http.js +76 -21
  8. package/dist/component/http.js.map +1 -1
  9. package/dist/component/internal_mutations.js +4 -4
  10. package/dist/component/internal_mutations.js.map +1 -1
  11. package/dist/component/mutations.d.ts.map +1 -1
  12. package/dist/component/mutations.js +28 -13
  13. package/dist/component/mutations.js.map +1 -1
  14. package/dist/component/queries.d.ts.map +1 -1
  15. package/dist/component/queries.js +57 -58
  16. package/dist/component/queries.js.map +1 -1
  17. package/dist/component/schema.d.ts +2 -4
  18. package/dist/component/schema.js +10 -5
  19. package/dist/component/schema.js.map +1 -1
  20. package/dist/component/validators.d.ts +27 -0
  21. package/dist/component/validators.d.ts.map +1 -1
  22. package/dist/component/validators.js +54 -1
  23. package/dist/component/validators.js.map +1 -1
  24. package/dist/react/index.d.ts +1 -1
  25. package/dist/react/index.js +1 -1
  26. package/dist/test.d.ts +2 -4
  27. package/dist/web/index.d.ts +2 -2
  28. package/dist/web/index.js +2 -2
  29. package/docs/api-reference.md +132 -0
  30. package/docs/architecture.md +70 -0
  31. package/docs/client-sdk.md +249 -0
  32. package/docs/multi-product.md +69 -0
  33. package/docs/quick-start.md +115 -0
  34. package/docs/schema.md +135 -0
  35. package/docs/web-preset.md +104 -0
  36. package/llms.txt +37 -0
  37. package/package.json +39 -27
  38. package/src/component/crons.ts +1 -1
  39. package/src/component/http.ts +78 -21
  40. package/src/component/internal_mutations.ts +5 -5
  41. package/src/component/mutations.ts +37 -15
  42. package/src/component/queries.ts +83 -64
  43. package/src/component/schema.ts +10 -5
  44. package/src/component/validators.ts +80 -1
  45. package/src/react/index.tsx +1 -1
  46. package/src/web/index.ts +2 -2
@@ -1,4 +1,4 @@
1
- import { v } from "convex/values";
1
+ import { ConvexError, v } from "convex/values";
2
2
 
3
3
  /** A single property value — string, number, boolean, or null. No nested objects/arrays. */
4
4
  export const scalarValidator = v.union(
@@ -92,3 +92,82 @@ export const distributionView = v.object({
92
92
  count: v.number(),
93
93
  sum: v.number(),
94
94
  });
95
+
96
+ /** Stable machine-readable codes carried in `ConvexError.data.code`. */
97
+ export type AnalyticsErrorCode =
98
+ | "EMPTY_STRING"
99
+ | "INVALID_TIMESTAMP"
100
+ | "INVALID_SAMPLE_RATE"
101
+ | "INVALID_PROP_KEY"
102
+ | "INVALID_PROP_VALUE"
103
+ | "INVALID_COUNT"
104
+ | "INVALID_CURSOR"
105
+ | "INVALID_BUCKETS"
106
+ | "INVALID_CONFIG";
107
+
108
+ /** Typed input error: `ConvexError` whose data is `{ code, message }`. */
109
+ export function invalidInput(
110
+ code: AnalyticsErrorCode,
111
+ message: string,
112
+ ): ConvexError<{ code: AnalyticsErrorCode; message: string }> {
113
+ return new ConvexError({ code, message });
114
+ }
115
+
116
+ /** Reject an empty string. `undefined` (an omitted optional field) passes. */
117
+ export function assertNonEmpty(field: string, value: string | undefined): void {
118
+ if (value === "") {
119
+ throw invalidInput("EMPTY_STRING", `${field} must not be empty`);
120
+ }
121
+ }
122
+
123
+ /** Reject NaN and +/-Infinity epoch-millis values. `undefined` passes. */
124
+ export function assertFiniteTs(field: string, value: number | undefined): void {
125
+ if (value !== undefined && !Number.isFinite(value)) {
126
+ throw invalidInput("INVALID_TIMESTAMP", `${field} must be a finite number`);
127
+ }
128
+ }
129
+
130
+ /** Reject non-finite `from` / `to` bounds of an optional range. */
131
+ export function assertRange(range: { from?: number; to?: number } | undefined): void {
132
+ assertFiniteTs("range.from", range?.from);
133
+ assertFiniteTs("range.to", range?.to);
134
+ }
135
+
136
+ /** Reject anything but a positive integer. `undefined` passes. */
137
+ export function assertCount(field: string, value: number | undefined): void {
138
+ if (value !== undefined && !(Number.isInteger(value) && value > 0)) {
139
+ throw invalidInput("INVALID_COUNT", `${field} must be a positive integer`);
140
+ }
141
+ }
142
+
143
+ /** Reject a sample rate that is not a finite number in `[0, 1]`. `undefined` passes. */
144
+ export function assertSampleRate(value: number | undefined): void {
145
+ if (value !== undefined && !(value >= 0 && value <= 1)) {
146
+ throw invalidInput("INVALID_SAMPLE_RATE", "sampleRate must be a number between 0 and 1");
147
+ }
148
+ }
149
+
150
+ /** Convex field names: 1-1024 printable ASCII characters, not starting with `$` (or empty). */
151
+ const PROP_KEY = /^(?!\$)[\x20-\x7e]{0,1024}$/;
152
+
153
+ /**
154
+ * Reject NaN / +/-Infinity numbers and prop keys Convex cannot store (non-ASCII, control
155
+ * characters, leading `$`, over 1024 characters). Convex rejects bad keys before a mutation
156
+ * runs, so the key check matters for callers that validate first, such as the HTTP route.
157
+ */
158
+ export function assertProps(props: Record<string, unknown>): void {
159
+ for (const [key, value] of Object.entries(props)) {
160
+ if (!PROP_KEY.test(key)) {
161
+ throw invalidInput(
162
+ "INVALID_PROP_KEY",
163
+ "props keys must be printable ASCII, at most 1024 characters, and not start with $",
164
+ );
165
+ }
166
+ if (value !== null && typeof value === "object") {
167
+ throw invalidInput("INVALID_PROP_VALUE", `props.${key} must be a scalar, not an object or array`);
168
+ }
169
+ if (typeof value === "number" && !Number.isFinite(value)) {
170
+ throw invalidInput("INVALID_PROP_VALUE", `props.${key} must be a finite number`);
171
+ }
172
+ }
173
+ }
@@ -4,7 +4,7 @@
4
4
  * Thin, tree-shakeable wrappers over `convex/react`'s `useQuery` for the
5
5
  * component's **aggregate** read verbs (metric, timeseries, top, uniques).
6
6
  * The backend core installs and runs with zero React — these hooks are an
7
- * opt-in `./react` entry; `react` and `convex` are optional peer deps.
7
+ * opt-in `./react` entry; `react` is an optional peer dep.
8
8
  *
9
9
  * No-leak: aggregates ONLY (no raw events) and every call is scope-gated, so a
10
10
  * hook can never surface another scope's rows or a raw event payload on the
package/src/web/index.ts CHANGED
@@ -173,7 +173,7 @@ export interface TrackPageviewOpts {
173
173
  * Host-side convenience: the component never sees web concepts — this helper
174
174
  * just assembles the `{@link webDimensions}`-shaped props and calls
175
175
  * `client.track(ctx, "page_view", { props })`. Pair with
176
- * `dimensions: webDimensions` on the client to get web rollups.
176
+ * `dimensions: [...webDimensions]` on the client to get web rollups.
177
177
  *
178
178
  * @typeParam C - The `AnalyticsClient` (structural) type.
179
179
  * @param client - An `AnalyticsClient` (or compatible) instance.
@@ -182,7 +182,7 @@ export interface TrackPageviewOpts {
182
182
  * @returns The `track` result passed through from the client.
183
183
  * @example
184
184
  * ```ts
185
- * const analytics = new AnalyticsClient(components.analytics, { dimensions: webDimensions });
185
+ * const analytics = new AnalyticsClient(components.analytics, { dimensions: [...webDimensions] });
186
186
  * await trackPageview(analytics, ctx, {
187
187
  * path: "/pricing",
188
188
  * ua: request.headers.get("user-agent") ?? undefined,