Skip to content

Commit

Permalink
🔧 fix: macro resolve subType
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 28, 2024
1 parent 958fde0 commit a9478c6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2.9 - 28 Dec 2024
Bug fix:
- Resolve macro unintentionally return instead of assign new context

# 1.2.8 - 27 Dec 2024
Bug fix:
- [#966](https://github.com/elysiajs/elysia/issues/966) generic error somehow return 200
Expand Down
22 changes: 17 additions & 5 deletions example/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ import { Elysia, t, error, StatusMap } from '../src'
import { req } from '../test/utils'

const app = new Elysia()
.get('/', () => {
throw new Error("A")
.macro({
user: (enabled: boolean) => ({
resolve: ({ query: { name = 'anon' } }) => ({
user: {
name
}
})
})
})
.listen(3000)
.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())
])

// console.log(await res.text())
// console.log(res.status)
console.log(a, b)
11 changes: 10 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,10 +1236,11 @@ export const traceBackMacro = (
const hook = v(value)

if (typeof hook === 'object') {
for (const [k, v] of Object.entries(hook))
for (const [k, v] of Object.entries(hook)) {
manage(k as keyof LifeCycleStore)({
fn: v as any
})
}
}

delete property[key as unknown as keyof typeof extension]
Expand Down Expand Up @@ -1270,6 +1271,14 @@ export const createMacroManager =
fn: type
}

// @ts-expect-error this is available in macro v2
if(stackName === "resolve") {
type = {
...type,
subType: 'resolve'
}
}

if ('fn' in type || Array.isArray(type)) {
if (!localHook[stackName]) localHook[stackName] = []
if (typeof localHook[stackName] === 'function')
Expand Down
43 changes: 37 additions & 6 deletions test/macro/macro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,6 @@ describe('Macro', () => {
})

await app.handle(req('/'))

console.log(called)
})

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

expect(a).toEqual({ user: { name: 'anon' } })
expect(b).toEqual({ user: { name: 'hoshino' } })
expect(a).toEqual({ name: 'anon' })
expect(b).toEqual({ name: 'hoshino' })
})

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

expect(a).toEqual({ user: { name: 'anon' } })
expect(b).toEqual({ user: { name: 'hoshino' } })
expect(a).toEqual({ name: 'anon' })
expect(b).toEqual({ name: 'hoshino' })
})

it('guard handle resolve macro', async () => {
Expand Down Expand Up @@ -794,4 +792,37 @@ describe('Macro', () => {
)
).toEqual([true, true, true])
})

// It may look duplicate to the test case above, but it occurs for some reason
it('handle macro resolve', async () => {
const app = new Elysia()
.macro({
user: (enabled: true) => ({
resolve() {
if (!enabled) return

return {
user: 'a'
}
}
})
})
.get(
'/',
({ user, error }) => {
if (!user) return error(401)

return { hello: 'hanabi' }
},
{
user: true
}
)

const response = await app.handle(req('/')).then((x) => x.json())

expect(response).toEqual({
hello: 'hanabi'
})
})
})

0 comments on commit a9478c6

Please sign in to comment.