File tree Expand file tree Collapse file tree 5 files changed +90
-10
lines changed Expand file tree Collapse file tree 5 files changed +90
-10
lines changed Original file line number Diff line number Diff line change 1
- import { Value } from '@sinclair/typebox/value'
2
- import { Elysia , t } from '../src'
3
- import { req } from '../test/utils'
4
-
5
- console . log ( Value . Create ( t . Date ( ) ) instanceof Date )
1
+ new Elysia ( )
2
+ . macro ( {
3
+ auth ( enabled : boolean ) {
4
+ return {
5
+ async resolve ( ) {
6
+ return {
7
+ user : 'saltyaom'
8
+ }
9
+ }
10
+ }
11
+ }
12
+ } )
13
+ . get ( '/' , ( { user } ) => { } , {
14
+ auth : true
15
+ } )
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " elysia" ,
3
3
"description" : " Ergonomic Framework for Human" ,
4
- "version" : " 1.2.0-exp.49 " ,
4
+ "version" : " 1.2.0-exp.50 " ,
5
5
"author" : {
6
6
"name" : " saltyAom" ,
7
7
"url" : " https://github.com/SaltyAom" ,
Original file line number Diff line number Diff line change @@ -635,17 +635,17 @@ export type MacroToContext<
635
635
...v : any [ ]
636
636
) => {
637
637
resolve : MaybeArray <
638
- ( ...v : any ) => Record < keyof any , unknown >
638
+ ( ...v : any ) => MaybePromise < Record < keyof any , unknown > >
639
639
>
640
640
}
641
641
? key
642
642
: never ] : ResolveResolutions <
643
643
// @ts -expect-error type is checked in key mapping
644
- ReturnType < MacroFn [ key ] > [ 'resolve' ]
644
+ Awaited < ReturnType < MacroFn [ key ] > [ 'resolve' ] >
645
645
>
646
646
} extends infer A extends Record < RecordKey , unknown >
647
647
? IsNever < A [ keyof A ] > extends false
648
- ? A [ keyof A ]
648
+ ? Awaited < A [ keyof A ] >
649
649
: { }
650
650
: { }
651
651
Original file line number Diff line number Diff line change @@ -555,4 +555,28 @@ describe('Macro', () => {
555
555
expect ( a ) . toEqual ( { user : { name : 'anon' } } )
556
556
expect ( b ) . toEqual ( { user : { name : 'hoshino' } } )
557
557
} )
558
+
559
+ it ( 'accept async resolve' , async ( ) => {
560
+ const app = new Elysia ( )
561
+ . macro ( {
562
+ user : ( enabled : boolean ) => ( {
563
+ resolve : async ( { query : { name = 'anon' } } ) => ( {
564
+ user : {
565
+ name
566
+ }
567
+ } )
568
+ } )
569
+ } )
570
+ . get ( '/' , ( { user } ) => user , {
571
+ user : true
572
+ } )
573
+
574
+ const [ a , b ] = await Promise . all ( [
575
+ app . handle ( req ( '/' ) ) . then ( ( x ) => x . json ( ) ) ,
576
+ app . handle ( req ( '/?name=hoshino' ) ) . then ( ( x ) => x . json ( ) )
577
+ ] )
578
+
579
+ expect ( a ) . toEqual ( { user : { name : 'anon' } } )
580
+ expect ( b ) . toEqual ( { user : { name : 'hoshino' } } )
581
+ } )
558
582
} )
Original file line number Diff line number Diff line change @@ -1923,7 +1923,6 @@ type a = keyof {}
1923
1923
// stuff: number
1924
1924
// }
1925
1925
// >()
1926
-
1927
1926
// return undefined as any
1928
1927
}
1929
1928
}
@@ -2117,3 +2116,50 @@ type a = keyof {}
2117
2116
response : t . String ( )
2118
2117
} )
2119
2118
}
2119
+
2120
+ // ? Macro resolve
2121
+ {
2122
+ const app = new Elysia ( )
2123
+ . macro ( {
2124
+ user : ( enabled : boolean ) => ( {
2125
+ resolve : async ( { query : { name = 'anon' } } ) => ( {
2126
+ user : {
2127
+ name,
2128
+ async : false
2129
+ } as const
2130
+ } )
2131
+ } ) ,
2132
+ asyncUser : ( enabled : boolean ) => ( {
2133
+ resolve : async ( { query : { name = 'anon' } } ) => ( {
2134
+ user : {
2135
+ name,
2136
+ async : true
2137
+ } as const
2138
+ } )
2139
+ } )
2140
+ } )
2141
+ . get (
2142
+ '/' ,
2143
+ ( { user } ) => {
2144
+ expectTypeOf < typeof user > ( ) . toEqualTypeOf < {
2145
+ readonly name : string
2146
+ readonly async : false
2147
+ } > ( )
2148
+ } ,
2149
+ {
2150
+ user : true
2151
+ }
2152
+ )
2153
+ . get (
2154
+ '/' ,
2155
+ ( { user } ) => {
2156
+ expectTypeOf < typeof user > ( ) . toEqualTypeOf < {
2157
+ readonly name : string
2158
+ readonly async : true
2159
+ } > ( )
2160
+ } ,
2161
+ {
2162
+ asyncUser : true
2163
+ }
2164
+ )
2165
+ }
You can’t perform that action at this time.
0 commit comments