Skip to content

Commit 61beb3c

Browse files
committed
🎉 feat: async resolve macro
1 parent f84e678 commit 61beb3c

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

example/a.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
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+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.2.0-exp.49",
4+
"version": "1.2.0-exp.50",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,17 +635,17 @@ export type MacroToContext<
635635
...v: any[]
636636
) => {
637637
resolve: MaybeArray<
638-
(...v: any) => Record<keyof any, unknown>
638+
(...v: any) => MaybePromise<Record<keyof any, unknown>>
639639
>
640640
}
641641
? key
642642
: never]: ResolveResolutions<
643643
// @ts-expect-error type is checked in key mapping
644-
ReturnType<MacroFn[key]>['resolve']
644+
Awaited<ReturnType<MacroFn[key]>['resolve']>
645645
>
646646
} extends infer A extends Record<RecordKey, unknown>
647647
? IsNever<A[keyof A]> extends false
648-
? A[keyof A]
648+
? Awaited<A[keyof A]>
649649
: {}
650650
: {}
651651

test/macro/macro.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,4 +555,28 @@ describe('Macro', () => {
555555
expect(a).toEqual({ user: { name: 'anon' } })
556556
expect(b).toEqual({ user: { name: 'hoshino' } })
557557
})
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+
})
558582
})

test/types/index.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,6 @@ type a = keyof {}
19231923
// stuff: number
19241924
// }
19251925
// >()
1926-
19271926
// return undefined as any
19281927
}
19291928
}
@@ -2117,3 +2116,50 @@ type a = keyof {}
21172116
response: t.String()
21182117
})
21192118
}
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+
}

0 commit comments

Comments
 (0)