spiceflow 1.0.1 → 1.0.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.
@@ -1,23 +1,21 @@
1
1
  // https://github.com/remorses/elysia/blob/main/src/types.ts#L6
2
+ import z from 'zod'
2
3
 
3
4
  /* eslint-disable @typescript-eslint/no-unused-vars */
4
5
 
5
- import type { BunFile, Serve, Server, WebSocketHandler } from 'bun'
6
-
7
6
  import type {
8
- TSchema,
9
- TObject,
7
+ OptionalKind,
10
8
  Static,
11
- TAnySchema,
12
- TNull,
13
- TUndefined,
14
9
  StaticDecode,
15
- OptionalKind
10
+ TAnySchema,
11
+ TSchema,
16
12
  } from '@sinclair/typebox'
17
13
  import type { TypeCheck, ValueError } from '@sinclair/typebox/compiler'
14
+ import type { BunFile, Server } from 'bun'
18
15
 
19
16
  import type { OpenAPIV3 } from 'openapi-types'
20
17
 
18
+ import { Spiceflow } from '../spiceflow'
21
19
  import type { Context, ErrorContext, PreContext } from './context'
22
20
  import {
23
21
  ELYSIA_RESPONSE,
@@ -25,10 +23,9 @@ import {
25
23
  InvalidCookieSignature,
26
24
  NotFoundError,
27
25
  ParseError,
28
- ValidationError
26
+ ValidationError,
29
27
  } from './error'
30
- import { Spiceflow } from '../spiceflow'
31
-
28
+ import { ZodTypeAny } from 'zod'
32
29
 
33
30
  export type MaybeArray<T> = T | T[]
34
31
  export type MaybePromise<T> = T | Promise<T>
@@ -93,7 +90,7 @@ export type Reconcile<
93
90
  B extends Object,
94
91
  Override extends boolean = false,
95
92
  // Detect Stack limit, eg. circular dependency
96
- Stack extends number[] = []
93
+ Stack extends number[] = [],
97
94
  > = Stack['length'] extends 16
98
95
  ? A
99
96
  : Override extends true
@@ -188,11 +185,15 @@ type OptionalField = {
188
185
  [OptionalKind]: 'Optional'
189
186
  }
190
187
 
188
+ export type TypeSchema = TSchema | ZodTypeAny
189
+
191
190
  export type UnwrapSchema<
192
- Schema extends TSchema | string | undefined,
193
- Definitions extends Record<string, unknown> = {}
191
+ Schema extends TypeSchema | string | undefined,
192
+ Definitions extends Record<string, unknown> = {},
194
193
  > = undefined extends Schema
195
194
  ? unknown
195
+ : Schema extends ZodTypeAny
196
+ ? z.infer<Schema>
196
197
  : Schema extends TSchema
197
198
  ? Schema extends OptionalField
198
199
  ? Prettify<Partial<Static<Schema>>>
@@ -203,31 +204,16 @@ export type UnwrapSchema<
203
204
  : Definitions
204
205
  : unknown
205
206
 
206
- export type UnwrapBodySchema<
207
- Schema extends TSchema | string | undefined,
208
- Definitions extends Record<string, unknown> = {}
209
- > = undefined extends Schema
210
- ? unknown
211
- : Schema extends TSchema
212
- ? Schema extends OptionalField
213
- ? Prettify<Partial<Static<Schema>>> | null
214
- : StaticDecode<Schema>
215
- : Schema extends string
216
- ? Definitions extends Record<Schema, infer NamedSchema>
217
- ? NamedSchema
218
- : Definitions
219
- : unknown
220
-
221
207
  export interface UnwrapRoute<
222
208
  in out Schema extends InputSchema<any>,
223
- in out Definitions extends DefinitionBase['type'] = {}
209
+ in out Definitions extends DefinitionBase['type'] = {},
224
210
  > {
225
- body: UnwrapBodySchema<Schema['body'], Definitions>
211
+ body: UnwrapSchema<Schema['body'], Definitions>
226
212
  // headers: UnwrapSchema<Schema['headers'], Definitions>
227
213
  // query: UnwrapSchema<Schema['query'], Definitions>
228
214
  // params: UnwrapSchema<Schema['params'], Definitions>
229
215
  // cookie: UnwrapSchema<Schema['cookie'], Definitions>
230
- response: Schema['response'] extends TSchema | string
216
+ response: Schema['response'] extends TypeSchema | string
231
217
  ? {
232
218
  200: CoExist<
233
219
  UnwrapSchema<Schema['response'], Definitions>,
@@ -235,7 +221,7 @@ export interface UnwrapRoute<
235
221
  BunFile
236
222
  >
237
223
  }
238
- : Schema['response'] extends Record<number, TAnySchema | string>
224
+ : Schema['response'] extends Record<number, TypeSchema | string>
239
225
  ? {
240
226
  [k in keyof Schema['response']]: CoExist<
241
227
  UnwrapSchema<Schema['response'][k], Definitions>,
@@ -246,50 +232,6 @@ export interface UnwrapRoute<
246
232
  : unknown | void
247
233
  }
248
234
 
249
- // export interface UnwrapGroupGuardRoute<
250
- // in out Schema extends InputSchema<any>,
251
- // in out Definitions extends Record<string, unknown> = {},
252
- // Path extends string = ''
253
- // > {
254
- // body: UnwrapBodySchema<Schema['body'], Definitions>
255
- // headers: UnwrapSchema<
256
- // Schema['headers'],
257
- // Definitions
258
- // > extends infer A extends Record<string, unknown>
259
- // ? A
260
- // : undefined
261
- // query: UnwrapSchema<
262
- // Schema['query'],
263
- // Definitions
264
- // > extends infer A extends Record<string, unknown>
265
- // ? A
266
- // : undefined
267
- // params: UnwrapSchema<
268
- // Schema['params'],
269
- // Definitions
270
- // > extends infer A extends Record<string, unknown>
271
- // ? A
272
- // : Path extends `${string}/${':' | '*'}${string}`
273
- // ? Record<GetPathParameter<Path>, string>
274
- // : never
275
- // cookie: UnwrapSchema<
276
- // Schema['cookie'],
277
- // Definitions
278
- // > extends infer A extends Record<string, unknown>
279
- // ? A
280
- // : undefined
281
- // response: Schema['response'] extends TSchema | string
282
- // ? UnwrapSchema<Schema['response'], Definitions>
283
- // : Schema['response'] extends {
284
- // [k in string]: TSchema | string
285
- // }
286
- // ? UnwrapSchema<
287
- // Schema['response'][keyof Schema['response']],
288
- // Definitions
289
- // >
290
- // : unknown | void
291
- // }
292
-
293
235
  export type HookContainer<T extends Function = Function> = {
294
236
  checksum?: number
295
237
  scope?: LifeCycleType
@@ -376,21 +318,21 @@ export type HTTPMethod =
376
318
  | 'ALL'
377
319
 
378
320
  export interface InputSchema<Name extends string = string> {
379
- body?: TSchema | Name
321
+ body?: TypeSchema | Name
380
322
  // headers?: TObject | TNull | TUndefined | Name
381
323
  // query?: TObject | TNull | TUndefined | Name
382
324
  // params?: TObject | TNull | TUndefined | Name
383
325
  // cookie?: TObject | TNull | TUndefined | Name
384
326
  response?:
385
- | TSchema
386
- | Record<number, TSchema>
327
+ | TypeSchema
328
+ | Record<number, TypeSchema>
387
329
  | Name
388
- | Record<number, Name | TSchema>
330
+ | Record<number, Name | TypeSchema>
389
331
  }
390
332
 
391
333
  export interface MergeSchema<
392
334
  in out A extends RouteSchema,
393
- in out B extends RouteSchema
335
+ in out B extends RouteSchema,
394
336
  > {
395
337
  body: undefined extends A['body'] ? B['body'] : A['body']
396
338
  headers: undefined extends A['headers'] ? B['headers'] : A['headers']
@@ -414,9 +356,9 @@ export type Handler<
414
356
  derive: {}
415
357
  resolve: {}
416
358
  },
417
- Path extends string = ''
359
+ Path extends string = '',
418
360
  > = (
419
- context: Context<Route, Singleton, Path>
361
+ context: Context<Route, Singleton, Path>,
420
362
  ) => MaybePromise<
421
363
  {} extends Route['response']
422
364
  ? unknown
@@ -458,48 +400,31 @@ export type InlineHandler<
458
400
  resolve: {}
459
401
  },
460
402
  Path extends string = '',
461
- MacroContext = {}
462
- > =
463
- | ((
464
- context: MacroContext extends Record<
465
- string | number | symbol,
466
- unknown
467
- >
468
- ? Prettify<MacroContext & Context<Route, Singleton, Path>>
469
- : Context<Route, Singleton, Path>
470
- ) =>
471
- | Response
472
- | MaybePromise<
473
- {} extends Route['response']
474
- ? unknown
475
- :
476
- | (Route['response'] extends { 200: any }
477
- ? Route['response']
478
- : string | number | boolean | Object)
479
- | Route['response'][keyof Route['response']]
480
- | {
481
- [Status in keyof Route['response']]: {
482
- _type: Record<
483
- Status,
484
- Route['response'][Status]
485
- >
486
- [ELYSIA_RESPONSE]: Status
487
- }
488
- }[keyof Route['response']]
489
- >)
490
- | ({} extends Route['response']
491
- ? string | number | boolean | Object
492
- :
493
- | (Route['response'] extends { 200: any }
494
- ? Route['response']
495
- : string | number | boolean | Object)
496
- | Route['response'][keyof Route['response']]
497
- | {
498
- [Status in keyof Route['response']]: {
499
- _type: Record<Status, Route['response'][Status]>
500
- [ELYSIA_RESPONSE]: Status
501
- }
502
- }[keyof Route['response']])
403
+ MacroContext = {},
404
+ > = (
405
+ context: MacroContext extends Record<string | number | symbol, unknown>
406
+ ? Prettify<MacroContext & Context<Route, Singleton, Path>>
407
+ : Context<Route, Singleton, Path>,
408
+ ) =>
409
+ | Response
410
+ | MaybePromise<
411
+ {} extends Route['response']
412
+ ? unknown
413
+ :
414
+ | (Route['response'] extends { 200: any }
415
+ ? Route['response']
416
+ : string | number | boolean | Object)
417
+ | Route['response'][keyof Route['response']]
418
+ | {
419
+ [Status in keyof Route['response']]: {
420
+ _type: Record<
421
+ Status,
422
+ Route['response'][Status]
423
+ >
424
+ [ELYSIA_RESPONSE]: Status
425
+ }
426
+ }[keyof Route['response']]
427
+ >
503
428
 
504
429
  export type OptionalHandler<
505
430
  in out Route extends RouteSchema = {},
@@ -509,9 +434,9 @@ export type OptionalHandler<
509
434
  derive: {}
510
435
  resolve: {}
511
436
  },
512
- Path extends string = ''
437
+ Path extends string = '',
513
438
  > = Handler<Route, Singleton, Path> extends (
514
- context: infer Context
439
+ context: infer Context,
515
440
  ) => infer Returned
516
441
  ? (context: Context) => Returned | MaybePromise<void>
517
442
  : never
@@ -524,16 +449,16 @@ export type AfterHandler<
524
449
  derive: {}
525
450
  resolve: {}
526
451
  },
527
- Path extends string = ''
452
+ Path extends string = '',
528
453
  > = Handler<Route, Singleton, Path> extends (
529
- context: infer Context
454
+ context: infer Context,
530
455
  ) => infer Returned
531
456
  ? (
532
457
  context: Prettify<
533
458
  {
534
459
  response: Route['response']
535
460
  } & Context
536
- >
461
+ >,
537
462
  ) => Returned | MaybePromise<void>
538
463
  : never
539
464
 
@@ -545,7 +470,7 @@ export type MapResponse<
545
470
  derive: {}
546
471
  resolve: {}
547
472
  },
548
- Path extends string = ''
473
+ Path extends string = '',
549
474
  > = Handler<
550
475
  Omit<Route, 'response'> & {
551
476
  response: MaybePromise<Response | undefined | unknown>
@@ -565,7 +490,7 @@ export type VoidHandler<
565
490
  store: {}
566
491
  derive: {}
567
492
  resolve: {}
568
- }
493
+ },
569
494
  > = (context: Context<Route, Singleton>) => MaybePromise<void>
570
495
 
571
496
  export type TransformHandler<
@@ -576,7 +501,7 @@ export type TransformHandler<
576
501
  derive: {}
577
502
  resolve: {}
578
503
  },
579
- BasePath extends string = ''
504
+ BasePath extends string = '',
580
505
  > = {
581
506
  (
582
507
  context: Prettify<
@@ -587,7 +512,7 @@ export type TransformHandler<
587
512
  },
588
513
  BasePath
589
514
  >
590
- >
515
+ >,
591
516
  ): MaybePromise<void>
592
517
  }
593
518
 
@@ -599,7 +524,7 @@ export type BodyHandler<
599
524
  derive: {}
600
525
  resolve: {}
601
526
  },
602
- Path extends string = ''
527
+ Path extends string = '',
603
528
  > = (
604
529
  context: Prettify<
605
530
  {
@@ -620,7 +545,7 @@ export type BodyHandler<
620
545
  * })
621
546
  * ```
622
547
  */
623
- contentType: string
548
+ contentType: string,
624
549
  ) => MaybePromise<any>
625
550
 
626
551
  export type PreHandler<
@@ -630,7 +555,7 @@ export type PreHandler<
630
555
  store: {}
631
556
  derive: {}
632
557
  resolve: {}
633
- }
558
+ },
634
559
  > = (context: PreContext<Singleton>) => MaybePromise<Route['response'] | void>
635
560
 
636
561
  export type AfterResponseHandler<
@@ -640,17 +565,17 @@ export type AfterResponseHandler<
640
565
  store: {}
641
566
  derive: {}
642
567
  resolve: {}
643
- }
568
+ },
644
569
  > = (
645
570
  context: Prettify<
646
571
  Context<Route, Singleton> & {
647
572
  response: Route['response']
648
573
  }
649
- >
574
+ >,
650
575
  ) => MaybePromise<void>
651
576
 
652
577
  export type GracefulHandler<
653
- in Instance extends Spiceflow<any, any, any, any, any, any, any, any>
578
+ in Instance extends Spiceflow<any, any, any, any, any, any, any, any>,
654
579
  > = (data: Instance) => any
655
580
 
656
581
  export type ErrorHandler<
@@ -673,7 +598,7 @@ export type ErrorHandler<
673
598
  derive: {}
674
599
  resolve: {}
675
600
  schema: {}
676
- }
601
+ },
677
602
  > = (
678
603
  context: ErrorContext<
679
604
  Route,
@@ -796,7 +721,7 @@ export type ErrorHandler<
796
721
  Volatile['resolve']
797
722
  >
798
723
  >
799
- )
724
+ ),
800
725
  ) => any | Promise<any>
801
726
 
802
727
  export type Isolate<T> = {
@@ -825,7 +750,7 @@ export type LocalHook<
825
750
  params: undefined extends Schema['params']
826
751
  ? ResolvePath<Path>
827
752
  : Schema['params']
828
- }
753
+ },
829
754
  > = (LocalSchema extends {} ? LocalSchema : Isolate<LocalSchema>) &
830
755
  Extension & {
831
756
  /**
@@ -948,54 +873,54 @@ export interface MacroManager<
948
873
  derive: {}
949
874
  resolve: {}
950
875
  },
951
- in out Errors extends Record<string, Error> = {}
876
+ in out Errors extends Record<string, Error> = {},
952
877
  > {
953
878
  onParse(fn: MaybeArray<BodyHandler<TypedRoute, Singleton>>): unknown
954
879
  onParse(
955
880
  options: MacroOptions,
956
- fn: MaybeArray<BodyHandler<TypedRoute, Singleton>>
881
+ fn: MaybeArray<BodyHandler<TypedRoute, Singleton>>,
957
882
  ): unknown
958
883
 
959
884
  onTransform(fn: MaybeArray<VoidHandler<TypedRoute, Singleton>>): unknown
960
885
  onTransform(
961
886
  options: MacroOptions,
962
- fn: MaybeArray<VoidHandler<TypedRoute, Singleton>>
887
+ fn: MaybeArray<VoidHandler<TypedRoute, Singleton>>,
963
888
  ): unknown
964
889
 
965
890
  onBeforeHandle(
966
- fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>
891
+ fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>,
967
892
  ): unknown
968
893
  onBeforeHandle(
969
894
  options: MacroOptions,
970
- fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>
895
+ fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>,
971
896
  ): unknown
972
897
 
973
898
  onAfterHandle(fn: MaybeArray<AfterHandler<TypedRoute, Singleton>>): unknown
974
899
  onAfterHandle(
975
900
  options: MacroOptions,
976
- fn: MaybeArray<AfterHandler<TypedRoute, Singleton>>
901
+ fn: MaybeArray<AfterHandler<TypedRoute, Singleton>>,
977
902
  ): unknown
978
903
 
979
904
  onError(
980
- fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>
905
+ fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>,
981
906
  ): unknown
982
907
  onError(
983
908
  options: MacroOptions,
984
- fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>
909
+ fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>,
985
910
  ): unknown
986
911
 
987
912
  mapResponse(fn: MaybeArray<MapResponse<TypedRoute, Singleton>>): unknown
988
913
  mapResponse(
989
914
  options: MacroOptions,
990
- fn: MaybeArray<MapResponse<TypedRoute, Singleton>>
915
+ fn: MaybeArray<MapResponse<TypedRoute, Singleton>>,
991
916
  ): unknown
992
917
 
993
918
  onAfterResponse(
994
- fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>
919
+ fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>,
995
920
  ): unknown
996
921
  onAfterResponse(
997
922
  options: MacroOptions,
998
- fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>
923
+ fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>,
999
924
  ): unknown
1000
925
 
1001
926
  events: {
@@ -1010,7 +935,7 @@ export type MacroQueue = HookContainer<
1010
935
 
1011
936
  type _CreateEden<
1012
937
  Path extends string,
1013
- Property extends Record<string, unknown> = {}
938
+ Property extends Record<string, unknown> = {},
1014
939
  > = Path extends `${infer Start}/${infer Rest}`
1015
940
  ? {
1016
941
  [x in Start]: _CreateEden<Rest, Property>
@@ -1021,7 +946,7 @@ type _CreateEden<
1021
946
 
1022
947
  export type CreateEden<
1023
948
  Path extends string,
1024
- Property extends Record<string, unknown> = {}
949
+ Property extends Record<string, unknown> = {},
1025
950
  > = Path extends `/${infer Rest}`
1026
951
  ? _CreateEden<Rest, Property>
1027
952
  : Path extends ''
@@ -1073,10 +998,10 @@ export type MergeSpiceflowInstances<
1073
998
  macro: {}
1074
999
  macroFn: {}
1075
1000
  },
1076
- Routes extends RouteBase = {}
1001
+ Routes extends RouteBase = {},
1077
1002
  > = Instances extends [
1078
1003
  infer Current extends Spiceflow<any, any, any, any, any, any>,
1079
- ...infer Rest extends Spiceflow<any, any, any, any, any, any>[]
1004
+ ...infer Rest extends Spiceflow<any, any, any, any, any, any>[],
1080
1005
  ]
1081
1006
  ? Current['_types']['Scoped'] extends true
1082
1007
  ? MergeSpiceflowInstances<
@@ -1132,7 +1057,7 @@ export type ExcludeSpiceflowResponse<T> = Exclude<
1132
1057
  export type InferContext<
1133
1058
  T extends Spiceflow<any, any, any, any, any, any, any, any>,
1134
1059
  Path extends string = T['_types']['Prefix'],
1135
- Schema extends RouteSchema = T['_types']['Metadata']['schema']
1060
+ Schema extends RouteSchema = T['_types']['Metadata']['schema'],
1136
1061
  > = Context<
1137
1062
  MergeSchema<Schema, T['_types']['Metadata']['schema']>,
1138
1063
  T['_types']['Singleton'] & {
@@ -1145,7 +1070,7 @@ export type InferContext<
1145
1070
  export type InferHandler<
1146
1071
  T extends Spiceflow<any, any, any, any, any, any, any, any>,
1147
1072
  Path extends string = T['_types']['Prefix'],
1148
- Schema extends RouteSchema = T['_types']['Metadata']['schema']
1073
+ Schema extends RouteSchema = T['_types']['Metadata']['schema'],
1149
1074
  > = InlineHandler<
1150
1075
  MergeSchema<Schema, T['_types']['Metadata']['schema']>,
1151
1076
  T['_types']['Singleton'] & {
@@ -1180,7 +1105,7 @@ export type UnionToIntersect<U> = (
1180
1105
 
1181
1106
  export type ResolveMacroContext<
1182
1107
  Macro extends BaseMacro,
1183
- MacroFn extends BaseMacroFn
1108
+ MacroFn extends BaseMacroFn,
1184
1109
  > = UnionToIntersect<
1185
1110
  {
1186
1111
  [K in keyof Macro]-?: undefined extends Macro[K]
@@ -1199,7 +1124,7 @@ export type ResolveMacroContext<
1199
1124
  export type ContextAppendType = 'append' | 'override'
1200
1125
 
1201
1126
  export type HigherOrderFunction<
1202
- T extends (...arg: unknown[]) => Function = (...arg: unknown[]) => Function
1127
+ T extends (...arg: unknown[]) => Function = (...arg: unknown[]) => Function,
1203
1128
  > = (fn: T, request: Request) => ReturnType<T>
1204
1129
 
1205
1130
  // new Spiceflow()