diff --git a/CHANGELOG.md b/CHANGELOG.md index ed3c274e..0f87355f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/example/a.ts b/example/a.ts index f139598e..4e6e06a4 100644 --- a/example/a.ts +++ b/example/a.ts @@ -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}` diff --git a/package.json b/package.json index a9715934..c73f9e90 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/compose.ts b/src/compose.ts index 7b2f5814..94ef8819 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -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 @@ -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` + `}` @@ -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() } diff --git a/test/lifecycle/map-response.test.ts b/test/lifecycle/map-response.test.ts index b13863b6..8445bcc4 100644 --- a/test/lifecycle/map-response.test.ts +++ b/test/lifecycle/map-response.test.ts @@ -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') + }) })