zlient 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -72,6 +72,179 @@ var BearerTokenAuth = class {
72
72
  }
73
73
  };
74
74
  //#endregion
75
+ //#region lib/logger.ts
76
+ /**
77
+ * Log levels for structured logging.
78
+ */
79
+ let LogLevel = /* @__PURE__ */ function(LogLevel) {
80
+ LogLevel["DEBUG"] = "debug";
81
+ LogLevel["INFO"] = "info";
82
+ LogLevel["WARN"] = "warn";
83
+ LogLevel["ERROR"] = "error";
84
+ return LogLevel;
85
+ }({});
86
+ /**
87
+ * Default console logger implementation.
88
+ * Formats log entries as JSON for easy parsing.
89
+ */
90
+ var ConsoleLogger = class {
91
+ constructor(minLevel = LogLevel.INFO) {
92
+ this.minLevel = minLevel;
93
+ }
94
+ log(entry) {
95
+ const levels = [
96
+ LogLevel.DEBUG,
97
+ LogLevel.INFO,
98
+ LogLevel.WARN,
99
+ LogLevel.ERROR
100
+ ];
101
+ if (levels.indexOf(entry.level) < levels.indexOf(this.minLevel)) return;
102
+ const output = {
103
+ ...entry,
104
+ error: entry.error ? {
105
+ message: entry.error.message,
106
+ stack: entry.error.stack,
107
+ name: entry.error.name
108
+ } : void 0
109
+ };
110
+ switch (entry.level) {
111
+ case LogLevel.DEBUG:
112
+ console.debug(JSON.stringify(output));
113
+ break;
114
+ case LogLevel.INFO:
115
+ console.info(JSON.stringify(output));
116
+ break;
117
+ case LogLevel.WARN:
118
+ console.warn(JSON.stringify(output));
119
+ break;
120
+ case LogLevel.ERROR:
121
+ console.error(JSON.stringify(output));
122
+ break;
123
+ }
124
+ }
125
+ };
126
+ /**
127
+ * No-op logger that discards all log entries.
128
+ * Use this in production if you don't want any logging.
129
+ */
130
+ var NoOpLogger = class {
131
+ log(_entry) {}
132
+ };
133
+ /**
134
+ * Utility class for creating structured log entries.
135
+ */
136
+ var LoggerUtil = class {
137
+ constructor(logger) {
138
+ this.logger = logger;
139
+ }
140
+ debug(message, context) {
141
+ this.logger.log({
142
+ level: LogLevel.DEBUG,
143
+ message,
144
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
145
+ context
146
+ });
147
+ }
148
+ info(message, context) {
149
+ this.logger.log({
150
+ level: LogLevel.INFO,
151
+ message,
152
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
153
+ context
154
+ });
155
+ }
156
+ warn(message, context) {
157
+ this.logger.log({
158
+ level: LogLevel.WARN,
159
+ message,
160
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
161
+ context
162
+ });
163
+ }
164
+ error(message, error, context) {
165
+ this.logger.log({
166
+ level: LogLevel.ERROR,
167
+ message,
168
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
169
+ context,
170
+ error
171
+ });
172
+ }
173
+ };
174
+ //#endregion
175
+ //#region lib/metrics.ts
176
+ /**
177
+ * No-op metrics collector that discards all metrics.
178
+ */
179
+ var NoOpMetricsCollector = class {
180
+ collect(_metrics) {}
181
+ };
182
+ /**
183
+ * In-memory metrics collector for testing and development.
184
+ * Stores metrics in memory with configurable retention.
185
+ */
186
+ var InMemoryMetricsCollector = class {
187
+ constructor(maxEntries = 1e3) {
188
+ this.metrics = [];
189
+ this.maxEntries = maxEntries;
190
+ }
191
+ collect(metrics) {
192
+ this.metrics.push(metrics);
193
+ if (this.metrics.length > this.maxEntries) this.metrics.shift();
194
+ }
195
+ /**
196
+ * Get all collected metrics.
197
+ */
198
+ getMetrics() {
199
+ return [...this.metrics];
200
+ }
201
+ /**
202
+ * Get metrics summary statistics.
203
+ */
204
+ getSummary() {
205
+ if (this.metrics.length === 0) return {
206
+ total: 0,
207
+ successful: 0,
208
+ failed: 0,
209
+ avgDurationMs: 0,
210
+ minDurationMs: 0,
211
+ maxDurationMs: 0
212
+ };
213
+ const successful = this.metrics.filter((m) => m.success).length;
214
+ let sum = 0;
215
+ let min = Infinity;
216
+ let max = -Infinity;
217
+ for (const m of this.metrics) {
218
+ const d = m.durationMs;
219
+ sum += d;
220
+ if (d < min) min = d;
221
+ if (d > max) max = d;
222
+ }
223
+ return {
224
+ total: this.metrics.length,
225
+ successful,
226
+ failed: this.metrics.length - successful,
227
+ avgDurationMs: sum / this.metrics.length,
228
+ minDurationMs: min === Infinity ? 0 : min,
229
+ maxDurationMs: max === -Infinity ? 0 : max
230
+ };
231
+ }
232
+ /**
233
+ * Clear all collected metrics.
234
+ */
235
+ clear() {
236
+ this.metrics = [];
237
+ }
238
+ };
239
+ /**
240
+ * Console-based metrics collector for debugging.
241
+ */
242
+ var ConsoleMetricsCollector = class {
243
+ collect(metrics) {
244
+ console.log("[METRICS]", JSON.stringify(metrics));
245
+ }
246
+ };
247
+ //#endregion
75
248
  //#region lib/types.ts
76
249
  const HTTPMethod = {
77
250
  GET: "GET",
@@ -321,7 +494,153 @@ function isStandardSchema(value) {
321
494
  return "~standard" in schema && typeof schema["~standard"] === "object" && schema["~standard"] !== null && schema["~standard"].version === 1 && typeof schema["~standard"].validate === "function";
322
495
  }
323
496
  //#endregion
324
- //#region lib/endpoint/base-endpoint.ts
497
+ //#region lib/sse/sse-client.ts
498
+ var SSEConnectionImpl = class {
499
+ constructor(url, responseSchema, skipResponseValidation = false, withCredentials = false) {
500
+ this.responseSchema = responseSchema;
501
+ this.skipResponseValidation = skipResponseValidation;
502
+ this.handlers = /* @__PURE__ */ new Map();
503
+ if (typeof EventSource === "undefined") throw new Error("EventSource is not defined. Ensure you are in a supported environment.");
504
+ this.es = new EventSource(url, { withCredentials });
505
+ this.es.onopen = (event) => this.emit("open", event);
506
+ this.es.onerror = (event) => this.emit("error", event);
507
+ this.es.onmessage = async (event) => {
508
+ let data = event.data;
509
+ try {
510
+ if (typeof data === "string") try {
511
+ data = JSON.parse(data);
512
+ } catch {}
513
+ if (!this.skipResponseValidation && this.responseSchema) data = await parseOrThrow(this.responseSchema, data);
514
+ this.emit("message", data);
515
+ } catch (error) {
516
+ this.emit("error", error);
517
+ }
518
+ };
519
+ }
520
+ on(event, handler) {
521
+ if (!this.handlers.has(event)) {
522
+ this.handlers.set(event, /* @__PURE__ */ new Set());
523
+ if (event !== "message" && event !== "open" && event !== "error") this.es.addEventListener(event, async (ev) => {
524
+ let data = ev.data;
525
+ try {
526
+ if (typeof data === "string") try {
527
+ data = JSON.parse(data);
528
+ } catch {}
529
+ if (!this.skipResponseValidation && this.responseSchema) data = await parseOrThrow(this.responseSchema, data);
530
+ this.emit(event, data);
531
+ } catch (error) {
532
+ this.emit("error", error);
533
+ }
534
+ });
535
+ }
536
+ this.handlers.get(event).add(handler);
537
+ }
538
+ off(event, handler) {
539
+ const handlers = this.handlers.get(event);
540
+ if (handlers) handlers.delete(handler);
541
+ }
542
+ emit(event, ...args) {
543
+ const handlers = this.handlers.get(event);
544
+ if (handlers) handlers.forEach((handler) => handler(...args));
545
+ }
546
+ close() {
547
+ this.es.close();
548
+ }
549
+ get readyState() {
550
+ return this.es.readyState;
551
+ }
552
+ };
553
+ //#endregion
554
+ //#region lib/sse/sse-endpoint.ts
555
+ var SSEEndpointImpl = class {
556
+ constructor(client, config) {
557
+ this.client = client;
558
+ this.config = config;
559
+ }
560
+ createCall() {
561
+ return (params) => {
562
+ const { query, pathParams } = params || {};
563
+ let pathStr;
564
+ if (typeof this.config.path === "function") {
565
+ if (!pathParams) throw new Error("Path function requires pathParams");
566
+ pathStr = this.config.path(pathParams);
567
+ } else pathStr = this.config.path;
568
+ return new SSEConnectionImpl(`${this.client.getBaseUrl(this.config.advanced?.baseUrlKey || "default")}${pathStr}${toQueryString(query)}`, this.config.response, this.config.advanced?.skipResponseValidation, this.config.advanced?.withCredentials);
569
+ };
570
+ }
571
+ };
572
+ //#endregion
573
+ //#region lib/ws/ws-client.ts
574
+ var WSConnectionImpl = class {
575
+ constructor(url, sendSchema, receiveSchema, skipRequestValidation = false, skipResponseValidation = false, protocols) {
576
+ this.sendSchema = sendSchema;
577
+ this.receiveSchema = receiveSchema;
578
+ this.skipRequestValidation = skipRequestValidation;
579
+ this.skipResponseValidation = skipResponseValidation;
580
+ this.handlers = /* @__PURE__ */ new Map();
581
+ if (typeof WebSocket === "undefined") throw new Error("WebSocket is not defined. Ensure you are in a supported environment.");
582
+ this.ws = new WebSocket(url, protocols);
583
+ this.ws.onopen = () => this.emit("open");
584
+ this.ws.onclose = (event) => this.emit("close", event);
585
+ this.ws.onerror = (event) => this.emit("error", event);
586
+ this.ws.onmessage = async (event) => {
587
+ let data = event.data;
588
+ try {
589
+ if (typeof data === "string") try {
590
+ data = JSON.parse(data);
591
+ } catch {}
592
+ if (!this.skipResponseValidation && this.receiveSchema) data = await parseOrThrow(this.receiveSchema, data);
593
+ this.emit("message", data);
594
+ } catch (error) {
595
+ this.emit("error", error);
596
+ }
597
+ };
598
+ }
599
+ async send(data) {
600
+ if (!this.skipRequestValidation && this.sendSchema) await parseOrThrow(this.sendSchema, data);
601
+ const message = data != null && typeof data === "object" ? JSON.stringify(data) : data;
602
+ this.ws.send(message);
603
+ }
604
+ on(event, handler) {
605
+ if (!this.handlers.has(event)) this.handlers.set(event, /* @__PURE__ */ new Set());
606
+ this.handlers.get(event).add(handler);
607
+ }
608
+ off(event, handler) {
609
+ const handlers = this.handlers.get(event);
610
+ if (handlers) handlers.delete(handler);
611
+ }
612
+ emit(event, ...args) {
613
+ const handlers = this.handlers.get(event);
614
+ if (handlers) handlers.forEach((handler) => handler(...args));
615
+ }
616
+ close(code, reason) {
617
+ this.ws.close(code, reason);
618
+ }
619
+ get readyState() {
620
+ return this.ws.readyState;
621
+ }
622
+ };
623
+ //#endregion
624
+ //#region lib/ws/ws-endpoint.ts
625
+ var WSEndpointImpl = class {
626
+ constructor(client, config) {
627
+ this.client = client;
628
+ this.config = config;
629
+ }
630
+ createCall() {
631
+ return (params) => {
632
+ const { query, pathParams, protocols } = params || {};
633
+ let pathStr;
634
+ if (typeof this.config.path === "function") {
635
+ if (!pathParams) throw new Error("Path function requires pathParams");
636
+ pathStr = this.config.path(pathParams);
637
+ } else pathStr = this.config.path;
638
+ return new WSConnectionImpl(`${this.client.getBaseUrl(this.config.advanced?.baseUrlKey || "default").replace(/^http/, "ws")}${pathStr}${toQueryString(query)}`, this.config.send, this.config.receive, this.config.advanced?.skipRequestValidation, this.config.advanced?.skipResponseValidation, protocols);
639
+ };
640
+ }
641
+ };
642
+ //#endregion
643
+ //#region lib/http/http-endpoint.ts
325
644
  var EndpointImpl = class {
326
645
  constructor(client, config) {
327
646
  this.client = client;
@@ -363,179 +682,6 @@ var EndpointImpl = class {
363
682
  }
364
683
  };
365
684
  //#endregion
366
- //#region lib/logger.ts
367
- /**
368
- * Log levels for structured logging.
369
- */
370
- let LogLevel = /* @__PURE__ */ function(LogLevel) {
371
- LogLevel["DEBUG"] = "debug";
372
- LogLevel["INFO"] = "info";
373
- LogLevel["WARN"] = "warn";
374
- LogLevel["ERROR"] = "error";
375
- return LogLevel;
376
- }({});
377
- /**
378
- * Default console logger implementation.
379
- * Formats log entries as JSON for easy parsing.
380
- */
381
- var ConsoleLogger = class {
382
- constructor(minLevel = LogLevel.INFO) {
383
- this.minLevel = minLevel;
384
- }
385
- log(entry) {
386
- const levels = [
387
- LogLevel.DEBUG,
388
- LogLevel.INFO,
389
- LogLevel.WARN,
390
- LogLevel.ERROR
391
- ];
392
- if (levels.indexOf(entry.level) < levels.indexOf(this.minLevel)) return;
393
- const output = {
394
- ...entry,
395
- error: entry.error ? {
396
- message: entry.error.message,
397
- stack: entry.error.stack,
398
- name: entry.error.name
399
- } : void 0
400
- };
401
- switch (entry.level) {
402
- case LogLevel.DEBUG:
403
- console.debug(JSON.stringify(output));
404
- break;
405
- case LogLevel.INFO:
406
- console.info(JSON.stringify(output));
407
- break;
408
- case LogLevel.WARN:
409
- console.warn(JSON.stringify(output));
410
- break;
411
- case LogLevel.ERROR:
412
- console.error(JSON.stringify(output));
413
- break;
414
- }
415
- }
416
- };
417
- /**
418
- * No-op logger that discards all log entries.
419
- * Use this in production if you don't want any logging.
420
- */
421
- var NoOpLogger = class {
422
- log(_entry) {}
423
- };
424
- /**
425
- * Utility class for creating structured log entries.
426
- */
427
- var LoggerUtil = class {
428
- constructor(logger) {
429
- this.logger = logger;
430
- }
431
- debug(message, context) {
432
- this.logger.log({
433
- level: LogLevel.DEBUG,
434
- message,
435
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
436
- context
437
- });
438
- }
439
- info(message, context) {
440
- this.logger.log({
441
- level: LogLevel.INFO,
442
- message,
443
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
444
- context
445
- });
446
- }
447
- warn(message, context) {
448
- this.logger.log({
449
- level: LogLevel.WARN,
450
- message,
451
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
452
- context
453
- });
454
- }
455
- error(message, error, context) {
456
- this.logger.log({
457
- level: LogLevel.ERROR,
458
- message,
459
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
460
- context,
461
- error
462
- });
463
- }
464
- };
465
- //#endregion
466
- //#region lib/metrics.ts
467
- /**
468
- * No-op metrics collector that discards all metrics.
469
- */
470
- var NoOpMetricsCollector = class {
471
- collect(_metrics) {}
472
- };
473
- /**
474
- * In-memory metrics collector for testing and development.
475
- * Stores metrics in memory with configurable retention.
476
- */
477
- var InMemoryMetricsCollector = class {
478
- constructor(maxEntries = 1e3) {
479
- this.metrics = [];
480
- this.maxEntries = maxEntries;
481
- }
482
- collect(metrics) {
483
- this.metrics.push(metrics);
484
- if (this.metrics.length > this.maxEntries) this.metrics.shift();
485
- }
486
- /**
487
- * Get all collected metrics.
488
- */
489
- getMetrics() {
490
- return [...this.metrics];
491
- }
492
- /**
493
- * Get metrics summary statistics.
494
- */
495
- getSummary() {
496
- if (this.metrics.length === 0) return {
497
- total: 0,
498
- successful: 0,
499
- failed: 0,
500
- avgDurationMs: 0,
501
- minDurationMs: 0,
502
- maxDurationMs: 0
503
- };
504
- const successful = this.metrics.filter((m) => m.success).length;
505
- let sum = 0;
506
- let min = Infinity;
507
- let max = -Infinity;
508
- for (const m of this.metrics) {
509
- const d = m.durationMs;
510
- sum += d;
511
- if (d < min) min = d;
512
- if (d > max) max = d;
513
- }
514
- return {
515
- total: this.metrics.length,
516
- successful,
517
- failed: this.metrics.length - successful,
518
- avgDurationMs: sum / this.metrics.length,
519
- minDurationMs: min === Infinity ? 0 : min,
520
- maxDurationMs: max === -Infinity ? 0 : max
521
- };
522
- }
523
- /**
524
- * Clear all collected metrics.
525
- */
526
- clear() {
527
- this.metrics = [];
528
- }
529
- };
530
- /**
531
- * Console-based metrics collector for debugging.
532
- */
533
- var ConsoleMetricsCollector = class {
534
- collect(metrics) {
535
- console.log("[METRICS]", JSON.stringify(metrics));
536
- }
537
- };
538
- //#endregion
539
685
  //#region lib/http/http-client.ts
540
686
  /**
541
687
  * HTTP client with built-in authentication, and interceptors.
@@ -896,8 +1042,26 @@ var HttpClient = class {
896
1042
  const endpoint = new EndpointImpl(this, config);
897
1043
  return (params) => endpoint.call(params);
898
1044
  }
1045
+ /**
1046
+ * Create a strongly-typed WebSocket endpoint builder.
1047
+ *
1048
+ * @param config - WebSocket endpoint configuration
1049
+ * @returns WebSocket endpoint call function
1050
+ */
1051
+ createWebSocket(config) {
1052
+ return new WSEndpointImpl(this, config).createCall();
1053
+ }
1054
+ /**
1055
+ * Create a strongly-typed Server-Sent Events (SSE) endpoint builder.
1056
+ *
1057
+ * @param config - SSE endpoint configuration
1058
+ * @returns SSE endpoint call function
1059
+ */
1060
+ createSSE(config) {
1061
+ return new SSEEndpointImpl(this, config).createCall();
1062
+ }
899
1063
  };
900
1064
  //#endregion
901
- export { ApiError, ApiKeyAuth, BearerTokenAuth, ConsoleLogger, ConsoleMetricsCollector, EndpointImpl, HTTPMethod, HTTPStatusCode, HttpClient, InMemoryMetricsCollector, LogLevel, LoggerUtil, NoAuth, NoOpLogger, NoOpMetricsCollector, SchemaDefinitionError, isStandardSchema, parseOrThrow, safeParse, toQueryString };
1065
+ export { ApiError, ApiKeyAuth, BearerTokenAuth, ConsoleLogger, ConsoleMetricsCollector, EndpointImpl, HTTPMethod, HTTPStatusCode, HttpClient, InMemoryMetricsCollector, LogLevel, LoggerUtil, NoAuth, NoOpLogger, NoOpMetricsCollector, SSEConnectionImpl, SSEEndpointImpl, SchemaDefinitionError, WSConnectionImpl, WSEndpointImpl, isStandardSchema, parseOrThrow, safeParse, toQueryString };
902
1066
 
903
1067
  //# sourceMappingURL=index.js.map