Skip to content

Commit 60bcb2e

Browse files
committed
🔧 fix: recursive macro with conflict value per status
1 parent e7a7f13 commit 60bcb2e

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

example/a.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ type Models = {
1313

1414
const app = new Elysia()
1515
.macro('isAuth', {
16-
// headers: t.Object({
17-
// authorization: t.TemplateLiteral('Authorization ${string}')
18-
// }),
16+
headers: t.Object({
17+
authorization: t.TemplateLiteral('Authorization ${string}')
18+
}),
1919
async resolve({ headers, status }) {
2020
// Mock authentication logic
2121
if (Math.random() > 0.5) return status(401, 'Not signed in')
2222
if (Math.random() > 0.5) return status(401, 'Deactivated account')
2323

24+
25+
26+
headers.authorization
27+
2428
return {
2529
user: 'saltyaom'
2630
}
@@ -51,16 +55,15 @@ const app = new Elysia()
5155
.post(
5256
'/',
5357
async ({ user, admin, body, headers }) => {
54-
return 'ok'
55-
// const updated = await admin.updateUser(body)
58+
const updated = await admin.updateUser(body)
5659

57-
// if (updated instanceof ElysiaStatus) return updated
60+
if (updated instanceof ElysiaStatus) return updated
5861

59-
// return `User ${user} updated user ${updated.id}` as const
62+
return `User ${user} updated user ${updated.id}` as const
6063
},
6164
{
62-
isAuth: true,
63-
// body: 'user.update'
65+
isAdmin: true,
66+
body: 'user.update'
6467
}
6568
)
6669

src/types.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,13 @@ type ExtractResolveFromMacro<A> =
965965
type ExtractOnlyResponseFromMacro<A> =
966966
IsNever<A> extends true
967967
? {}
968-
: {} extends A
969-
? {}
970-
: {
971-
return: MergeResponseStatus<A>
972-
}
968+
: Extract<A, AnyElysiaCustomStatusResponse> extends infer A
969+
? IsNever<A> extends true
970+
? {}
971+
: {
972+
return: MergeResponseStatus<A>
973+
}
974+
: {}
973975

974976
type MergeResponseStatus<A> = {
975977
[status in keyof UnionToIntersect<
@@ -978,7 +980,12 @@ type MergeResponseStatus<A> = {
978980
? { [A in Status]: 1 }
979981
: never
980982
// @ts-ignore A is checked in key computation
981-
>]: Extract<A, { code: status }>['response']
983+
>]: Extract<A, { code: status }>['response'] extends infer Value
984+
? IsAny<Value> extends true
985+
? // @ts-ignore status is always in Status Map
986+
InvertedStatusMap[status]
987+
: Value
988+
: never
982989
}
983990

984991
type MergeAllStatus<T> = {
@@ -990,29 +997,24 @@ type MergeAllStatus<T> = {
990997
type ExtractAllResponseFromMacro<A> =
991998
IsNever<A> extends true
992999
? {}
993-
: {} extends A
994-
? {}
995-
: {
996-
// Merge all status to single object first
997-
return: MergeResponseStatus<A> &
998-
(Exclude<
999-
A,
1000-
AnyElysiaCustomStatusResponse
1001-
> extends infer A
1002-
? IsAny<A> extends true
1000+
: {
1001+
// Merge all status to single object first
1002+
return: MergeResponseStatus<A> &
1003+
(Exclude<A, AnyElysiaCustomStatusResponse> extends infer A
1004+
? IsAny<A> extends true
1005+
? {}
1006+
: IsNever<A> extends true
10031007
? {}
1004-
: IsNever<A> extends true
1008+
: // FunctionArrayReturnType
1009+
NonNullable<void> extends A
10051010
? {}
1006-
: // FunctionArrayReturnType
1007-
NonNullable<void> extends A
1011+
: undefined extends A
10081012
? {}
1009-
: undefined extends A
1010-
? {}
1011-
: {
1012-
200: A
1013-
}
1014-
: {})
1015-
}
1013+
: {
1014+
200: A
1015+
}
1016+
: {})
1017+
}
10161018

10171019
type FlattenMacroResponse<T> = T extends object
10181020
? '_' extends keyof T

test/types/lifecycle/soundness.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,7 @@ import { Prettify } from '../../../src/types'
22442244
.macro('multiple', {
22452245
resolve({ status }) {
22462246
if (Math.random() > 0.5) return status(401)
2247+
22472248
return status(403)
22482249
}
22492250
})
@@ -2370,6 +2371,7 @@ import { Prettify } from '../../../src/types'
23702371
if (Math.random()) return status(400, 'a')
23712372
if (Math.random()) return status(401, 'a')
23722373
if (Math.random()) return status(401, 'b')
2374+
if (Math.random()) return status(402)
23732375

23742376
if (Math.random())
23752377
// Test status-like response but literal not box
@@ -2389,5 +2391,6 @@ import { Prettify } from '../../../src/types'
23892391
200: string | { readonly status: 401; readonly response: 'c' }
23902392
400: 'a'
23912393
401: 'a' | 'b'
2394+
402: 'Payment Required'
23922395
}>
23932396
}

0 commit comments

Comments
 (0)