Skip to content

Commit 7773183

Browse files
committed
🎉 feat: merge multiple macro resolve response
1 parent 1a170fd commit 7773183

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Improvement:
44
- ValidationError.detail now accept optional 2nd parameter `allowUnsafeValidatorDetails`
55
- macro: add `introspect`
66
- prevent redundant route compilation
7+
- merge multiple macro resolve response
78

89
Bug fix:
910
- [#1537](https://github.com/elysiajs/elysia/issues/1537) websocket: ping/pong not being called

example/a.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import { InternalSymbolName } from 'typescript'
2-
import { Elysia, t } from '../src'
3-
import { req } from '../test/utils'
1+
import { Elysia, status, t } from '../src'
42

5-
const app = new Elysia().get('/', () => 'ok').compile()
6-
for (const route of app.routes) route.compile()
3+
status(401)
74

8-
console.log(app.fetch.toString())
9-
console.log(app.routes[0].compile().toString())
5+
const app = new Elysia()
6+
.macro({
7+
multiple: {
8+
resolve({ status }) {
9+
if (Math.random() > 0.5) return status(401)
10+
return status(403)
11+
}
12+
}
13+
})
14+
.get('/multiple', () => 'Ok', { multiple: true })
1015

11-
// Bun.sleepSync(7)
12-
// console.log('Slept')
13-
14-
const res = app
15-
.handle(req('/'))
16-
.then((x) => x.text())
17-
.then(console.log)
18-
19-
// process.exit(0)
16+
app['~Routes']['multiple']['get']['response']

src/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const emptyHttpStatus = {
3939
308: undefined
4040
} as const
4141

42-
export type SelectiveStatus<Res> = <
42+
export type SelectiveStatus<in out Res> = <
4343
const Code extends
4444
| keyof Res
4545
| InvertedStatusMap[Extract<keyof InvertedStatusMap, keyof Res>]
@@ -91,7 +91,7 @@ export const status = <
9191
>(
9292
code: Code,
9393
response?: T
94-
) => new ElysiaCustomStatusResponse<Code, T>(code, response as any)
94+
) => new ElysiaCustomStatusResponse<Code, T>(code, response as T)
9595

9696
export class InternalServerError extends Error {
9797
code = 'INTERNAL_SERVER_ERROR'

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ type ExtractOnlyResponseFromMacro<A> =
966966
? IsNever<A> extends true
967967
? {}
968968
: {
969-
return: A extends ElysiaCustomStatusResponse<
969+
return: UnionToIntersect< A extends ElysiaCustomStatusResponse<
970970
any,
971971
infer Value,
972972
infer Status
@@ -977,7 +977,7 @@ type ExtractOnlyResponseFromMacro<A> =
977977
InvertedStatusMap[Status]
978978
: Value
979979
}
980-
: {}
980+
: {}>
981981
}
982982
: {}
983983

test/types/lifecycle/soundness.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,3 +2192,91 @@ import { Prettify } from '../../../src/types'
21922192
}
21932193
})
21942194
}
2195+
2196+
// intersect multiple resolve macro response
2197+
{
2198+
const app = new Elysia()
2199+
.macro({
2200+
multiple: {
2201+
resolve({ status }) {
2202+
if (Math.random() > 0.5) return status(401)
2203+
return status(403)
2204+
}
2205+
}
2206+
})
2207+
.get('/multiple', () => 'Ok', { multiple: true })
2208+
2209+
expectTypeOf<
2210+
(typeof app)['~Routes']['multiple']['get']['response']
2211+
>().toEqualTypeOf<{
2212+
200: string
2213+
401: 'Unauthorized'
2214+
403: 'Forbidden'
2215+
}>()
2216+
}
2217+
2218+
// intersect multiple resolve macro response
2219+
{
2220+
// intersect multiple resolve macro response
2221+
{
2222+
const app = new Elysia()
2223+
.macro({
2224+
multiple: {
2225+
resolve({ status }) {
2226+
if (Math.random() > 0.5) return status(401)
2227+
return status(403)
2228+
}
2229+
}
2230+
})
2231+
.get('/multiple', () => 'Ok', { multiple: true })
2232+
2233+
expectTypeOf<
2234+
(typeof app)['~Routes']['multiple']['get']['response']
2235+
>().toEqualTypeOf<{
2236+
200: string
2237+
401: 'Unauthorized'
2238+
403: 'Forbidden'
2239+
}>()
2240+
2241+
const app = new Elysia()
2242+
.macro('multiple', {
2243+
resolve({ status }) {
2244+
if (Math.random() > 0.5) return status(401)
2245+
return status(403)
2246+
}
2247+
})
2248+
.get('/multiple', () => 'Ok', { multiple: true })
2249+
2250+
expectTypeOf<
2251+
(typeof app)['~Routes']['multiple']['get']['response']
2252+
>().toEqualTypeOf<{
2253+
200: string
2254+
401: 'Unauthorized'
2255+
403: 'Forbidden'
2256+
}>()
2257+
}
2258+
const app = new Elysia()
2259+
.macro('multiple', {
2260+
resolve({ status }) {
2261+
if (Math.random() > 0.5) return status(401)
2262+
return status(403)
2263+
}
2264+
})
2265+
.get('/multiple', () => 'Ok', { multiple: true })
2266+
2267+
expectTypeOf<
2268+
(typeof app)['~Routes']['multiple']['get']['response']
2269+
>().toEqualTypeOf<{
2270+
200: string
2271+
401: 'Unauthorized'
2272+
403: 'Forbidden'
2273+
}>()
2274+
2275+
expectTypeOf<
2276+
(typeof app)['~Routes']['multiple']['get']['response']
2277+
>().toEqualTypeOf<{
2278+
200: string
2279+
401: 'Unauthorized'
2280+
403: 'Forbidden'
2281+
}>()
2282+
}

0 commit comments

Comments
 (0)