@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
@@ -0,0 +1,287 @@
1
+ /**
2
+ * A {@link LogSink} that writes log events to the browser or Node.js console,
3
+ * filtering by a configurable verbosity threshold.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * import { logger, ConsoleLogSink } from '@contentful/optimization-api-client/logger'
8
+ *
9
+ * logger.addSink(new ConsoleLogSink('debug'))
10
+ * ```
11
+ *
12
+ * @public
13
+ */
14
+ export declare class ConsoleLogSink extends LogSink {
15
+ /** Identifies this sink when registered with the {@link Logger}. */
16
+ name: string;
17
+ /**
18
+ * Minimum log level required for events to be output.
19
+ *
20
+ * @defaultValue 'error'
21
+ */
22
+ readonly verbosity: LogLevels;
23
+ /**
24
+ * Creates a new ConsoleLogSink.
25
+ *
26
+ * @param verbosity - Minimum log level to output.
27
+ */
28
+ constructor(verbosity?: LogLevels);
29
+ /**
30
+ * Writes a log event to the console if its level meets the verbosity threshold.
31
+ *
32
+ * @param event - The log event to process.
33
+ * @returns Nothing.
34
+ */
35
+ ingest(event: LogEvent): void;
36
+ }
37
+
38
+ /**
39
+ * Creates a {@link ScopedLogger} that automatically prepends the given location to every log call.
40
+ *
41
+ * @param location - The module or component identifier to prepend.
42
+ * @returns A {@link ScopedLogger} bound to the specified location.
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * import { createScopedLogger } from '@contentful/optimization-api-client/logger'
47
+ *
48
+ * const log = createScopedLogger('MyModule')
49
+ * log.info('Initialization complete')
50
+ * ```
51
+ *
52
+ * @public
53
+ */
54
+ export declare function createScopedLogger(location: string): ScopedLogger;
55
+
56
+ /**
57
+ * A log event emitted by the logger package.
58
+ *
59
+ * @public
60
+ */
61
+ export declare interface LogEvent {
62
+ /** Logger scope name. */
63
+ name: string;
64
+ /** Event severity level. */
65
+ level: LogLevels;
66
+ /** Event payload, where the first entry is the formatted message. */
67
+ messages: unknown[];
68
+ /** Optional additional context attached by sinks or middleware. */
69
+ [other: string]: unknown;
70
+ }
71
+
72
+ /**
73
+ * Central logger that routes log events through registered {@link LogSink} instances.
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * import { logger } from '@contentful/optimization-api-client/logger'
78
+ *
79
+ * logger.info('MyModule', 'Application started')
80
+ * ```
81
+ *
82
+ * @public
83
+ */
84
+ declare class Logger {
85
+ /** The logger's identifier, used as the event scope name. */
86
+ readonly name = "@contentful/optimization";
87
+ private readonly PREFIX_PARTS;
88
+ private readonly DELIMITER;
89
+ private sinks;
90
+ private assembleLocationPrefix;
91
+ /**
92
+ * Registers a log sink. If a sink with the same name already exists, it is replaced.
93
+ *
94
+ * @param sink - The {@link LogSink} instance to register.
95
+ * @returns Nothing.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { logger, ConsoleLogSink } from '@contentful/optimization-api-client/logger'
100
+ *
101
+ * logger.addSink(new ConsoleLogSink('debug'))
102
+ * ```
103
+ */
104
+ addSink(sink: LogSink): void;
105
+ /**
106
+ * Removes a registered sink by name.
107
+ *
108
+ * @param name - The name of the sink to remove.
109
+ * @returns Nothing.
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * logger.removeSink('ConsoleLogSink')
114
+ * ```
115
+ */
116
+ removeSink(name: string): void;
117
+ /**
118
+ * Removes all registered sinks.
119
+ *
120
+ * @returns Nothing.
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * logger.removeSinks()
125
+ * ```
126
+ */
127
+ removeSinks(): void;
128
+ /**
129
+ * Logs a message at the debug level.
130
+ *
131
+ * @param logLocation - The module or component identifier.
132
+ * @param message - The log message.
133
+ * @param args - Additional arguments forwarded in the log event.
134
+ * @returns Nothing.
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * logger.debug('MyModule', 'Debugging value', someVariable)
139
+ * ```
140
+ */
141
+ debug(logLocation: string, message: string, ...args: unknown[]): void;
142
+ /**
143
+ * Logs a message at the info level.
144
+ *
145
+ * @param logLocation - The module or component identifier.
146
+ * @param message - The log message.
147
+ * @param args - Additional arguments forwarded in the log event.
148
+ * @returns Nothing.
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * logger.info('MyModule', 'Operation completed')
153
+ * ```
154
+ */
155
+ info(logLocation: string, message: string, ...args: unknown[]): void;
156
+ /**
157
+ * Logs a message at the log level.
158
+ *
159
+ * @param logLocation - The module or component identifier.
160
+ * @param message - The log message.
161
+ * @param args - Additional arguments forwarded in the log event.
162
+ * @returns Nothing.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * logger.log('MyModule', 'General log entry')
167
+ * ```
168
+ */
169
+ log(logLocation: string, message: string, ...args: unknown[]): void;
170
+ /**
171
+ * Logs a message at the warn level.
172
+ *
173
+ * @param logLocation - The module or component identifier.
174
+ * @param message - The log message.
175
+ * @param args - Additional arguments forwarded in the log event.
176
+ * @returns Nothing.
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * logger.warn('MyModule', 'Deprecated method called')
181
+ * ```
182
+ */
183
+ warn(logLocation: string, message: string, ...args: unknown[]): void;
184
+ /**
185
+ * Logs a message at the error level.
186
+ *
187
+ * @param logLocation - The module or component identifier.
188
+ * @param message - The log message or Error object.
189
+ * @param args - Additional arguments forwarded in the log event.
190
+ * @returns Nothing.
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * logger.error('MyModule', new Error('Something failed'))
195
+ * ```
196
+ */
197
+ error(logLocation: string, message: string | Error, ...args: unknown[]): void;
198
+ /**
199
+ * Logs a message at the fatal level.
200
+ *
201
+ * @param logLocation - The module or component identifier.
202
+ * @param message - The log message or Error object.
203
+ * @param args - Additional arguments forwarded in the log event.
204
+ * @returns Nothing.
205
+ *
206
+ * @example
207
+ * ```typescript
208
+ * logger.fatal('MyModule', new Error('Unrecoverable failure'))
209
+ * ```
210
+ */
211
+ fatal(logLocation: string, message: string | Error, ...args: unknown[]): void;
212
+ private emit;
213
+ private onLogEvent;
214
+ }
215
+ export { Logger }
216
+ export default Logger;
217
+
218
+ /**
219
+ * Shared singleton {@link Logger} instance used across the SDK.
220
+ *
221
+ * @public
222
+ */
223
+ export declare const logger: Logger;
224
+
225
+ /**
226
+ * Supported log levels ordered from highest to lowest severity.
227
+ *
228
+ * @public
229
+ */
230
+ export declare type LogLevels = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'log';
231
+
232
+ /**
233
+ * Numeric severity map used for log-level threshold comparisons.
234
+ *
235
+ * @public
236
+ */
237
+ export declare const logLevelSeverity: Readonly<Record<LogLevels, number>>;
238
+
239
+ /**
240
+ * Abstract base class for log sinks that receive and process log events.
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * class MyCustomSink extends LogSink {
245
+ * name = 'MyCustomSink'
246
+ *
247
+ * ingest(event: LogEvent): void {
248
+ * // process the event
249
+ * }
250
+ * }
251
+ * ```
252
+ *
253
+ * @public
254
+ */
255
+ export declare abstract class LogSink {
256
+ /** Display name used to identify this sink for addition and removal. */
257
+ abstract name: string;
258
+ /**
259
+ * Processes an incoming log event.
260
+ *
261
+ * @param event - The log event to process.
262
+ * @returns Nothing.
263
+ */
264
+ abstract ingest(event: LogEvent): void;
265
+ }
266
+
267
+ /**
268
+ * A location-scoped logger interface whose methods omit the `logLocation` parameter.
269
+ *
270
+ * @public
271
+ */
272
+ export declare interface ScopedLogger {
273
+ /** Logs at debug level. */
274
+ debug: (message: string, ...args: unknown[]) => void;
275
+ /** Logs at info level. */
276
+ info: (message: string, ...args: unknown[]) => void;
277
+ /** Logs at log level. */
278
+ log: (message: string, ...args: unknown[]) => void;
279
+ /** Logs at warn level. */
280
+ warn: (message: string, ...args: unknown[]) => void;
281
+ /** Logs at error level. */
282
+ error: (message: string | Error, ...args: unknown[]) => void;
283
+ /** Logs at fatal level. */
284
+ fatal: (message: string | Error, ...args: unknown[]) => void;
285
+ }
286
+
287
+ export { }
@@ -0,0 +1 @@
1
+ export { ConsoleLogSink, LogSink, Logger, createScopedLogger, default, logLevelSeverity, logger } from "./649.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/optimization-api-client",
3
- "version": "0.1.0-alpha8",
3
+ "version": "0.1.0-alpha9",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -17,16 +17,34 @@
17
17
  "default": "./dist/index.cjs"
18
18
  }
19
19
  },
20
+ "./logger": {
21
+ "import": {
22
+ "types": "./dist/logger.d.mts",
23
+ "default": "./dist/logger.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/logger.d.cts",
27
+ "default": "./dist/logger.cjs"
28
+ }
29
+ },
30
+ "./api-schemas": {
31
+ "import": {
32
+ "types": "./dist/api-schemas.d.mts",
33
+ "default": "./dist/api-schemas.mjs"
34
+ },
35
+ "require": {
36
+ "types": "./dist/api-schemas.d.cts",
37
+ "default": "./dist/api-schemas.cjs"
38
+ }
39
+ },
20
40
  "./package.json": "./package.json"
21
41
  },
22
42
  "files": [
23
43
  "dist/**/*"
24
44
  ],
25
45
  "dependencies": {
26
- "diary": "^0.4.5",
27
- "es-toolkit": "^1.39.10",
28
- "p-retry": "^6.2.1",
29
- "zod": "^4.1.5",
30
- "@contentful/optimization-api-schemas": "0.1.0-alpha8"
46
+ "es-toolkit": "^1.44.0",
47
+ "zod": "^4.3.6",
48
+ "@contentful/optimization-api-schemas": "0.1.0-alpha9"
31
49
  }
32
50
  }
@@ -1,74 +0,0 @@
1
- import type { ApiConfig, GlobalApiConfigProperties } from './ApiClientBase';
2
- import ExperienceApiClient, { type ExperienceApiClientConfig } from './experience';
3
- import InsightsApiClient, { type InsightsApiClientConfig } from './insights';
4
- /**
5
- * Configuration for the high-level {@link ApiClient}.
6
- *
7
- * @public
8
- */
9
- export interface ApiClientConfig extends Pick<ApiConfig, GlobalApiConfigProperties> {
10
- /**
11
- * Configuration for the personalization (Experience) API client.
12
- *
13
- * @remarks
14
- * Any properties shared with {@link ApiConfig} are taken from the top-level
15
- * config and overridden by this object when specified.
16
- */
17
- personalization?: Omit<ExperienceApiClientConfig, GlobalApiConfigProperties>;
18
- /**
19
- * Configuration for the analytics (Insights) API client.
20
- *
21
- * @remarks
22
- * Any properties shared with {@link ApiConfig} are taken from the top-level
23
- * config and overridden by this object when specified.
24
- */
25
- analytics?: Omit<InsightsApiClientConfig, GlobalApiConfigProperties>;
26
- }
27
- /**
28
- * Aggregated API client providing access to Experience and Insights APIs.
29
- *
30
- * @public
31
- *
32
- * @remarks
33
- * This client encapsulates shared configuration and exposes dedicated
34
- * sub-clients for personalization and analytics use cases.
35
- *
36
- * @example
37
- * ```ts
38
- * const client = new ApiClient({
39
- * clientId: 'org-id',
40
- * environment: 'main',
41
- * preview: false,
42
- * personalization: {
43
- * // experience-specific overrides
44
- * },
45
- * analytics: {
46
- * // insights-specific overrides
47
- * },
48
- * })
49
- *
50
- * const profile = await client.experience.getProfile('profile-id')
51
- * const batch = await client.insights.upsertManyProfiles({ events: batchEvents })
52
- * ```
53
- */
54
- export default class ApiClient {
55
- /**
56
- * Shared configuration applied to both Experience and Insights clients.
57
- */
58
- readonly config: ApiConfig;
59
- /**
60
- * Client for personalization and experience-related operations.
61
- */
62
- readonly experience: ExperienceApiClient;
63
- /**
64
- * Client for analytics and insights-related operations.
65
- */
66
- readonly insights: InsightsApiClient;
67
- /**
68
- * Creates a new aggregated {@link ApiClient} instance.
69
- *
70
- * @param config - Global API client configuration with optional per-client overrides.
71
- */
72
- constructor(config: ApiClientConfig);
73
- }
74
- //# sourceMappingURL=ApiClient.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApiClient.d.ts","sourceRoot":"","sources":["../src/ApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAA;AAC3E,OAAO,mBAAmB,EAAE,EAAE,KAAK,yBAAyB,EAAE,MAAM,cAAc,CAAA;AAClF,OAAO,iBAAiB,EAAE,EAAE,KAAK,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAE5E;;;;GAIG;AACH,MAAM,WAAW,eAAgB,SAAQ,IAAI,CAAC,SAAS,EAAE,yBAAyB,CAAC;IACjF;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,IAAI,CAAC,yBAAyB,EAAE,yBAAyB,CAAC,CAAA;IAE5E;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,IAAI,CAAC,uBAAuB,EAAE,yBAAyB,CAAC,CAAA;CACrE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAA;IAE1B;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,mBAAmB,CAAA;IAExC;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAA;IAEpC;;;;OAIG;gBACS,MAAM,EAAE,eAAe;CAepC"}
package/dist/ApiClient.js DELETED
@@ -1,61 +0,0 @@
1
- import ExperienceApiClient from './experience';
2
- import InsightsApiClient from './insights';
3
- /**
4
- * Aggregated API client providing access to Experience and Insights APIs.
5
- *
6
- * @public
7
- *
8
- * @remarks
9
- * This client encapsulates shared configuration and exposes dedicated
10
- * sub-clients for personalization and analytics use cases.
11
- *
12
- * @example
13
- * ```ts
14
- * const client = new ApiClient({
15
- * clientId: 'org-id',
16
- * environment: 'main',
17
- * preview: false,
18
- * personalization: {
19
- * // experience-specific overrides
20
- * },
21
- * analytics: {
22
- * // insights-specific overrides
23
- * },
24
- * })
25
- *
26
- * const profile = await client.experience.getProfile('profile-id')
27
- * const batch = await client.insights.upsertManyProfiles({ events: batchEvents })
28
- * ```
29
- */
30
- export default class ApiClient {
31
- /**
32
- * Shared configuration applied to both Experience and Insights clients.
33
- */
34
- config;
35
- /**
36
- * Client for personalization and experience-related operations.
37
- */
38
- experience;
39
- /**
40
- * Client for analytics and insights-related operations.
41
- */
42
- insights;
43
- /**
44
- * Creates a new aggregated {@link ApiClient} instance.
45
- *
46
- * @param config - Global API client configuration with optional per-client overrides.
47
- */
48
- constructor(config) {
49
- const { personalization, analytics, ...apiConfig } = config;
50
- this.config = apiConfig;
51
- this.experience = new ExperienceApiClient({
52
- ...apiConfig,
53
- ...personalization,
54
- });
55
- this.insights = new InsightsApiClient({
56
- ...apiConfig,
57
- ...analytics,
58
- });
59
- }
60
- }
61
- //# sourceMappingURL=ApiClient.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApiClient.js","sourceRoot":"","sources":["../src/ApiClient.ts"],"names":[],"mappings":"AACA,OAAO,mBAAuD,MAAM,cAAc,CAAA;AAClF,OAAO,iBAAmD,MAAM,YAAY,CAAA;AA2B5E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;IAC5B;;OAEG;IACM,MAAM,CAAW;IAE1B;;OAEG;IACM,UAAU,CAAqB;IAExC;;OAEG;IACM,QAAQ,CAAmB;IAEpC;;;;OAIG;IACH,YAAY,MAAuB;QACjC,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE,GAAG,MAAM,CAAA;QAE3D,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;QAEvB,IAAI,CAAC,UAAU,GAAG,IAAI,mBAAmB,CAAC;YACxC,GAAG,SAAS;YACZ,GAAG,eAAe;SACnB,CAAC,CAAA;QAEF,IAAI,CAAC,QAAQ,GAAG,IAAI,iBAAiB,CAAC;YACpC,GAAG,SAAS;YACZ,GAAG,SAAS;SACb,CAAC,CAAA;IACJ,CAAC;CACF"}
@@ -1,113 +0,0 @@
1
- import { type FetchMethod, type ProtectedFetchMethodOptions } from './fetch';
2
- /**
3
- * Configuration options for API clients extending {@link ApiClientBase}.
4
- *
5
- * @public
6
- */
7
- export interface ApiConfig {
8
- /**
9
- * Base URL for the API.
10
- *
11
- * @remarks
12
- * When omitted, the concrete client is expected to construct full URLs
13
- * internally.
14
- */
15
- baseUrl?: string;
16
- /**
17
- * Contentful environment identifier.
18
- *
19
- * @remarks
20
- * Defaults to `main` when not provided.
21
- */
22
- environment?: string;
23
- /**
24
- * Options used to configure the underlying protected fetch method.
25
- *
26
- * @remarks
27
- * `apiName` is derived from the client name and must not be provided here.
28
- */
29
- fetchOptions?: Omit<ProtectedFetchMethodOptions, 'apiName'>;
30
- /**
31
- * Client identifier used for authentication or tracking.
32
- */
33
- clientId: string;
34
- }
35
- /**
36
- * Properties that may be shared between global and per-client API configuration.
37
- *
38
- * @public
39
- */
40
- export type GlobalApiConfigProperties = 'environment' | 'fetchOptions' | 'clientId';
41
- /**
42
- * Base class for API clients that provides shared configuration and error logging.
43
- *
44
- * @internal
45
- *
46
- * @remarks
47
- * This abstract class is intended for internal use within the package and
48
- * should not be treated as part of the public API surface.
49
- *
50
- * Concrete API clients should extend this class to inherit consistent logging
51
- * behavior and fetch configuration.
52
- *
53
- * @example
54
- * ```ts
55
- * interface MyClientConfig extends ApiConfig {
56
- * // additional config
57
- * }
58
- *
59
- * class MyClient extends ApiClientBase {
60
- * constructor(config: MyClientConfig) {
61
- * super('MyClient', config)
62
- * }
63
- *
64
- * async getSomething() {
65
- * const response = await this.fetch('https://example.com', { method: 'GET' })
66
- * return response.json()
67
- * }
68
- * }
69
- * ```
70
- */
71
- declare abstract class ApiClientBase {
72
- /**
73
- * Name of the API client, used in log messages and as the `apiName` for fetch.
74
- */
75
- protected readonly name: string;
76
- /**
77
- * Client identifier used for authentication or tracking.
78
- */
79
- protected readonly clientId: string;
80
- /**
81
- * Contentful environment associated with this client.
82
- */
83
- protected readonly environment: string;
84
- /**
85
- * Protected fetch method used by the client to perform HTTP requests.
86
- */
87
- protected readonly fetch: FetchMethod;
88
- /**
89
- * Creates a new API client base instance.
90
- *
91
- * @param name - Human-readable name of the client (used for logging and `apiName`).
92
- * @param config - Configuration options for the client.
93
- */
94
- constructor(name: string, { fetchOptions, clientId, environment }: ApiConfig);
95
- /**
96
- * Logs errors that occur during API requests with standardized messages.
97
- *
98
- * @param error - The error thrown by the underlying operation.
99
- * @param options - Additional metadata about the request.
100
- * @param options.requestName - Human-readable name of the request operation.
101
- *
102
- * @protected
103
- *
104
- * @remarks
105
- * Abort errors are logged at `warn` level and other errors at `error` level.
106
- * The log message includes the client name for better debugging context.
107
- */
108
- protected logRequestError(error: unknown, { requestName }: {
109
- requestName: string;
110
- }): void;
111
- }
112
- export default ApiClientBase;
113
- //# sourceMappingURL=ApiClientBase.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApiClientBase.d.ts","sourceRoot":"","sources":["../src/ApiClientBase.ts"],"names":[],"mappings":"AACA,OAAc,EAAE,KAAK,WAAW,EAAE,KAAK,2BAA2B,EAAE,MAAM,SAAS,CAAA;AAWnF;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,IAAI,CAAC,2BAA2B,EAAE,SAAS,CAAC,CAAA;IAE3D;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED;;;;GAIG;AACH,MAAM,MAAM,yBAAyB,GAAG,aAAa,GAAG,cAAc,GAAG,UAAU,CAAA;AAEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,uBAAe,aAAa;IAC1B;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAE/B;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IAEnC;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAEtC;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAA;IAErC;;;;;OAKG;gBACS,IAAI,EAAE,MAAM,EAAE,EAAE,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,EAAE,SAAS;IAQ5E;;;;;;;;;;;;OAYG;IACH,SAAS,CAAC,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,WAAW,EAAE,EAAE;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;CAW1F;AAED,eAAe,aAAa,CAAA"}
@@ -1,94 +0,0 @@
1
- import { createScopedLogger } from 'logger';
2
- import Fetch from './fetch';
3
- const logger = createScopedLogger('ApiClient');
4
- /**
5
- * Default Contentful environment used when none is explicitly provided.
6
- *
7
- * @internal
8
- */
9
- const DEFAULT_ENVIRONMENT = 'main';
10
- /**
11
- * Base class for API clients that provides shared configuration and error logging.
12
- *
13
- * @internal
14
- *
15
- * @remarks
16
- * This abstract class is intended for internal use within the package and
17
- * should not be treated as part of the public API surface.
18
- *
19
- * Concrete API clients should extend this class to inherit consistent logging
20
- * behavior and fetch configuration.
21
- *
22
- * @example
23
- * ```ts
24
- * interface MyClientConfig extends ApiConfig {
25
- * // additional config
26
- * }
27
- *
28
- * class MyClient extends ApiClientBase {
29
- * constructor(config: MyClientConfig) {
30
- * super('MyClient', config)
31
- * }
32
- *
33
- * async getSomething() {
34
- * const response = await this.fetch('https://example.com', { method: 'GET' })
35
- * return response.json()
36
- * }
37
- * }
38
- * ```
39
- */
40
- class ApiClientBase {
41
- /**
42
- * Name of the API client, used in log messages and as the `apiName` for fetch.
43
- */
44
- name;
45
- /**
46
- * Client identifier used for authentication or tracking.
47
- */
48
- clientId;
49
- /**
50
- * Contentful environment associated with this client.
51
- */
52
- environment;
53
- /**
54
- * Protected fetch method used by the client to perform HTTP requests.
55
- */
56
- fetch;
57
- /**
58
- * Creates a new API client base instance.
59
- *
60
- * @param name - Human-readable name of the client (used for logging and `apiName`).
61
- * @param config - Configuration options for the client.
62
- */
63
- constructor(name, { fetchOptions, clientId, environment }) {
64
- this.clientId = clientId;
65
- this.environment = environment ?? DEFAULT_ENVIRONMENT;
66
- this.name = name;
67
- this.fetch = Fetch.create({ ...(fetchOptions ?? {}), apiName: name });
68
- }
69
- /**
70
- * Logs errors that occur during API requests with standardized messages.
71
- *
72
- * @param error - The error thrown by the underlying operation.
73
- * @param options - Additional metadata about the request.
74
- * @param options.requestName - Human-readable name of the request operation.
75
- *
76
- * @protected
77
- *
78
- * @remarks
79
- * Abort errors are logged at `warn` level and other errors at `error` level.
80
- * The log message includes the client name for better debugging context.
81
- */
82
- logRequestError(error, { requestName }) {
83
- if (error instanceof Error) {
84
- if (error.name === 'AbortError') {
85
- logger.warn(`[${this.name}] "${requestName}" request aborted due to network issues. This request may not be retried.`);
86
- }
87
- else {
88
- logger.error(`[${this.name}] "${requestName}" request failed:`, error);
89
- }
90
- }
91
- }
92
- }
93
- export default ApiClientBase;
94
- //# sourceMappingURL=ApiClientBase.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApiClientBase.js","sourceRoot":"","sources":["../src/ApiClientBase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAA;AAC3C,OAAO,KAA6D,MAAM,SAAS,CAAA;AAEnF,MAAM,MAAM,GAAG,kBAAkB,CAAC,WAAW,CAAC,CAAA;AAE9C;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,MAAM,CAAA;AA8ClC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAe,aAAa;IAC1B;;OAEG;IACgB,IAAI,CAAQ;IAE/B;;OAEG;IACgB,QAAQ,CAAQ;IAEnC;;OAEG;IACgB,WAAW,CAAQ;IAEtC;;OAEG;IACgB,KAAK,CAAa;IAErC;;;;;OAKG;IACH,YAAY,IAAY,EAAE,EAAE,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAa;QAC1E,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,WAAW,GAAG,WAAW,IAAI,mBAAmB,CAAA;QACrD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAEhB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;IACvE,CAAC;IAED;;;;;;;;;;;;OAYG;IACO,eAAe,CAAC,KAAc,EAAE,EAAE,WAAW,EAA2B;QAChF,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAChC,MAAM,CAAC,IAAI,CACT,IAAI,IAAI,CAAC,IAAI,MAAM,WAAW,2EAA2E,CAC1G,CAAA;YACH,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,IAAI,MAAM,WAAW,mBAAmB,EAAE,KAAK,CAAC,CAAA;YACxE,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED,eAAe,aAAa,CAAA"}