@kaito-http/core 4.0.0-beta.14 → 4.0.0-beta.16

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.
@@ -64,6 +64,16 @@ var BaseSchema = class {
64
64
  /** @internal */
65
65
  _output;
66
66
  def;
67
+ getSchemaObject() {
68
+ const schema = {};
69
+ if (this.def.description !== void 0) {
70
+ schema.description = this.def.description;
71
+ }
72
+ if (this.def.example !== void 0) {
73
+ schema.example = this.def.example;
74
+ }
75
+ return schema;
76
+ }
67
77
  clone(def) {
68
78
  return new this.constructor({
69
79
  ...this.def,
@@ -73,6 +83,9 @@ var BaseSchema = class {
73
83
  constructor(def) {
74
84
  this.def = def;
75
85
  }
86
+ or(other) {
87
+ return k.union([this, other]);
88
+ }
76
89
  example(example) {
77
90
  if (example === void 0) {
78
91
  return this.def.example;
@@ -104,7 +117,9 @@ var KString = class _KString extends BaseSchema {
104
117
  return this.clone({ [check.type]: check });
105
118
  }
106
119
  toOpenAPI() {
120
+ const baseSchema = this.getSchemaObject();
107
121
  const schema = {
122
+ ...baseSchema,
108
123
  type: "string"
109
124
  };
110
125
  if (this.def.regex) {
@@ -119,9 +134,6 @@ var KString = class _KString extends BaseSchema {
119
134
  if (this.def.max !== void 0) {
120
135
  schema.maxLength = this.def.max.val;
121
136
  }
122
- if (this.def.description) {
123
- schema.description = this.def.description;
124
- }
125
137
  return schema;
126
138
  }
127
139
  /**
@@ -293,7 +305,11 @@ var KNumber = class _KNumber extends BaseSchema {
293
305
  return this.clone({ [check.type]: check });
294
306
  }
295
307
  toOpenAPI() {
296
- const schema = { type: "number" };
308
+ const baseSchema = this.getSchemaObject();
309
+ const schema = {
310
+ ...baseSchema,
311
+ type: "number"
312
+ };
297
313
  if (this.def.min !== void 0) {
298
314
  schema.minimum = this.def.min.val;
299
315
  }
@@ -329,9 +345,6 @@ var KNumber = class _KNumber extends BaseSchema {
329
345
  if (this.def.format) {
330
346
  schema.format = this.def.format.format;
331
347
  }
332
- if (this.def.description) {
333
- schema.description = this.def.description;
334
- }
335
348
  return schema;
336
349
  }
337
350
  min(min) {
@@ -395,9 +408,10 @@ var KBoolean = class _KBoolean extends BaseSchema {
395
408
  return value;
396
409
  }
397
410
  toOpenAPI() {
411
+ const baseSchema = this.getSchemaObject();
398
412
  return {
399
- type: "boolean",
400
- ...this.def.description ? { description: this.def.description } : {}
413
+ ...baseSchema,
414
+ type: "boolean"
401
415
  };
402
416
  }
403
417
  parseSafe(json) {
@@ -425,10 +439,11 @@ var KArray = class _KArray extends BaseSchema {
425
439
  return this.clone({ [check.type]: check });
426
440
  }
427
441
  toOpenAPI() {
442
+ const baseSchema = this.getSchemaObject();
428
443
  return {
444
+ ...baseSchema,
429
445
  type: "array",
430
446
  items: this.def.items.toOpenAPI(),
431
- ...this.def.description ? { description: this.def.description } : {},
432
447
  ...this.def.minItems !== void 0 ? { minItems: this.def.minItems.val } : {},
433
448
  ...this.def.maxItems !== void 0 ? { maxItems: this.def.maxItems.val } : {},
434
449
  ...this.def.uniqueItems !== void 0 ? { uniqueItems: this.def.uniqueItems.val } : {}
@@ -487,9 +502,10 @@ var KNull = class _KNull extends BaseSchema {
487
502
  return value;
488
503
  }
489
504
  toOpenAPI() {
505
+ const baseSchema = this.getSchemaObject();
490
506
  return {
491
- type: "null",
492
- ...this.def.description ? { description: this.def.description } : {}
507
+ ...baseSchema,
508
+ type: "null"
493
509
  };
494
510
  }
495
511
  parseSafe(json) {
@@ -524,7 +540,9 @@ var KObject = class _KObject extends BaseSchema {
524
540
  return result;
525
541
  }
526
542
  toOpenAPI() {
543
+ const baseSchema = this.getSchemaObject();
527
544
  return {
545
+ ...baseSchema,
528
546
  type: "object",
529
547
  properties: Object.fromEntries(
530
548
  Object.entries(this.def.shape).map((entry) => {
@@ -532,8 +550,7 @@ var KObject = class _KObject extends BaseSchema {
532
550
  return [key, value.toOpenAPI()];
533
551
  })
534
552
  ),
535
- required: Object.keys(this.def.shape),
536
- ...this.def.description ? { description: this.def.description } : {}
553
+ required: Object.keys(this.def.shape)
537
554
  };
538
555
  }
539
556
  parseSafe(json) {
@@ -615,6 +632,9 @@ var KRef = class _KRef extends BaseSchema {
615
632
  }
616
633
  return result;
617
634
  }
635
+ example() {
636
+ throw new Error("Cannot set an example on a KRef");
637
+ }
618
638
  toOpenAPI() {
619
639
  return {
620
640
  $ref: `#/components/schemas/${this.def.name}`,
@@ -708,9 +728,10 @@ var KUnion = class _KUnion extends BaseSchema {
708
728
  throw new Error("Value does not match any union option for serialization");
709
729
  }
710
730
  toOpenAPI() {
731
+ const baseSchema = this.getSchemaObject();
711
732
  return {
712
- oneOf: this.def.items.map((option) => option.toOpenAPI()),
713
- ...this.def.description ? { description: this.def.description } : {}
733
+ ...baseSchema,
734
+ oneOf: this.def.items.map((option) => option.toOpenAPI())
714
735
  };
715
736
  }
716
737
  parseSafe(json) {
@@ -746,8 +767,13 @@ var KLiteral = class _KLiteral extends BaseSchema {
746
767
  return value;
747
768
  }
748
769
  toOpenAPI() {
770
+ const baseSchema = this.getSchemaObject();
749
771
  const type = typeof this.def.value;
750
- return { type, enum: [this.def.value] };
772
+ return {
773
+ ...baseSchema,
774
+ type,
775
+ enum: [this.def.value]
776
+ };
751
777
  }
752
778
  parseSafe(json) {
753
779
  return ParseContext.result((ctx) => {
package/dist/index.cjs CHANGED
@@ -222,6 +222,16 @@ var BaseSchema = class {
222
222
  /** @internal */
223
223
  _output;
224
224
  def;
225
+ getSchemaObject() {
226
+ const schema = {};
227
+ if (this.def.description !== void 0) {
228
+ schema.description = this.def.description;
229
+ }
230
+ if (this.def.example !== void 0) {
231
+ schema.example = this.def.example;
232
+ }
233
+ return schema;
234
+ }
225
235
  clone(def) {
226
236
  return new this.constructor({
227
237
  ...this.def,
@@ -231,6 +241,9 @@ var BaseSchema = class {
231
241
  constructor(def) {
232
242
  this.def = def;
233
243
  }
244
+ or(other) {
245
+ return k.union([this, other]);
246
+ }
234
247
  example(example) {
235
248
  if (example === void 0) {
236
249
  return this.def.example;
@@ -262,7 +275,9 @@ var KString = class _KString extends BaseSchema {
262
275
  return this.clone({ [check.type]: check });
263
276
  }
264
277
  toOpenAPI() {
278
+ const baseSchema = this.getSchemaObject();
265
279
  const schema = {
280
+ ...baseSchema,
266
281
  type: "string"
267
282
  };
268
283
  if (this.def.regex) {
@@ -277,9 +292,6 @@ var KString = class _KString extends BaseSchema {
277
292
  if (this.def.max !== void 0) {
278
293
  schema.maxLength = this.def.max.val;
279
294
  }
280
- if (this.def.description) {
281
- schema.description = this.def.description;
282
- }
283
295
  return schema;
284
296
  }
285
297
  /**
@@ -451,7 +463,11 @@ var KNumber = class _KNumber extends BaseSchema {
451
463
  return this.clone({ [check.type]: check });
452
464
  }
453
465
  toOpenAPI() {
454
- const schema = { type: "number" };
466
+ const baseSchema = this.getSchemaObject();
467
+ const schema = {
468
+ ...baseSchema,
469
+ type: "number"
470
+ };
455
471
  if (this.def.min !== void 0) {
456
472
  schema.minimum = this.def.min.val;
457
473
  }
@@ -487,9 +503,6 @@ var KNumber = class _KNumber extends BaseSchema {
487
503
  if (this.def.format) {
488
504
  schema.format = this.def.format.format;
489
505
  }
490
- if (this.def.description) {
491
- schema.description = this.def.description;
492
- }
493
506
  return schema;
494
507
  }
495
508
  min(min) {
@@ -553,9 +566,10 @@ var KBoolean = class _KBoolean extends BaseSchema {
553
566
  return value;
554
567
  }
555
568
  toOpenAPI() {
569
+ const baseSchema = this.getSchemaObject();
556
570
  return {
557
- type: "boolean",
558
- ...this.def.description ? { description: this.def.description } : {}
571
+ ...baseSchema,
572
+ type: "boolean"
559
573
  };
560
574
  }
561
575
  parseSafe(json) {
@@ -583,10 +597,11 @@ var KArray = class _KArray extends BaseSchema {
583
597
  return this.clone({ [check.type]: check });
584
598
  }
585
599
  toOpenAPI() {
600
+ const baseSchema = this.getSchemaObject();
586
601
  return {
602
+ ...baseSchema,
587
603
  type: "array",
588
604
  items: this.def.items.toOpenAPI(),
589
- ...this.def.description ? { description: this.def.description } : {},
590
605
  ...this.def.minItems !== void 0 ? { minItems: this.def.minItems.val } : {},
591
606
  ...this.def.maxItems !== void 0 ? { maxItems: this.def.maxItems.val } : {},
592
607
  ...this.def.uniqueItems !== void 0 ? { uniqueItems: this.def.uniqueItems.val } : {}
@@ -645,9 +660,10 @@ var KNull = class _KNull extends BaseSchema {
645
660
  return value;
646
661
  }
647
662
  toOpenAPI() {
663
+ const baseSchema = this.getSchemaObject();
648
664
  return {
649
- type: "null",
650
- ...this.def.description ? { description: this.def.description } : {}
665
+ ...baseSchema,
666
+ type: "null"
651
667
  };
652
668
  }
653
669
  parseSafe(json) {
@@ -682,7 +698,9 @@ var KObject = class _KObject extends BaseSchema {
682
698
  return result;
683
699
  }
684
700
  toOpenAPI() {
701
+ const baseSchema = this.getSchemaObject();
685
702
  return {
703
+ ...baseSchema,
686
704
  type: "object",
687
705
  properties: Object.fromEntries(
688
706
  Object.entries(this.def.shape).map((entry) => {
@@ -690,8 +708,7 @@ var KObject = class _KObject extends BaseSchema {
690
708
  return [key, value.toOpenAPI()];
691
709
  })
692
710
  ),
693
- required: Object.keys(this.def.shape),
694
- ...this.def.description ? { description: this.def.description } : {}
711
+ required: Object.keys(this.def.shape)
695
712
  };
696
713
  }
697
714
  parseSafe(json) {
@@ -773,6 +790,9 @@ var KRef = class _KRef extends BaseSchema {
773
790
  }
774
791
  return result;
775
792
  }
793
+ example() {
794
+ throw new Error("Cannot set an example on a KRef");
795
+ }
776
796
  toOpenAPI() {
777
797
  return {
778
798
  $ref: `#/components/schemas/${this.def.name}`,
@@ -866,9 +886,10 @@ var KUnion = class _KUnion extends BaseSchema {
866
886
  throw new Error("Value does not match any union option for serialization");
867
887
  }
868
888
  toOpenAPI() {
889
+ const baseSchema = this.getSchemaObject();
869
890
  return {
870
- oneOf: this.def.items.map((option) => option.toOpenAPI()),
871
- ...this.def.description ? { description: this.def.description } : {}
891
+ ...baseSchema,
892
+ oneOf: this.def.items.map((option) => option.toOpenAPI())
872
893
  };
873
894
  }
874
895
  parseSafe(json) {
@@ -904,8 +925,13 @@ var KLiteral = class _KLiteral extends BaseSchema {
904
925
  return value;
905
926
  }
906
927
  toOpenAPI() {
928
+ const baseSchema = this.getSchemaObject();
907
929
  const type = typeof this.def.value;
908
- return { type, enum: [this.def.value] };
930
+ return {
931
+ ...baseSchema,
932
+ type,
933
+ enum: [this.def.value]
934
+ };
909
935
  }
910
936
  parseSafe(json) {
911
937
  return ParseContext.result((ctx) => {
package/dist/index.d.cts CHANGED
@@ -23,7 +23,7 @@ declare class KaitoRequest {
23
23
  arrayBuffer(): Promise<ArrayBuffer>;
24
24
  blob(): Promise<Blob>;
25
25
  formData(): Promise<FormData>;
26
- bytes(): Promise<Uint8Array>;
26
+ bytes(): Promise<Uint8Array<ArrayBuffer>>;
27
27
  json(): Promise<unknown>;
28
28
  text(): Promise<string>;
29
29
  get request(): Request;
@@ -100,15 +100,15 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, R ex
100
100
  openapi: ({ info, servers, }: {
101
101
  info: OpenAPI.InfoObject;
102
102
  servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
103
- }) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, JSONValue, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
103
+ }) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
104
104
  private readonly method;
105
- get: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, Input>;
106
- post: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, Input>;
107
- put: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, Input>;
108
- patch: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, Input>;
109
- delete: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, Input>;
110
- head: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, Input>;
111
- options: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
105
+ get: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, Input>;
106
+ post: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, Input>;
107
+ put: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, Input>;
108
+ patch: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, Input>;
109
+ delete: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, Input>;
110
+ head: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, Input>;
111
+ options: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
112
112
  through: <NextContext>(through: (context: ContextTo, params: Record<RequiredParams, string>) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
113
113
  }
114
114
 
@@ -132,11 +132,11 @@ type JSONOutputSpec<Result extends JSONValue> = {
132
132
  schema: AnySchemaFor<Result>;
133
133
  description?: string;
134
134
  };
135
- type OutputSpec<Result extends JSONValue> = {
135
+ type OutputSpec<Result> = {
136
136
  description?: string;
137
- body: Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<Extract<R, JSONValue>> : JSONOutputSpec<Result>;
137
+ body: Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<Extract<R, JSONValue>> : JSONOutputSpec<Extract<Result, JSONValue>>;
138
138
  };
139
- type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Result extends JSONValue, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
139
+ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Result, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
140
140
  body?: AnySchemaFor<Body>;
141
141
  query?: {
142
142
  [Key in keyof Query]: AnySchemaFor<Query[Key]>;
@@ -145,7 +145,7 @@ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Resul
145
145
  method: Method;
146
146
  openapi?: OutputSpec<NoInfer<Result>>;
147
147
  router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
148
- run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<Result | Response> | Response | Result;
148
+ run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
149
149
  };
150
150
  type AnyRoute = Route<any, any, any, any, any, any, any, any, any>;
151
151
 
package/dist/index.d.ts CHANGED
@@ -23,7 +23,7 @@ declare class KaitoRequest {
23
23
  arrayBuffer(): Promise<ArrayBuffer>;
24
24
  blob(): Promise<Blob>;
25
25
  formData(): Promise<FormData>;
26
- bytes(): Promise<Uint8Array>;
26
+ bytes(): Promise<Uint8Array<ArrayBuffer>>;
27
27
  json(): Promise<unknown>;
28
28
  text(): Promise<string>;
29
29
  get request(): Request;
@@ -100,15 +100,15 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, R ex
100
100
  openapi: ({ info, servers, }: {
101
101
  info: OpenAPI.InfoObject;
102
102
  servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
103
- }) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, JSONValue, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
103
+ }) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
104
104
  private readonly method;
105
- get: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, Input>;
106
- post: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, Input>;
107
- put: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, Input>;
108
- patch: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, Input>;
109
- delete: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, Input>;
110
- head: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, Input>;
111
- options: <Result extends JSONValue, Path extends string, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Response | Result | Promise<Response | Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
105
+ get: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "GET", Query, Body>, Input>;
106
+ post: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "POST", Query, Body>, Input>;
107
+ put: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PUT", Query, Body>, Input>;
108
+ patch: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "PATCH", Query, Body>, Input>;
109
+ delete: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "DELETE", Query, Body>, Input>;
110
+ head: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "HEAD", Query, Body>, Input>;
111
+ options: <Path extends string, Result, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextFrom, ContextTo, Input, Result, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
112
112
  through: <NextContext>(through: (context: ContextTo, params: Record<RequiredParams, string>) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
113
113
  }
114
114
 
@@ -132,11 +132,11 @@ type JSONOutputSpec<Result extends JSONValue> = {
132
132
  schema: AnySchemaFor<Result>;
133
133
  description?: string;
134
134
  };
135
- type OutputSpec<Result extends JSONValue> = {
135
+ type OutputSpec<Result> = {
136
136
  description?: string;
137
- body: Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<Extract<R, JSONValue>> : JSONOutputSpec<Result>;
137
+ body: Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<Extract<R, JSONValue>> : JSONOutputSpec<Extract<Result, JSONValue>>;
138
138
  };
139
- type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Result extends JSONValue, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
139
+ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Result, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
140
140
  body?: AnySchemaFor<Body>;
141
141
  query?: {
142
142
  [Key in keyof Query]: AnySchemaFor<Query[Key]>;
@@ -145,7 +145,7 @@ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Resul
145
145
  method: Method;
146
146
  openapi?: OutputSpec<NoInfer<Result>>;
147
147
  router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
148
- run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<Result | Response> | Response | Result;
148
+ run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
149
149
  };
150
150
  type AnyRoute = Route<any, any, any, any, any, any, any, any, any>;
151
151
 
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ import {
16
16
  SchemaError,
17
17
  isPrimitiveJSONValue,
18
18
  k
19
- } from "./chunk-THVWVAMX.js";
19
+ } from "./chunk-ISGKY24N.js";
20
20
 
21
21
  // src/router/router.ts
22
22
  import "openapi3-ts/oas31";
@@ -104,6 +104,16 @@ var BaseSchema = class {
104
104
  /** @internal */
105
105
  _output;
106
106
  def;
107
+ getSchemaObject() {
108
+ const schema = {};
109
+ if (this.def.description !== void 0) {
110
+ schema.description = this.def.description;
111
+ }
112
+ if (this.def.example !== void 0) {
113
+ schema.example = this.def.example;
114
+ }
115
+ return schema;
116
+ }
107
117
  clone(def) {
108
118
  return new this.constructor({
109
119
  ...this.def,
@@ -113,6 +123,9 @@ var BaseSchema = class {
113
123
  constructor(def) {
114
124
  this.def = def;
115
125
  }
126
+ or(other) {
127
+ return k.union([this, other]);
128
+ }
116
129
  example(example) {
117
130
  if (example === void 0) {
118
131
  return this.def.example;
@@ -144,7 +157,9 @@ var KString = class _KString extends BaseSchema {
144
157
  return this.clone({ [check.type]: check });
145
158
  }
146
159
  toOpenAPI() {
160
+ const baseSchema = this.getSchemaObject();
147
161
  const schema = {
162
+ ...baseSchema,
148
163
  type: "string"
149
164
  };
150
165
  if (this.def.regex) {
@@ -159,9 +174,6 @@ var KString = class _KString extends BaseSchema {
159
174
  if (this.def.max !== void 0) {
160
175
  schema.maxLength = this.def.max.val;
161
176
  }
162
- if (this.def.description) {
163
- schema.description = this.def.description;
164
- }
165
177
  return schema;
166
178
  }
167
179
  /**
@@ -333,7 +345,11 @@ var KNumber = class _KNumber extends BaseSchema {
333
345
  return this.clone({ [check.type]: check });
334
346
  }
335
347
  toOpenAPI() {
336
- const schema = { type: "number" };
348
+ const baseSchema = this.getSchemaObject();
349
+ const schema = {
350
+ ...baseSchema,
351
+ type: "number"
352
+ };
337
353
  if (this.def.min !== void 0) {
338
354
  schema.minimum = this.def.min.val;
339
355
  }
@@ -369,9 +385,6 @@ var KNumber = class _KNumber extends BaseSchema {
369
385
  if (this.def.format) {
370
386
  schema.format = this.def.format.format;
371
387
  }
372
- if (this.def.description) {
373
- schema.description = this.def.description;
374
- }
375
388
  return schema;
376
389
  }
377
390
  min(min) {
@@ -435,9 +448,10 @@ var KBoolean = class _KBoolean extends BaseSchema {
435
448
  return value;
436
449
  }
437
450
  toOpenAPI() {
451
+ const baseSchema = this.getSchemaObject();
438
452
  return {
439
- type: "boolean",
440
- ...this.def.description ? { description: this.def.description } : {}
453
+ ...baseSchema,
454
+ type: "boolean"
441
455
  };
442
456
  }
443
457
  parseSafe(json) {
@@ -465,10 +479,11 @@ var KArray = class _KArray extends BaseSchema {
465
479
  return this.clone({ [check.type]: check });
466
480
  }
467
481
  toOpenAPI() {
482
+ const baseSchema = this.getSchemaObject();
468
483
  return {
484
+ ...baseSchema,
469
485
  type: "array",
470
486
  items: this.def.items.toOpenAPI(),
471
- ...this.def.description ? { description: this.def.description } : {},
472
487
  ...this.def.minItems !== void 0 ? { minItems: this.def.minItems.val } : {},
473
488
  ...this.def.maxItems !== void 0 ? { maxItems: this.def.maxItems.val } : {},
474
489
  ...this.def.uniqueItems !== void 0 ? { uniqueItems: this.def.uniqueItems.val } : {}
@@ -527,9 +542,10 @@ var KNull = class _KNull extends BaseSchema {
527
542
  return value;
528
543
  }
529
544
  toOpenAPI() {
545
+ const baseSchema = this.getSchemaObject();
530
546
  return {
531
- type: "null",
532
- ...this.def.description ? { description: this.def.description } : {}
547
+ ...baseSchema,
548
+ type: "null"
533
549
  };
534
550
  }
535
551
  parseSafe(json) {
@@ -564,7 +580,9 @@ var KObject = class _KObject extends BaseSchema {
564
580
  return result;
565
581
  }
566
582
  toOpenAPI() {
583
+ const baseSchema = this.getSchemaObject();
567
584
  return {
585
+ ...baseSchema,
568
586
  type: "object",
569
587
  properties: Object.fromEntries(
570
588
  Object.entries(this.def.shape).map((entry) => {
@@ -572,8 +590,7 @@ var KObject = class _KObject extends BaseSchema {
572
590
  return [key, value.toOpenAPI()];
573
591
  })
574
592
  ),
575
- required: Object.keys(this.def.shape),
576
- ...this.def.description ? { description: this.def.description } : {}
593
+ required: Object.keys(this.def.shape)
577
594
  };
578
595
  }
579
596
  parseSafe(json) {
@@ -655,6 +672,9 @@ var KRef = class _KRef extends BaseSchema {
655
672
  }
656
673
  return result;
657
674
  }
675
+ example() {
676
+ throw new Error("Cannot set an example on a KRef");
677
+ }
658
678
  toOpenAPI() {
659
679
  return {
660
680
  $ref: `#/components/schemas/${this.def.name}`,
@@ -748,9 +768,10 @@ var KUnion = class _KUnion extends BaseSchema {
748
768
  throw new Error("Value does not match any union option for serialization");
749
769
  }
750
770
  toOpenAPI() {
771
+ const baseSchema = this.getSchemaObject();
751
772
  return {
752
- oneOf: this.def.items.map((option) => option.toOpenAPI()),
753
- ...this.def.description ? { description: this.def.description } : {}
773
+ ...baseSchema,
774
+ oneOf: this.def.items.map((option) => option.toOpenAPI())
754
775
  };
755
776
  }
756
777
  parseSafe(json) {
@@ -786,8 +807,13 @@ var KLiteral = class _KLiteral extends BaseSchema {
786
807
  return value;
787
808
  }
788
809
  toOpenAPI() {
810
+ const baseSchema = this.getSchemaObject();
789
811
  const type = typeof this.def.value;
790
- return { type, enum: [this.def.value] };
812
+ return {
813
+ ...baseSchema,
814
+ type,
815
+ enum: [this.def.value]
816
+ };
791
817
  }
792
818
  parseSafe(json) {
793
819
  return ParseContext.result((ctx) => {
@@ -51,10 +51,12 @@ declare abstract class BaseSchema<Input extends JSONValue, Output, Def extends B
51
51
  abstract parse(json: unknown): Output;
52
52
  abstract parseSafe(json: unknown): ParseResult<Output>;
53
53
  abstract serialize(value: Output): Input;
54
- abstract toOpenAPI(): SchemaObject | ReferenceObject;
55
54
  protected readonly def: Def;
55
+ abstract toOpenAPI(): SchemaObject | ReferenceObject;
56
+ protected getSchemaObject(): SchemaObject;
56
57
  protected clone(def: Partial<Def>): this;
57
58
  protected constructor(def: Def);
59
+ or<OtherInput extends JSONValue, OtherOutput, Def extends BaseSchemaDef<OtherInput>>(other: BaseSchema<OtherInput, OtherOutput, Def>): KUnion<(this | BaseSchema<OtherInput, OtherOutput, Def>)["_input"], (this | BaseSchema<OtherInput, OtherOutput, Def>)["_output"]>;
58
60
  example(example: Input): this;
59
61
  example(): Input | undefined;
60
62
  description(description: string): this;
@@ -245,6 +247,7 @@ interface RefDef<Input extends Record<keyof Output, JSONValue>, Output extends R
245
247
  declare class KRef<Input extends Record<keyof Output, JSONValue>, Output extends Record<keyof Input, JSONValue>> extends BaseSchema<Input, Output, RefDef<Input, Output>> {
246
248
  static create: <Input_1 extends Record<keyof Output_1, JSONValue>, Output_1 extends Record<keyof Input_1, JSONValue>>(name: string, shape: { [K in keyof Input_1 | keyof Output_1]: BaseSchema<Input_1[K], Output_1[K], BaseSchemaDef<Input_1[K]>>; }) => KRef<Input_1, Output_1>;
247
249
  serialize(value: Output): Input;
250
+ example(): never;
248
251
  toOpenAPI(): ReferenceObject;
249
252
  parseSafe(json: unknown): ParseResult<Output>;
250
253
  parse(json: unknown): Output;
@@ -51,10 +51,12 @@ declare abstract class BaseSchema<Input extends JSONValue, Output, Def extends B
51
51
  abstract parse(json: unknown): Output;
52
52
  abstract parseSafe(json: unknown): ParseResult<Output>;
53
53
  abstract serialize(value: Output): Input;
54
- abstract toOpenAPI(): SchemaObject | ReferenceObject;
55
54
  protected readonly def: Def;
55
+ abstract toOpenAPI(): SchemaObject | ReferenceObject;
56
+ protected getSchemaObject(): SchemaObject;
56
57
  protected clone(def: Partial<Def>): this;
57
58
  protected constructor(def: Def);
59
+ or<OtherInput extends JSONValue, OtherOutput, Def extends BaseSchemaDef<OtherInput>>(other: BaseSchema<OtherInput, OtherOutput, Def>): KUnion<(this | BaseSchema<OtherInput, OtherOutput, Def>)["_input"], (this | BaseSchema<OtherInput, OtherOutput, Def>)["_output"]>;
58
60
  example(example: Input): this;
59
61
  example(): Input | undefined;
60
62
  description(description: string): this;
@@ -245,6 +247,7 @@ interface RefDef<Input extends Record<keyof Output, JSONValue>, Output extends R
245
247
  declare class KRef<Input extends Record<keyof Output, JSONValue>, Output extends Record<keyof Input, JSONValue>> extends BaseSchema<Input, Output, RefDef<Input, Output>> {
246
248
  static create: <Input_1 extends Record<keyof Output_1, JSONValue>, Output_1 extends Record<keyof Input_1, JSONValue>>(name: string, shape: { [K in keyof Input_1 | keyof Output_1]: BaseSchema<Input_1[K], Output_1[K], BaseSchemaDef<Input_1[K]>>; }) => KRef<Input_1, Output_1>;
247
249
  serialize(value: Output): Input;
250
+ example(): never;
248
251
  toOpenAPI(): ReferenceObject;
249
252
  parseSafe(json: unknown): ParseResult<Output>;
250
253
  parse(json: unknown): Output;
@@ -16,7 +16,7 @@ import {
16
16
  SchemaError,
17
17
  isPrimitiveJSONValue,
18
18
  k
19
- } from "../chunk-THVWVAMX.js";
19
+ } from "../chunk-ISGKY24N.js";
20
20
  export {
21
21
  BaseSchema,
22
22
  KArray,
@@ -43,23 +43,20 @@ var KaitoSSEResponse = class extends Response {
43
43
  }
44
44
  };
45
45
  function sseEventToString(event) {
46
- let result = "";
46
+ const lines = [];
47
47
  if (event.event) {
48
- result += `event:${event.event}
49
- `;
48
+ lines.push(`event:${event.event}`);
50
49
  }
51
50
  if (event.id) {
52
- result += `id:${event.id}
53
- `;
51
+ lines.push(`id:${event.id}`);
54
52
  }
55
53
  if (event.retry) {
56
- result += `retry:${event.retry}
57
- `;
54
+ lines.push(`retry:${event.retry}`);
58
55
  }
59
56
  if (event.data !== void 0) {
60
- result += `data:${JSON.stringify(event.data)}`;
57
+ lines.push(`data:${JSON.stringify(event.data)}`);
61
58
  }
62
- return result;
59
+ return lines.join("\n");
63
60
  }
64
61
  var SSEController = class {
65
62
  controller;
@@ -1,3 +1,6 @@
1
+ import { JSONValue } from '../schema/schema.cjs';
2
+ import 'openapi3-ts/oas31';
3
+
1
4
  declare class KaitoSSEResponse<_T> extends Response {
2
5
  constructor(body: ReadableStream<string>, init?: ResponseInit);
3
6
  get [Symbol.toStringTag](): string;
@@ -17,20 +20,20 @@ type SSEEvent<T, E extends string> = ({
17
20
  * @param event The SSE Event
18
21
  * @returns A stringified version
19
22
  */
20
- declare function sseEventToString(event: SSEEvent<unknown, string>): string;
21
- declare class SSEController<U, E extends string> implements Disposable {
23
+ declare function sseEventToString(event: SSEEvent<JSONValue, string>): string;
24
+ declare class SSEController<U extends JSONValue, E extends string, T extends SSEEvent<U, E>> implements Disposable {
22
25
  private readonly controller;
23
26
  constructor(controller: ReadableStreamDefaultController<string>);
24
- enqueue(event: SSEEvent<U, E>): void;
27
+ enqueue(event: T): void;
25
28
  close(): void;
26
29
  [Symbol.dispose](): void;
27
30
  }
28
- interface SSESource<U, E extends string> {
31
+ interface SSESource<U extends JSONValue, E extends string, T extends SSEEvent<U, E>> {
29
32
  cancel?: UnderlyingSourceCancelCallback;
30
- start?(controller: SSEController<U, E>): Promise<void>;
31
- pull?(controller: SSEController<U, E>): Promise<void>;
33
+ start?(controller: SSEController<U, E, T>): Promise<void>;
34
+ pull?(controller: SSEController<U, E, T>): Promise<void>;
32
35
  }
33
- declare function sse<U, E extends string, T extends SSEEvent<U, E>>(source: SSESource<U, E> | AsyncGenerator<T, unknown, unknown> | (() => AsyncGenerator<T, unknown, unknown>)): KaitoSSEResponse<T>;
34
- declare function sseFromAnyReadable<R, U, E extends string>(stream: ReadableStream<R>, transform: (chunk: R) => SSEEvent<U, E>): KaitoSSEResponse<SSEEvent<U, E>>;
36
+ declare function sse<U extends JSONValue, E extends string, T extends SSEEvent<U, E>>(source: SSESource<U, E, T> | AsyncGenerator<T, unknown, unknown> | (() => AsyncGenerator<T, unknown, unknown>)): KaitoSSEResponse<T>;
37
+ declare function sseFromAnyReadable<R, U extends JSONValue, E extends string>(stream: ReadableStream<R>, transform: (chunk: R) => SSEEvent<U, E>): KaitoSSEResponse<SSEEvent<U, E>>;
35
38
 
36
39
  export { KaitoSSEResponse, SSEController, type SSEEvent, type SSESource, sse, sseEventToString, sseFromAnyReadable };
@@ -1,3 +1,6 @@
1
+ import { JSONValue } from '../schema/schema.js';
2
+ import 'openapi3-ts/oas31';
3
+
1
4
  declare class KaitoSSEResponse<_T> extends Response {
2
5
  constructor(body: ReadableStream<string>, init?: ResponseInit);
3
6
  get [Symbol.toStringTag](): string;
@@ -17,20 +20,20 @@ type SSEEvent<T, E extends string> = ({
17
20
  * @param event The SSE Event
18
21
  * @returns A stringified version
19
22
  */
20
- declare function sseEventToString(event: SSEEvent<unknown, string>): string;
21
- declare class SSEController<U, E extends string> implements Disposable {
23
+ declare function sseEventToString(event: SSEEvent<JSONValue, string>): string;
24
+ declare class SSEController<U extends JSONValue, E extends string, T extends SSEEvent<U, E>> implements Disposable {
22
25
  private readonly controller;
23
26
  constructor(controller: ReadableStreamDefaultController<string>);
24
- enqueue(event: SSEEvent<U, E>): void;
27
+ enqueue(event: T): void;
25
28
  close(): void;
26
29
  [Symbol.dispose](): void;
27
30
  }
28
- interface SSESource<U, E extends string> {
31
+ interface SSESource<U extends JSONValue, E extends string, T extends SSEEvent<U, E>> {
29
32
  cancel?: UnderlyingSourceCancelCallback;
30
- start?(controller: SSEController<U, E>): Promise<void>;
31
- pull?(controller: SSEController<U, E>): Promise<void>;
33
+ start?(controller: SSEController<U, E, T>): Promise<void>;
34
+ pull?(controller: SSEController<U, E, T>): Promise<void>;
32
35
  }
33
- declare function sse<U, E extends string, T extends SSEEvent<U, E>>(source: SSESource<U, E> | AsyncGenerator<T, unknown, unknown> | (() => AsyncGenerator<T, unknown, unknown>)): KaitoSSEResponse<T>;
34
- declare function sseFromAnyReadable<R, U, E extends string>(stream: ReadableStream<R>, transform: (chunk: R) => SSEEvent<U, E>): KaitoSSEResponse<SSEEvent<U, E>>;
36
+ declare function sse<U extends JSONValue, E extends string, T extends SSEEvent<U, E>>(source: SSESource<U, E, T> | AsyncGenerator<T, unknown, unknown> | (() => AsyncGenerator<T, unknown, unknown>)): KaitoSSEResponse<T>;
37
+ declare function sseFromAnyReadable<R, U extends JSONValue, E extends string>(stream: ReadableStream<R>, transform: (chunk: R) => SSEEvent<U, E>): KaitoSSEResponse<SSEEvent<U, E>>;
35
38
 
36
39
  export { KaitoSSEResponse, SSEController, type SSEEvent, type SSESource, sse, sseEventToString, sseFromAnyReadable };
@@ -15,23 +15,20 @@ var KaitoSSEResponse = class extends Response {
15
15
  }
16
16
  };
17
17
  function sseEventToString(event) {
18
- let result = "";
18
+ const lines = [];
19
19
  if (event.event) {
20
- result += `event:${event.event}
21
- `;
20
+ lines.push(`event:${event.event}`);
22
21
  }
23
22
  if (event.id) {
24
- result += `id:${event.id}
25
- `;
23
+ lines.push(`id:${event.id}`);
26
24
  }
27
25
  if (event.retry) {
28
- result += `retry:${event.retry}
29
- `;
26
+ lines.push(`retry:${event.retry}`);
30
27
  }
31
28
  if (event.data !== void 0) {
32
- result += `data:${JSON.stringify(event.data)}`;
29
+ lines.push(`data:${JSON.stringify(event.data)}`);
33
30
  }
34
- return result;
31
+ return lines.join("\n");
35
32
  }
36
33
  var SSEController = class {
37
34
  controller;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaito-http/core",
3
3
  "type": "module",
4
- "version": "4.0.0-beta.14",
4
+ "version": "4.0.0-beta.16",
5
5
  "author": "Alistair Smith <hi@alistair.sh>",
6
6
  "repository": "https://github.com/kaito-http/kaito",
7
7
  "dependencies": {