Skip to content

Commit 9c68be6

Browse files
committed
🔧 fix: #1353 normalize encodeSchema with Transform
1 parent f0977c6 commit 9c68be6

File tree

5 files changed

+89
-23
lines changed

5 files changed

+89
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 1.3.16 - 22 Aug 2025
1+
# 1.3.17 - 23 Aug 2025
2+
Bug fix:
3+
- [#1353](https://github.com/elysiajs/elysia/issues/1353) normalize encodeSchema with Transform
4+
5+
# 1.3.16 - 23 Aug 2025
26
Improvement:
37
- `sse` now infer type
48
- `sse` now accepts `ReadableStream` to return stream as `text/event-stream`

example/a.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import { Elysia, sse, t } from '../src'
2-
import { streamResponse } from '../src/adapter/utils'
32
import { req } from '../test/utils'
43

5-
const app = new Elysia()
6-
.get('/', function () {
7-
return new ReadableStream({
8-
async start(controller) {
9-
controller.enqueue('a')
10-
await Bun.sleep(100)
11-
controller.enqueue('b')
12-
await Bun.sleep(100)
13-
controller.close()
14-
}
4+
const app = new Elysia().get(
5+
'/',
6+
() => ({
7+
hasMore: true,
8+
total: 1,
9+
offset: 0,
10+
totalPages: 1,
11+
currentPage: 1,
12+
items: [{ username: 'Bob', secret: 'shhh' }]
13+
}),
14+
{
15+
response: t.Object({
16+
hasMore: t.Boolean(),
17+
items: t.Array(
18+
t.Object({
19+
username: t.String()
20+
})
21+
),
22+
total: t
23+
.Transform(t.Number())
24+
.Decode((x) => x)
25+
.Encode((x) => x),
26+
offset: t.Number({ minimum: 0 }),
27+
totalPages: t.Number(),
28+
currentPage: t.Number({ minimum: 1 })
1529
})
16-
})
17-
.listen(3000)
30+
}
31+
)
32+
.listen(3000)
1833

19-
const response = await app.handle(req('/'))
20-
21-
for await (const a of streamResponse(response)) {
22-
console.log(a)
23-
}
34+
const data = await app.handle(req('/')).then((x) => x.text())

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.3.16",
4+
"version": "1.3.17",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/compose.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,14 @@ const composeValidationFactory = ({
262262
!appliedCleaner && normalize && !noValidate
263263

264264
// Encode call TypeCheck.Check internally
265-
if (encodeSchema && value.hasTransform)
265+
if (encodeSchema && value.hasTransform) {
266266
code +=
267267
`try{` +
268-
`${name}=validator.response[${status}].Encode(${name})\n` +
268+
`${name}=validator.response[${status}].Encode(${name})\n`
269+
270+
if (!appliedCleaner) code += clean({ ignoreTryCatch: true })
271+
272+
code +=
269273
`c.set.status=${status}` +
270274
`}catch{` +
271275
(applyErrorCleaner
@@ -277,7 +281,7 @@ const composeValidationFactory = ({
277281
`}`
278282
: `throw new ValidationError('response',validator.response[${status}],${name})`) +
279283
`}`
280-
else {
284+
} else {
281285
if (!appliedCleaner) code += clean()
282286

283287
if (!noValidate)

test/core/normalize.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,53 @@ describe('Normalize', () => {
508508
])
509509
})
510510

511+
it('normalize encodeSchema with Transform', async () => {
512+
const app = new Elysia().get(
513+
'/',
514+
() => ({
515+
hasMore: true,
516+
total: 1,
517+
offset: 0,
518+
totalPages: 1,
519+
currentPage: 1,
520+
items: [{ username: 'Bob', secret: 'shhh' }]
521+
}),
522+
{
523+
// I don't know why but it must be this exact shape
524+
response: t.Object({
525+
hasMore: t.Boolean(),
526+
items: t.Array(
527+
t.Object({
528+
username: t.String()
529+
})
530+
),
531+
total: t
532+
.Transform(t.Number())
533+
.Decode((x) => x)
534+
.Encode((x) => x),
535+
offset: t.Number({ minimum: 0 }),
536+
totalPages: t.Number(),
537+
currentPage: t.Number({ minimum: 1 })
538+
})
539+
}
540+
)
541+
542+
const data = await app.handle(req('/')).then((x) => x.json())
543+
544+
expect(data).toEqual({
545+
hasMore: true,
546+
items: [
547+
{
548+
username: 'Bob'
549+
}
550+
],
551+
total: 1,
552+
offset: 0,
553+
totalPages: 1,
554+
currentPage: 1
555+
})
556+
})
557+
511558
// it('normalize response with getter fields on class', async () => {
512559
// const app = new Elysia({
513560
// normalize: true

0 commit comments

Comments
 (0)