@contentful/optimization-api-client 0.1.0-alpha8 → 0.1.0-alpha9

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 (79) hide show
  1. package/README.md +55 -24
  2. package/dist/199.mjs +2 -0
  3. package/dist/649.mjs +131 -0
  4. package/dist/649.mjs.map +1 -0
  5. package/dist/api-schemas.cjs +60 -0
  6. package/dist/api-schemas.cjs.map +1 -0
  7. package/dist/api-schemas.d.cts +4 -0
  8. package/dist/api-schemas.d.mts +4 -0
  9. package/dist/api-schemas.d.ts +4 -0
  10. package/dist/api-schemas.mjs +2 -0
  11. package/dist/index.cjs +588 -575
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.cts +327 -68
  14. package/dist/index.d.mts +327 -68
  15. package/dist/index.d.ts +1649 -8
  16. package/dist/index.mjs +103 -136
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/logger.cjs +207 -0
  19. package/dist/logger.cjs.map +1 -0
  20. package/dist/logger.d.cts +287 -0
  21. package/dist/logger.d.mts +287 -0
  22. package/dist/logger.d.ts +287 -0
  23. package/dist/logger.mjs +1 -0
  24. package/package.json +24 -6
  25. package/dist/ApiClient.d.ts +0 -74
  26. package/dist/ApiClient.d.ts.map +0 -1
  27. package/dist/ApiClient.js +0 -61
  28. package/dist/ApiClient.js.map +0 -1
  29. package/dist/ApiClientBase.d.ts +0 -113
  30. package/dist/ApiClientBase.d.ts.map +0 -1
  31. package/dist/ApiClientBase.js +0 -94
  32. package/dist/ApiClientBase.js.map +0 -1
  33. package/dist/builders/EventBuilder.d.ts +0 -589
  34. package/dist/builders/EventBuilder.d.ts.map +0 -1
  35. package/dist/builders/EventBuilder.js +0 -349
  36. package/dist/builders/EventBuilder.js.map +0 -1
  37. package/dist/builders/index.d.ts +0 -3
  38. package/dist/builders/index.d.ts.map +0 -1
  39. package/dist/builders/index.js +0 -3
  40. package/dist/builders/index.js.map +0 -1
  41. package/dist/experience/ExperienceApiClient.d.ts +0 -267
  42. package/dist/experience/ExperienceApiClient.d.ts.map +0 -1
  43. package/dist/experience/ExperienceApiClient.js +0 -324
  44. package/dist/experience/ExperienceApiClient.js.map +0 -1
  45. package/dist/experience/index.d.ts +0 -4
  46. package/dist/experience/index.d.ts.map +0 -1
  47. package/dist/experience/index.js +0 -4
  48. package/dist/experience/index.js.map +0 -1
  49. package/dist/fetch/Fetch.d.ts +0 -96
  50. package/dist/fetch/Fetch.d.ts.map +0 -1
  51. package/dist/fetch/Fetch.js +0 -27
  52. package/dist/fetch/Fetch.js.map +0 -1
  53. package/dist/fetch/createProtectedFetchMethod.d.ts +0 -40
  54. package/dist/fetch/createProtectedFetchMethod.d.ts.map +0 -1
  55. package/dist/fetch/createProtectedFetchMethod.js +0 -53
  56. package/dist/fetch/createProtectedFetchMethod.js.map +0 -1
  57. package/dist/fetch/createRetryFetchMethod.d.ts +0 -60
  58. package/dist/fetch/createRetryFetchMethod.d.ts.map +0 -1
  59. package/dist/fetch/createRetryFetchMethod.js +0 -138
  60. package/dist/fetch/createRetryFetchMethod.js.map +0 -1
  61. package/dist/fetch/createTimeoutFetchMethod.d.ts +0 -51
  62. package/dist/fetch/createTimeoutFetchMethod.d.ts.map +0 -1
  63. package/dist/fetch/createTimeoutFetchMethod.js +0 -51
  64. package/dist/fetch/createTimeoutFetchMethod.js.map +0 -1
  65. package/dist/fetch/index.d.ts +0 -7
  66. package/dist/fetch/index.d.ts.map +0 -1
  67. package/dist/fetch/index.js +0 -7
  68. package/dist/fetch/index.js.map +0 -1
  69. package/dist/index.d.ts.map +0 -1
  70. package/dist/index.js +0 -8
  71. package/dist/index.js.map +0 -1
  72. package/dist/insights/InsightsApiClient.d.ts +0 -130
  73. package/dist/insights/InsightsApiClient.d.ts.map +0 -1
  74. package/dist/insights/InsightsApiClient.js +0 -143
  75. package/dist/insights/InsightsApiClient.js.map +0 -1
  76. package/dist/insights/index.d.ts +0 -4
  77. package/dist/insights/index.d.ts.map +0 -1
  78. package/dist/insights/index.js +0 -4
  79. package/dist/insights/index.js.map +0 -1
@@ -1,138 +0,0 @@
1
- import { createScopedLogger } from 'logger';
2
- import retry from 'p-retry';
3
- const logger = createScopedLogger('ApiClient:Retry');
4
- /**
5
- * Default interval (in milliseconds) between retry attempts.
6
- *
7
- * @internal
8
- */
9
- const DEFAULT_INTERVAL_TIMEOUT = 0;
10
- /**
11
- * Default number of retry attempts.
12
- *
13
- * @internal
14
- */
15
- const DEFAULT_RETRY_COUNT = 1;
16
- /**
17
- * HTTP status code that triggers a retry.
18
- *
19
- * @internal
20
- *
21
- * @remarks
22
- * This value is currently fixed to `503 Service Unavailable`.
23
- */
24
- const RETRY_RESPONSE_STATUS = 503;
25
- /**
26
- * Default HTTP status code used for {@link HttpError}.
27
- *
28
- * @internal
29
- */
30
- const HTTP_ERROR_RESPONSE_STATUS = 500;
31
- /**
32
- * Error type representing HTTP failures with an associated status code.
33
- *
34
- * @internal
35
- */
36
- class HttpError extends Error {
37
- /**
38
- * The HTTP status code associated with the error.
39
- */
40
- status;
41
- /**
42
- * Creates a new {@link HttpError}.
43
- *
44
- * @param message - Description of the error.
45
- * @param status - HTTP status code associated with the error.
46
- */
47
- constructor(message, status = HTTP_ERROR_RESPONSE_STATUS) {
48
- super(message);
49
- Object.setPrototypeOf(this, HttpError.prototype);
50
- this.status = status;
51
- }
52
- }
53
- /**
54
- * Creates a callback function used by `p-retry` to perform a fetch with retry logic.
55
- *
56
- * @param options - Internal options controlling the retry behavior.
57
- * @returns A function that, when invoked, performs the fetch and applies retry rules.
58
- *
59
- * @internal
60
- */
61
- function createRetryFetchCallback({ apiName = 'Optimization', controller, fetchMethod = fetch, init, url, }) {
62
- return async () => {
63
- try {
64
- const response = await fetchMethod(url, init);
65
- if (response.status === RETRY_RESPONSE_STATUS) {
66
- throw new HttpError(`${apiName} API request to "${url.toString()}" failed with status: "[${response.status}] ${response.statusText}".`, RETRY_RESPONSE_STATUS);
67
- }
68
- if (!response.ok) {
69
- const httpError = new Error(`Request to "${url.toString()}" failed with status: [${response.status}] ${response.statusText} - traceparent: ${response.headers.get('traceparent')}`);
70
- logger.error('Request failed with non-OK status:', httpError);
71
- controller.abort();
72
- return;
73
- }
74
- logger.debug(`Response from "${url.toString()}":`, response);
75
- return response;
76
- }
77
- catch (error) {
78
- if (error instanceof HttpError && error.status === RETRY_RESPONSE_STATUS) {
79
- throw error;
80
- }
81
- logger.error(`Request to "${url.toString()}" failed:`, error);
82
- controller.abort();
83
- }
84
- };
85
- }
86
- /**
87
- * Creates a {@link FetchMethod} that retries failed requests according to the
88
- * provided configuration.
89
- *
90
- * @param options - Configuration options that control retry behavior.
91
- * @returns A {@link FetchMethod} that automatically retries qualifying failures.
92
- *
93
- * @remarks
94
- * This wrapper integrates with `p-retry` and uses an {@link AbortController}
95
- * to cancel pending requests when a non-retriable error occurs.
96
- *
97
- * @throws {@link Error}
98
- * Thrown when the request cannot be retried and no successful response is obtained.
99
- *
100
- * @example
101
- * ```ts
102
- * const fetchWithRetry = createRetryFetchMethod({
103
- * apiName: 'Optimization',
104
- * retries: 3,
105
- * intervalTimeout: 200,
106
- * onFailedAttempt: ({ attemptNumber, retriesLeft }) => {
107
- * console.warn(`Attempt ${attemptNumber} failed. Retries left: ${retriesLeft}`)
108
- * },
109
- * })
110
- *
111
- * const response = await fetchWithRetry('https://example.com', { method: 'GET' })
112
- * ```
113
- */
114
- export function createRetryFetchMethod({ apiName = 'Optimization', fetchMethod = fetch, intervalTimeout = DEFAULT_INTERVAL_TIMEOUT, onFailedAttempt, retries = DEFAULT_RETRY_COUNT, } = {}) {
115
- return async (url, init) => {
116
- const controller = new AbortController();
117
- let retryResponse = undefined;
118
- try {
119
- retryResponse = await retry(createRetryFetchCallback({ apiName, controller, fetchMethod, init, url }), {
120
- minTimeout: intervalTimeout,
121
- onFailedAttempt: (options) => onFailedAttempt?.({ ...options, apiName }),
122
- retries,
123
- signal: controller.signal,
124
- });
125
- }
126
- catch (error) {
127
- // Abort errors caused by timeouts should not bubble up and be reported by third-party tools (e.g. Sentry)
128
- if (!(error instanceof Error) || error.name !== 'AbortError') {
129
- throw error;
130
- }
131
- }
132
- if (!retryResponse) {
133
- throw new Error(`${apiName} API request to "${url.toString()}" may not be retried.`);
134
- }
135
- return retryResponse;
136
- };
137
- }
138
- //# sourceMappingURL=createRetryFetchMethod.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createRetryFetchMethod.js","sourceRoot":"","sources":["../../src/fetch/createRetryFetchMethod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAA;AAC3C,OAAO,KAAK,MAAM,SAAS,CAAA;AAG3B,MAAM,MAAM,GAAG,kBAAkB,CAAC,iBAAiB,CAAC,CAAA;AAEpD;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,CAAC,CAAA;AAElC;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,CAAC,CAAA;AAE7B;;;;;;;GAOG;AACH,MAAM,qBAAqB,GAAG,GAAG,CAAA;AAEjC;;;;GAIG;AACH,MAAM,0BAA0B,GAAG,GAAG,CAAA;AAEtC;;;;GAIG;AACH,MAAM,SAAU,SAAQ,KAAK;IAC3B;;OAEG;IACI,MAAM,CAAQ;IAErB;;;;;OAKG;IACH,YAAY,OAAe,EAAE,SAAiB,0BAA0B;QACtE,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;QAChD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;CACF;AAwDD;;;;;;;GAOG;AACH,SAAS,wBAAwB,CAAC,EAChC,OAAO,GAAG,cAAc,EACxB,UAAU,EACV,WAAW,GAAG,KAAK,EACnB,IAAI,EACJ,GAAG,GACuB;IAC1B,OAAO,KAAK,IAAI,EAAE;QAChB,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE,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AAe,EACf,OAAO,GAAG,mBAAmB,MACF,EAAE;IAC7B,OAAO,KAAK,EAAE,GAAiB,EAAE,IAAiB,EAAE,EAAE;QACpD,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;QAExC,IAAI,aAAa,GAAyB,SAAS,CAAA;QAEnD,IAAI,CAAC;YACH,aAAa,GAAG,MAAM,KAAK,CACzB,wBAAwB,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EACzE;gBACE,UAAU,EAAE,eAAe;gBAC3B,eAAe,EAAE,CAAC,OAAmC,EAAE,EAAE,CACvD,eAAe,EAAE,CAAC,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,CAAC;gBAC5C,OAAO;gBACP,MAAM,EAAE,UAAU,CAAC,MAAM;aAC1B,CACF,CAAA;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,0GAA0G;YAC1G,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAC7D,MAAM,KAAK,CAAA;YACb,CAAC;QACH,CAAC;QAED,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,GAAG,OAAO,oBAAoB,GAAG,CAAC,QAAQ,EAAE,uBAAuB,CAAC,CAAA;QACtF,CAAC;QAED,OAAO,aAAa,CAAA;IACtB,CAAC,CAAA;AACH,CAAC"}
@@ -1,51 +0,0 @@
1
- import type { BaseFetchMethodOptions, FetchMethod, FetchMethodCallbackOptions } from './Fetch';
2
- /**
3
- * Configuration options for {@link createTimeoutFetchMethod}.
4
- */
5
- export interface TimeoutFetchMethodOptions extends BaseFetchMethodOptions {
6
- /**
7
- * Callback invoked when a request exceeds the configured timeout.
8
- *
9
- * @param options - Information about the timed-out request.
10
- *
11
- * @remarks
12
- * If this callback is not provided, a default error is logged.
13
- *
14
- * @see {@link FetchMethodCallbackOptions}
15
- */
16
- onRequestTimeout?: (options: FetchMethodCallbackOptions) => void;
17
- /**
18
- * Maximum time (in milliseconds) to wait for a response before aborting the request.
19
- *
20
- * @remarks
21
- * Defaults to {@link DEFAULT_REQUEST_TIMEOUT}.
22
- */
23
- requestTimeout?: number;
24
- }
25
- /**
26
- * Creates a {@link FetchMethod} that aborts requests after a configurable timeout.
27
- *
28
- * @param options - Configuration options controlling timeout behavior.
29
- * @returns A {@link FetchMethod} that enforces a timeout for each request.
30
- *
31
- * @remarks
32
- * When a timeout occurs, the request is aborted using an {@link AbortController}.
33
- * If `onRequestTimeout` is not provided, an error is logged by the {@link logger}.
34
- *
35
- * @example
36
- * ```ts
37
- * const fetchWithTimeout = createTimeoutFetchMethod({
38
- * apiName: 'Optimization',
39
- * requestTimeout: 5000,
40
- * onRequestTimeout: ({ apiName }) => {
41
- * console.warn(`${apiName} request timed out`)
42
- * },
43
- * })
44
- *
45
- * const response = await fetchWithTimeout('https://example.com', { method: 'GET' })
46
- * ```
47
- *
48
- * @see {@link TimeoutFetchMethodOptions}
49
- */
50
- export declare function createTimeoutFetchMethod({ apiName, fetchMethod, onRequestTimeout, requestTimeout, }?: TimeoutFetchMethodOptions): FetchMethod;
51
- //# sourceMappingURL=createTimeoutFetchMethod.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createTimeoutFetchMethod.d.ts","sourceRoot":"","sources":["../../src/fetch/createTimeoutFetchMethod.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,WAAW,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAA;AAW9F;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,sBAAsB;IACvE;;;;;;;;;OASG;IACH,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,0BAA0B,KAAK,IAAI,CAAA;IAEhE;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,wBAAwB,CAAC,EACvC,OAAwB,EACxB,WAAmB,EACnB,gBAAgB,EAChB,cAAwC,GACzC,GAAE,yBAA8B,GAAG,WAAW,CAoB9C"}
@@ -1,51 +0,0 @@
1
- import { createScopedLogger } from 'logger';
2
- const logger = createScopedLogger('ApiClient:Timeout');
3
- /**
4
- * Default timeout (in milliseconds) for outgoing requests.
5
- *
6
- * @internal
7
- */
8
- const DEFAULT_REQUEST_TIMEOUT = 3000;
9
- /**
10
- * Creates a {@link FetchMethod} that aborts requests after a configurable timeout.
11
- *
12
- * @param options - Configuration options controlling timeout behavior.
13
- * @returns A {@link FetchMethod} that enforces a timeout for each request.
14
- *
15
- * @remarks
16
- * When a timeout occurs, the request is aborted using an {@link AbortController}.
17
- * If `onRequestTimeout` is not provided, an error is logged by the {@link logger}.
18
- *
19
- * @example
20
- * ```ts
21
- * const fetchWithTimeout = createTimeoutFetchMethod({
22
- * apiName: 'Optimization',
23
- * requestTimeout: 5000,
24
- * onRequestTimeout: ({ apiName }) => {
25
- * console.warn(`${apiName} request timed out`)
26
- * },
27
- * })
28
- *
29
- * const response = await fetchWithTimeout('https://example.com', { method: 'GET' })
30
- * ```
31
- *
32
- * @see {@link TimeoutFetchMethodOptions}
33
- */
34
- export function createTimeoutFetchMethod({ apiName = 'Optimization', fetchMethod = fetch, onRequestTimeout, requestTimeout = DEFAULT_REQUEST_TIMEOUT, } = {}) {
35
- return async (url, init) => {
36
- const controller = new AbortController();
37
- const id = setTimeout(() => {
38
- if (typeof onRequestTimeout === 'function') {
39
- onRequestTimeout({ apiName });
40
- }
41
- else {
42
- logger.error(`Request to "${url.toString()}" timed out`, new Error('Request timeout'));
43
- }
44
- controller.abort();
45
- }, requestTimeout);
46
- const response = await fetchMethod(url, { ...init, signal: controller.signal });
47
- clearTimeout(id);
48
- return response;
49
- };
50
- }
51
- //# sourceMappingURL=createTimeoutFetchMethod.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createTimeoutFetchMethod.js","sourceRoot":"","sources":["../../src/fetch/createTimeoutFetchMethod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAA;AAG3C,MAAM,MAAM,GAAG,kBAAkB,CAAC,mBAAmB,CAAC,CAAA;AAEtD;;;;GAIG;AACH,MAAM,uBAAuB,GAAG,IAAI,CAAA;AA2BpC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,wBAAwB,CAAC,EACvC,OAAO,GAAG,cAAc,EACxB,WAAW,GAAG,KAAK,EACnB,gBAAgB,EAChB,cAAc,GAAG,uBAAuB,MACX,EAAE;IAC/B,OAAO,KAAK,EAAE,GAAiB,EAAE,IAAiB,EAAE,EAAE;QACpD,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;QAExC,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE;YACzB,IAAI,OAAO,gBAAgB,KAAK,UAAU,EAAE,CAAC;gBAC3C,gBAAgB,CAAC,EAAE,OAAO,EAAE,CAAC,CAAA;YAC/B,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,KAAK,CAAC,eAAe,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC,CAAA;YACxF,CAAC;YAED,UAAU,CAAC,KAAK,EAAE,CAAA;QACpB,CAAC,EAAE,cAAc,CAAC,CAAA;QAElB,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,CAAC,CAAA;QAE/E,YAAY,CAAC,EAAE,CAAC,CAAA;QAEhB,OAAO,QAAQ,CAAA;IACjB,CAAC,CAAA;AACH,CAAC"}
@@ -1,7 +0,0 @@
1
- import Fetch from './Fetch';
2
- export * from './createProtectedFetchMethod';
3
- export * from './createRetryFetchMethod';
4
- export * from './createTimeoutFetchMethod';
5
- export * from './Fetch';
6
- export default Fetch;
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAA;AAE3B,cAAc,8BAA8B,CAAA;AAC5C,cAAc,0BAA0B,CAAA;AACxC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,SAAS,CAAA;AAEvB,eAAe,KAAK,CAAA"}
@@ -1,7 +0,0 @@
1
- import Fetch from './Fetch';
2
- export * from './createProtectedFetchMethod';
3
- export * from './createRetryFetchMethod';
4
- export * from './createTimeoutFetchMethod';
5
- export * from './Fetch';
6
- export default Fetch;
7
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAA;AAE3B,cAAc,8BAA8B,CAAA;AAC5C,cAAc,0BAA0B,CAAA;AACxC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,SAAS,CAAA;AAEvB,eAAe,KAAK,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAA;AAElD,cAAc,sCAAsC,CAAA;AACpD,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
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.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAA;AAElD,cAAc,sCAAsC,CAAA;AACpD,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
@@ -1,130 +0,0 @@
1
- import { BatchInsightsEventArray } from '@contentful/optimization-api-schemas';
2
- import ApiClientBase, { type ApiConfig } from '../ApiClientBase';
3
- /**
4
- * Default base URL for the Insights ingest API.
5
- *
6
- * @public
7
- */
8
- export declare const INSIGHTS_BASE_URL = "https://ingest.insights.ninetailed.co/";
9
- /**
10
- * Options that control how Insights events are sent.
11
- *
12
- * @public
13
- */
14
- interface RequestOptions {
15
- /**
16
- * Handler used to enqueue events via the Beacon API or a similar mechanism.
17
- *
18
- * @param url - Target URL for the batched events.
19
- * @param data - Array of batched insights events to be sent.
20
- * @returns `true` if the events were successfully queued, `false` otherwise.
21
- *
22
- * @remarks
23
- * When provided, this handler is preferred over direct `fetch` calls. If it
24
- * returns `false`, the client falls back to emitting events immediately via
25
- * `fetch`.
26
- */
27
- beaconHandler?: (url: string | URL, data: BatchInsightsEventArray) => boolean;
28
- }
29
- /**
30
- * Configuration for {@link InsightsApiClient}.
31
- *
32
- * @public
33
- */
34
- export interface InsightsApiClientConfig extends ApiConfig, RequestOptions {
35
- }
36
- /**
37
- * Client for sending analytics and insights events to the Ninetailed Insights API.
38
- *
39
- * @public
40
- *
41
- * @remarks
42
- * This client is optimized for sending batched events, optionally using a
43
- * custom beacon-like handler when available.
44
- *
45
- * @example
46
- * ```ts
47
- * const insightsClient = new InsightsApiClient({
48
- * clientId: 'org-id',
49
- * environment: 'main',
50
- * preview: false,
51
- * })
52
- *
53
- * await insightsClient.sendBatchEvents([
54
- * {
55
- * profile: { id: 'profile-123', ... },
56
- * events: [
57
- * {
58
- * type: 'track',
59
- * event: 'button_clicked',
60
- * properties: { id: 'primary-cta' },
61
- * },
62
- * ],
63
- * }
64
- * ])
65
- * ```
66
- */
67
- export default class InsightsApiClient extends ApiClientBase {
68
- /**
69
- * Base URL used for Insights API requests.
70
- */
71
- protected readonly baseUrl: string;
72
- /**
73
- * Optional handler used to enqueue events via the Beacon API or a similar mechanism.
74
- */
75
- private readonly beaconHandler;
76
- /**
77
- * Creates a new {@link InsightsApiClient} instance.
78
- *
79
- * @param config - Configuration for the Insights API client.
80
- *
81
- * @example
82
- * ```ts
83
- * const client = new InsightsApiClient({
84
- * clientId: 'org-id',
85
- * environment: 'main',
86
- * beaconHandler: (url, data) => {
87
- * return navigator.sendBeacon(url.toString(), JSON.stringify(data))
88
- * },
89
- * })
90
- * ```
91
- */
92
- constructor(config: InsightsApiClientConfig);
93
- /**
94
- * Sends batches of insights events to the Ninetailed Insights API.
95
- *
96
- * @param batches - Array of event batches to send.
97
- * @param options - Optional request options, including a per-call `beaconHandler`.
98
- * @returns A promise that resolves when the events have been sent or queued.
99
- *
100
- * @remarks
101
- * If a `beaconHandler` is provided (either in the method call or in the
102
- * client configuration) it will be invoked first. When the handler returns
103
- * `true`, the events are considered successfully queued and no network
104
- * request is made by this method.
105
- *
106
- * If the handler is missing or returns `false`, the events are emitted
107
- * immediately via `fetch`.
108
- *
109
- * @returns A boolean value that is true when either the event batch is successfully
110
- * queued by the beacon handler or a direct request is successfully sent.
111
- *
112
- * @example
113
- * ```ts
114
- * const success = await insightsClient.sendBatchEvents(batches)
115
- * ```
116
- *
117
- * @example
118
- * ```ts
119
- * // Override beaconHandler for a single call
120
- * const success = await insightsClient.sendBatchEvents(batches, {
121
- * beaconHandler: (url, data) => {
122
- * return navigator.sendBeacon(url.toString(), JSON.stringify(data))
123
- * },
124
- * })
125
- * ```
126
- */
127
- sendBatchEvents(batches: BatchInsightsEventArray, options?: RequestOptions): Promise<boolean>;
128
- }
129
- export {};
130
- //# sourceMappingURL=InsightsApiClient.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"InsightsApiClient.d.ts","sourceRoot":"","sources":["../../src/insights/InsightsApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAA;AAE9E,OAAO,aAAa,EAAE,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAIhE;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,2CAA2C,CAAA;AAEzE;;;;GAIG;AACH,UAAU,cAAc;IACtB;;;;;;;;;;;OAWG;IACH,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,EAAE,uBAAuB,KAAK,OAAO,CAAA;CAC9E;AAED;;;;GAIG;AACH,MAAM,WAAW,uBAAwB,SAAQ,SAAS,EAAE,cAAc;CAAG;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAkB,SAAQ,aAAa;IAC1D;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAElC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAiC;IAE/D;;;;;;;;;;;;;;;OAeG;gBACS,MAAM,EAAE,uBAAuB;IAU3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACU,eAAe,CAC1B,OAAO,EAAE,uBAAuB,EAChC,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,OAAO,CAAC;CAiDpB"}
@@ -1,143 +0,0 @@
1
- import { BatchInsightsEventArray } from '@contentful/optimization-api-schemas';
2
- import { createScopedLogger } from 'logger';
3
- import ApiClientBase from '../ApiClientBase';
4
- const logger = createScopedLogger('ApiClient:Insights');
5
- /**
6
- * Default base URL for the Insights ingest API.
7
- *
8
- * @public
9
- */
10
- export const INSIGHTS_BASE_URL = 'https://ingest.insights.ninetailed.co/';
11
- /**
12
- * Client for sending analytics and insights events to the Ninetailed Insights API.
13
- *
14
- * @public
15
- *
16
- * @remarks
17
- * This client is optimized for sending batched events, optionally using a
18
- * custom beacon-like handler when available.
19
- *
20
- * @example
21
- * ```ts
22
- * const insightsClient = new InsightsApiClient({
23
- * clientId: 'org-id',
24
- * environment: 'main',
25
- * preview: false,
26
- * })
27
- *
28
- * await insightsClient.sendBatchEvents([
29
- * {
30
- * profile: { id: 'profile-123', ... },
31
- * events: [
32
- * {
33
- * type: 'track',
34
- * event: 'button_clicked',
35
- * properties: { id: 'primary-cta' },
36
- * },
37
- * ],
38
- * }
39
- * ])
40
- * ```
41
- */
42
- export default class InsightsApiClient extends ApiClientBase {
43
- /**
44
- * Base URL used for Insights API requests.
45
- */
46
- baseUrl;
47
- /**
48
- * Optional handler used to enqueue events via the Beacon API or a similar mechanism.
49
- */
50
- beaconHandler;
51
- /**
52
- * Creates a new {@link InsightsApiClient} instance.
53
- *
54
- * @param config - Configuration for the Insights API client.
55
- *
56
- * @example
57
- * ```ts
58
- * const client = new InsightsApiClient({
59
- * clientId: 'org-id',
60
- * environment: 'main',
61
- * beaconHandler: (url, data) => {
62
- * return navigator.sendBeacon(url.toString(), JSON.stringify(data))
63
- * },
64
- * })
65
- * ```
66
- */
67
- constructor(config) {
68
- super('Insights', config);
69
- const { baseUrl, beaconHandler } = config;
70
- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- Set default for anything falsey
71
- this.baseUrl = baseUrl || INSIGHTS_BASE_URL;
72
- this.beaconHandler = beaconHandler;
73
- }
74
- /**
75
- * Sends batches of insights events to the Ninetailed Insights API.
76
- *
77
- * @param batches - Array of event batches to send.
78
- * @param options - Optional request options, including a per-call `beaconHandler`.
79
- * @returns A promise that resolves when the events have been sent or queued.
80
- *
81
- * @remarks
82
- * If a `beaconHandler` is provided (either in the method call or in the
83
- * client configuration) it will be invoked first. When the handler returns
84
- * `true`, the events are considered successfully queued and no network
85
- * request is made by this method.
86
- *
87
- * If the handler is missing or returns `false`, the events are emitted
88
- * immediately via `fetch`.
89
- *
90
- * @returns A boolean value that is true when either the event batch is successfully
91
- * queued by the beacon handler or a direct request is successfully sent.
92
- *
93
- * @example
94
- * ```ts
95
- * const success = await insightsClient.sendBatchEvents(batches)
96
- * ```
97
- *
98
- * @example
99
- * ```ts
100
- * // Override beaconHandler for a single call
101
- * const success = await insightsClient.sendBatchEvents(batches, {
102
- * beaconHandler: (url, data) => {
103
- * return navigator.sendBeacon(url.toString(), JSON.stringify(data))
104
- * },
105
- * })
106
- * ```
107
- */
108
- async sendBatchEvents(batches, options = {}) {
109
- const { beaconHandler = this.beaconHandler } = options;
110
- const url = new URL(`v1/organizations/${this.clientId}/environments/${this.environment}/events`, this.baseUrl);
111
- const body = BatchInsightsEventArray.parse(batches);
112
- if (typeof beaconHandler === 'function') {
113
- logger.debug('Queueing events via beaconHandler');
114
- const beaconSuccessfullyQueued = beaconHandler(url, body);
115
- if (beaconSuccessfullyQueued) {
116
- return true;
117
- }
118
- else {
119
- logger.warn('beaconHandler failed to queue events; events will be emitted immediately via fetch');
120
- }
121
- }
122
- const requestName = 'Event Batches';
123
- logger.info(`Sending "${requestName}" request`);
124
- logger.debug(`"${requestName}" request body:`, body);
125
- try {
126
- await this.fetch(url, {
127
- method: 'POST',
128
- headers: {
129
- 'Content-Type': 'application/json',
130
- },
131
- body: JSON.stringify(body),
132
- keepalive: true,
133
- });
134
- logger.debug(`"${requestName}" request successfully completed`);
135
- return true;
136
- }
137
- catch (error) {
138
- this.logRequestError(error, { requestName });
139
- return false;
140
- }
141
- }
142
- }
143
- //# sourceMappingURL=InsightsApiClient.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"InsightsApiClient.js","sourceRoot":"","sources":["../../src/insights/InsightsApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAA;AAC9E,OAAO,EAAE,kBAAkB,EAAE,MAAM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7B,OAAO,IAAI,CAAA;YACb,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,IAAI,CACT,oFAAoF,CACrF,CAAA;YACH,CAAC;QACH,CAAC;QAED,MAAM,WAAW,GAAG,eAAe,CAAA;QAEnC,MAAM,CAAC,IAAI,CAAC,YAAY,WAAW,WAAW,CAAC,CAAA;QAE/C,MAAM,CAAC,KAAK,CAAC,IAAI,WAAW,iBAAiB,EAAE,IAAI,CAAC,CAAA;QAEpD,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE;gBACpB,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE;oBACP,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;gBAC1B,SAAS,EAAE,IAAI;aAChB,CAAC,CAAA;YAEF,MAAM,CAAC,KAAK,CAAC,IAAI,WAAW,kCAAkC,CAAC,CAAA;YAE/D,OAAO,IAAI,CAAA;QACb,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,EAAE,WAAW,EAAE,CAAC,CAAA;YAE5C,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;CACF"}
@@ -1,4 +0,0 @@
1
- import InsightsApiClient from './InsightsApiClient';
2
- export * from './InsightsApiClient';
3
- export default InsightsApiClient;
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/insights/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,qBAAqB,CAAA;AAEnD,cAAc,qBAAqB,CAAA;AAEnC,eAAe,iBAAiB,CAAA"}
@@ -1,4 +0,0 @@
1
- import InsightsApiClient from './InsightsApiClient';
2
- export * from './InsightsApiClient';
3
- export default InsightsApiClient;
4
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/insights/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,qBAAqB,CAAA;AAEnD,cAAc,qBAAqB,CAAA;AAEnC,eAAe,iBAAiB,CAAA"}