@disconetwork/core 0.0.4 → 0.0.6
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/README.md +240 -0
- package/dist/TrackingManager-_IENEPuL.js +852 -0
- package/dist/TrackingManager-_IENEPuL.js.map +1 -0
- package/dist/errors-C-xomnpl.d.ts +408 -0
- package/dist/errors-C-xomnpl.d.ts.map +1 -0
- package/dist/index.d.ts +40 -376
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +728 -319
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +208 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +369 -0
- package/dist/internal.js.map +1 -0
- package/package.json +13 -4
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
import * as z from "zod/v4";
|
|
2
|
+
//#region src/api/generated/recommendations/zod.gen.ts
|
|
3
|
+
/**
|
|
4
|
+
* Address
|
|
5
|
+
*/
|
|
6
|
+
const zAddress = z.object({
|
|
7
|
+
address_line_1: z.string().nullish(),
|
|
8
|
+
address_line_2: z.string().nullish(),
|
|
9
|
+
city: z.string().nullish(),
|
|
10
|
+
state: z.string().nullish(),
|
|
11
|
+
zip: z.string().nullish(),
|
|
12
|
+
phone: z.string().nullish(),
|
|
13
|
+
country_code: z.string().nullish()
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Advertiser
|
|
17
|
+
*/
|
|
18
|
+
const zAdvertiser = z.object({
|
|
19
|
+
id: z.uuid(),
|
|
20
|
+
name: z.string(),
|
|
21
|
+
logo_url: z.url().min(1),
|
|
22
|
+
description: z.string().nullish()
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* AssetType
|
|
26
|
+
*/
|
|
27
|
+
const zAssetType = z.enum(["PRODUCT", "IMAGE"]);
|
|
28
|
+
/**
|
|
29
|
+
* Asset
|
|
30
|
+
*/
|
|
31
|
+
const zAsset = z.object({
|
|
32
|
+
asset_type: zAssetType,
|
|
33
|
+
image_url: z.url().min(1),
|
|
34
|
+
insertion_order: z.int().gte(0),
|
|
35
|
+
id: z.string().nullable(),
|
|
36
|
+
target_url: z.url().min(1).nullish(),
|
|
37
|
+
should_trigger_claim_flow: z.boolean().optional().default(false)
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* AttributionDetails
|
|
41
|
+
*/
|
|
42
|
+
const zAttributionDetails = z.object({
|
|
43
|
+
copy: z.string(),
|
|
44
|
+
disco_logo_url: z.url().min(1),
|
|
45
|
+
target_url: z.url().min(1)
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* CostDetails
|
|
49
|
+
*/
|
|
50
|
+
const zCostDetails = z.object({
|
|
51
|
+
amount: z.number().nullish(),
|
|
52
|
+
currency_code: z.string().nullish()
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* CtaDetails
|
|
56
|
+
*/
|
|
57
|
+
const zCtaDetails = z.object({
|
|
58
|
+
accept_copy: z.string(),
|
|
59
|
+
decline_copy: z.string(),
|
|
60
|
+
transition_copy: z.string().nullish(),
|
|
61
|
+
claimed_accept_copy: z.string().nullish(),
|
|
62
|
+
claimed_decline_copy: z.string().nullish()
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* CustomMetadata
|
|
66
|
+
*/
|
|
67
|
+
const zCustomMetadata = z.object({
|
|
68
|
+
is_add_to_order_customer: z.boolean().nullish(),
|
|
69
|
+
is_sports_betting_eligible: z.boolean().nullish(),
|
|
70
|
+
is_fam: z.boolean().nullish(),
|
|
71
|
+
is_student: z.boolean().nullish(),
|
|
72
|
+
is_sandbox: z.boolean().nullish(),
|
|
73
|
+
studio_id: z.string().nullish(),
|
|
74
|
+
booking_for_self: z.boolean().nullish(),
|
|
75
|
+
is_survey_present: z.boolean().nullish(),
|
|
76
|
+
transaction_type: z.string().nullish(),
|
|
77
|
+
inventory_type: z.string().nullish(),
|
|
78
|
+
booking_datetime: z.iso.datetime().nullish(),
|
|
79
|
+
booking_duration: z.string().nullish(),
|
|
80
|
+
booking_follow_up_action: z.string().nullish(),
|
|
81
|
+
booking_instructor_id: z.string().nullish(),
|
|
82
|
+
pricing_type: z.string().nullish(),
|
|
83
|
+
no_targeting: z.boolean().nullish(),
|
|
84
|
+
no_functional: z.boolean().nullish(),
|
|
85
|
+
registration_date: z.iso.datetime().nullish(),
|
|
86
|
+
flow_session: z.string().nullish(),
|
|
87
|
+
long_flow_session: z.string().nullish(),
|
|
88
|
+
client_version: z.string().nullish(),
|
|
89
|
+
sdk_version: z.string().nullish(),
|
|
90
|
+
core_persona_id: z.int().nullish(),
|
|
91
|
+
track_id: z.string().nullish(),
|
|
92
|
+
shipping_method: z.string().nullish(),
|
|
93
|
+
tracking_status: z.string().nullish(),
|
|
94
|
+
customer_tier: z.string().nullish(),
|
|
95
|
+
order_channel: z.string().nullish(),
|
|
96
|
+
fulfillment_type: z.string().nullish(),
|
|
97
|
+
narvar_session_id: z.string().nullish(),
|
|
98
|
+
delivery_minutes: z.string().nullish()
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* DisclosureDetails
|
|
102
|
+
*/
|
|
103
|
+
const zDisclosureDetails = z.object({
|
|
104
|
+
copy: z.string(),
|
|
105
|
+
target_url: z.url().min(1).nullish(),
|
|
106
|
+
content: z.string().nullish()
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* DisplayMode
|
|
110
|
+
*/
|
|
111
|
+
const zDisplayMode = z.enum([
|
|
112
|
+
"OVERLAY",
|
|
113
|
+
"FULLSCREEN",
|
|
114
|
+
"PULLUP",
|
|
115
|
+
"INLINE"
|
|
116
|
+
]);
|
|
117
|
+
/**
|
|
118
|
+
* Gender
|
|
119
|
+
*/
|
|
120
|
+
const zGender = z.enum([
|
|
121
|
+
"MALE",
|
|
122
|
+
"FEMALE",
|
|
123
|
+
"OTHER",
|
|
124
|
+
"UNSPECIFIED"
|
|
125
|
+
]);
|
|
126
|
+
/**
|
|
127
|
+
* LayoutType
|
|
128
|
+
*/
|
|
129
|
+
const zLayoutType = z.enum([
|
|
130
|
+
"TEXT_RICH",
|
|
131
|
+
"BRAND_VISUAL",
|
|
132
|
+
"FEATURED_IMAGE",
|
|
133
|
+
"BANNER_VISUAL",
|
|
134
|
+
"STACKED_TILE",
|
|
135
|
+
"LAYERED_TILE",
|
|
136
|
+
"PRODUCT_VISUAL"
|
|
137
|
+
]);
|
|
138
|
+
/**
|
|
139
|
+
* NoPromoCodeType
|
|
140
|
+
*/
|
|
141
|
+
const zNoPromoCodeType = z.enum(["NO_PROMO_CODE"]);
|
|
142
|
+
/**
|
|
143
|
+
* NoPromoCodeMetadata
|
|
144
|
+
*/
|
|
145
|
+
const zNoPromoCodeMetadata = z.object({
|
|
146
|
+
type: zNoPromoCodeType,
|
|
147
|
+
layout_type: zLayoutType
|
|
148
|
+
});
|
|
149
|
+
/**
|
|
150
|
+
* OrderDetails
|
|
151
|
+
*/
|
|
152
|
+
const zOrderDetails = z.object({
|
|
153
|
+
cost_details: zCostDetails.nullish(),
|
|
154
|
+
order_id: z.string().nullish(),
|
|
155
|
+
payment_type: z.string().nullish()
|
|
156
|
+
});
|
|
157
|
+
/**
|
|
158
|
+
* PlatformType
|
|
159
|
+
*/
|
|
160
|
+
const zPlatformType = z.enum(["MOBILE", "WEB"]);
|
|
161
|
+
/**
|
|
162
|
+
* Attributes
|
|
163
|
+
*/
|
|
164
|
+
const zAttributes = z.object({
|
|
165
|
+
user_agent: z.string().nullish(),
|
|
166
|
+
operating_system: z.string().nullish(),
|
|
167
|
+
operating_system_version: z.string().nullish(),
|
|
168
|
+
ip_address: z.string().nullish(),
|
|
169
|
+
ip_address_hash: z.string().nullish(),
|
|
170
|
+
platform_type: zPlatformType.nullish(),
|
|
171
|
+
language: z.string().nullish(),
|
|
172
|
+
device_type: z.string().nullish()
|
|
173
|
+
});
|
|
174
|
+
/**
|
|
175
|
+
* PrivacyPolicy
|
|
176
|
+
*/
|
|
177
|
+
const zPrivacyPolicy = z.object({
|
|
178
|
+
copy: z.string(),
|
|
179
|
+
target_url: z.url().min(1)
|
|
180
|
+
});
|
|
181
|
+
/**
|
|
182
|
+
* Footer
|
|
183
|
+
*/
|
|
184
|
+
const zFooter = z.object({
|
|
185
|
+
attribution_details: zAttributionDetails,
|
|
186
|
+
privacy_policy: zPrivacyPolicy
|
|
187
|
+
});
|
|
188
|
+
/**
|
|
189
|
+
* PromoCodeType
|
|
190
|
+
*/
|
|
191
|
+
const zPromoCodeType = z.enum(["PROMO_CODE"]);
|
|
192
|
+
/**
|
|
193
|
+
* PromoCodeMetadata
|
|
194
|
+
*/
|
|
195
|
+
const zPromoCodeMetadata = z.object({
|
|
196
|
+
code: z.string(),
|
|
197
|
+
type: zPromoCodeType,
|
|
198
|
+
layout_type: zLayoutType
|
|
199
|
+
});
|
|
200
|
+
/**
|
|
201
|
+
* Offer
|
|
202
|
+
*/
|
|
203
|
+
const zOffer = z.object({
|
|
204
|
+
id: z.uuid(),
|
|
205
|
+
heading: z.string(),
|
|
206
|
+
subheading: z.string().nullish(),
|
|
207
|
+
json_content: z.record(z.string(), z.unknown()).nullish(),
|
|
208
|
+
target_url: z.url().min(1),
|
|
209
|
+
cta_details: zCtaDetails,
|
|
210
|
+
assets: z.array(zAsset).nullish(),
|
|
211
|
+
metadata: z.union([zPromoCodeMetadata, zNoPromoCodeMetadata]),
|
|
212
|
+
disclosure_details: zDisclosureDetails.nullish()
|
|
213
|
+
});
|
|
214
|
+
/**
|
|
215
|
+
* PurchaseAttributes
|
|
216
|
+
*/
|
|
217
|
+
const zPurchaseAttributes = z.object({
|
|
218
|
+
is_clearance: z.boolean().nullish(),
|
|
219
|
+
is_gift: z.boolean().nullish()
|
|
220
|
+
});
|
|
221
|
+
/**
|
|
222
|
+
* Product
|
|
223
|
+
*/
|
|
224
|
+
const zProduct = z.object({
|
|
225
|
+
name: z.string().nullish(),
|
|
226
|
+
type: z.string().nullish(),
|
|
227
|
+
variant: z.union([z.record(z.string(), z.string()), z.string()]).nullish(),
|
|
228
|
+
quantity: z.number().gte(1).lte(9999999999).nullish(),
|
|
229
|
+
description: z.string().nullish(),
|
|
230
|
+
cost_details: zCostDetails.nullish(),
|
|
231
|
+
product_id: z.string().nullish(),
|
|
232
|
+
is_gift: z.boolean().nullish(),
|
|
233
|
+
attributes: zPurchaseAttributes.nullish()
|
|
234
|
+
});
|
|
235
|
+
/**
|
|
236
|
+
* Recommendation
|
|
237
|
+
*/
|
|
238
|
+
const zRecommendation = z.object({
|
|
239
|
+
advertiser: zAdvertiser,
|
|
240
|
+
offer: zOffer
|
|
241
|
+
});
|
|
242
|
+
/**
|
|
243
|
+
* RecommendationGroupEntry
|
|
244
|
+
*/
|
|
245
|
+
const zRecommendationGroupEntry = z.object({
|
|
246
|
+
insertion_order: z.int().gte(0),
|
|
247
|
+
insertion_id: z.string(),
|
|
248
|
+
offer_id: z.string(),
|
|
249
|
+
advertiser_id: z.string()
|
|
250
|
+
});
|
|
251
|
+
/**
|
|
252
|
+
* RunningExperiment
|
|
253
|
+
*/
|
|
254
|
+
const zRunningExperiment = z.object({
|
|
255
|
+
name: z.string(),
|
|
256
|
+
version: z.int().gte(0).lte(999)
|
|
257
|
+
});
|
|
258
|
+
/**
|
|
259
|
+
* SlotContext
|
|
260
|
+
*/
|
|
261
|
+
const zSlotContext = z.enum(["HERO", "SCROLLABLE"]);
|
|
262
|
+
/**
|
|
263
|
+
* RecommendationGroupProperty
|
|
264
|
+
*/
|
|
265
|
+
const zRecommendationGroupProperty = z.object({
|
|
266
|
+
heading: z.string().nullish(),
|
|
267
|
+
slot_context: zSlotContext
|
|
268
|
+
});
|
|
269
|
+
/**
|
|
270
|
+
* RecommendationsResponse
|
|
271
|
+
*/
|
|
272
|
+
const zRecommendationsResponse = z.object({
|
|
273
|
+
session_id: z.uuid(),
|
|
274
|
+
recommendations: z.record(z.string(), zRecommendation),
|
|
275
|
+
recommendation_groups: z.record(z.string(), z.array(zRecommendationGroupEntry)),
|
|
276
|
+
recommendation_group_properties: z.record(z.string(), zRecommendationGroupProperty).nullish(),
|
|
277
|
+
running_experiments: z.array(zRunningExperiment).nullish(),
|
|
278
|
+
footer: zFooter
|
|
279
|
+
});
|
|
280
|
+
/**
|
|
281
|
+
* SubView
|
|
282
|
+
*
|
|
283
|
+
* Used to distinguish two different views of the same page type
|
|
284
|
+
*/
|
|
285
|
+
const zSubView = z.enum([
|
|
286
|
+
"DEFAULT",
|
|
287
|
+
"POST_CLAIM",
|
|
288
|
+
"BOOKING",
|
|
289
|
+
"PURCHASE",
|
|
290
|
+
"BOOKING_AND_PURCHASE"
|
|
291
|
+
]);
|
|
292
|
+
/**
|
|
293
|
+
* Title
|
|
294
|
+
*/
|
|
295
|
+
const zTitle = z.enum([
|
|
296
|
+
"MR",
|
|
297
|
+
"MRS",
|
|
298
|
+
"MS",
|
|
299
|
+
"MISS"
|
|
300
|
+
]);
|
|
301
|
+
/**
|
|
302
|
+
* EmailHashUserDetails
|
|
303
|
+
*/
|
|
304
|
+
const zEmailHashUserDetails = z.object({
|
|
305
|
+
title: zTitle.nullish(),
|
|
306
|
+
first_name: z.string().nullish(),
|
|
307
|
+
last_name: z.string().nullish(),
|
|
308
|
+
phone: z.string().nullish(),
|
|
309
|
+
gender: zGender.nullish(),
|
|
310
|
+
dob: z.string().nullish(),
|
|
311
|
+
age: z.number().gte(0).nullish(),
|
|
312
|
+
email_hash: z.string().min(1)
|
|
313
|
+
});
|
|
314
|
+
/**
|
|
315
|
+
* EmailUserDetails
|
|
316
|
+
*/
|
|
317
|
+
const zEmailUserDetails = z.object({
|
|
318
|
+
title: zTitle.nullish(),
|
|
319
|
+
first_name: z.string().nullish(),
|
|
320
|
+
last_name: z.string().nullish(),
|
|
321
|
+
phone: z.string().nullish(),
|
|
322
|
+
gender: zGender.nullish(),
|
|
323
|
+
dob: z.string().nullish(),
|
|
324
|
+
age: z.number().gte(0).nullish(),
|
|
325
|
+
email: z.email()
|
|
326
|
+
});
|
|
327
|
+
/**
|
|
328
|
+
* ExternalGuidUserDetails
|
|
329
|
+
*/
|
|
330
|
+
const zExternalGuidUserDetails = z.object({
|
|
331
|
+
title: zTitle.nullish(),
|
|
332
|
+
first_name: z.string().nullish(),
|
|
333
|
+
last_name: z.string().nullish(),
|
|
334
|
+
phone: z.string().nullish(),
|
|
335
|
+
gender: zGender.nullish(),
|
|
336
|
+
dob: z.string().nullish(),
|
|
337
|
+
age: z.number().gte(0).nullish(),
|
|
338
|
+
external_guid: z.string().min(1)
|
|
339
|
+
});
|
|
340
|
+
/**
|
|
341
|
+
* PhoneNumberUserDetails
|
|
342
|
+
*/
|
|
343
|
+
const zPhoneNumberUserDetails = z.object({
|
|
344
|
+
title: zTitle.nullish(),
|
|
345
|
+
first_name: z.string().nullish(),
|
|
346
|
+
last_name: z.string().nullish(),
|
|
347
|
+
phone: z.string().min(1),
|
|
348
|
+
gender: zGender.nullish(),
|
|
349
|
+
dob: z.string().nullish(),
|
|
350
|
+
age: z.number().gte(0).nullish()
|
|
351
|
+
});
|
|
352
|
+
/**
|
|
353
|
+
* ValidationError
|
|
354
|
+
*/
|
|
355
|
+
const zValidationError = z.object({
|
|
356
|
+
loc: z.array(z.union([z.string(), z.int()])),
|
|
357
|
+
msg: z.string(),
|
|
358
|
+
type: z.string()
|
|
359
|
+
});
|
|
360
|
+
z.object({ detail: z.array(zValidationError).optional() });
|
|
361
|
+
/**
|
|
362
|
+
* View
|
|
363
|
+
*
|
|
364
|
+
* deprecated, please use PageType instead
|
|
365
|
+
*/
|
|
366
|
+
const zView = z.enum([
|
|
367
|
+
"ORDER_STATUS",
|
|
368
|
+
"CHECKOUT",
|
|
369
|
+
"THANK_YOU",
|
|
370
|
+
"POST_ORDER",
|
|
371
|
+
"ORDER_TRACKING",
|
|
372
|
+
"SUPPORT_CENTER"
|
|
373
|
+
]);
|
|
374
|
+
/**
|
|
375
|
+
* PlacementDetails
|
|
376
|
+
*/
|
|
377
|
+
const zPlacementDetails = z.object({
|
|
378
|
+
display_mode: zDisplayMode,
|
|
379
|
+
layout_id: z.string().nullish(),
|
|
380
|
+
view: zView,
|
|
381
|
+
sub_view: zSubView.optional().default("DEFAULT")
|
|
382
|
+
});
|
|
383
|
+
const zRecommendationsBody = z.object({
|
|
384
|
+
user_details: z.union([
|
|
385
|
+
zEmailUserDetails,
|
|
386
|
+
zEmailHashUserDetails,
|
|
387
|
+
zPhoneNumberUserDetails,
|
|
388
|
+
zExternalGuidUserDetails
|
|
389
|
+
]),
|
|
390
|
+
placement_details: zPlacementDetails,
|
|
391
|
+
order_details: zOrderDetails.nullish(),
|
|
392
|
+
purchases: z.array(zProduct).nullish(),
|
|
393
|
+
attributes: zAttributes.nullish(),
|
|
394
|
+
custom_metadata: zCustomMetadata.nullish(),
|
|
395
|
+
shipping: zAddress.nullish(),
|
|
396
|
+
billing: zAddress.nullish(),
|
|
397
|
+
running_experiments: z.array(zRunningExperiment).nullish()
|
|
398
|
+
});
|
|
399
|
+
/**
|
|
400
|
+
* Successful Response
|
|
401
|
+
*/
|
|
402
|
+
const zRecommendationsResponse2 = zRecommendationsResponse;
|
|
403
|
+
//#endregion
|
|
404
|
+
//#region src/env.ts
|
|
405
|
+
/**
|
|
406
|
+
* Derive the environment from a Disco public API key.
|
|
407
|
+
*
|
|
408
|
+
* Public keys are prefixed `disco_pk_<env>_…`, where `<env>` is `live`
|
|
409
|
+
* (→ production) or `sandbox` (→ staging). The key is the single source
|
|
410
|
+
* of truth for which backend the SDK talks to — there is no separate
|
|
411
|
+
* `env` to pass or keep in sync, and a `live` key can't be pointed at
|
|
412
|
+
* staging (or vice versa).
|
|
413
|
+
*
|
|
414
|
+
* Throws on anything that isn't a recognized public key — including a
|
|
415
|
+
* secret (`disco_sk_…`) key, which must never be used in browser code.
|
|
416
|
+
*/
|
|
417
|
+
function envFromApiKey(apiKey) {
|
|
418
|
+
if (apiKey.startsWith("disco_pk_live_")) return "production";
|
|
419
|
+
if (apiKey.startsWith("disco_pk_sandbox_")) return "staging";
|
|
420
|
+
throw new Error("Invalid Disco API key: expected a public key prefixed \"disco_pk_live_\" (production) or \"disco_pk_sandbox_\" (staging).");
|
|
421
|
+
}
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/errors.ts
|
|
424
|
+
/**
|
|
425
|
+
* Base class for every error thrown by the Disco clients. Catch this to handle
|
|
426
|
+
* any SDK-originated failure; narrow to a subclass for specifics.
|
|
427
|
+
*
|
|
428
|
+
* Note: a request aborted via an `AbortSignal` is intentionally *not* a
|
|
429
|
+
* `DiscoError` — the native `AbortError` propagates untouched so callers can
|
|
430
|
+
* detect cancellation idiomatically (`err.name === "AbortError"`).
|
|
431
|
+
*/
|
|
432
|
+
var DiscoError = class extends Error {};
|
|
433
|
+
/**
|
|
434
|
+
* Thrown when a recommendations/events request returns a non-2xx status. The
|
|
435
|
+
* HTTP status and an optional parsed body are preserved for error UIs and
|
|
436
|
+
* retry logic. Shared by both the external and internal clients.
|
|
437
|
+
*/
|
|
438
|
+
var DiscoApiError = class extends DiscoError {
|
|
439
|
+
status;
|
|
440
|
+
statusText;
|
|
441
|
+
body;
|
|
442
|
+
constructor(message, info) {
|
|
443
|
+
super(message);
|
|
444
|
+
this.name = "DiscoApiError";
|
|
445
|
+
this.status = info.status;
|
|
446
|
+
this.statusText = info.statusText;
|
|
447
|
+
this.body = info.body;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* Thrown when request or response data fails schema validation.
|
|
452
|
+
*
|
|
453
|
+
* Wraps the underlying Zod `ZodError` and the offending value for debugging,
|
|
454
|
+
* but presents a plain message so consumers never have to import or
|
|
455
|
+
* pattern-match on Zod's own types. `cause` is typed as the common `Error`
|
|
456
|
+
* base because the major differs by client — the external client validates with
|
|
457
|
+
* the generated SDK's `zod/v4` schemas, the internal client with its own
|
|
458
|
+
* `zod/v3` schemas. Narrow `cause` to your Zod major for the structured issues,
|
|
459
|
+
* or read `rawValue` for what was being validated.
|
|
460
|
+
*/
|
|
461
|
+
var DiscoValidationError = class extends DiscoError {
|
|
462
|
+
/** The underlying Zod error (a `ZodError`; narrow to read `.issues`). */
|
|
463
|
+
cause;
|
|
464
|
+
/** The value that failed validation. */
|
|
465
|
+
rawValue;
|
|
466
|
+
constructor(message, cause, rawValue) {
|
|
467
|
+
super(`${message}: ${cause.message}`);
|
|
468
|
+
this.name = "DiscoValidationError";
|
|
469
|
+
this.cause = cause;
|
|
470
|
+
this.rawValue = rawValue;
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
/**
|
|
474
|
+
* Thrown when a request never completes — a network failure, DNS error,
|
|
475
|
+
* connection reset, timeout, etc. A cancellation via `AbortSignal` is excluded:
|
|
476
|
+
* the native `AbortError` propagates as-is (see {@link isAbortError}).
|
|
477
|
+
*/
|
|
478
|
+
var DiscoNetworkError = class extends DiscoError {
|
|
479
|
+
/** The underlying transport error (typically a `TypeError` from `fetch`). */
|
|
480
|
+
cause;
|
|
481
|
+
constructor(message, cause) {
|
|
482
|
+
super(message);
|
|
483
|
+
this.name = "DiscoNetworkError";
|
|
484
|
+
this.cause = cause;
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
/**
|
|
488
|
+
* True for an `AbortController` cancellation. Such errors propagate untouched
|
|
489
|
+
* (never wrapped in `DiscoNetworkError`) so callers can detect cancellation by
|
|
490
|
+
* name. Internal helper — not part of the public surface.
|
|
491
|
+
*/
|
|
492
|
+
function isAbortError(error) {
|
|
493
|
+
return error instanceof Error && error.name === "AbortError";
|
|
494
|
+
}
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/lib/primitives.ts
|
|
497
|
+
/**
|
|
498
|
+
* Return a new object with `inp`'s keys renamed according to `mappings`.
|
|
499
|
+
* A key mapped to a string is renamed; a key mapped to `null` is dropped; a key
|
|
500
|
+
* absent from the mapping is kept as-is.
|
|
501
|
+
*/
|
|
502
|
+
function remap(inp, mappings) {
|
|
503
|
+
let out = {};
|
|
504
|
+
if (!Object.keys(mappings).length) {
|
|
505
|
+
out = inp;
|
|
506
|
+
return out;
|
|
507
|
+
}
|
|
508
|
+
for (const [k, v] of Object.entries(inp)) {
|
|
509
|
+
const j = mappings[k];
|
|
510
|
+
if (j === null) continue;
|
|
511
|
+
out[j ?? k] = v;
|
|
512
|
+
}
|
|
513
|
+
return out;
|
|
514
|
+
}
|
|
515
|
+
//#endregion
|
|
516
|
+
//#region src/lib/case.ts
|
|
517
|
+
/**
|
|
518
|
+
* Recursively convert an object's keys from camelCase to snake_case, descending
|
|
519
|
+
* into nested objects and arrays. Values are left untouched.
|
|
520
|
+
*
|
|
521
|
+
* Reserved for the request's `customMetadata` — an untyped map whose keys are
|
|
522
|
+
* known field names we want recased, not arbitrary data. Everything else on the
|
|
523
|
+
* request path is remapped field-by-field (see `recommendationsTransforms.ts`), so
|
|
524
|
+
* data-keyed values like a purchase `variant` map are never blindly recased.
|
|
525
|
+
*/
|
|
526
|
+
function toSnakeKeys(value) {
|
|
527
|
+
if (Array.isArray(value)) return value.map(toSnakeKeys);
|
|
528
|
+
if (value !== null && typeof value === "object") {
|
|
529
|
+
const out = {};
|
|
530
|
+
for (const [key, v] of Object.entries(value)) out[snakeCase(key)] = toSnakeKeys(v);
|
|
531
|
+
return out;
|
|
532
|
+
}
|
|
533
|
+
return value;
|
|
534
|
+
}
|
|
535
|
+
function snakeCase(key) {
|
|
536
|
+
return key.replace(/([A-Za-z])([0-9])/g, "$1_$2").replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
|
|
537
|
+
}
|
|
538
|
+
//#endregion
|
|
539
|
+
//#region src/external/recommendationsTransforms.ts
|
|
540
|
+
/**
|
|
541
|
+
* Zod schemas + casing transforms for the **external** recommendations contract.
|
|
542
|
+
*
|
|
543
|
+
* The public types mirror the endpoint 1:1 in structure, so the only
|
|
544
|
+
* transformation is snake_case ↔ camelCase:
|
|
545
|
+
* - Request: validate the camelCase input, then build the wire body with
|
|
546
|
+
* explicit per-field `remap`s (the same mechanism Speakeasy-generated SDKs
|
|
547
|
+
* use). Named fields are renamed deterministically; genuinely data-keyed
|
|
548
|
+
* values — `variant`'s map, `customMetadata` — are handled explicitly so a
|
|
549
|
+
* blind recaser can't corrupt them.
|
|
550
|
+
* - Response: validate the snake_case wire, then recase to camelCase
|
|
551
|
+
* **preserving map keys** (offer ids / group keys are data, not field names).
|
|
552
|
+
*
|
|
553
|
+
* @internal Built into the external `DiscoClient`; not part of the public API.
|
|
554
|
+
*/
|
|
555
|
+
/**
|
|
556
|
+
* Build the snake_case wire body from the public request via explicit per-field
|
|
557
|
+
* `remap`s. Transform-only: validation is performed by the generated SDK's
|
|
558
|
+
* request validator (which surfaces as `DiscoValidationError` from
|
|
559
|
+
* `DiscoClient`), so this does not parse or throw on bad input.
|
|
560
|
+
*/
|
|
561
|
+
function transformRequest(request) {
|
|
562
|
+
const v = request;
|
|
563
|
+
return compact({
|
|
564
|
+
user_details: remap(v.userDetails, {
|
|
565
|
+
firstName: "first_name",
|
|
566
|
+
lastName: "last_name",
|
|
567
|
+
emailHash: "email_hash",
|
|
568
|
+
externalGuid: "external_guid"
|
|
569
|
+
}),
|
|
570
|
+
placement_details: remap(v.placementDetails, {
|
|
571
|
+
displayMode: "display_mode",
|
|
572
|
+
subView: "sub_view",
|
|
573
|
+
layoutId: "layout_id"
|
|
574
|
+
}),
|
|
575
|
+
order_details: v.orderDetails && wireOrderDetails(v.orderDetails),
|
|
576
|
+
purchases: v.purchases?.map(wirePurchase),
|
|
577
|
+
attributes: v.attributes && remap(v.attributes, attributesKeyMap),
|
|
578
|
+
custom_metadata: v.customMetadata && toSnakeKeys(v.customMetadata),
|
|
579
|
+
shipping: v.shipping && remap(v.shipping, addressKeyMap),
|
|
580
|
+
billing: v.billing && remap(v.billing, addressKeyMap),
|
|
581
|
+
running_experiments: v.runningExperiments
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
const attributesKeyMap = {
|
|
585
|
+
userAgent: "user_agent",
|
|
586
|
+
operatingSystem: "operating_system",
|
|
587
|
+
operatingSystemVersion: "operating_system_version",
|
|
588
|
+
ipAddress: "ip_address",
|
|
589
|
+
ipAddressHash: "ip_address_hash",
|
|
590
|
+
platformType: "platform_type",
|
|
591
|
+
deviceType: "device_type"
|
|
592
|
+
};
|
|
593
|
+
const addressKeyMap = {
|
|
594
|
+
addressLine1: "address_line_1",
|
|
595
|
+
addressLine2: "address_line_2",
|
|
596
|
+
countryCode: "country_code"
|
|
597
|
+
};
|
|
598
|
+
function wireCostDetails(c) {
|
|
599
|
+
return remap(c, { currencyCode: "currency_code" });
|
|
600
|
+
}
|
|
601
|
+
function wireOrderDetails(o) {
|
|
602
|
+
return {
|
|
603
|
+
...remap(o, {
|
|
604
|
+
orderId: "order_id",
|
|
605
|
+
paymentType: "payment_type",
|
|
606
|
+
costDetails: null
|
|
607
|
+
}),
|
|
608
|
+
...o.costDetails && { cost_details: wireCostDetails(o.costDetails) }
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function wirePurchase(p) {
|
|
612
|
+
return {
|
|
613
|
+
...remap(p, {
|
|
614
|
+
productId: "product_id",
|
|
615
|
+
costDetails: null,
|
|
616
|
+
attributes: null
|
|
617
|
+
}),
|
|
618
|
+
...p.costDetails && { cost_details: wireCostDetails(p.costDetails) },
|
|
619
|
+
...p.attributes && { attributes: remap(p.attributes, {
|
|
620
|
+
isClearance: "is_clearance",
|
|
621
|
+
isGift: "is_gift"
|
|
622
|
+
}) }
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
/** Drop keys whose value is `undefined` so absent optionals leave no trace. */
|
|
626
|
+
function compact(obj) {
|
|
627
|
+
const out = {};
|
|
628
|
+
for (const [k, val] of Object.entries(obj)) if (val !== void 0) out[k] = val;
|
|
629
|
+
return out;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Recase the rec-api wire response into the friendly camelCase shape, keeping
|
|
633
|
+
* the normalized maps intact. Transform-only: the generated SDK validates the
|
|
634
|
+
* response before this runs, so it neither parses nor throws.
|
|
635
|
+
*/
|
|
636
|
+
function transformResponse(raw) {
|
|
637
|
+
return {
|
|
638
|
+
sessionId: raw.session_id,
|
|
639
|
+
recommendations: mapValues(raw.recommendations ?? {}, mapRecommendation),
|
|
640
|
+
recommendationGroups: mapValues(raw.recommendation_groups ?? {}, (entries) => entries.map(mapGroupEntry)),
|
|
641
|
+
recommendationGroupProperties: raw.recommendation_group_properties ? mapValues(raw.recommendation_group_properties, mapGroupProperty) : void 0,
|
|
642
|
+
runningExperiments: raw.running_experiments?.map((e) => ({
|
|
643
|
+
name: e.name,
|
|
644
|
+
version: e.version
|
|
645
|
+
})),
|
|
646
|
+
footer: mapFooter(raw.footer)
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
/** Map a dictionary's values while preserving its (data) keys. */
|
|
650
|
+
function mapValues(obj, fn) {
|
|
651
|
+
const out = {};
|
|
652
|
+
for (const [key, value] of Object.entries(obj)) out[key] = fn(value);
|
|
653
|
+
return out;
|
|
654
|
+
}
|
|
655
|
+
function mapRecommendation(raw) {
|
|
656
|
+
return {
|
|
657
|
+
advertiser: mapAdvertiser(raw.advertiser),
|
|
658
|
+
offer: mapOffer(raw.offer)
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function mapGroupEntry(raw) {
|
|
662
|
+
return {
|
|
663
|
+
insertionOrder: raw.insertion_order,
|
|
664
|
+
insertionId: raw.insertion_id,
|
|
665
|
+
offerId: raw.offer_id,
|
|
666
|
+
advertiserId: raw.advertiser_id
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function mapGroupProperty(raw) {
|
|
670
|
+
return {
|
|
671
|
+
heading: raw.heading ?? null,
|
|
672
|
+
slotContext: raw.slot_context
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
function mapAdvertiser(raw) {
|
|
676
|
+
return {
|
|
677
|
+
id: raw.id,
|
|
678
|
+
name: raw.name,
|
|
679
|
+
logoUrl: raw.logo_url,
|
|
680
|
+
description: raw.description ?? null
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
function mapOffer(raw) {
|
|
684
|
+
return {
|
|
685
|
+
id: raw.id,
|
|
686
|
+
heading: raw.heading,
|
|
687
|
+
subheading: raw.subheading ?? null,
|
|
688
|
+
jsonContent: raw.json_content ?? null,
|
|
689
|
+
targetUrl: raw.target_url,
|
|
690
|
+
ctaDetails: mapCtaDetails(raw.cta_details),
|
|
691
|
+
assets: (raw.assets ?? []).map(mapAsset),
|
|
692
|
+
metadata: mapMetadata(raw.metadata),
|
|
693
|
+
disclosureDetails: raw.disclosure_details ? {
|
|
694
|
+
copy: raw.disclosure_details.copy,
|
|
695
|
+
targetUrl: raw.disclosure_details.target_url ?? null,
|
|
696
|
+
content: raw.disclosure_details.content ?? null
|
|
697
|
+
} : null
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
function mapCtaDetails(raw) {
|
|
701
|
+
return {
|
|
702
|
+
acceptCopy: raw.accept_copy,
|
|
703
|
+
declineCopy: raw.decline_copy,
|
|
704
|
+
transitionCopy: raw.transition_copy ?? null,
|
|
705
|
+
claimedAcceptCopy: raw.claimed_accept_copy ?? null,
|
|
706
|
+
claimedDeclineCopy: raw.claimed_decline_copy ?? null
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
function mapAsset(raw) {
|
|
710
|
+
return {
|
|
711
|
+
assetType: raw.asset_type,
|
|
712
|
+
imageUrl: raw.image_url,
|
|
713
|
+
insertionOrder: raw.insertion_order,
|
|
714
|
+
id: raw.id ?? null,
|
|
715
|
+
targetUrl: raw.target_url ?? null,
|
|
716
|
+
shouldTriggerClaimFlow: raw.should_trigger_claim_flow ?? false
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
function mapMetadata(raw) {
|
|
720
|
+
return raw.type === "PROMO_CODE" ? {
|
|
721
|
+
type: "PROMO_CODE",
|
|
722
|
+
code: raw.code,
|
|
723
|
+
layoutType: raw.layout_type
|
|
724
|
+
} : {
|
|
725
|
+
type: "NO_PROMO_CODE",
|
|
726
|
+
layoutType: raw.layout_type
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
function mapFooter(raw) {
|
|
730
|
+
return {
|
|
731
|
+
attributionDetails: {
|
|
732
|
+
copy: raw.attribution_details.copy,
|
|
733
|
+
discoLogoUrl: raw.attribution_details.disco_logo_url,
|
|
734
|
+
targetUrl: raw.attribution_details.target_url
|
|
735
|
+
},
|
|
736
|
+
privacyPolicy: {
|
|
737
|
+
copy: raw.privacy_policy.copy,
|
|
738
|
+
targetUrl: raw.privacy_policy.target_url
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
//#endregion
|
|
743
|
+
//#region src/TrackingManager.ts
|
|
744
|
+
/**
|
|
745
|
+
* Renderer-agnostic event tracker for Disco recommendation surfaces.
|
|
746
|
+
*
|
|
747
|
+
* Owns:
|
|
748
|
+
* - the dedup set (so repeated display/view events for the same brand
|
|
749
|
+
* don't get re-sent within a session)
|
|
750
|
+
* - the network call to the partner-events endpoint
|
|
751
|
+
* - payload formatting (snake_case keys, BE-friendly datetime)
|
|
752
|
+
*
|
|
753
|
+
* Does not know about React. Wrap it in a Provider (`@disconetwork/react`'s
|
|
754
|
+
* `DiscoProvider`) when consuming from a React tree, or instantiate it
|
|
755
|
+
* directly from any other JS environment.
|
|
756
|
+
*
|
|
757
|
+
* `track` is implemented as an instance-bound arrow function so consumers
|
|
758
|
+
* can safely destructure it without losing `this`.
|
|
759
|
+
*/
|
|
760
|
+
var TrackingManager = class {
|
|
761
|
+
#endpoint;
|
|
762
|
+
#headers;
|
|
763
|
+
#sessionId;
|
|
764
|
+
#fetch;
|
|
765
|
+
#dedupe = /* @__PURE__ */ new Set();
|
|
766
|
+
constructor(config) {
|
|
767
|
+
this.#endpoint = config.endpoint;
|
|
768
|
+
this.#headers = config.headers ?? {};
|
|
769
|
+
this.#sessionId = config.sessionId;
|
|
770
|
+
this.#fetch = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Send a tracking event. Fire-and-forget — never throws.
|
|
774
|
+
* Failures are logged via `console.error`.
|
|
775
|
+
*
|
|
776
|
+
* Idempotency: `WIDGET_DISPLAY`, `WIDGET_VIEW`, `BRAND_DISPLAY`, and
|
|
777
|
+
* `BRAND_VIEW` are deduped per-instance by (event type, insertionId).
|
|
778
|
+
*/
|
|
779
|
+
track = (event) => {
|
|
780
|
+
if (this.#isDeduped(event)) return;
|
|
781
|
+
this.#fetch(this.#endpoint, {
|
|
782
|
+
method: "POST",
|
|
783
|
+
headers: {
|
|
784
|
+
"Content-Type": "application/json",
|
|
785
|
+
...this.#headers
|
|
786
|
+
},
|
|
787
|
+
body: JSON.stringify(this.#toBody(event)),
|
|
788
|
+
keepalive: true
|
|
789
|
+
}).catch((error) => {
|
|
790
|
+
console.error("Event tracking failed:", error);
|
|
791
|
+
});
|
|
792
|
+
};
|
|
793
|
+
#isDeduped(event) {
|
|
794
|
+
if (event.type === "WIDGET_DISPLAY" || event.type === "WIDGET_VIEW" || event.type === "BRAND_DISPLAY" || event.type === "BRAND_VIEW") {
|
|
795
|
+
const key = event.type + ("brand" in event ? event.brand.insertionId : "widget");
|
|
796
|
+
if (this.#dedupe.has(key)) return true;
|
|
797
|
+
this.#dedupe.add(key);
|
|
798
|
+
}
|
|
799
|
+
return false;
|
|
800
|
+
}
|
|
801
|
+
#toBody(event) {
|
|
802
|
+
const commonEventFields = {
|
|
803
|
+
session_id: this.#sessionId,
|
|
804
|
+
event_name: event.type,
|
|
805
|
+
event_datetime: (/* @__PURE__ */ new Date()).toISOString().replace("Z", "+00:00"),
|
|
806
|
+
placement_details: event.placement ? {
|
|
807
|
+
display_mode: event.placement.displayMode,
|
|
808
|
+
view: event.placement.view,
|
|
809
|
+
layout_id: event.placement.layoutId
|
|
810
|
+
} : void 0
|
|
811
|
+
};
|
|
812
|
+
switch (event.type) {
|
|
813
|
+
case "BRAND_CLICK": return {
|
|
814
|
+
...commonEventFields,
|
|
815
|
+
brand_properties: brandProperties(event.brand),
|
|
816
|
+
initiating_target: event.target,
|
|
817
|
+
asset_id: event.assetId
|
|
818
|
+
};
|
|
819
|
+
case "BRAND_DISPLAY":
|
|
820
|
+
case "BRAND_VIEW": return {
|
|
821
|
+
...commonEventFields,
|
|
822
|
+
brand_properties: brandProperties(event.brand),
|
|
823
|
+
initiating_target: event.target
|
|
824
|
+
};
|
|
825
|
+
case "PRODUCT_DISPLAY":
|
|
826
|
+
case "PRODUCT_VIEW":
|
|
827
|
+
case "PRODUCT_CLICK": return {
|
|
828
|
+
...commonEventFields,
|
|
829
|
+
brand_properties: brandProperties(event.brand),
|
|
830
|
+
product_properties: { product_id: event.product.productId },
|
|
831
|
+
initiating_target: event.target
|
|
832
|
+
};
|
|
833
|
+
case "ELEMENT_CLICK": return {
|
|
834
|
+
...commonEventFields,
|
|
835
|
+
brand_properties: event.brand ? brandProperties(event.brand) : void 0,
|
|
836
|
+
initiating_target: event.target
|
|
837
|
+
};
|
|
838
|
+
default: return commonEventFields;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
function brandProperties(brand) {
|
|
843
|
+
return {
|
|
844
|
+
offer_id: brand.offerId,
|
|
845
|
+
advertiser_id: brand.advertiserId,
|
|
846
|
+
insertion_id: brand.insertionId
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
//#endregion
|
|
850
|
+
export { DiscoApiError as a, DiscoValidationError as c, zRecommendationsBody as d, zRecommendationsResponse2 as f, remap as i, isAbortError as l, transformRequest as n, DiscoError as o, transformResponse as r, DiscoNetworkError as s, TrackingManager as t, envFromApiKey as u };
|
|
851
|
+
|
|
852
|
+
//# sourceMappingURL=TrackingManager-_IENEPuL.js.map
|