@contentful/optimization-api-client 0.1.0-alpha → 0.1.0-alpha11
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 +70 -95
- package/dist/199.mjs +2 -0
- package/dist/649.mjs +131 -0
- package/dist/649.mjs.map +1 -0
- package/dist/api-schemas.cjs +60 -0
- package/dist/api-schemas.cjs.map +1 -0
- package/dist/api-schemas.d.cts +4 -0
- package/dist/api-schemas.d.mts +4 -0
- package/dist/api-schemas.d.ts +4 -0
- package/dist/api-schemas.mjs +2 -0
- package/dist/index.cjs +425 -580
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +865 -0
- package/dist/index.d.mts +865 -0
- package/dist/index.d.ts +865 -8
- package/dist/index.mjs +67 -277
- package/dist/index.mjs.map +1 -1
- package/dist/logger.cjs +207 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +287 -0
- package/dist/logger.d.mts +287 -0
- package/dist/logger.d.ts +287 -0
- package/dist/logger.mjs +1 -0
- package/package.json +46 -10
- package/dist/ApiClient.d.ts +0 -74
- package/dist/ApiClient.d.ts.map +0 -1
- package/dist/ApiClient.js +0 -61
- package/dist/ApiClient.js.map +0 -1
- package/dist/ApiClientBase.d.ts +0 -113
- package/dist/ApiClientBase.d.ts.map +0 -1
- package/dist/ApiClientBase.js +0 -94
- package/dist/ApiClientBase.js.map +0 -1
- package/dist/builders/EventBuilder.d.ts +0 -589
- package/dist/builders/EventBuilder.d.ts.map +0 -1
- package/dist/builders/EventBuilder.js +0 -349
- package/dist/builders/EventBuilder.js.map +0 -1
- package/dist/builders/index.d.ts +0 -3
- package/dist/builders/index.d.ts.map +0 -1
- package/dist/builders/index.js +0 -3
- package/dist/builders/index.js.map +0 -1
- package/dist/experience/ExperienceApiClient.d.ts +0 -267
- package/dist/experience/ExperienceApiClient.d.ts.map +0 -1
- package/dist/experience/ExperienceApiClient.js +0 -324
- package/dist/experience/ExperienceApiClient.js.map +0 -1
- package/dist/experience/index.d.ts +0 -4
- package/dist/experience/index.d.ts.map +0 -1
- package/dist/experience/index.js +0 -4
- package/dist/experience/index.js.map +0 -1
- package/dist/fetch/Fetch.d.ts +0 -96
- package/dist/fetch/Fetch.d.ts.map +0 -1
- package/dist/fetch/Fetch.js +0 -27
- package/dist/fetch/Fetch.js.map +0 -1
- package/dist/fetch/createProtectedFetchMethod.d.ts +0 -40
- package/dist/fetch/createProtectedFetchMethod.d.ts.map +0 -1
- package/dist/fetch/createProtectedFetchMethod.js +0 -53
- package/dist/fetch/createProtectedFetchMethod.js.map +0 -1
- package/dist/fetch/createRetryFetchMethod.d.ts +0 -60
- package/dist/fetch/createRetryFetchMethod.d.ts.map +0 -1
- package/dist/fetch/createRetryFetchMethod.js +0 -138
- package/dist/fetch/createRetryFetchMethod.js.map +0 -1
- package/dist/fetch/createTimeoutFetchMethod.d.ts +0 -51
- package/dist/fetch/createTimeoutFetchMethod.d.ts.map +0 -1
- package/dist/fetch/createTimeoutFetchMethod.js +0 -51
- package/dist/fetch/createTimeoutFetchMethod.js.map +0 -1
- package/dist/fetch/index.d.ts +0 -7
- package/dist/fetch/index.d.ts.map +0 -1
- package/dist/fetch/index.js +0 -7
- package/dist/fetch/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +0 -1
- package/dist/insights/InsightsApiClient.d.ts +0 -130
- package/dist/insights/InsightsApiClient.d.ts.map +0 -1
- package/dist/insights/InsightsApiClient.js +0 -142
- package/dist/insights/InsightsApiClient.js.map +0 -1
- package/dist/insights/index.d.ts +0 -4
- package/dist/insights/index.d.ts.map +0 -1
- package/dist/insights/index.js +0 -4
- package/dist/insights/index.js.map +0 -1
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,865 @@
|
|
|
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
|
+
* should not be treated as part of the public API surface.
|
|
77
|
+
*
|
|
78
|
+
* Concrete API clients should 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.
|
|
217
|
+
*/
|
|
218
|
+
fetchMethod?: FetchMethod;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Parameters used when performing a batch profile update.
|
|
223
|
+
*
|
|
224
|
+
* @public
|
|
225
|
+
*/
|
|
226
|
+
export declare interface BatchUpdateProfileParams {
|
|
227
|
+
/**
|
|
228
|
+
* Batch of events to process.
|
|
229
|
+
*/
|
|
230
|
+
events: BatchExperienceEventArray;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Parameters used when creating a profile.
|
|
235
|
+
*
|
|
236
|
+
* @public
|
|
237
|
+
*/
|
|
238
|
+
export declare interface CreateProfileParams {
|
|
239
|
+
/**
|
|
240
|
+
* Events used to aggregate the profile state.
|
|
241
|
+
*/
|
|
242
|
+
events: ExperienceEventArray;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Creates a {@link FetchMethod} that combines timeout and retry protection.
|
|
247
|
+
*
|
|
248
|
+
* @param options - Configuration options for both timeout and retry behavior.
|
|
249
|
+
* @returns A {@link FetchMethod} that applies timeout and retry logic to requests.
|
|
250
|
+
*
|
|
251
|
+
* @throws Error
|
|
252
|
+
* Rethrows the original error after logging, including abort errors.
|
|
253
|
+
*
|
|
254
|
+
* @remarks
|
|
255
|
+
* The resulting method first wraps the base fetch with a timeout (via
|
|
256
|
+
* {@link createTimeoutFetchMethod}), then applies retry behavior (via
|
|
257
|
+
* {@link createRetryFetchMethod}).
|
|
258
|
+
*
|
|
259
|
+
* If an error is thrown during configuration or request execution, it is logged.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* const fetchProtected = createProtectedFetchMethod({
|
|
264
|
+
* apiName: 'Optimization',
|
|
265
|
+
* requestTimeout: 4000,
|
|
266
|
+
* retries: 2,
|
|
267
|
+
* })
|
|
268
|
+
*
|
|
269
|
+
* const response = await fetchProtected('https://example.com/experiences', {
|
|
270
|
+
* method: 'GET',
|
|
271
|
+
* })
|
|
272
|
+
* ```
|
|
273
|
+
*
|
|
274
|
+
* @public
|
|
275
|
+
*/
|
|
276
|
+
export declare function createProtectedFetchMethod(options: ProtectedFetchMethodOptions): FetchMethod;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Creates a {@link FetchMethod} that retries failed requests according to the
|
|
280
|
+
* provided configuration.
|
|
281
|
+
*
|
|
282
|
+
* @param options - Configuration options that control retry behavior.
|
|
283
|
+
* @returns A {@link FetchMethod} that automatically retries qualifying failures.
|
|
284
|
+
*
|
|
285
|
+
* @throws Error
|
|
286
|
+
* Thrown when the request cannot be retried and no successful response is obtained.
|
|
287
|
+
*
|
|
288
|
+
* @remarks
|
|
289
|
+
* This wrapper uses a lightweight internal retry loop and an {@link AbortController}
|
|
290
|
+
* to cancel pending requests when a non-retriable error occurs.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* const fetchWithRetry = createRetryFetchMethod({
|
|
295
|
+
* apiName: 'Optimization',
|
|
296
|
+
* retries: 3,
|
|
297
|
+
* intervalTimeout: 200,
|
|
298
|
+
* onFailedAttempt: ({ attemptNumber, retriesLeft }) => {
|
|
299
|
+
* console.warn(`Attempt ${attemptNumber} failed. Retries left: ${retriesLeft}`)
|
|
300
|
+
* },
|
|
301
|
+
* })
|
|
302
|
+
*
|
|
303
|
+
* const response = await fetchWithRetry('https://example.com', { method: 'GET' })
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* @public
|
|
307
|
+
*/
|
|
308
|
+
export declare function createRetryFetchMethod({ apiName, fetchMethod, intervalTimeout, onFailedAttempt, retries, }?: RetryFetchMethodOptions): FetchMethod;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Creates a {@link FetchMethod} that aborts requests after a configurable timeout.
|
|
312
|
+
*
|
|
313
|
+
* @param options - Configuration options controlling timeout behavior.
|
|
314
|
+
* @returns A {@link FetchMethod} that enforces a timeout for each request.
|
|
315
|
+
*
|
|
316
|
+
* @remarks
|
|
317
|
+
* When a timeout occurs, the request is aborted using an {@link AbortController}.
|
|
318
|
+
* If `onRequestTimeout` is not provided, an error is logged by the package logger.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* const fetchWithTimeout = createTimeoutFetchMethod({
|
|
323
|
+
* apiName: 'Optimization',
|
|
324
|
+
* requestTimeout: 5000,
|
|
325
|
+
* onRequestTimeout: ({ apiName }) => {
|
|
326
|
+
* console.warn(`${apiName} request timed out`)
|
|
327
|
+
* },
|
|
328
|
+
* })
|
|
329
|
+
*
|
|
330
|
+
* const response = await fetchWithTimeout('https://example.com', { method: 'GET' })
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @see {@link TimeoutFetchMethodOptions}
|
|
334
|
+
*
|
|
335
|
+
* @public
|
|
336
|
+
*/
|
|
337
|
+
export declare function createTimeoutFetchMethod({ apiName, fetchMethod, onRequestTimeout, requestTimeout, }?: TimeoutFetchMethodOptions): FetchMethod;
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Default base URL for the Experience API.
|
|
341
|
+
*
|
|
342
|
+
* @public
|
|
343
|
+
*/
|
|
344
|
+
export declare const EXPERIENCE_BASE_URL = "https://experience.ninetailed.co/";
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Client for interacting with the Experience API.
|
|
348
|
+
*
|
|
349
|
+
* @remarks
|
|
350
|
+
* This client is responsible for reading and mutating Ninetailed profiles
|
|
351
|
+
* using the Experience API.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* const client = new ExperienceApiClient({
|
|
356
|
+
* clientId: 'org-id',
|
|
357
|
+
* environment: 'main',
|
|
358
|
+
* })
|
|
359
|
+
*
|
|
360
|
+
* const profile = await client.getProfile('profile-id')
|
|
361
|
+
* ```
|
|
362
|
+
*
|
|
363
|
+
* Extends `ApiClientBase`.
|
|
364
|
+
*
|
|
365
|
+
* @public
|
|
366
|
+
*/
|
|
367
|
+
export declare class ExperienceApiClient extends ApiClientBase {
|
|
368
|
+
/**
|
|
369
|
+
* Base URL used for Experience API requests.
|
|
370
|
+
*/
|
|
371
|
+
protected readonly baseUrl: string;
|
|
372
|
+
private readonly enabledFeatures?;
|
|
373
|
+
private readonly ip?;
|
|
374
|
+
private readonly locale?;
|
|
375
|
+
private readonly plainText?;
|
|
376
|
+
private readonly preflight?;
|
|
377
|
+
/**
|
|
378
|
+
* Creates a new {@link ExperienceApiClient} instance.
|
|
379
|
+
*
|
|
380
|
+
* @param config - Configuration for the Experience API client.
|
|
381
|
+
*/
|
|
382
|
+
constructor(config: ExperienceApiClientConfig);
|
|
383
|
+
/**
|
|
384
|
+
* Retrieves a profile by ID.
|
|
385
|
+
*
|
|
386
|
+
* @param id - The profile ID to retrieve.
|
|
387
|
+
* @param options - Optional request options. `preflight` and `plainText` are not allowed here.
|
|
388
|
+
* @returns The current optimization data for the profile.
|
|
389
|
+
*
|
|
390
|
+
* @throws Error
|
|
391
|
+
* Thrown if `id` is missing or the underlying request fails.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* const profile = await client.getProfile('profile-id', {
|
|
396
|
+
* locale: 'en-US',
|
|
397
|
+
* })
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
getProfile(id: string, options?: Omit<ExperienceApiClientRequestOptions, 'preflight' | 'plainText'>): Promise<OptimizationData>;
|
|
401
|
+
/**
|
|
402
|
+
* Sends a POST request to mutate a profile or profiles.
|
|
403
|
+
*
|
|
404
|
+
* @param request - Mutation request options including URL, body, and request options.
|
|
405
|
+
* @returns The raw {@link Response} from the underlying fetch.
|
|
406
|
+
*
|
|
407
|
+
* @internal
|
|
408
|
+
*/
|
|
409
|
+
private makeProfileMutationRequest;
|
|
410
|
+
/**
|
|
411
|
+
* Creates a profile and returns the resulting optimization data.
|
|
412
|
+
*
|
|
413
|
+
* @param params - Parameters containing the events to aggregate into the profile.
|
|
414
|
+
* @param options - Optional request options.
|
|
415
|
+
* @returns The optimization data for the newly created profile.
|
|
416
|
+
*
|
|
417
|
+
* @remarks
|
|
418
|
+
* The returned profile ID can be used for subsequent update requests.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```ts
|
|
422
|
+
* const data = await client.createProfile({
|
|
423
|
+
* events: [{ type: 'identify', userId: 'user-123' }],
|
|
424
|
+
* })
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
createProfile({ events }: CreateProfileParams, options?: ExperienceApiClientRequestOptions): Promise<OptimizationData>;
|
|
428
|
+
/**
|
|
429
|
+
* Updates an existing profile with the given profile ID.
|
|
430
|
+
*
|
|
431
|
+
* @param params - Parameters including the profile ID and events.
|
|
432
|
+
* @param options - Optional request options.
|
|
433
|
+
* @returns The updated optimization data for the profile.
|
|
434
|
+
*
|
|
435
|
+
* @throws Error
|
|
436
|
+
* Thrown if `profileId` is missing or the underlying request fails.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* const data = await client.updateProfile({
|
|
441
|
+
* profileId: 'profile-id',
|
|
442
|
+
* events: [{ type: 'track', event: 'viewed_video' }],
|
|
443
|
+
* })
|
|
444
|
+
* ```
|
|
445
|
+
*/
|
|
446
|
+
updateProfile({ profileId, events }: UpdateProfileParams, options?: ExperienceApiClientRequestOptions): Promise<OptimizationData>;
|
|
447
|
+
/**
|
|
448
|
+
* Creates or updates a profile depending on whether a `profileId` is provided.
|
|
449
|
+
*
|
|
450
|
+
* @param params - Parameters including optional profile ID and events.
|
|
451
|
+
* @param options - Optional request options.
|
|
452
|
+
* @returns The resulting optimization data.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```ts
|
|
456
|
+
* // Create
|
|
457
|
+
* await client.upsertProfile({ events })
|
|
458
|
+
*
|
|
459
|
+
* // Update
|
|
460
|
+
* await client.upsertProfile({ profileId: 'profile-id', events })
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
463
|
+
upsertProfile({ profileId, events }: UpsertProfileParams, options?: ExperienceApiClientRequestOptions): Promise<OptimizationData>;
|
|
464
|
+
/**
|
|
465
|
+
* Sends multiple events to the Ninetailed Experience API to upsert many profiles.
|
|
466
|
+
*
|
|
467
|
+
* @param params - Parameters containing the batch of events.
|
|
468
|
+
* @param options - Optional request options.
|
|
469
|
+
* @returns The list of profiles affected by the batch operation.
|
|
470
|
+
*
|
|
471
|
+
* @remarks
|
|
472
|
+
* Batch requests must contain at least one event. Every event must contain
|
|
473
|
+
* an anonymous ID. Profiles will be created or updated according to the
|
|
474
|
+
* anonymous ID.
|
|
475
|
+
*
|
|
476
|
+
* This method is intended to be used from server environments.
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* const profiles = await client.upsertManyProfiles({
|
|
481
|
+
* events: [
|
|
482
|
+
* { anonymousId: 'anon-1', type: 'identify', ... },
|
|
483
|
+
* { anonymousId: 'anon-2', type: 'identify', ... },
|
|
484
|
+
* ],
|
|
485
|
+
* })
|
|
486
|
+
* ```
|
|
487
|
+
*/
|
|
488
|
+
upsertManyProfiles({ events }: BatchUpdateProfileParams, options?: ExperienceApiClientRequestOptions): Promise<BatchExperienceResponseData['profiles']>;
|
|
489
|
+
/**
|
|
490
|
+
* Constructs a request URL with query parameters derived from request options.
|
|
491
|
+
*
|
|
492
|
+
* @param path - Path relative to the Experience API base URL.
|
|
493
|
+
* @param options - Request options that may influence query parameters.
|
|
494
|
+
* @returns The fully constructed URL as a string.
|
|
495
|
+
*
|
|
496
|
+
* @internal
|
|
497
|
+
*/
|
|
498
|
+
private constructUrl;
|
|
499
|
+
/**
|
|
500
|
+
* Constructs request headers based on request options and default configuration.
|
|
501
|
+
*
|
|
502
|
+
* @param options - Request options that may influence headers.
|
|
503
|
+
* @returns A record of HTTP headers to send with the request.
|
|
504
|
+
*
|
|
505
|
+
* @internal
|
|
506
|
+
*/
|
|
507
|
+
private constructHeaders;
|
|
508
|
+
/**
|
|
509
|
+
* Constructs the `options` section of the request body for profile mutations.
|
|
510
|
+
*
|
|
511
|
+
* @param options - Request options that may specify enabled features.
|
|
512
|
+
* @returns Experience API body options including feature flags.
|
|
513
|
+
*
|
|
514
|
+
* @internal
|
|
515
|
+
*/
|
|
516
|
+
private readonly constructBodyOptions;
|
|
517
|
+
/**
|
|
518
|
+
* Construct and validate the singular Experience mutation request payload.
|
|
519
|
+
*
|
|
520
|
+
* @param events - Events that should be evaluated by the Experience API.
|
|
521
|
+
* @param options - Request options that may contribute body options.
|
|
522
|
+
* @returns Validated singular Experience request body.
|
|
523
|
+
*
|
|
524
|
+
* @internal
|
|
525
|
+
*/
|
|
526
|
+
private constructExperienceRequestBody;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Configuration for {@link ExperienceApiClient}.
|
|
531
|
+
*
|
|
532
|
+
* @public
|
|
533
|
+
*/
|
|
534
|
+
export declare interface ExperienceApiClientConfig extends ApiConfig, ExperienceApiClientRequestOptions {
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Options that control how requests to the Experience API are handled.
|
|
539
|
+
*
|
|
540
|
+
* @public
|
|
541
|
+
*/
|
|
542
|
+
export declare interface ExperienceApiClientRequestOptions {
|
|
543
|
+
/**
|
|
544
|
+
* Enabled features (for example, `"ip-enrichment"`) which the API should use for this request.
|
|
545
|
+
*
|
|
546
|
+
* @remarks
|
|
547
|
+
* When omitted, a default set of features may be applied.
|
|
548
|
+
*/
|
|
549
|
+
enabledFeatures?: Feature[];
|
|
550
|
+
/**
|
|
551
|
+
* IP address to override the API behavior for IP analysis.
|
|
552
|
+
*
|
|
553
|
+
* @remarks
|
|
554
|
+
* Commonly used in ESR or SSR environments, as the API would otherwise use
|
|
555
|
+
* the server IP.
|
|
556
|
+
*/
|
|
557
|
+
ip?: string;
|
|
558
|
+
/**
|
|
559
|
+
* Locale used to translate `location.city` and `location.country`.
|
|
560
|
+
*
|
|
561
|
+
* @remarks
|
|
562
|
+
* When omitted, a server-side default may be used.
|
|
563
|
+
*/
|
|
564
|
+
locale?: string;
|
|
565
|
+
/**
|
|
566
|
+
* When `true`, sends performance-critical endpoints in plain text.
|
|
567
|
+
*
|
|
568
|
+
* @remarks
|
|
569
|
+
* The Ninetailed API accepts certain endpoints in plain text to avoid CORS
|
|
570
|
+
* preflight requests, which can improve performance in browser environments.
|
|
571
|
+
*/
|
|
572
|
+
plainText?: boolean;
|
|
573
|
+
/**
|
|
574
|
+
* When `true`, instructs the API to aggregate a new profile state but not store it.
|
|
575
|
+
*
|
|
576
|
+
* @remarks
|
|
577
|
+
* This is commonly used in ESR or SSR environments where you want to
|
|
578
|
+
* preview the result without persisting changes.
|
|
579
|
+
*/
|
|
580
|
+
preflight?: boolean;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Feature flags supported by the Experience API.
|
|
585
|
+
*
|
|
586
|
+
* @internal
|
|
587
|
+
*/
|
|
588
|
+
export declare type Feature = 'ip-enrichment' | 'location';
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Signature of a fetch method used by the API clients.
|
|
592
|
+
*
|
|
593
|
+
* @param url - The request URL.
|
|
594
|
+
* @param init - Initialization options passed to `fetch`.
|
|
595
|
+
* @returns A promise that resolves with the {@link Response}.
|
|
596
|
+
*
|
|
597
|
+
* @remarks
|
|
598
|
+
* This abstraction allows the underlying implementation to be replaced,
|
|
599
|
+
* for example in tests or different runtime environments.
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
602
|
+
* ```ts
|
|
603
|
+
* const method: FetchMethod = async (url, init) => {
|
|
604
|
+
* return fetch(url, init)
|
|
605
|
+
* }
|
|
606
|
+
* ```
|
|
607
|
+
*
|
|
608
|
+
* @public
|
|
609
|
+
*/
|
|
610
|
+
export declare type FetchMethod = (url: string | URL, init: RequestInit) => Promise<Response>;
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Options passed to callback functions invoked by fetch wrappers.
|
|
614
|
+
*
|
|
615
|
+
* @remarks
|
|
616
|
+
* Not all fields are guaranteed to be present in all callback scenarios.
|
|
617
|
+
*
|
|
618
|
+
* @public
|
|
619
|
+
*/
|
|
620
|
+
export declare interface FetchMethodCallbackOptions {
|
|
621
|
+
/**
|
|
622
|
+
* Name of the API associated with the request.
|
|
623
|
+
*/
|
|
624
|
+
apiName?: string;
|
|
625
|
+
/**
|
|
626
|
+
* Error that caused the callback to be invoked, if available.
|
|
627
|
+
*/
|
|
628
|
+
error?: Error;
|
|
629
|
+
/**
|
|
630
|
+
* The current attempt number (for retry callbacks).
|
|
631
|
+
*/
|
|
632
|
+
attemptNumber?: number;
|
|
633
|
+
/**
|
|
634
|
+
* Number of retry attempts remaining (for retry callbacks).
|
|
635
|
+
*/
|
|
636
|
+
retriesLeft?: number;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Properties that may be shared between global and per-client API configuration.
|
|
641
|
+
*
|
|
642
|
+
* @public
|
|
643
|
+
*/
|
|
644
|
+
export declare type GlobalApiConfigProperties = 'environment' | 'fetchOptions' | 'clientId';
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Default base URL for the Insights ingest API.
|
|
648
|
+
*
|
|
649
|
+
* @public
|
|
650
|
+
*/
|
|
651
|
+
export declare const INSIGHTS_BASE_URL = "https://ingest.insights.ninetailed.co/";
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Client for sending analytics and insights events to the Ninetailed Insights API.
|
|
655
|
+
*
|
|
656
|
+
* @remarks
|
|
657
|
+
* This client is optimized for sending batched events, optionally using a
|
|
658
|
+
* custom beacon-like handler when available.
|
|
659
|
+
*
|
|
660
|
+
* @example
|
|
661
|
+
* ```ts
|
|
662
|
+
* const insightsClient = new InsightsApiClient({
|
|
663
|
+
* clientId: 'org-id',
|
|
664
|
+
* environment: 'main',
|
|
665
|
+
* })
|
|
666
|
+
*
|
|
667
|
+
* await insightsClient.sendBatchEvents([
|
|
668
|
+
* {
|
|
669
|
+
* profile: { id: 'profile-123', ... },
|
|
670
|
+
* events: [
|
|
671
|
+
* {
|
|
672
|
+
* type: 'track',
|
|
673
|
+
* event: 'button_clicked',
|
|
674
|
+
* properties: { id: 'primary-cta' },
|
|
675
|
+
* },
|
|
676
|
+
* ],
|
|
677
|
+
* }
|
|
678
|
+
* ])
|
|
679
|
+
* ```
|
|
680
|
+
*
|
|
681
|
+
* Extends `ApiClientBase`.
|
|
682
|
+
*
|
|
683
|
+
* @public
|
|
684
|
+
*/
|
|
685
|
+
export declare class InsightsApiClient extends ApiClientBase {
|
|
686
|
+
/**
|
|
687
|
+
* Base URL used for Insights API requests.
|
|
688
|
+
*/
|
|
689
|
+
protected readonly baseUrl: string;
|
|
690
|
+
/**
|
|
691
|
+
* Optional handler used to enqueue events via the Beacon API or a similar mechanism.
|
|
692
|
+
*/
|
|
693
|
+
private readonly beaconHandler;
|
|
694
|
+
/**
|
|
695
|
+
* Creates a new {@link InsightsApiClient} instance.
|
|
696
|
+
*
|
|
697
|
+
* @param config - Configuration for the Insights API client.
|
|
698
|
+
*
|
|
699
|
+
* @example
|
|
700
|
+
* ```ts
|
|
701
|
+
* const client = new InsightsApiClient({
|
|
702
|
+
* clientId: 'org-id',
|
|
703
|
+
* environment: 'main',
|
|
704
|
+
* beaconHandler: (url, data) => {
|
|
705
|
+
* return navigator.sendBeacon(url.toString(), JSON.stringify(data))
|
|
706
|
+
* },
|
|
707
|
+
* })
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
constructor(config: InsightsApiClientConfig);
|
|
711
|
+
/**
|
|
712
|
+
* Sends batches of insights events to the Ninetailed Insights API.
|
|
713
|
+
*
|
|
714
|
+
* @param batches - Array of event batches to send.
|
|
715
|
+
* @param options - Optional request options, including a per-call `beaconHandler`.
|
|
716
|
+
* @returns `true` when the event batch is successfully queued by the beacon
|
|
717
|
+
* handler or a direct request is successfully sent, `false` otherwise.
|
|
718
|
+
*
|
|
719
|
+
* @remarks
|
|
720
|
+
* If a `beaconHandler` is provided (either in the method call or in the
|
|
721
|
+
* client configuration) it will be invoked first. When the handler returns
|
|
722
|
+
* `true`, the events are considered successfully queued and no network
|
|
723
|
+
* request is made by this method.
|
|
724
|
+
*
|
|
725
|
+
* If the handler is missing or returns `false`, the events are emitted
|
|
726
|
+
* immediately via `fetch`.
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* const success = await insightsClient.sendBatchEvents(batches)
|
|
731
|
+
* ```
|
|
732
|
+
*
|
|
733
|
+
* @example
|
|
734
|
+
* ```ts
|
|
735
|
+
* // Override beaconHandler for a single call
|
|
736
|
+
* const success = await insightsClient.sendBatchEvents(batches, {
|
|
737
|
+
* beaconHandler: (url, data) => {
|
|
738
|
+
* return navigator.sendBeacon(url.toString(), JSON.stringify(data))
|
|
739
|
+
* },
|
|
740
|
+
* })
|
|
741
|
+
* ```
|
|
742
|
+
*/
|
|
743
|
+
sendBatchEvents(batches: BatchInsightsEventArray, options?: InsightsApiClientRequestOptions): Promise<boolean>;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Configuration for {@link InsightsApiClient}.
|
|
748
|
+
*
|
|
749
|
+
* @public
|
|
750
|
+
*/
|
|
751
|
+
export declare interface InsightsApiClientConfig extends ApiConfig, InsightsApiClientRequestOptions {
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Options that control how Insights events are sent.
|
|
756
|
+
*
|
|
757
|
+
* @public
|
|
758
|
+
*/
|
|
759
|
+
export declare interface InsightsApiClientRequestOptions {
|
|
760
|
+
/**
|
|
761
|
+
* Handler used to enqueue events via the Beacon API or a similar mechanism.
|
|
762
|
+
*
|
|
763
|
+
* @param url - Target URL for the batched events.
|
|
764
|
+
* @param data - Array of batched insights events to be sent.
|
|
765
|
+
* @returns `true` if the events were successfully queued, `false` otherwise.
|
|
766
|
+
*
|
|
767
|
+
* @remarks
|
|
768
|
+
* When provided, this handler is preferred over direct `fetch` calls. If it
|
|
769
|
+
* returns `false`, the client falls back to emitting events immediately via
|
|
770
|
+
* `fetch`.
|
|
771
|
+
*/
|
|
772
|
+
beaconHandler?: (url: string | URL, data: BatchInsightsEventArray) => boolean;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Options for {@link createProtectedFetchMethod}, combining timeout and retry behavior.
|
|
777
|
+
*
|
|
778
|
+
* @see {@link RetryFetchMethodOptions}
|
|
779
|
+
* @see {@link TimeoutFetchMethodOptions}
|
|
780
|
+
*
|
|
781
|
+
* @public
|
|
782
|
+
*/
|
|
783
|
+
export declare interface ProtectedFetchMethodOptions extends RetryFetchMethodOptions, TimeoutFetchMethodOptions {
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Configuration options for {@link createRetryFetchMethod}.
|
|
788
|
+
*
|
|
789
|
+
* @public
|
|
790
|
+
*/
|
|
791
|
+
export declare interface RetryFetchMethodOptions extends BaseFetchMethodOptions {
|
|
792
|
+
/**
|
|
793
|
+
* Delay (in milliseconds) between retry attempts.
|
|
794
|
+
*
|
|
795
|
+
* @defaultValue `0`
|
|
796
|
+
*/
|
|
797
|
+
intervalTimeout?: number;
|
|
798
|
+
/**
|
|
799
|
+
* Callback invoked whenever a retry attempt fails.
|
|
800
|
+
*
|
|
801
|
+
* @param options - Information about the failed attempt.
|
|
802
|
+
*
|
|
803
|
+
* @remarks
|
|
804
|
+
* This callback is invoked with additional metadata such as the attempt
|
|
805
|
+
* number and the number of retries left.
|
|
806
|
+
*/
|
|
807
|
+
onFailedAttempt?: (options: FetchMethodCallbackOptions) => void;
|
|
808
|
+
/**
|
|
809
|
+
* Maximum number of retry attempts.
|
|
810
|
+
*
|
|
811
|
+
* @defaultValue `1`
|
|
812
|
+
*/
|
|
813
|
+
retries?: number;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Configuration options for {@link createTimeoutFetchMethod}.
|
|
818
|
+
*
|
|
819
|
+
* @public
|
|
820
|
+
*/
|
|
821
|
+
export declare interface TimeoutFetchMethodOptions extends BaseFetchMethodOptions {
|
|
822
|
+
/**
|
|
823
|
+
* Callback invoked when a request exceeds the configured timeout.
|
|
824
|
+
*
|
|
825
|
+
* @param options - Information about the timed-out request.
|
|
826
|
+
*
|
|
827
|
+
* @remarks
|
|
828
|
+
* If this callback is not provided, a default error is logged.
|
|
829
|
+
*
|
|
830
|
+
* @see {@link FetchMethodCallbackOptions}
|
|
831
|
+
*/
|
|
832
|
+
onRequestTimeout?: (options: FetchMethodCallbackOptions) => void;
|
|
833
|
+
/**
|
|
834
|
+
* Maximum time (in milliseconds) to wait for a response before aborting the request.
|
|
835
|
+
*
|
|
836
|
+
* @defaultValue `3000`
|
|
837
|
+
*/
|
|
838
|
+
requestTimeout?: number;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Parameters used when updating an existing profile.
|
|
843
|
+
*
|
|
844
|
+
* @public
|
|
845
|
+
*/
|
|
846
|
+
export declare interface UpdateProfileParams extends CreateProfileParams {
|
|
847
|
+
/**
|
|
848
|
+
* ID of the profile to update.
|
|
849
|
+
*/
|
|
850
|
+
profileId: string;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* Parameters used when creating or updating a profile.
|
|
855
|
+
*
|
|
856
|
+
* @public
|
|
857
|
+
*/
|
|
858
|
+
export declare interface UpsertProfileParams extends CreateProfileParams {
|
|
859
|
+
/**
|
|
860
|
+
* Optional ID of the profile; when omitted, a new profile is created.
|
|
861
|
+
*/
|
|
862
|
+
profileId?: string;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
export { }
|