Skip to content

Commit

Permalink
🎉 feat: async resolve macro
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 10, 2024
1 parent f84e678 commit 61beb3c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
20 changes: 15 additions & 5 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Value } from '@sinclair/typebox/value'
import { Elysia, t } from '../src'
import { req } from '../test/utils'

console.log(Value.Create(t.Date()) instanceof Date)
new Elysia()
.macro({
auth(enabled: boolean) {
return {
async resolve() {
return {
user: 'saltyaom'
}
}
}
}
})
.get('/', ({ user }) => {}, {
auth: true
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.2.0-exp.49",
"version": "1.2.0-exp.50",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,17 +635,17 @@ export type MacroToContext<
...v: any[]
) => {
resolve: MaybeArray<
(...v: any) => Record<keyof any, unknown>
(...v: any) => MaybePromise<Record<keyof any, unknown>>
>
}
? key
: never]: ResolveResolutions<
// @ts-expect-error type is checked in key mapping
ReturnType<MacroFn[key]>['resolve']
Awaited<ReturnType<MacroFn[key]>['resolve']>
>
} extends infer A extends Record<RecordKey, unknown>
? IsNever<A[keyof A]> extends false
? A[keyof A]
? Awaited<A[keyof A]>
: {}
: {}

Expand Down
24 changes: 24 additions & 0 deletions test/macro/macro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,28 @@ describe('Macro', () => {
expect(a).toEqual({ user: { name: 'anon' } })
expect(b).toEqual({ user: { name: 'hoshino' } })
})

it('accept async resolve', async () => {
const app = new Elysia()
.macro({
user: (enabled: boolean) => ({
resolve: async ({ query: { name = 'anon' } }) => ({
user: {
name
}
})
})
})
.get('/', ({ user }) => user, {
user: true
})

const [a, b] = await Promise.all([
app.handle(req('/')).then((x) => x.json()),
app.handle(req('/?name=hoshino')).then((x) => x.json())
])

expect(a).toEqual({ user: { name: 'anon' } })
expect(b).toEqual({ user: { name: 'hoshino' } })
})
})
48 changes: 47 additions & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,6 @@ type a = keyof {}
// stuff: number
// }
// >()

// return undefined as any
}
}
Expand Down Expand Up @@ -2117,3 +2116,50 @@ type a = keyof {}
response: t.String()
})
}

// ? Macro resolve
{
const app = new Elysia()
.macro({
user: (enabled: boolean) => ({
resolve: async ({ query: { name = 'anon' } }) => ({
user: {
name,
async: false
} as const
})
}),
asyncUser: (enabled: boolean) => ({
resolve: async ({ query: { name = 'anon' } }) => ({
user: {
name,
async: true
} as const
})
})
})
.get(
'/',
({ user }) => {
expectTypeOf<typeof user>().toEqualTypeOf<{
readonly name: string
readonly async: false
}>()
},
{
user: true
}
)
.get(
'/',
({ user }) => {
expectTypeOf<typeof user>().toEqualTypeOf<{
readonly name: string
readonly async: true
}>()
},
{
asyncUser: true
}
)
}

0 comments on commit 61beb3c

Please sign in to comment.