@contentful/optimization-api-client 0.1.0-alpha8 → 1.0.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 (80) hide show
  1. package/README.md +110 -218
  2. package/THIRD_PARTY_NOTICES.txt +2038 -0
  3. package/dist/199.mjs +2 -0
  4. package/dist/649.mjs +131 -0
  5. package/dist/649.mjs.map +1 -0
  6. package/dist/api-schemas.cjs +60 -0
  7. package/dist/api-schemas.cjs.map +1 -0
  8. package/dist/api-schemas.d.cts +4 -0
  9. package/dist/api-schemas.d.mts +4 -0
  10. package/dist/api-schemas.d.ts +4 -0
  11. package/dist/api-schemas.mjs +2 -0
  12. package/dist/index.cjs +452 -580
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.cts +520 -1042
  15. package/dist/index.d.mts +520 -1042
  16. package/dist/index.d.ts +868 -8
  17. package/dist/index.mjs +115 -298
  18. package/dist/index.mjs.map +1 -1
  19. package/dist/logger.cjs +207 -0
  20. package/dist/logger.cjs.map +1 -0
  21. package/dist/logger.d.cts +287 -0
  22. package/dist/logger.d.mts +287 -0
  23. package/dist/logger.d.ts +287 -0
  24. package/dist/logger.mjs +1 -0
  25. package/package.json +37 -7
  26. package/dist/ApiClient.d.ts +0 -74
  27. package/dist/ApiClient.d.ts.map +0 -1
  28. package/dist/ApiClient.js +0 -61
  29. package/dist/ApiClient.js.map +0 -1
  30. package/dist/ApiClientBase.d.ts +0 -113
  31. package/dist/ApiClientBase.d.ts.map +0 -1
  32. package/dist/ApiClientBase.js +0 -94
  33. package/dist/ApiClientBase.js.map +0 -1
  34. package/dist/builders/EventBuilder.d.ts +0 -589
  35. package/dist/builders/EventBuilder.d.ts.map +0 -1
  36. package/dist/builders/EventBuilder.js +0 -349
  37. package/dist/builders/EventBuilder.js.map +0 -1
  38. package/dist/builders/index.d.ts +0 -3
  39. package/dist/builders/index.d.ts.map +0 -1
  40. package/dist/builders/index.js +0 -3
  41. package/dist/builders/index.js.map +0 -1
  42. package/dist/experience/ExperienceApiClient.d.ts +0 -267
  43. package/dist/experience/ExperienceApiClient.d.ts.map +0 -1
  44. package/dist/experience/ExperienceApiClient.js +0 -324
  45. package/dist/experience/ExperienceApiClient.js.map +0 -1
  46. package/dist/experience/index.d.ts +0 -4
  47. package/dist/experience/index.d.ts.map +0 -1
  48. package/dist/experience/index.js +0 -4
  49. package/dist/experience/index.js.map +0 -1
  50. package/dist/fetch/Fetch.d.ts +0 -96
  51. package/dist/fetch/Fetch.d.ts.map +0 -1
  52. package/dist/fetch/Fetch.js +0 -27
  53. package/dist/fetch/Fetch.js.map +0 -1
  54. package/dist/fetch/createProtectedFetchMethod.d.ts +0 -40
  55. package/dist/fetch/createProtectedFetchMethod.d.ts.map +0 -1
  56. package/dist/fetch/createProtectedFetchMethod.js +0 -53
  57. package/dist/fetch/createProtectedFetchMethod.js.map +0 -1
  58. package/dist/fetch/createRetryFetchMethod.d.ts +0 -60
  59. package/dist/fetch/createRetryFetchMethod.d.ts.map +0 -1
  60. package/dist/fetch/createRetryFetchMethod.js +0 -138
  61. package/dist/fetch/createRetryFetchMethod.js.map +0 -1
  62. package/dist/fetch/createTimeoutFetchMethod.d.ts +0 -51
  63. package/dist/fetch/createTimeoutFetchMethod.d.ts.map +0 -1
  64. package/dist/fetch/createTimeoutFetchMethod.js +0 -51
  65. package/dist/fetch/createTimeoutFetchMethod.js.map +0 -1
  66. package/dist/fetch/index.d.ts +0 -7
  67. package/dist/fetch/index.d.ts.map +0 -1
  68. package/dist/fetch/index.js +0 -7
  69. package/dist/fetch/index.js.map +0 -1
  70. package/dist/index.d.ts.map +0 -1
  71. package/dist/index.js +0 -8
  72. package/dist/index.js.map +0 -1
  73. package/dist/insights/InsightsApiClient.d.ts +0 -130
  74. package/dist/insights/InsightsApiClient.d.ts.map +0 -1
  75. package/dist/insights/InsightsApiClient.js +0 -143
  76. package/dist/insights/InsightsApiClient.js.map +0 -1
  77. package/dist/insights/index.d.ts +0 -4
  78. package/dist/insights/index.d.ts.map +0 -1
  79. package/dist/insights/index.js +0 -4
  80. package/dist/insights/index.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,8 +1,868 @@
1
- export { default as ApiClient } from './ApiClient';
2
- export * from '@contentful/optimization-api-schemas';
3
- export * from './ApiClient';
4
- export * from './ApiClientBase';
5
- export * from './builders';
6
- export * from './experience';
7
- export * from './insights';
8
- //# sourceMappingURL=index.d.ts.map
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * The Contentful Optimization API Client Library provides methods for
5
+ * interfacing with Contentful's Experience and Insights APIs, which serve its
6
+ * Optimization and Analytics products.
7
+ */
8
+
9
+ import { BatchExperienceEventArray } from '@contentful/optimization-api-schemas';
10
+ import { BatchExperienceResponseData } from '@contentful/optimization-api-schemas';
11
+ import { BatchInsightsEventArray } from '@contentful/optimization-api-schemas';
12
+ import { ExperienceEventArray } from '@contentful/optimization-api-schemas';
13
+ import { OptimizationData } from '@contentful/optimization-api-schemas';
14
+
15
+ /**
16
+ * Aggregated API client providing access to Experience and Insights APIs.
17
+ *
18
+ * @remarks
19
+ * This client encapsulates shared configuration and exposes dedicated
20
+ * sub-clients for experience and insights use cases.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const client = new ApiClient({
25
+ * clientId: 'org-id',
26
+ * environment: 'main',
27
+ * experience: {
28
+ * // experience-specific overrides
29
+ * },
30
+ * insights: {
31
+ * // insights-specific overrides
32
+ * },
33
+ * })
34
+ *
35
+ * const profile = await client.experience.getProfile('profile-id')
36
+ * await client.insights.sendBatchEvents(batches)
37
+ * ```
38
+ *
39
+ * @see {@link ExperienceApiClient}
40
+ * @see {@link InsightsApiClient}
41
+ *
42
+ * @public
43
+ */
44
+ export declare class ApiClient {
45
+ /**
46
+ * Shared configuration applied to both Experience and Insights clients.
47
+ *
48
+ * @see {@link ApiConfig}
49
+ */
50
+ readonly config: ApiConfig;
51
+ /**
52
+ * Client for experience-related operations.
53
+ *
54
+ * @see {@link ExperienceApiClient}
55
+ */
56
+ readonly experience: ExperienceApiClient;
57
+ /**
58
+ * Client for insights-related operations.
59
+ *
60
+ * @see {@link InsightsApiClient}
61
+ */
62
+ readonly insights: InsightsApiClient;
63
+ /**
64
+ * Creates a new aggregated {@link ApiClient} instance.
65
+ *
66
+ * @param config - Global API client configuration with optional per-client overrides.
67
+ */
68
+ constructor(config: ApiClientConfig);
69
+ }
70
+
71
+ /**
72
+ * Base class for API clients that provides shared configuration and error logging.
73
+ *
74
+ * @remarks
75
+ * This abstract class is intended for internal use within the package and
76
+ * must not be treated as part of the public API surface.
77
+ *
78
+ * Concrete API clients must extend this class to inherit consistent logging
79
+ * behavior and fetch configuration.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * interface MyClientConfig extends ApiConfig {
84
+ * // additional config
85
+ * }
86
+ *
87
+ * class MyClient extends ApiClientBase {
88
+ * constructor(config: MyClientConfig) {
89
+ * super('MyClient', config)
90
+ * }
91
+ *
92
+ * async getSomething() {
93
+ * const response = await this.fetch('https://example.com', { method: 'GET' })
94
+ * return response.json()
95
+ * }
96
+ * }
97
+ * ```
98
+ *
99
+ * @internal
100
+ */
101
+ declare abstract class ApiClientBase {
102
+ /**
103
+ * Name of the API client, used in log messages and as the `apiName` for fetch.
104
+ */
105
+ protected readonly name: string;
106
+ /**
107
+ * Client identifier used for authentication or tracking.
108
+ */
109
+ protected readonly clientId: string;
110
+ /**
111
+ * Contentful environment associated with this client.
112
+ */
113
+ protected readonly environment: string;
114
+ /**
115
+ * Protected fetch method used by the client to perform HTTP requests.
116
+ */
117
+ protected readonly fetch: FetchMethod;
118
+ /**
119
+ * Creates a new API client base instance.
120
+ *
121
+ * @param name - Human-readable name of the client (used for logging and `apiName`).
122
+ * @param config - Configuration options for the client.
123
+ */
124
+ constructor(name: string, { fetchOptions, clientId, environment }: ApiConfig);
125
+ /**
126
+ * Logs errors that occur during API requests with standardized messages.
127
+ *
128
+ * @param error - The error thrown by the underlying operation.
129
+ * @param options - Additional metadata about the request.
130
+ * @param options.requestName - Human-readable name of the request operation.
131
+ *
132
+ * @remarks
133
+ * Abort errors are logged at `warn` level and other errors at `error` level.
134
+ * The log message includes the client name for better debugging context.
135
+ */
136
+ protected logRequestError(error: unknown, { requestName }: {
137
+ requestName: string;
138
+ }): void;
139
+ }
140
+
141
+ /**
142
+ * Configuration for the high-level {@link ApiClient}.
143
+ *
144
+ * @public
145
+ */
146
+ export declare interface ApiClientConfig extends Pick<ApiConfig, GlobalApiConfigProperties> {
147
+ /**
148
+ * Configuration for the Experience API client.
149
+ *
150
+ * @remarks
151
+ * Shared fields (`clientId`, `environment`, `fetchOptions`) are inherited
152
+ * from top-level config; this object is for Experience-specific options.
153
+ */
154
+ experience?: Omit<ExperienceApiClientConfig, GlobalApiConfigProperties>;
155
+ /**
156
+ * Configuration for the Insights API client.
157
+ *
158
+ * @remarks
159
+ * Shared fields (`clientId`, `environment`, `fetchOptions`) are inherited
160
+ * from top-level config; this object is for Insights-specific options.
161
+ */
162
+ insights?: Omit<InsightsApiClientConfig, GlobalApiConfigProperties>;
163
+ }
164
+
165
+ /**
166
+ * Configuration options for API clients extending `ApiClientBase`.
167
+ *
168
+ * @public
169
+ */
170
+ export declare interface ApiConfig {
171
+ /**
172
+ * Base URL for the API.
173
+ *
174
+ * @remarks
175
+ * When omitted, the concrete client is expected to construct full URLs
176
+ * internally.
177
+ */
178
+ baseUrl?: string;
179
+ /**
180
+ * Contentful environment identifier.
181
+ *
182
+ * @defaultValue `'main'`
183
+ */
184
+ environment?: string;
185
+ /**
186
+ * Options used to configure the underlying protected fetch method.
187
+ *
188
+ * @remarks
189
+ * `apiName` is derived from the client name and must not be provided here.
190
+ */
191
+ fetchOptions?: Omit<ProtectedFetchMethodOptions, 'apiName'>;
192
+ /**
193
+ * Client identifier used for authentication or tracking.
194
+ */
195
+ clientId: string;
196
+ }
197
+
198
+ /**
199
+ * Base options shared across fetch method factories.
200
+ *
201
+ * @public
202
+ */
203
+ export declare interface BaseFetchMethodOptions {
204
+ /**
205
+ * Human-readable name of the API being called.
206
+ *
207
+ * @remarks
208
+ * Used primarily for logging and error messages.
209
+ */
210
+ apiName?: string;
211
+ /**
212
+ * Custom fetch implementation to use instead of the global `fetch`.
213
+ *
214
+ * @remarks
215
+ * This is useful for providing polyfills, mocks, or instrumented fetch
216
+ * implementations. It is required in runtimes that do not provide
217
+ * `globalThis.fetch`.
218
+ */
219
+ fetchMethod?: FetchMethod;
220
+ }
221
+
222
+ /**
223
+ * Parameters used when performing a batch profile update.
224
+ *
225
+ * @public
226
+ */
227
+ export declare interface BatchUpdateProfileParams {
228
+ /**
229
+ * Batch of events to process.
230
+ */
231
+ events: BatchExperienceEventArray;
232
+ }
233
+
234
+ /**
235
+ * Parameters used when creating a profile.
236
+ *
237
+ * @public
238
+ */
239
+ export declare interface CreateProfileParams {
240
+ /**
241
+ * Events used to aggregate the profile state.
242
+ */
243
+ events: ExperienceEventArray;
244
+ }
245
+
246
+ /**
247
+ * Creates a {@link FetchMethod} that combines timeout and retry protection.
248
+ *
249
+ * @param options - Configuration options for both timeout and retry behavior.
250
+ * @returns A {@link FetchMethod} that applies timeout and retry logic to requests.
251
+ *
252
+ * @throws Error
253
+ * Rethrows the original error after logging, including abort errors.
254
+ *
255
+ * @remarks
256
+ * The resulting method first wraps the base fetch with a timeout (via
257
+ * {@link createTimeoutFetchMethod}), then applies retry behavior (via
258
+ * {@link createRetryFetchMethod}).
259
+ *
260
+ * If an error is thrown during configuration or request execution, it is logged.
261
+ *
262
+ * @example
263
+ * ```ts
264
+ * const fetchProtected = createProtectedFetchMethod({
265
+ * apiName: 'Optimization',
266
+ * requestTimeout: 4000,
267
+ * retries: 2,
268
+ * })
269
+ *
270
+ * const response = await fetchProtected('https://example.com/experiences', {
271
+ * method: 'GET',
272
+ * })
273
+ * ```
274
+ *
275
+ * @public
276
+ */
277
+ export declare function createProtectedFetchMethod(options: ProtectedFetchMethodOptions): FetchMethod;
278
+
279
+ /**
280
+ * Creates a {@link FetchMethod} that retries failed requests according to the
281
+ * provided configuration.
282
+ *
283
+ * @param options - Configuration options that control retry behavior.
284
+ * @returns A {@link FetchMethod} that automatically retries qualifying failures.
285
+ *
286
+ * @throws Error
287
+ * Thrown when the request cannot be retried and no successful response is obtained.
288
+ *
289
+ * @remarks
290
+ * This wrapper uses a lightweight internal retry loop and an {@link AbortController}
291
+ * to cancel pending requests when a non-retriable error occurs.
292
+ *
293
+ * @example
294
+ * ```ts
295
+ * const fetchWithRetry = createRetryFetchMethod({
296
+ * apiName: 'Optimization',
297
+ * retries: 3,
298
+ * intervalTimeout: 200,
299
+ * onFailedAttempt: ({ attemptNumber, retriesLeft }) => {
300
+ * console.warn(`Attempt ${attemptNumber} failed. Retries left: ${retriesLeft}`)
301
+ * },
302
+ * })
303
+ *
304
+ * const response = await fetchWithRetry('https://example.com', { method: 'GET' })
305
+ * ```
306
+ *
307
+ * @public
308
+ */
309
+ export declare function createRetryFetchMethod({ apiName, fetchMethod, intervalTimeout, onFailedAttempt, retries, }?: RetryFetchMethodOptions): FetchMethod;
310
+
311
+ /**
312
+ * Creates a {@link FetchMethod} that aborts requests after a configurable timeout.
313
+ *
314
+ * @param options - Configuration options controlling timeout behavior.
315
+ * @returns A {@link FetchMethod} that enforces a timeout for each request.
316
+ *
317
+ * @remarks
318
+ * When a timeout occurs, the request is aborted using an {@link AbortController}.
319
+ * If `onRequestTimeout` is not provided, an error is logged by the package logger.
320
+ *
321
+ * @example
322
+ * ```ts
323
+ * const fetchWithTimeout = createTimeoutFetchMethod({
324
+ * apiName: 'Optimization',
325
+ * requestTimeout: 5000,
326
+ * onRequestTimeout: ({ apiName }) => {
327
+ * console.warn(`${apiName} request timed out`)
328
+ * },
329
+ * })
330
+ *
331
+ * const response = await fetchWithTimeout('https://example.com', { method: 'GET' })
332
+ * ```
333
+ *
334
+ * @see {@link TimeoutFetchMethodOptions}
335
+ *
336
+ * @public
337
+ */
338
+ export declare function createTimeoutFetchMethod({ apiName, fetchMethod, onRequestTimeout, requestTimeout, }?: TimeoutFetchMethodOptions): FetchMethod;
339
+
340
+ /**
341
+ * Default base URL for the Experience API.
342
+ *
343
+ * @public
344
+ */
345
+ export declare const EXPERIENCE_BASE_URL = "https://experience.ninetailed.co/";
346
+
347
+ /**
348
+ * Client for interacting with the Experience API.
349
+ *
350
+ * @remarks
351
+ * This client is responsible for reading and mutating Ninetailed profiles
352
+ * using the Experience API.
353
+ *
354
+ * @example
355
+ * ```ts
356
+ * const client = new ExperienceApiClient({
357
+ * clientId: 'org-id',
358
+ * environment: 'main',
359
+ * })
360
+ *
361
+ * const profile = await client.getProfile('profile-id')
362
+ * ```
363
+ *
364
+ * Extends `ApiClientBase`.
365
+ *
366
+ * @public
367
+ */
368
+ export declare class ExperienceApiClient extends ApiClientBase {
369
+ /**
370
+ * Base URL used for Experience API requests.
371
+ */
372
+ protected readonly baseUrl: string;
373
+ private readonly enabledFeatures?;
374
+ private readonly ip?;
375
+ private locale?;
376
+ private readonly plainText?;
377
+ private readonly preflight?;
378
+ /**
379
+ * Creates a new {@link ExperienceApiClient} instance.
380
+ *
381
+ * @param config - Configuration for the Experience API client.
382
+ */
383
+ constructor(config: ExperienceApiClientConfig);
384
+ /**
385
+ * Update the default Experience API locale used when request options omit `locale`.
386
+ *
387
+ * @param locale - Next default Experience API locale.
388
+ *
389
+ * @public
390
+ */
391
+ setLocale(locale: ExperienceApiClientRequestOptions['locale']): void;
392
+ /**
393
+ * Retrieves a profile by ID.
394
+ *
395
+ * @param id - The profile ID to retrieve.
396
+ * @param options - Optional request options. `preflight` and `plainText` are not allowed here.
397
+ * @returns The current optimization data for the profile.
398
+ *
399
+ * @throws Error
400
+ * Thrown if `id` is missing or the underlying request fails.
401
+ *
402
+ * @example
403
+ * ```ts
404
+ * const profile = await client.getProfile('profile-id', {
405
+ * locale: 'en-US',
406
+ * })
407
+ * ```
408
+ */
409
+ getProfile(id: string, options?: Omit<ExperienceApiClientRequestOptions, 'preflight' | 'plainText'>): Promise<OptimizationData>;
410
+ /**
411
+ * Sends a POST request to mutate a profile or profiles.
412
+ *
413
+ * @param request - Mutation request options including URL, body, and request options.
414
+ * @returns The raw {@link Response} from the underlying fetch.
415
+ *
416
+ * @internal
417
+ */
418
+ private makeProfileMutationRequest;
419
+ /**
420
+ * Creates a profile and returns the resulting optimization data.
421
+ *
422
+ * @param params - Parameters containing the events to aggregate into the profile.
423
+ * @param options - Optional request options.
424
+ * @returns The optimization data for the newly created profile.
425
+ *
426
+ * @remarks
427
+ * The returned profile ID can be used for subsequent update requests.
428
+ *
429
+ * @example
430
+ * ```ts
431
+ * const data = await client.createProfile({
432
+ * events: [{ type: 'identify', userId: 'user-123' }],
433
+ * })
434
+ * ```
435
+ */
436
+ createProfile({ events }: CreateProfileParams, options?: ExperienceApiClientRequestOptions): Promise<OptimizationData>;
437
+ /**
438
+ * Updates an existing profile with the given profile ID.
439
+ *
440
+ * @param params - Parameters including the profile ID and events.
441
+ * @param options - Optional request options.
442
+ * @returns The updated optimization data for the profile.
443
+ *
444
+ * @throws Error
445
+ * Thrown if `profileId` is missing or the underlying request fails.
446
+ *
447
+ * @example
448
+ * ```ts
449
+ * const data = await client.updateProfile({
450
+ * profileId: 'profile-id',
451
+ * events: [{ type: 'track', event: 'viewed_video' }],
452
+ * })
453
+ * ```
454
+ */
455
+ updateProfile({ profileId, events }: UpdateProfileParams, options?: ExperienceApiClientRequestOptions): Promise<OptimizationData>;
456
+ /**
457
+ * Creates or updates a profile depending on whether a `profileId` is provided.
458
+ *
459
+ * @param params - Parameters including optional profile ID and events.
460
+ * @param options - Optional request options.
461
+ * @returns The resulting optimization data.
462
+ *
463
+ * @example
464
+ * ```ts
465
+ * // Create
466
+ * await client.upsertProfile({ events })
467
+ *
468
+ * // Update
469
+ * await client.upsertProfile({ profileId: 'profile-id', events })
470
+ * ```
471
+ */
472
+ upsertProfile({ profileId, events }: UpsertProfileParams, options?: ExperienceApiClientRequestOptions): Promise<OptimizationData>;
473
+ /**
474
+ * Sends multiple events to the Ninetailed Experience API to upsert many profiles.
475
+ *
476
+ * @param params - Parameters containing the batch of events.
477
+ * @param options - Optional request options.
478
+ * @returns The list of profiles affected by the batch operation.
479
+ *
480
+ * @remarks
481
+ * Batch requests must contain at least one event. Every event must contain
482
+ * an anonymous ID. Profiles will be created or updated according to the
483
+ * anonymous ID.
484
+ *
485
+ * This method is intended to be used from server environments.
486
+ *
487
+ * @example
488
+ * ```ts
489
+ * const profiles = await client.upsertManyProfiles({
490
+ * events: [
491
+ * { anonymousId: 'anon-1', type: 'identify', ... },
492
+ * { anonymousId: 'anon-2', type: 'identify', ... },
493
+ * ],
494
+ * })
495
+ * ```
496
+ */
497
+ upsertManyProfiles({ events }: BatchUpdateProfileParams, options?: ExperienceApiClientRequestOptions): Promise<BatchExperienceResponseData['profiles']>;
498
+ /**
499
+ * Constructs a request URL with query parameters derived from request options.
500
+ *
501
+ * @param path - Path relative to the Experience API base URL.
502
+ * @param options - Request options that can influence query parameters.
503
+ * @returns The fully constructed URL as a string.
504
+ *
505
+ * @internal
506
+ */
507
+ private constructUrl;
508
+ /**
509
+ * Constructs request headers based on request options and default configuration.
510
+ *
511
+ * @param options - Request options that can influence headers.
512
+ * @returns A record of HTTP headers to send with the request.
513
+ *
514
+ * @internal
515
+ */
516
+ private constructHeaders;
517
+ /**
518
+ * Constructs the `options` section of the request body for profile mutations.
519
+ *
520
+ * @param options - Request options that can specify enabled features.
521
+ * @returns Experience API body options including feature flags.
522
+ *
523
+ * @internal
524
+ */
525
+ private readonly constructBodyOptions;
526
+ /**
527
+ * Construct and validate the singular Experience mutation request payload.
528
+ *
529
+ * @param events - Events for the Experience API to evaluate.
530
+ * @param options - Request options that can contribute body options.
531
+ * @returns Validated singular Experience request body.
532
+ *
533
+ * @internal
534
+ */
535
+ private constructExperienceRequestBody;
536
+ }
537
+
538
+ /**
539
+ * Configuration for {@link ExperienceApiClient}.
540
+ *
541
+ * @public
542
+ */
543
+ export declare interface ExperienceApiClientConfig extends ApiConfig, ExperienceApiClientRequestOptions {
544
+ }
545
+
546
+ /**
547
+ * Options that control how requests to the Experience API are handled.
548
+ *
549
+ * @public
550
+ */
551
+ export declare interface ExperienceApiClientRequestOptions {
552
+ /**
553
+ * Enabled features (for example, `"ip-enrichment"`) for the API to use for this request.
554
+ *
555
+ * @remarks
556
+ * When omitted, the API can apply a default set of features.
557
+ */
558
+ enabledFeatures?: Feature[];
559
+ /**
560
+ * IP address to override the API behavior for IP analysis.
561
+ *
562
+ * @remarks
563
+ * Commonly used in ESR or SSR environments, as the API otherwise uses
564
+ * the server IP.
565
+ */
566
+ ip?: string;
567
+ /**
568
+ * Experience API `locale` query parameter used to translate profile location fields such as
569
+ * `location.city` and `location.country`.
570
+ *
571
+ * @remarks
572
+ * When omitted, the API can use a server-side default.
573
+ */
574
+ locale?: string;
575
+ /**
576
+ * When `true`, sends performance-critical endpoints in plain text.
577
+ *
578
+ * @remarks
579
+ * The Ninetailed API accepts certain endpoints in plain text to avoid CORS
580
+ * preflight requests, which can improve performance in browser environments.
581
+ */
582
+ plainText?: boolean;
583
+ /**
584
+ * When `true`, instructs the API to aggregate a new profile state but not store it.
585
+ *
586
+ * @remarks
587
+ * This is commonly used in ESR or SSR environments where you want to
588
+ * preview the result without persisting changes.
589
+ */
590
+ preflight?: boolean;
591
+ }
592
+
593
+ /**
594
+ * Feature flags supported by the Experience API.
595
+ *
596
+ * @internal
597
+ */
598
+ export declare type Feature = 'ip-enrichment' | 'location';
599
+
600
+ /**
601
+ * Signature of a fetch method used by the API clients.
602
+ *
603
+ * @param url - The request URL.
604
+ * @param init - Initialization options passed to `fetch`.
605
+ * @returns A promise that resolves with the {@link Response}.
606
+ *
607
+ * @remarks
608
+ * This abstraction allows the underlying implementation to be replaced,
609
+ * for example in tests or different runtime environments.
610
+ *
611
+ * @example
612
+ * ```ts
613
+ * const method: FetchMethod = async (input, init) => {
614
+ * return fetch(input, init)
615
+ * }
616
+ * ```
617
+ *
618
+ * @public
619
+ */
620
+ export declare type FetchInput = string | Request;
621
+
622
+ export declare type FetchMethod = (input: FetchInput, init?: RequestInit) => Promise<Response>;
623
+
624
+ /**
625
+ * Options passed to callback functions invoked by fetch wrappers.
626
+ *
627
+ * @remarks
628
+ * Not all fields are guaranteed to be present in all callback scenarios.
629
+ *
630
+ * @public
631
+ */
632
+ export declare interface FetchMethodCallbackOptions {
633
+ /**
634
+ * Name of the API associated with the request.
635
+ */
636
+ apiName?: string;
637
+ /**
638
+ * Error that caused the callback to be invoked, if available.
639
+ */
640
+ error?: Error;
641
+ /**
642
+ * The current attempt number (for retry callbacks).
643
+ */
644
+ attemptNumber?: number;
645
+ /**
646
+ * Number of retry attempts remaining (for retry callbacks).
647
+ */
648
+ retriesLeft?: number;
649
+ }
650
+
651
+ /**
652
+ * Properties that can be shared between global and per-client API configuration.
653
+ *
654
+ * @public
655
+ */
656
+ export declare type GlobalApiConfigProperties = 'environment' | 'fetchOptions' | 'clientId';
657
+
658
+ /**
659
+ * Default base URL for the Insights ingest API.
660
+ *
661
+ * @public
662
+ */
663
+ export declare const INSIGHTS_BASE_URL = "https://ingest.insights.ninetailed.co/";
664
+
665
+ /**
666
+ * Client for sending analytics and insights events to the Ninetailed Insights API.
667
+ *
668
+ * @remarks
669
+ * This client is optimized for sending batched events, optionally using a
670
+ * custom beacon-like sender for last-chance lifecycle delivery.
671
+ *
672
+ * @example
673
+ * ```ts
674
+ * const insightsClient = new InsightsApiClient({
675
+ * clientId: 'org-id',
676
+ * environment: 'main',
677
+ * })
678
+ *
679
+ * await insightsClient.sendBatchEvents([
680
+ * {
681
+ * profile: { id: 'profile-123', ... },
682
+ * events: [
683
+ * {
684
+ * type: 'track',
685
+ * event: 'button_clicked',
686
+ * properties: { id: 'primary-cta' },
687
+ * },
688
+ * ],
689
+ * }
690
+ * ])
691
+ * ```
692
+ *
693
+ * Extends `ApiClientBase`.
694
+ *
695
+ * @public
696
+ */
697
+ export declare class InsightsApiClient extends ApiClientBase {
698
+ /**
699
+ * Base URL used for Insights API requests.
700
+ */
701
+ protected readonly baseUrl: string;
702
+ /**
703
+ * Creates a new {@link InsightsApiClient} instance.
704
+ *
705
+ * @param config - Configuration for the Insights API client.
706
+ *
707
+ * @example
708
+ * ```ts
709
+ * const client = new InsightsApiClient({
710
+ * clientId: 'org-id',
711
+ * environment: 'main',
712
+ * })
713
+ * ```
714
+ */
715
+ constructor(config: InsightsApiClientConfig);
716
+ /**
717
+ * Sends batches of insights events to the Ninetailed Insights API.
718
+ *
719
+ * @param batches - Array of event batches to send.
720
+ * @param options - Optional request options, including a per-call `beacon` sender.
721
+ * @returns `true` when the event batch is successfully queued by the beacon
722
+ * sender or a direct request is successfully sent, `false` otherwise.
723
+ *
724
+ * @remarks
725
+ * If a `beacon` sender is provided, it will be invoked first with the
726
+ * serialized request body. When the sender returns `true`, the events are
727
+ * considered successfully queued and no network request is made by this
728
+ * method.
729
+ *
730
+ * If the sender is missing, returns `false`, or throws, the events are
731
+ * emitted immediately via `fetch`.
732
+ *
733
+ * @example
734
+ * ```ts
735
+ * const success = await insightsClient.sendBatchEvents(batches)
736
+ * ```
737
+ *
738
+ * @example
739
+ * ```ts
740
+ * // Use Beacon for a last-chance lifecycle flush
741
+ * const success = await insightsClient.sendBatchEvents(batches, {
742
+ * beacon: (url, body) => navigator.sendBeacon(url, body),
743
+ * })
744
+ * ```
745
+ */
746
+ sendBatchEvents(batches: BatchInsightsEventArray, options?: InsightsApiClientRequestOptions): Promise<boolean>;
747
+ }
748
+
749
+ /**
750
+ * Configuration for {@link InsightsApiClient}.
751
+ *
752
+ * @public
753
+ */
754
+ export declare interface InsightsApiClientConfig extends ApiConfig {
755
+ }
756
+
757
+ /**
758
+ * Options that control how Insights events are sent.
759
+ *
760
+ * @public
761
+ */
762
+ export declare interface InsightsApiClientRequestOptions {
763
+ /**
764
+ * Sender used to enqueue serialized events via the Beacon API or a similar mechanism.
765
+ *
766
+ * @param url - Target URL for the batched events.
767
+ * @param body - Serialized batched insights events to be sent.
768
+ * @returns `true` if the events were successfully queued, `false` otherwise.
769
+ *
770
+ * @remarks
771
+ * This option is intended for last-chance lifecycle delivery. If it returns
772
+ * `false` or throws, the client falls back to emitting events immediately via
773
+ * `fetch` with `keepalive`.
774
+ */
775
+ beacon?: (url: string, body: string) => boolean;
776
+ }
777
+
778
+ /**
779
+ * Options for {@link createProtectedFetchMethod}, combining timeout and retry behavior.
780
+ *
781
+ * @see {@link RetryFetchMethodOptions}
782
+ * @see {@link TimeoutFetchMethodOptions}
783
+ *
784
+ * @public
785
+ */
786
+ export declare interface ProtectedFetchMethodOptions extends RetryFetchMethodOptions, TimeoutFetchMethodOptions {
787
+ }
788
+
789
+ /**
790
+ * Configuration options for {@link createRetryFetchMethod}.
791
+ *
792
+ * @public
793
+ */
794
+ export declare interface RetryFetchMethodOptions extends BaseFetchMethodOptions {
795
+ /**
796
+ * Delay (in milliseconds) between retry attempts.
797
+ *
798
+ * @defaultValue `0`
799
+ */
800
+ intervalTimeout?: number;
801
+ /**
802
+ * Callback invoked whenever a retry attempt fails.
803
+ *
804
+ * @param options - Information about the failed attempt.
805
+ *
806
+ * @remarks
807
+ * This callback is invoked with additional metadata such as the attempt
808
+ * number and the number of retries left.
809
+ */
810
+ onFailedAttempt?: (options: FetchMethodCallbackOptions) => void;
811
+ /**
812
+ * Maximum number of retry attempts.
813
+ *
814
+ * @defaultValue `1`
815
+ */
816
+ retries?: number;
817
+ }
818
+
819
+ /**
820
+ * Configuration options for {@link createTimeoutFetchMethod}.
821
+ *
822
+ * @public
823
+ */
824
+ export declare interface TimeoutFetchMethodOptions extends BaseFetchMethodOptions {
825
+ /**
826
+ * Callback invoked when a request exceeds the configured timeout.
827
+ *
828
+ * @param options - Information about the timed-out request.
829
+ *
830
+ * @remarks
831
+ * If this callback is not provided, a default error is logged.
832
+ *
833
+ * @see {@link FetchMethodCallbackOptions}
834
+ */
835
+ onRequestTimeout?: (options: FetchMethodCallbackOptions) => void;
836
+ /**
837
+ * Maximum time (in milliseconds) to wait for a response before aborting the request.
838
+ *
839
+ * @defaultValue `3000`
840
+ */
841
+ requestTimeout?: number;
842
+ }
843
+
844
+ /**
845
+ * Parameters used when updating an existing profile.
846
+ *
847
+ * @public
848
+ */
849
+ export declare interface UpdateProfileParams extends CreateProfileParams {
850
+ /**
851
+ * ID of the profile to update.
852
+ */
853
+ profileId: string;
854
+ }
855
+
856
+ /**
857
+ * Parameters used when creating or updating a profile.
858
+ *
859
+ * @public
860
+ */
861
+ export declare interface UpsertProfileParams extends CreateProfileParams {
862
+ /**
863
+ * Optional ID of the profile; when omitted, a new profile is created.
864
+ */
865
+ profileId?: string;
866
+ }
867
+
868
+ export { }