zlient 3.3.0 → 3.3.2

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 CHANGED
@@ -259,9 +259,16 @@ const stream = client.createSSE({
259
259
  message: z.object({ type: z.literal('connected') }),
260
260
  time: z.string(),
261
261
  },
262
+ advanced: {
263
+ method: 'POST', // Support GET (default), POST, etc.
264
+ }
265
+ });
266
+
267
+ const sse = await stream({
268
+ data: { filter: 'active' }, // Support request body for POST/PUT
269
+ headers: { 'X-Custom-ID': '123' }, // Custom headers
262
270
  });
263
271
 
264
- const sse = stream();
265
272
  sse.on('message', (data) => console.log(data.type)); // Typed as { type: 'connected' }
266
273
  sse.on('time', (data) => console.log(data)); // Typed as string
267
274
  ```
@@ -192,6 +192,6 @@ export declare class HttpClient {
192
192
  * @param config - SSE endpoint configuration
193
193
  * @returns SSE endpoint call function
194
194
  */
195
- createSSE<ResSchema extends SSEResponseSchema | undefined = undefined, QuerySchema extends StandardSchemaV1 | undefined = undefined, PathSchema extends StandardSchemaV1 | undefined = undefined>(config: SSEEndpointConfig<ResSchema, QuerySchema, PathSchema>): SSEEndpointCall<ResSchema, QuerySchema, PathSchema>;
195
+ createSSE<ResSchema extends SSEResponseSchema | undefined = undefined, ReqSchema extends StandardSchemaV1 | undefined = undefined, QuerySchema extends StandardSchemaV1 | undefined = undefined, PathSchema extends StandardSchemaV1 | undefined = undefined>(config: SSEEndpointConfig<ResSchema, ReqSchema, QuerySchema, PathSchema>): SSEEndpointCall<ResSchema, ReqSchema, QuerySchema, PathSchema>;
196
196
  }
197
197
  //# sourceMappingURL=http-client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"http-client.d.ts","sourceRoot":"","sources":["../../lib/http/http-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAK5C,OAAO,EAEL,aAAa,EAEb,UAAU,EACV,oBAAoB,EAEpB,cAAc,EACd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,EAAE,UAAU,EAAE,cAAc,CAAC;IAK9E;;;;;OAKG;IACH,eAAe,CACb,UAAU,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC3D,aAAa,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC9D,WAAW,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC5D,UAAU,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAE3D,MAAM,EAAE,gBAAgB,CAAC,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,CAAC,GAC3E,cAAc,CAAC,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,CAAC;IAKrE;;;;;OAKG;IACH,SAAS,CACP,SAAS,SAAS,iBAAiB,GAAG,SAAS,GAAG,SAAS,EAC3D,SAAS,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC1D,WAAW,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAC5D,UAAU,SAAS,gBAAgB,GAAG,SAAS,GAAG,SAAS,EAE3D,MAAM,EAAE,iBAAiB,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,CAAC,GACvE,eAAe,CAAC,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,CAAC;CAIlE"}
package/dist/index.cjs CHANGED
@@ -497,27 +497,94 @@ function isStandardSchema(value) {
497
497
  //#endregion
498
498
  //#region lib/sse/sse-client.ts
499
499
  var SSEConnectionImpl = class {
500
- constructor(url, responseSchema, skipResponseValidation = false, withCredentials = false) {
500
+ constructor(url, responseSchema, options = {}) {
501
+ this.url = url;
501
502
  this.responseSchema = responseSchema;
502
- this.skipResponseValidation = skipResponseValidation;
503
+ this.options = options;
504
+ this.abortController = new AbortController();
503
505
  this.handlers = /* @__PURE__ */ new Map();
504
- if (typeof EventSource === "undefined") throw new Error("EventSource is not defined. Ensure you are in a supported environment.");
505
- this.es = new EventSource(url, { withCredentials });
506
- this.es.onopen = (event) => this.emit("open", event);
507
- this.es.onerror = (event) => this.emit("error", event);
508
- this.es.onmessage = async (event) => {
509
- let data = event.data;
510
- try {
511
- if (typeof data === "string") try {
512
- data = JSON.parse(data);
513
- } catch {}
514
- const schema = this.getSchema("message");
515
- if (!this.skipResponseValidation && schema) data = await parseOrThrow(schema, data);
516
- this.emit("message", data);
517
- } catch (error) {
518
- this.emit("error", error);
506
+ this._readyState = 0;
507
+ this.start();
508
+ }
509
+ async start() {
510
+ try {
511
+ const { method = "GET", data, headers = {}, withCredentials, signal } = this.options;
512
+ const init = {
513
+ method,
514
+ headers: {
515
+ Accept: "text/event-stream",
516
+ ...headers
517
+ },
518
+ signal: signal || this.abortController.signal
519
+ };
520
+ if (withCredentials) init.credentials = "include";
521
+ if (data) {
522
+ init.body = typeof data === "object" ? JSON.stringify(data) : String(data);
523
+ if (!init.headers || !("Content-Type" in init.headers)) init.headers["Content-Type"] = "application/json";
519
524
  }
520
- };
525
+ const response = await fetch(this.url, init);
526
+ if (!response.ok) throw new Error(`SSE request failed with status ${response.status}`);
527
+ this._readyState = 1;
528
+ this.emit("open", { type: "open" });
529
+ const reader = response.body?.getReader();
530
+ if (!reader) throw new Error("Response body is not readable");
531
+ const decoder = new TextDecoder();
532
+ let buffer = "";
533
+ while (true) {
534
+ const { done, value } = await reader.read();
535
+ if (done) break;
536
+ buffer += decoder.decode(value, { stream: true });
537
+ const parts = buffer.split(/\r\n|\r|\n/);
538
+ buffer = parts.pop() || "";
539
+ let eventData = "";
540
+ let eventName = "message";
541
+ for (const line of parts) {
542
+ if (line === "") {
543
+ if (eventData) {
544
+ this.handleEvent(eventName, eventData.trim());
545
+ eventData = "";
546
+ eventName = "message";
547
+ }
548
+ continue;
549
+ }
550
+ if (line.startsWith(":")) continue;
551
+ const colonIndex = line.indexOf(":");
552
+ if (colonIndex === -1) this.processField(line, "", (name, data) => {
553
+ eventName = name || eventName;
554
+ eventData += data;
555
+ });
556
+ else {
557
+ const field = line.slice(0, colonIndex);
558
+ const value = line.slice(colonIndex + 1).trim();
559
+ if (field === "event") eventName = value;
560
+ else if (field === "data") eventData += (eventData ? "\n" : "") + value;
561
+ }
562
+ }
563
+ }
564
+ } catch (error) {
565
+ if (error.name === "AbortError") return;
566
+ this._readyState = 2;
567
+ this.emit("error", error);
568
+ } finally {
569
+ this._readyState = 2;
570
+ }
571
+ }
572
+ processField(field, value, callback) {
573
+ if (field === "event") callback(value, "");
574
+ else if (field === "data") callback("", value);
575
+ }
576
+ async handleEvent(event, data) {
577
+ let parsedData = data;
578
+ try {
579
+ if (typeof data === "string") try {
580
+ parsedData = JSON.parse(data);
581
+ } catch {}
582
+ const schema = this.getSchema(event);
583
+ if (!this.options.skipResponseValidation && schema) parsedData = await parseOrThrow(schema, parsedData);
584
+ this.emit(event, parsedData);
585
+ } catch (error) {
586
+ this.emit("error", error);
587
+ }
521
588
  }
522
589
  getSchema(event) {
523
590
  if (!this.responseSchema) return void 0;
@@ -528,22 +595,7 @@ var SSEConnectionImpl = class {
528
595
  return this.responseSchema[event];
529
596
  }
530
597
  on(event, handler) {
531
- if (!this.handlers.has(event)) {
532
- this.handlers.set(event, /* @__PURE__ */ new Set());
533
- if (event !== "message" && event !== "open" && event !== "error") this.es.addEventListener(event, async (ev) => {
534
- let data = ev.data;
535
- try {
536
- if (typeof data === "string") try {
537
- data = JSON.parse(data);
538
- } catch {}
539
- const schema = this.getSchema(event);
540
- if (!this.skipResponseValidation && schema) data = await parseOrThrow(schema, data);
541
- this.emit(event, data);
542
- } catch (error) {
543
- this.emit("error", error);
544
- }
545
- });
546
- }
598
+ if (!this.handlers.has(event)) this.handlers.set(event, /* @__PURE__ */ new Set());
547
599
  this.handlers.get(event).add(handler);
548
600
  }
549
601
  off(event, handler) {
@@ -555,10 +607,11 @@ var SSEConnectionImpl = class {
555
607
  if (handlers) handlers.forEach((handler) => handler(...args));
556
608
  }
557
609
  close() {
558
- this.es.close();
610
+ this.abortController.abort();
611
+ this._readyState = 2;
559
612
  }
560
613
  get readyState() {
561
- return this.es.readyState;
614
+ return this._readyState;
562
615
  }
563
616
  };
564
617
  //#endregion
@@ -569,14 +622,30 @@ var SSEEndpointImpl = class {
569
622
  this.config = config;
570
623
  }
571
624
  createCall() {
572
- return (params) => {
573
- const { query, pathParams } = params || {};
625
+ return async (params) => {
626
+ const { query, pathParams, data, headers, signal } = params || {};
627
+ const skipRequestValidation = this.config.advanced?.skipRequestValidation ?? false;
628
+ if (!skipRequestValidation && this.config.request && data !== void 0) await parseOrThrow(this.config.request, data);
629
+ if (!skipRequestValidation && this.config.query && query !== void 0) await parseOrThrow(this.config.query, query);
630
+ if (!skipRequestValidation && this.config.pathParams && pathParams !== void 0) await parseOrThrow(this.config.pathParams, pathParams);
631
+ if (this.config.request && data === void 0) throw new Error("Missing required request body (data)");
632
+ if (this.config.pathParams && pathParams === void 0) throw new Error("Missing required path parameters (pathParams)");
574
633
  let pathStr;
575
634
  if (typeof this.config.path === "function") {
576
635
  if (!pathParams) throw new Error("Path function requires pathParams");
577
636
  pathStr = this.config.path(pathParams);
578
637
  } else pathStr = this.config.path;
579
- 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);
638
+ return new SSEConnectionImpl(`${this.client.getBaseUrl(this.config.advanced?.baseUrlKey || "default")}${pathStr}${toQueryString(query)}`, this.config.response, {
639
+ skipResponseValidation: this.config.advanced?.skipResponseValidation,
640
+ withCredentials: this.config.advanced?.withCredentials,
641
+ method: this.config.method,
642
+ data,
643
+ headers: {
644
+ ...this.config.advanced?.headers || {},
645
+ ...headers || {}
646
+ },
647
+ signal
648
+ });
580
649
  };
581
650
  }
582
651
  };