Skip to content

Commit

Permalink
🔧 fix: mapResponse caused compilation error
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 24, 2024
1 parent bb3e780 commit 60cab72
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 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.6 - 25 Dec 2024
Bug fix:
- mapResponse with onError caused compilation error

# 1.2.5 - 25 Dec 2024
Bug fix:
- define universal/file in package export
Expand Down
10 changes: 7 additions & 3 deletions example/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import { Elysia, file, getSchemaValidator, t } from '../src'
import { post, req } from '../test/utils'

const app = new Elysia()
.get('/image', async () => {
return file('test/kyuukurarin.mp4')
.onError(() => {})
.mapResponse(() => {
if (Math.random() > 2) return new Response('error', { status: 500 })
})
.get('/', async () => {
return 'ok'
})
.listen(3000)

// console.log(app.routes[0].compile().toString())

// app.handle(req('/'))
app.handle(req('/')).then(x => x.text()).then(console.log)

// console.log(
// `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.2.5",
"version": "1.2.6",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
8 changes: 4 additions & 4 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ export const composeHandler = ({
`c.set.cookie['${name}'].value=await signCookie(_setCookie['${name}'].value,'${secret}')` +
'}'

encodeCookie += '}'
encodeCookie += '}\n'
}

const normalize = app.config.normalize
Expand Down Expand Up @@ -1706,7 +1706,7 @@ export const composeHandler = ({

fnLiteral +=
`if(mr===undefined){` +
`mr=${isAsyncName(mapResponse) ? 'await ' : ''}onMapResponse[${i}](c)` +
`mr=${isAsyncName(mapResponse) ? 'await ' : ''}onMapResponse[${i}](c)\n` +
`if(mr!==undefined)r=c.response=mr` +
`}`

Expand Down Expand Up @@ -1808,8 +1808,8 @@ export const composeHandler = ({

fnLiteral +=
`c.response=er\n` +
`er=${isAsyncName(mapResponse) ? 'await ' : ''}onMapResponse[${i}](c)\n` +
`if(er instanceof Promise)er=await er`
`er=onMapResponse[${i}](c)\n` +
`if(er instanceof Promise)er=await er\n`

endUnit()
}
Expand Down
11 changes: 11 additions & 0 deletions test/lifecycle/map-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,15 @@ describe('Map Response', () => {

expect(response).toBe('aru')
})

it('mapResponse with onError', async () => {
const app = new Elysia()
.onError(() => {})
.mapResponse(() => {})
.get('/', () => 'ok')

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

expect(response).toBe('ok')
})
})

0 comments on commit 60cab72

Please sign in to comment.