@storewatch/schema 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/dist/chunk-MEHWUFJG.js +71 -0
- package/dist/constants.cjs +103 -0
- package/dist/constants.d.cts +43 -0
- package/dist/constants.d.ts +43 -0
- package/dist/constants.js +22 -0
- package/dist/envelope.d.cts +114 -0
- package/dist/envelope.d.ts +114 -0
- package/dist/events.d.cts +135 -0
- package/dist/events.d.ts +135 -0
- package/dist/index.cjs +312 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +217 -0
- package/dist/primitives.d.cts +16 -0
- package/dist/primitives.d.ts +16 -0
- package/package.json +50 -3
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type CanonicalEventName } from "./constants.cjs";
|
|
3
|
+
export declare const CANONICAL_PROPERTY_SCHEMAS: {
|
|
4
|
+
page_viewed: z.ZodObject<{
|
|
5
|
+
pageType: z.ZodOptional<z.ZodString>;
|
|
6
|
+
}, z.core.$loose>;
|
|
7
|
+
product_viewed: z.ZodObject<{
|
|
8
|
+
productId: z.ZodString;
|
|
9
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
10
|
+
name: z.ZodOptional<z.ZodString>;
|
|
11
|
+
price: z.ZodOptional<z.ZodObject<{
|
|
12
|
+
amount: z.ZodNumber;
|
|
13
|
+
currency: z.ZodString;
|
|
14
|
+
}, z.core.$strict>>;
|
|
15
|
+
}, z.core.$loose>;
|
|
16
|
+
collection_viewed: z.ZodObject<{
|
|
17
|
+
collectionId: z.ZodString;
|
|
18
|
+
name: z.ZodOptional<z.ZodString>;
|
|
19
|
+
}, z.core.$loose>;
|
|
20
|
+
search_performed: z.ZodObject<{
|
|
21
|
+
query: z.ZodString;
|
|
22
|
+
resultCount: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
}, z.core.$loose>;
|
|
24
|
+
add_to_cart: z.ZodObject<{
|
|
25
|
+
productId: z.ZodString;
|
|
26
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
27
|
+
name: z.ZodOptional<z.ZodString>;
|
|
28
|
+
quantity: z.ZodNumber;
|
|
29
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
30
|
+
price: z.ZodOptional<z.ZodObject<{
|
|
31
|
+
amount: z.ZodNumber;
|
|
32
|
+
currency: z.ZodString;
|
|
33
|
+
}, z.core.$strict>>;
|
|
34
|
+
cartValue: z.ZodOptional<z.ZodObject<{
|
|
35
|
+
amount: z.ZodNumber;
|
|
36
|
+
currency: z.ZodString;
|
|
37
|
+
}, z.core.$strict>>;
|
|
38
|
+
}, z.core.$loose>;
|
|
39
|
+
remove_from_cart: z.ZodObject<{
|
|
40
|
+
productId: z.ZodString;
|
|
41
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
42
|
+
quantity: z.ZodNumber;
|
|
43
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
44
|
+
cartValue: z.ZodOptional<z.ZodObject<{
|
|
45
|
+
amount: z.ZodNumber;
|
|
46
|
+
currency: z.ZodString;
|
|
47
|
+
}, z.core.$strict>>;
|
|
48
|
+
}, z.core.$loose>;
|
|
49
|
+
cart_updated: z.ZodObject<{
|
|
50
|
+
cartId: z.ZodString;
|
|
51
|
+
cartValue: z.ZodObject<{
|
|
52
|
+
amount: z.ZodNumber;
|
|
53
|
+
currency: z.ZodString;
|
|
54
|
+
}, z.core.$strict>;
|
|
55
|
+
itemCount: z.ZodNumber;
|
|
56
|
+
}, z.core.$loose>;
|
|
57
|
+
checkout_started: z.ZodObject<{
|
|
58
|
+
checkoutId: z.ZodString;
|
|
59
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
60
|
+
cartValue: z.ZodObject<{
|
|
61
|
+
amount: z.ZodNumber;
|
|
62
|
+
currency: z.ZodString;
|
|
63
|
+
}, z.core.$strict>;
|
|
64
|
+
}, z.core.$loose>;
|
|
65
|
+
checkout_step_viewed: z.ZodObject<{
|
|
66
|
+
checkoutId: z.ZodString;
|
|
67
|
+
step: z.ZodString;
|
|
68
|
+
}, z.core.$loose>;
|
|
69
|
+
customer_information_entered: z.ZodObject<{
|
|
70
|
+
checkoutId: z.ZodString;
|
|
71
|
+
}, z.core.$strict>;
|
|
72
|
+
shipping_selected: z.ZodObject<{
|
|
73
|
+
checkoutId: z.ZodString;
|
|
74
|
+
method: z.ZodOptional<z.ZodString>;
|
|
75
|
+
shippingCost: z.ZodOptional<z.ZodObject<{
|
|
76
|
+
amount: z.ZodNumber;
|
|
77
|
+
currency: z.ZodString;
|
|
78
|
+
}, z.core.$strict>>;
|
|
79
|
+
}, z.core.$loose>;
|
|
80
|
+
payment_started: z.ZodObject<{
|
|
81
|
+
checkoutId: z.ZodString;
|
|
82
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
83
|
+
}, z.core.$loose>;
|
|
84
|
+
payment_failed: z.ZodObject<{
|
|
85
|
+
checkoutId: z.ZodString;
|
|
86
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
87
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
88
|
+
amount: z.ZodOptional<z.ZodObject<{
|
|
89
|
+
amount: z.ZodNumber;
|
|
90
|
+
currency: z.ZodString;
|
|
91
|
+
}, z.core.$strict>>;
|
|
92
|
+
}, z.core.$loose>;
|
|
93
|
+
payment_authorized: z.ZodObject<{
|
|
94
|
+
checkoutId: z.ZodString;
|
|
95
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
96
|
+
amount: z.ZodObject<{
|
|
97
|
+
amount: z.ZodNumber;
|
|
98
|
+
currency: z.ZodString;
|
|
99
|
+
}, z.core.$strict>;
|
|
100
|
+
}, z.core.$loose>;
|
|
101
|
+
order_completed: z.ZodObject<{
|
|
102
|
+
orderId: z.ZodString;
|
|
103
|
+
checkoutId: z.ZodOptional<z.ZodString>;
|
|
104
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
105
|
+
total: z.ZodObject<{
|
|
106
|
+
amount: z.ZodNumber;
|
|
107
|
+
currency: z.ZodString;
|
|
108
|
+
}, z.core.$strict>;
|
|
109
|
+
items: z.ZodArray<z.ZodObject<{
|
|
110
|
+
productId: z.ZodString;
|
|
111
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
112
|
+
name: z.ZodOptional<z.ZodString>;
|
|
113
|
+
quantity: z.ZodNumber;
|
|
114
|
+
price: z.ZodObject<{
|
|
115
|
+
amount: z.ZodNumber;
|
|
116
|
+
currency: z.ZodString;
|
|
117
|
+
}, z.core.$strict>;
|
|
118
|
+
}, z.core.$loose>>;
|
|
119
|
+
}, z.core.$loose>;
|
|
120
|
+
order_cancelled: z.ZodObject<{
|
|
121
|
+
orderId: z.ZodString;
|
|
122
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
123
|
+
}, z.core.$loose>;
|
|
124
|
+
refund_created: z.ZodObject<{
|
|
125
|
+
orderId: z.ZodString;
|
|
126
|
+
amount: z.ZodObject<{
|
|
127
|
+
amount: z.ZodNumber;
|
|
128
|
+
currency: z.ZodString;
|
|
129
|
+
}, z.core.$strict>;
|
|
130
|
+
}, z.core.$loose>;
|
|
131
|
+
session_heartbeat: z.ZodObject<{}, z.core.$loose>;
|
|
132
|
+
};
|
|
133
|
+
export type CanonicalProperties = {
|
|
134
|
+
[K in CanonicalEventName]: z.infer<(typeof CANONICAL_PROPERTY_SCHEMAS)[K]>;
|
|
135
|
+
};
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type CanonicalEventName } from "./constants.js";
|
|
3
|
+
export declare const CANONICAL_PROPERTY_SCHEMAS: {
|
|
4
|
+
page_viewed: z.ZodObject<{
|
|
5
|
+
pageType: z.ZodOptional<z.ZodString>;
|
|
6
|
+
}, z.core.$loose>;
|
|
7
|
+
product_viewed: z.ZodObject<{
|
|
8
|
+
productId: z.ZodString;
|
|
9
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
10
|
+
name: z.ZodOptional<z.ZodString>;
|
|
11
|
+
price: z.ZodOptional<z.ZodObject<{
|
|
12
|
+
amount: z.ZodNumber;
|
|
13
|
+
currency: z.ZodString;
|
|
14
|
+
}, z.core.$strict>>;
|
|
15
|
+
}, z.core.$loose>;
|
|
16
|
+
collection_viewed: z.ZodObject<{
|
|
17
|
+
collectionId: z.ZodString;
|
|
18
|
+
name: z.ZodOptional<z.ZodString>;
|
|
19
|
+
}, z.core.$loose>;
|
|
20
|
+
search_performed: z.ZodObject<{
|
|
21
|
+
query: z.ZodString;
|
|
22
|
+
resultCount: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
}, z.core.$loose>;
|
|
24
|
+
add_to_cart: z.ZodObject<{
|
|
25
|
+
productId: z.ZodString;
|
|
26
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
27
|
+
name: z.ZodOptional<z.ZodString>;
|
|
28
|
+
quantity: z.ZodNumber;
|
|
29
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
30
|
+
price: z.ZodOptional<z.ZodObject<{
|
|
31
|
+
amount: z.ZodNumber;
|
|
32
|
+
currency: z.ZodString;
|
|
33
|
+
}, z.core.$strict>>;
|
|
34
|
+
cartValue: z.ZodOptional<z.ZodObject<{
|
|
35
|
+
amount: z.ZodNumber;
|
|
36
|
+
currency: z.ZodString;
|
|
37
|
+
}, z.core.$strict>>;
|
|
38
|
+
}, z.core.$loose>;
|
|
39
|
+
remove_from_cart: z.ZodObject<{
|
|
40
|
+
productId: z.ZodString;
|
|
41
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
42
|
+
quantity: z.ZodNumber;
|
|
43
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
44
|
+
cartValue: z.ZodOptional<z.ZodObject<{
|
|
45
|
+
amount: z.ZodNumber;
|
|
46
|
+
currency: z.ZodString;
|
|
47
|
+
}, z.core.$strict>>;
|
|
48
|
+
}, z.core.$loose>;
|
|
49
|
+
cart_updated: z.ZodObject<{
|
|
50
|
+
cartId: z.ZodString;
|
|
51
|
+
cartValue: z.ZodObject<{
|
|
52
|
+
amount: z.ZodNumber;
|
|
53
|
+
currency: z.ZodString;
|
|
54
|
+
}, z.core.$strict>;
|
|
55
|
+
itemCount: z.ZodNumber;
|
|
56
|
+
}, z.core.$loose>;
|
|
57
|
+
checkout_started: z.ZodObject<{
|
|
58
|
+
checkoutId: z.ZodString;
|
|
59
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
60
|
+
cartValue: z.ZodObject<{
|
|
61
|
+
amount: z.ZodNumber;
|
|
62
|
+
currency: z.ZodString;
|
|
63
|
+
}, z.core.$strict>;
|
|
64
|
+
}, z.core.$loose>;
|
|
65
|
+
checkout_step_viewed: z.ZodObject<{
|
|
66
|
+
checkoutId: z.ZodString;
|
|
67
|
+
step: z.ZodString;
|
|
68
|
+
}, z.core.$loose>;
|
|
69
|
+
customer_information_entered: z.ZodObject<{
|
|
70
|
+
checkoutId: z.ZodString;
|
|
71
|
+
}, z.core.$strict>;
|
|
72
|
+
shipping_selected: z.ZodObject<{
|
|
73
|
+
checkoutId: z.ZodString;
|
|
74
|
+
method: z.ZodOptional<z.ZodString>;
|
|
75
|
+
shippingCost: z.ZodOptional<z.ZodObject<{
|
|
76
|
+
amount: z.ZodNumber;
|
|
77
|
+
currency: z.ZodString;
|
|
78
|
+
}, z.core.$strict>>;
|
|
79
|
+
}, z.core.$loose>;
|
|
80
|
+
payment_started: z.ZodObject<{
|
|
81
|
+
checkoutId: z.ZodString;
|
|
82
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
83
|
+
}, z.core.$loose>;
|
|
84
|
+
payment_failed: z.ZodObject<{
|
|
85
|
+
checkoutId: z.ZodString;
|
|
86
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
87
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
88
|
+
amount: z.ZodOptional<z.ZodObject<{
|
|
89
|
+
amount: z.ZodNumber;
|
|
90
|
+
currency: z.ZodString;
|
|
91
|
+
}, z.core.$strict>>;
|
|
92
|
+
}, z.core.$loose>;
|
|
93
|
+
payment_authorized: z.ZodObject<{
|
|
94
|
+
checkoutId: z.ZodString;
|
|
95
|
+
provider: z.ZodOptional<z.ZodString>;
|
|
96
|
+
amount: z.ZodObject<{
|
|
97
|
+
amount: z.ZodNumber;
|
|
98
|
+
currency: z.ZodString;
|
|
99
|
+
}, z.core.$strict>;
|
|
100
|
+
}, z.core.$loose>;
|
|
101
|
+
order_completed: z.ZodObject<{
|
|
102
|
+
orderId: z.ZodString;
|
|
103
|
+
checkoutId: z.ZodOptional<z.ZodString>;
|
|
104
|
+
cartId: z.ZodOptional<z.ZodString>;
|
|
105
|
+
total: z.ZodObject<{
|
|
106
|
+
amount: z.ZodNumber;
|
|
107
|
+
currency: z.ZodString;
|
|
108
|
+
}, z.core.$strict>;
|
|
109
|
+
items: z.ZodArray<z.ZodObject<{
|
|
110
|
+
productId: z.ZodString;
|
|
111
|
+
variantId: z.ZodOptional<z.ZodString>;
|
|
112
|
+
name: z.ZodOptional<z.ZodString>;
|
|
113
|
+
quantity: z.ZodNumber;
|
|
114
|
+
price: z.ZodObject<{
|
|
115
|
+
amount: z.ZodNumber;
|
|
116
|
+
currency: z.ZodString;
|
|
117
|
+
}, z.core.$strict>;
|
|
118
|
+
}, z.core.$loose>>;
|
|
119
|
+
}, z.core.$loose>;
|
|
120
|
+
order_cancelled: z.ZodObject<{
|
|
121
|
+
orderId: z.ZodString;
|
|
122
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
123
|
+
}, z.core.$loose>;
|
|
124
|
+
refund_created: z.ZodObject<{
|
|
125
|
+
orderId: z.ZodString;
|
|
126
|
+
amount: z.ZodObject<{
|
|
127
|
+
amount: z.ZodNumber;
|
|
128
|
+
currency: z.ZodString;
|
|
129
|
+
}, z.core.$strict>;
|
|
130
|
+
}, z.core.$loose>;
|
|
131
|
+
session_heartbeat: z.ZodObject<{}, z.core.$loose>;
|
|
132
|
+
};
|
|
133
|
+
export type CanonicalProperties = {
|
|
134
|
+
[K in CanonicalEventName]: z.infer<(typeof CANONICAL_PROPERTY_SCHEMAS)[K]>;
|
|
135
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CANONICAL_EVENTS: () => CANONICAL_EVENTS,
|
|
24
|
+
CANONICAL_PROPERTY_SCHEMAS: () => CANONICAL_PROPERTY_SCHEMAS,
|
|
25
|
+
ContextSchema: () => ContextSchema,
|
|
26
|
+
CurrencySchema: () => CurrencySchema,
|
|
27
|
+
EPHEMERAL_EVENTS: () => EPHEMERAL_EVENTS,
|
|
28
|
+
EVENT_NAME_PATTERN: () => EVENT_NAME_PATTERN,
|
|
29
|
+
IdSchema: () => IdSchema,
|
|
30
|
+
LIMITS: () => LIMITS,
|
|
31
|
+
MoneySchema: () => MoneySchema,
|
|
32
|
+
PropertiesSchema: () => PropertiesSchema,
|
|
33
|
+
SERVER_AUTHORITATIVE_EVENTS: () => SERVER_AUTHORITATIVE_EVENTS,
|
|
34
|
+
ShortString: () => ShortString,
|
|
35
|
+
UrlString: () => UrlString,
|
|
36
|
+
WireBatchSchema: () => WireBatchSchema,
|
|
37
|
+
WireEventSchema: () => WireEventSchema,
|
|
38
|
+
currencyExponent: () => currencyExponent,
|
|
39
|
+
isCanonicalEvent: () => isCanonicalEvent,
|
|
40
|
+
jsonDepth: () => jsonDepth,
|
|
41
|
+
money: () => money,
|
|
42
|
+
toMajor: () => toMajor,
|
|
43
|
+
validateEvent: () => validateEvent
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
47
|
+
// src/constants.ts
|
|
48
|
+
var CANONICAL_EVENTS = [
|
|
49
|
+
"page_viewed",
|
|
50
|
+
"product_viewed",
|
|
51
|
+
"collection_viewed",
|
|
52
|
+
"search_performed",
|
|
53
|
+
"add_to_cart",
|
|
54
|
+
"remove_from_cart",
|
|
55
|
+
"cart_updated",
|
|
56
|
+
"checkout_started",
|
|
57
|
+
"checkout_step_viewed",
|
|
58
|
+
"customer_information_entered",
|
|
59
|
+
"shipping_selected",
|
|
60
|
+
"payment_started",
|
|
61
|
+
"payment_failed",
|
|
62
|
+
"payment_authorized",
|
|
63
|
+
"order_completed",
|
|
64
|
+
"order_cancelled",
|
|
65
|
+
"refund_created",
|
|
66
|
+
"session_heartbeat"
|
|
67
|
+
];
|
|
68
|
+
var canonicalSet = new Set(CANONICAL_EVENTS);
|
|
69
|
+
function isCanonicalEvent(name) {
|
|
70
|
+
return canonicalSet.has(name);
|
|
71
|
+
}
|
|
72
|
+
var SERVER_AUTHORITATIVE_EVENTS = /* @__PURE__ */ new Set([
|
|
73
|
+
"payment_failed",
|
|
74
|
+
"payment_authorized",
|
|
75
|
+
"order_completed",
|
|
76
|
+
"order_cancelled",
|
|
77
|
+
"refund_created"
|
|
78
|
+
]);
|
|
79
|
+
var EPHEMERAL_EVENTS = /* @__PURE__ */ new Set(["session_heartbeat"]);
|
|
80
|
+
var LIMITS = {
|
|
81
|
+
batchMaxEvents: 100,
|
|
82
|
+
batchMaxBytes: 256 * 1024,
|
|
83
|
+
propertiesMaxBytes: 4 * 1024,
|
|
84
|
+
propertiesMaxDepth: 4,
|
|
85
|
+
stringMaxLength: 1024,
|
|
86
|
+
urlMaxLength: 2048,
|
|
87
|
+
idMaxLength: 128,
|
|
88
|
+
sessionIdMaxLength: 64,
|
|
89
|
+
eventNameMaxLength: 64,
|
|
90
|
+
searchQueryMaxLength: 100,
|
|
91
|
+
orderMaxItems: 100
|
|
92
|
+
};
|
|
93
|
+
var EVENT_NAME_PATTERN = /^[a-z][a-z0-9_.]{0,63}$/;
|
|
94
|
+
function currencyExponent(currency) {
|
|
95
|
+
const digits = new Intl.NumberFormat("en", { style: "currency", currency }).resolvedOptions().maximumFractionDigits;
|
|
96
|
+
return digits ?? 2;
|
|
97
|
+
}
|
|
98
|
+
function money(major, currency) {
|
|
99
|
+
const code = currency.toUpperCase();
|
|
100
|
+
const factor = 10 ** currencyExponent(code);
|
|
101
|
+
return { amount: Math.round(major * factor), currency: code };
|
|
102
|
+
}
|
|
103
|
+
function toMajor(value) {
|
|
104
|
+
return value.amount / 10 ** currencyExponent(value.currency);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/primitives.ts
|
|
108
|
+
var import_zod = require("zod");
|
|
109
|
+
var knownCurrencies = new Set(Intl.supportedValuesOf("currency"));
|
|
110
|
+
var CurrencySchema = import_zod.z.string().regex(/^[A-Z]{3}$/, "must be an upper-case ISO-4217 code").refine((c) => knownCurrencies.has(c), "unknown ISO-4217 currency");
|
|
111
|
+
var MoneySchema = import_zod.z.strictObject({
|
|
112
|
+
amount: import_zod.z.number().int("money amounts are integer minor units").safe(),
|
|
113
|
+
currency: CurrencySchema
|
|
114
|
+
});
|
|
115
|
+
var IdSchema = import_zod.z.string().min(1).max(LIMITS.idMaxLength);
|
|
116
|
+
var ShortString = import_zod.z.string().max(LIMITS.stringMaxLength);
|
|
117
|
+
var UrlString = import_zod.z.string().max(LIMITS.urlMaxLength);
|
|
118
|
+
function jsonDepth(value) {
|
|
119
|
+
if (value === null || typeof value !== "object") return 0;
|
|
120
|
+
const children = Array.isArray(value) ? value : Object.values(value);
|
|
121
|
+
let max = 0;
|
|
122
|
+
for (const child of children) max = Math.max(max, jsonDepth(child));
|
|
123
|
+
return max + 1;
|
|
124
|
+
}
|
|
125
|
+
var JsonValueSchema = import_zod.z.lazy(
|
|
126
|
+
() => import_zod.z.union([
|
|
127
|
+
import_zod.z.string().max(LIMITS.stringMaxLength),
|
|
128
|
+
import_zod.z.number().refine(Number.isFinite, "must be finite"),
|
|
129
|
+
import_zod.z.boolean(),
|
|
130
|
+
import_zod.z.null(),
|
|
131
|
+
import_zod.z.array(JsonValueSchema),
|
|
132
|
+
import_zod.z.record(import_zod.z.string().max(LIMITS.eventNameMaxLength), JsonValueSchema)
|
|
133
|
+
])
|
|
134
|
+
);
|
|
135
|
+
var utf8 = new TextEncoder();
|
|
136
|
+
var PropertiesSchema = import_zod.z.record(import_zod.z.string().max(LIMITS.eventNameMaxLength), JsonValueSchema).superRefine((value, ctx) => {
|
|
137
|
+
if (jsonDepth(value) > LIMITS.propertiesMaxDepth) {
|
|
138
|
+
ctx.addIssue({ code: "custom", message: `nesting deeper than ${LIMITS.propertiesMaxDepth}` });
|
|
139
|
+
}
|
|
140
|
+
if (utf8.encode(JSON.stringify(value)).length > LIMITS.propertiesMaxBytes) {
|
|
141
|
+
ctx.addIssue({ code: "custom", message: `larger than ${LIMITS.propertiesMaxBytes} bytes` });
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// src/events.ts
|
|
146
|
+
var import_zod2 = require("zod");
|
|
147
|
+
var props = import_zod2.z.looseObject;
|
|
148
|
+
var OrderItemSchema = import_zod2.z.looseObject({
|
|
149
|
+
productId: IdSchema,
|
|
150
|
+
variantId: IdSchema.optional(),
|
|
151
|
+
name: ShortString.optional(),
|
|
152
|
+
quantity: import_zod2.z.number().int().positive(),
|
|
153
|
+
price: MoneySchema
|
|
154
|
+
});
|
|
155
|
+
var CANONICAL_PROPERTY_SCHEMAS = {
|
|
156
|
+
page_viewed: props({ pageType: import_zod2.z.string().max(32).optional() }),
|
|
157
|
+
product_viewed: props({
|
|
158
|
+
productId: IdSchema,
|
|
159
|
+
variantId: IdSchema.optional(),
|
|
160
|
+
name: ShortString.optional(),
|
|
161
|
+
price: MoneySchema.optional()
|
|
162
|
+
}),
|
|
163
|
+
collection_viewed: props({ collectionId: IdSchema, name: ShortString.optional() }),
|
|
164
|
+
search_performed: props({
|
|
165
|
+
query: import_zod2.z.string().max(LIMITS.searchQueryMaxLength),
|
|
166
|
+
resultCount: import_zod2.z.number().int().nonnegative().optional()
|
|
167
|
+
}),
|
|
168
|
+
add_to_cart: props({
|
|
169
|
+
productId: IdSchema,
|
|
170
|
+
variantId: IdSchema.optional(),
|
|
171
|
+
name: ShortString.optional(),
|
|
172
|
+
quantity: import_zod2.z.number().int().positive(),
|
|
173
|
+
cartId: IdSchema.optional(),
|
|
174
|
+
price: MoneySchema.optional(),
|
|
175
|
+
cartValue: MoneySchema.optional()
|
|
176
|
+
}),
|
|
177
|
+
remove_from_cart: props({
|
|
178
|
+
productId: IdSchema,
|
|
179
|
+
variantId: IdSchema.optional(),
|
|
180
|
+
quantity: import_zod2.z.number().int().positive(),
|
|
181
|
+
cartId: IdSchema.optional(),
|
|
182
|
+
cartValue: MoneySchema.optional()
|
|
183
|
+
}),
|
|
184
|
+
cart_updated: props({
|
|
185
|
+
cartId: IdSchema,
|
|
186
|
+
cartValue: MoneySchema,
|
|
187
|
+
itemCount: import_zod2.z.number().int().nonnegative()
|
|
188
|
+
}),
|
|
189
|
+
checkout_started: props({ checkoutId: IdSchema, cartId: IdSchema.optional(), cartValue: MoneySchema }),
|
|
190
|
+
checkout_step_viewed: props({ checkoutId: IdSchema, step: import_zod2.z.string().max(32) }),
|
|
191
|
+
// Deliberately carries no field contents: only that the step happened.
|
|
192
|
+
customer_information_entered: import_zod2.z.strictObject({ checkoutId: IdSchema }),
|
|
193
|
+
shipping_selected: props({
|
|
194
|
+
checkoutId: IdSchema,
|
|
195
|
+
method: ShortString.optional(),
|
|
196
|
+
shippingCost: MoneySchema.optional()
|
|
197
|
+
}),
|
|
198
|
+
payment_started: props({ checkoutId: IdSchema, provider: import_zod2.z.string().max(64).optional() }),
|
|
199
|
+
payment_failed: props({
|
|
200
|
+
checkoutId: IdSchema,
|
|
201
|
+
provider: import_zod2.z.string().max(64).optional(),
|
|
202
|
+
// A provider error *code*, never a free-text message (those leak PII).
|
|
203
|
+
reason: import_zod2.z.string().max(64).optional(),
|
|
204
|
+
amount: MoneySchema.optional()
|
|
205
|
+
}),
|
|
206
|
+
payment_authorized: props({
|
|
207
|
+
checkoutId: IdSchema,
|
|
208
|
+
provider: import_zod2.z.string().max(64).optional(),
|
|
209
|
+
amount: MoneySchema
|
|
210
|
+
}),
|
|
211
|
+
order_completed: props({
|
|
212
|
+
orderId: IdSchema,
|
|
213
|
+
checkoutId: IdSchema.optional(),
|
|
214
|
+
cartId: IdSchema.optional(),
|
|
215
|
+
total: MoneySchema,
|
|
216
|
+
items: import_zod2.z.array(OrderItemSchema).max(LIMITS.orderMaxItems)
|
|
217
|
+
}),
|
|
218
|
+
order_cancelled: props({ orderId: IdSchema, reason: import_zod2.z.string().max(64).optional() }),
|
|
219
|
+
refund_created: props({ orderId: IdSchema, amount: MoneySchema }),
|
|
220
|
+
session_heartbeat: props({})
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// src/envelope.ts
|
|
224
|
+
var import_zod3 = require("zod");
|
|
225
|
+
var HexId = (length) => import_zod3.z.string().regex(new RegExp(`^[0-9a-f]{${length}}$`));
|
|
226
|
+
var ContextSchema = import_zod3.z.object({
|
|
227
|
+
page: import_zod3.z.object({
|
|
228
|
+
url: UrlString.optional(),
|
|
229
|
+
path: UrlString.optional(),
|
|
230
|
+
referrer: UrlString.optional(),
|
|
231
|
+
title: ShortString.optional()
|
|
232
|
+
}).optional(),
|
|
233
|
+
utm: import_zod3.z.object({
|
|
234
|
+
source: ShortString.optional(),
|
|
235
|
+
medium: ShortString.optional(),
|
|
236
|
+
campaign: ShortString.optional(),
|
|
237
|
+
term: ShortString.optional(),
|
|
238
|
+
content: ShortString.optional()
|
|
239
|
+
}).optional(),
|
|
240
|
+
locale: import_zod3.z.string().max(35).optional(),
|
|
241
|
+
timezone: import_zod3.z.string().max(64).optional(),
|
|
242
|
+
currency: CurrencySchema.optional(),
|
|
243
|
+
screen: import_zod3.z.object({ width: import_zod3.z.number().int().nonnegative(), height: import_zod3.z.number().int().nonnegative() }).optional(),
|
|
244
|
+
trace: import_zod3.z.object({ traceId: HexId(32), spanId: HexId(16).optional() }).optional(),
|
|
245
|
+
library: import_zod3.z.object({ name: import_zod3.z.string().max(64), version: import_zod3.z.string().max(32) }).optional(),
|
|
246
|
+
consent: import_zod3.z.enum(["pending", "granted", "denied"]).optional()
|
|
247
|
+
});
|
|
248
|
+
var WireEventSchema = import_zod3.z.object({
|
|
249
|
+
eventId: import_zod3.z.uuid(),
|
|
250
|
+
name: import_zod3.z.string().regex(EVENT_NAME_PATTERN, "event names are snake_case, \u2264 64 chars"),
|
|
251
|
+
timestamp: import_zod3.z.iso.datetime({ offset: true }),
|
|
252
|
+
sessionId: import_zod3.z.string().min(1).max(LIMITS.sessionIdMaxLength),
|
|
253
|
+
anonymousId: import_zod3.z.string().min(1).max(LIMITS.sessionIdMaxLength).optional(),
|
|
254
|
+
customerId: IdSchema.optional(),
|
|
255
|
+
properties: PropertiesSchema.optional(),
|
|
256
|
+
context: ContextSchema.optional()
|
|
257
|
+
});
|
|
258
|
+
var WireBatchSchema = import_zod3.z.object({
|
|
259
|
+
sentAt: import_zod3.z.iso.datetime({ offset: true }).optional(),
|
|
260
|
+
key: import_zod3.z.string().max(128).optional(),
|
|
261
|
+
// Elements are validated one by one so a bad event doesn't sink the batch.
|
|
262
|
+
batch: import_zod3.z.array(import_zod3.z.unknown()).min(1).max(LIMITS.batchMaxEvents)
|
|
263
|
+
});
|
|
264
|
+
function toIssues(error, prefix = "") {
|
|
265
|
+
return error.issues.map((i) => ({
|
|
266
|
+
path: [prefix, ...i.path.map(String)].filter(Boolean).join("."),
|
|
267
|
+
message: i.message
|
|
268
|
+
}));
|
|
269
|
+
}
|
|
270
|
+
function validateEvent(raw) {
|
|
271
|
+
const envelope = WireEventSchema.safeParse(raw);
|
|
272
|
+
if (!envelope.success) {
|
|
273
|
+
const eventId = typeof raw === "object" && raw !== null && "eventId" in raw && typeof raw.eventId === "string" ? raw.eventId : void 0;
|
|
274
|
+
return { ok: false, eventId, issues: toIssues(envelope.error) };
|
|
275
|
+
}
|
|
276
|
+
const { properties = {}, ...rest } = envelope.data;
|
|
277
|
+
if (!isCanonicalEvent(rest.name)) {
|
|
278
|
+
return { ok: true, event: { ...rest, kind: "custom", properties } };
|
|
279
|
+
}
|
|
280
|
+
const parsed = CANONICAL_PROPERTY_SCHEMAS[rest.name].safeParse(properties);
|
|
281
|
+
if (!parsed.success) {
|
|
282
|
+
return { ok: false, eventId: rest.eventId, issues: toIssues(parsed.error, "properties") };
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
ok: true,
|
|
286
|
+
event: { ...rest, kind: "canonical", name: rest.name, properties: parsed.data }
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
290
|
+
0 && (module.exports = {
|
|
291
|
+
CANONICAL_EVENTS,
|
|
292
|
+
CANONICAL_PROPERTY_SCHEMAS,
|
|
293
|
+
ContextSchema,
|
|
294
|
+
CurrencySchema,
|
|
295
|
+
EPHEMERAL_EVENTS,
|
|
296
|
+
EVENT_NAME_PATTERN,
|
|
297
|
+
IdSchema,
|
|
298
|
+
LIMITS,
|
|
299
|
+
MoneySchema,
|
|
300
|
+
PropertiesSchema,
|
|
301
|
+
SERVER_AUTHORITATIVE_EVENTS,
|
|
302
|
+
ShortString,
|
|
303
|
+
UrlString,
|
|
304
|
+
WireBatchSchema,
|
|
305
|
+
WireEventSchema,
|
|
306
|
+
currencyExponent,
|
|
307
|
+
isCanonicalEvent,
|
|
308
|
+
jsonDepth,
|
|
309
|
+
money,
|
|
310
|
+
toMajor,
|
|
311
|
+
validateEvent
|
|
312
|
+
});
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED