@@ -8,8 +8,10 @@ import {
8
8
mergeHook ,
9
9
mergeDeep ,
10
10
createValidationError ,
11
+ getSchemaValidator ,
11
12
SCHEMA ,
12
- getSchemaValidator
13
+ DEFS ,
14
+ getResponseSchemaValidator
13
15
} from './utils'
14
16
import { registerSchemaPath } from './schema'
15
17
import { mapErrorCode , mapErrorStatus } from './error'
@@ -44,8 +46,10 @@ import type {
44
46
NoReturnHandler ,
45
47
ElysiaRoute ,
46
48
MaybePromise ,
47
- IsNever
49
+ IsNever ,
50
+ InferSchema
48
51
} from './types'
52
+ import { type TSchema } from '@sinclair/typebox'
49
53
50
54
/**
51
55
* ### Elysia Server
@@ -65,7 +69,8 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
65
69
config : ElysiaConfig
66
70
67
71
store : Instance [ 'store' ] = {
68
- [ SCHEMA ] : { }
72
+ [ SCHEMA ] : { } ,
73
+ [ DEFS ] : { }
69
74
}
70
75
// Will be applied to Context
71
76
protected decorators : Record < string , unknown > | null = null
@@ -99,7 +104,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
99
104
}
100
105
101
106
private _addHandler <
102
- Schema extends TypedSchema = TypedSchema ,
107
+ Schema extends TypedSchema ,
103
108
Path extends string = string
104
109
> (
105
110
method : HTTPMethod ,
@@ -116,28 +121,36 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
116
121
hooks : mergeHook ( clone ( this . event ) , hook as RegisteredHook )
117
122
} )
118
123
124
+ const defs = this . store [ DEFS ]
125
+
119
126
const body = getSchemaValidator (
120
- hook ?. schema ?. body ?? this . $schema ?. body
127
+ hook ?. schema ?. body ?? this . $schema ?. body ,
128
+ defs
121
129
)
122
130
const header = getSchemaValidator (
123
131
hook ?. schema ?. headers ?? this . $schema ?. headers ,
132
+ defs ,
124
133
true
125
134
)
126
135
const params = getSchemaValidator (
127
- hook ?. schema ?. params ?? this . $schema ?. params
136
+ hook ?. schema ?. params ?? this . $schema ?. params ,
137
+ defs
128
138
)
129
139
const query = getSchemaValidator (
130
- hook ?. schema ?. query ?? this . $schema ?. query
140
+ hook ?. schema ?. query ?? this . $schema ?. query ,
141
+ defs
131
142
)
132
- const response = getSchemaValidator (
133
- hook ?. schema ?. response ?? this . $schema ?. response
143
+ const response = getResponseSchemaValidator (
144
+ hook ?. schema ?. response ?? this . $schema ?. response ,
145
+ defs
134
146
)
135
147
136
148
registerSchemaPath ( {
137
149
schema : this . store [ SCHEMA ] ,
138
150
hook,
139
151
method,
140
- path
152
+ path,
153
+ models : this . store [ DEFS ]
141
154
} )
142
155
143
156
const validator =
@@ -639,7 +652,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
639
652
* ```
640
653
*/
641
654
get <
642
- Schema extends TypedSchema = TypedSchema ,
655
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
643
656
Path extends string = string ,
644
657
Response = unknown
645
658
> (
@@ -671,7 +684,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
671
684
* ```
672
685
*/
673
686
post <
674
- Schema extends TypedSchema = { } ,
687
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
675
688
Path extends string = string ,
676
689
Response = unknown
677
690
> (
@@ -703,7 +716,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
703
716
* ```
704
717
*/
705
718
put <
706
- Schema extends TypedSchema = { } ,
719
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
707
720
Path extends string = string ,
708
721
Response = unknown
709
722
> (
@@ -735,7 +748,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
735
748
* ```
736
749
*/
737
750
patch <
738
- Schema extends TypedSchema = { } ,
751
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
739
752
Path extends string = string ,
740
753
Response = unknown
741
754
> (
@@ -767,7 +780,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
767
780
* ```
768
781
*/
769
782
delete <
770
- Schema extends TypedSchema = { } ,
783
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
771
784
Path extends string = string ,
772
785
Response = unknown
773
786
> (
@@ -799,7 +812,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
799
812
* ```
800
813
*/
801
814
options <
802
- Schema extends TypedSchema = { } ,
815
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
803
816
Path extends string = string ,
804
817
Response = unknown
805
818
> (
@@ -826,7 +839,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
826
839
* ```
827
840
*/
828
841
all <
829
- Schema extends TypedSchema = { } ,
842
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
830
843
Path extends string = string ,
831
844
Response = unknown
832
845
> (
@@ -858,7 +871,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
858
871
* ```
859
872
*/
860
873
head <
861
- Schema extends TypedSchema = { } ,
874
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
862
875
Path extends string = string ,
863
876
Response = unknown
864
877
> (
@@ -890,7 +903,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
890
903
* ```
891
904
*/
892
905
trace <
893
- Schema extends TypedSchema = { } ,
906
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
894
907
Path extends string = string ,
895
908
Response = unknown
896
909
> (
@@ -922,7 +935,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
922
935
* ```
923
936
*/
924
937
connect <
925
- Schema extends TypedSchema = { } ,
938
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
926
939
Path extends string = string ,
927
940
Response = unknown
928
941
> (
@@ -955,7 +968,7 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
955
968
*/
956
969
route <
957
970
Method extends HTTPMethod = HTTPMethod ,
958
- Schema extends TypedSchema = { } ,
971
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
959
972
Path extends string = string ,
960
973
Response = unknown
961
974
> (
@@ -1104,21 +1117,22 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
1104
1117
* ```
1105
1118
*/
1106
1119
schema <
1107
- Schema extends TypedSchema = TypedSchema ,
1120
+ Schema extends InferSchema < Instance > = InferSchema < Instance > ,
1108
1121
NewInstance = Elysia < {
1109
1122
request : Instance [ 'request' ]
1110
1123
store : Instance [ 'store' ]
1111
1124
schema : MergeSchema < Schema , Instance [ 'schema' ] >
1112
1125
} >
1113
1126
> ( schema : Schema ) : NewInstance {
1127
+ const defs = this . store [ DEFS ]
1128
+
1114
1129
this . $schema = {
1115
- body : getSchemaValidator ( schema ?. body ) ,
1116
- headers : getSchemaValidator ( schema ?. headers ) ,
1117
- params : getSchemaValidator ( schema ?. params ) ,
1118
- query : getSchemaValidator ( schema ?. query ) ,
1119
- response : getSchemaValidator (
1120
- schema ?. response ?. [ '200' ] ?? schema . response
1121
- )
1130
+ body : getSchemaValidator ( schema . body , defs ) ,
1131
+ headers : getSchemaValidator ( schema ?. headers , defs , true ) ,
1132
+ params : getSchemaValidator ( schema ?. params , defs ) ,
1133
+ query : getSchemaValidator ( schema ?. query , defs ) ,
1134
+ // @ts -ignore
1135
+ response : getSchemaValidator ( schema ?. response , defs )
1122
1136
}
1123
1137
1124
1138
return this as unknown as NewInstance
@@ -1421,13 +1435,28 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> {
1421
1435
get modules ( ) {
1422
1436
return Promise . all ( this . lazyLoadModules )
1423
1437
}
1438
+
1439
+ setModel < Recorder extends Record < string , TSchema > > (
1440
+ record : Recorder
1441
+ ) : Elysia < {
1442
+ store : Instance [ 'store' ] & {
1443
+ [ Defs in typeof DEFS ] : Recorder
1444
+ }
1445
+ request : Instance [ 'request' ]
1446
+ schema : Instance [ 'schema' ]
1447
+ } > {
1448
+ Object . assign ( this . store [ DEFS ] , record )
1449
+
1450
+ return this as unknown as any
1451
+ }
1424
1452
}
1425
1453
1426
1454
export { Elysia , Router }
1427
1455
export { Type as t } from '@sinclair/typebox'
1428
1456
1429
1457
export {
1430
1458
SCHEMA ,
1459
+ DEFS ,
1431
1460
getPath ,
1432
1461
createValidationError ,
1433
1462
getSchemaValidator
0 commit comments