Skip to content

Commit b4448bc

Browse files
committed
🎉 feat: merge guard and hook responses status
1 parent 93d457b commit b4448bc

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.20 - 10 Oct 2024
2+
Bug fix:
3+
- merge guard and not specified hook responses status
4+
15
# 1.1.19 - 7 Oct 2024
26
Bug fix:
37
- unable to return `error` from derive/resolve

example/a.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
import { Elysia } from '../src'
1+
import { Elysia, t } from '../src'
22

3-
const optionalUserMiddleware = (app: Elysia) =>
4-
app.derive({ as: 'scoped' }, async () => {
5-
const user = { name: 'something' }
6-
7-
return {
8-
user
3+
const app = new Elysia()
4+
.guard({
5+
response: {
6+
400: t.String(),
7+
500: t.String()
98
}
109
})
11-
12-
export const isUserAuthenticated = (app: Elysia) =>
13-
app.derive(({ error }) => {
14-
if (true) return error('Unauthorized')
15-
16-
return {
17-
user: 'a'
18-
}
10+
.get('/', () => '', {
11+
response: t.String()
1912
})
2013

21-
export default optionalUserMiddleware
14+
console.log(app.routes[0].hooks)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.1.19",
4+
"version": "1.1.20",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ export const mergeResponse = (
165165

166166
if (isRecordNumber(a) && isRecordNumber(b))
167167
return { ...(a as RecordNumber), ...(b as RecordNumber) }
168+
else if (a && !isRecordNumber(a) && isRecordNumber(b))
169+
return { 200: a, ...(b as RecordNumber) }
168170

169171
return b ?? a
170172
}

test/path/guard.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,23 @@ describe('guard', () => {
401401
expect(called).toBe(3)
402402
expect(response).toEqual([422, 200])
403403
})
404+
405+
it('handle merge guard and hook responses status', () => {
406+
const app = new Elysia()
407+
.guard({
408+
response: {
409+
400: t.String(),
410+
500: t.String()
411+
}
412+
})
413+
.get('/', () => '', {
414+
response: t.String()
415+
})
416+
417+
expect(Object.keys(app.routes[0].hooks.response)).toEqual([
418+
'200',
419+
'400',
420+
'500'
421+
])
422+
})
404423
})

0 commit comments

Comments
 (0)