Skip to content

Commit a9478c6

Browse files
committed
🔧 fix: macro resolve subType
1 parent 958fde0 commit a9478c6

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.9 - 28 Dec 2024
2+
Bug fix:
3+
- Resolve macro unintentionally return instead of assign new context
4+
15
# 1.2.8 - 27 Dec 2024
26
Bug fix:
37
- [#966](https://github.com/elysiajs/elysia/issues/966) generic error somehow return 200

example/a.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ import { Elysia, t, error, StatusMap } from '../src'
22
import { req } from '../test/utils'
33

44
const app = new Elysia()
5-
.get('/', () => {
6-
throw new Error("A")
5+
.macro({
6+
user: (enabled: boolean) => ({
7+
resolve: ({ query: { name = 'anon' } }) => ({
8+
user: {
9+
name
10+
}
11+
})
12+
})
713
})
8-
.listen(3000)
14+
.get('/', ({ user }) => user, {
15+
user: true
16+
})
17+
18+
const [a, b] = await Promise.all([
19+
app.handle(req('/')).then((x) => x.json()),
20+
app.handle(req('/?name=hoshino')).then((x) => x.json())
21+
])
922

10-
// console.log(await res.text())
11-
// console.log(res.status)
23+
console.log(a, b)

src/utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,10 +1236,11 @@ export const traceBackMacro = (
12361236
const hook = v(value)
12371237

12381238
if (typeof hook === 'object') {
1239-
for (const [k, v] of Object.entries(hook))
1239+
for (const [k, v] of Object.entries(hook)) {
12401240
manage(k as keyof LifeCycleStore)({
12411241
fn: v as any
12421242
})
1243+
}
12431244
}
12441245

12451246
delete property[key as unknown as keyof typeof extension]
@@ -1270,6 +1271,14 @@ export const createMacroManager =
12701271
fn: type
12711272
}
12721273

1274+
// @ts-expect-error this is available in macro v2
1275+
if(stackName === "resolve") {
1276+
type = {
1277+
...type,
1278+
subType: 'resolve'
1279+
}
1280+
}
1281+
12731282
if ('fn' in type || Array.isArray(type)) {
12741283
if (!localHook[stackName]) localHook[stackName] = []
12751284
if (typeof localHook[stackName] === 'function')

test/macro/macro.test.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ describe('Macro', () => {
410410
})
411411

412412
await app.handle(req('/'))
413-
414-
console.log(called)
415413
})
416414

417415
it('inherits macro in group', async () => {
@@ -551,8 +549,8 @@ describe('Macro', () => {
551549
app.handle(req('/?name=hoshino')).then((x) => x.json())
552550
])
553551

554-
expect(a).toEqual({ user: { name: 'anon' } })
555-
expect(b).toEqual({ user: { name: 'hoshino' } })
552+
expect(a).toEqual({ name: 'anon' })
553+
expect(b).toEqual({ name: 'hoshino' })
556554
})
557555

558556
it('accept async resolve', async () => {
@@ -575,8 +573,8 @@ describe('Macro', () => {
575573
app.handle(req('/?name=hoshino')).then((x) => x.json())
576574
])
577575

578-
expect(a).toEqual({ user: { name: 'anon' } })
579-
expect(b).toEqual({ user: { name: 'hoshino' } })
576+
expect(a).toEqual({ name: 'anon' })
577+
expect(b).toEqual({ name: 'hoshino' })
580578
})
581579

582580
it('guard handle resolve macro', async () => {
@@ -794,4 +792,37 @@ describe('Macro', () => {
794792
)
795793
).toEqual([true, true, true])
796794
})
795+
796+
// It may look duplicate to the test case above, but it occurs for some reason
797+
it('handle macro resolve', async () => {
798+
const app = new Elysia()
799+
.macro({
800+
user: (enabled: true) => ({
801+
resolve() {
802+
if (!enabled) return
803+
804+
return {
805+
user: 'a'
806+
}
807+
}
808+
})
809+
})
810+
.get(
811+
'/',
812+
({ user, error }) => {
813+
if (!user) return error(401)
814+
815+
return { hello: 'hanabi' }
816+
},
817+
{
818+
user: true
819+
}
820+
)
821+
822+
const response = await app.handle(req('/')).then((x) => x.json())
823+
824+
expect(response).toEqual({
825+
hello: 'hanabi'
826+
})
827+
})
797828
})

0 commit comments

Comments
 (0)