Skip to content

Commit daaea8b

Browse files
committed
🔧 fix: handle group with empty prefix
1 parent 620d96b commit daaea8b

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Bug fix:
99
- add set immediate fallback
1010
- [#1596](https://github.com/elysiajs/elysia/issues/1596) handle context pass to function with sub context
1111
- [#1597](https://github.com/elysiajs/elysia/issues/1597) safeParse, parse to static type
12+
- handle group with empty prefix
1213

1314
# 1.4.18 - 4 Dec 2025
1415
Security:

example/a.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { t, getSchemaValidator } from '../src'
2-
import { sucrose } from '../src/sucrose'
1+
import { Elysia } from '../src'
32

4-
const v = sucrose({
5-
handler: (context) => {
6-
console.log('path >>> ', context.path)
7-
console.log(context)
3+
const app = new Elysia()
4+
.group('', (app) => {
5+
return app.get('/ok', () => 'Hello World')
6+
})
7+
.listen(3000)
88

9-
return { id: 'test' }
10-
}
11-
})
12-
13-
console.log(v)
9+
type Routes = keyof typeof app['~Routes']

src/index.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
getCookieValidator,
4949
ElysiaTypeCheck,
5050
hasType,
51-
resolveSchema,
51+
resolveSchema
5252
} from './schema'
5353
import {
5454
composeHandler,
@@ -164,7 +164,12 @@ import type {
164164
InlineHandlerNonMacro,
165165
Router
166166
} from './types'
167-
import { coercePrimitiveRoot, coerceFormData, queryCoercions, stringToStructureCoercions } from './replace-schema'
167+
import {
168+
coercePrimitiveRoot,
169+
coerceFormData,
170+
queryCoercions,
171+
stringToStructureCoercions
172+
} from './replace-schema'
168173

169174
export type AnyElysia = Elysia<any, any, any, any, any, any, any>
170175

@@ -589,9 +594,16 @@ export default class Elysia<
589594
models,
590595
normalize,
591596
additionalCoerce: (() => {
592-
const resolved = resolveSchema(cloned.body, models, modules)
597+
const resolved = resolveSchema(
598+
cloned.body,
599+
models,
600+
modules
601+
)
593602
// Only check for Files if resolved schema is a TypeBox schema (has Kind symbol)
594-
return (resolved && Kind in resolved && (hasType('File', resolved) || hasType('Files', resolved)))
603+
return resolved &&
604+
Kind in resolved &&
605+
(hasType('File', resolved) ||
606+
hasType('Files', resolved))
595607
? coerceFormData()
596608
: coercePrimitiveRoot()
597609
})(),
@@ -657,9 +669,16 @@ export default class Elysia<
657669
models,
658670
normalize,
659671
additionalCoerce: (() => {
660-
const resolved = resolveSchema(cloned.body, models, modules)
672+
const resolved = resolveSchema(
673+
cloned.body,
674+
models,
675+
modules
676+
)
661677
// Only check for Files if resolved schema is a TypeBox schema (has Kind symbol)
662-
return (resolved && Kind in resolved && (hasType('File', resolved) || hasType('Files', resolved)))
678+
return resolved &&
679+
Kind in resolved &&
680+
(hasType('File', resolved) ||
681+
hasType('Files', resolved))
663682
? coerceFormData()
664683
: coercePrimitiveRoot()
665684
})(),
@@ -820,7 +839,6 @@ export default class Elysia<
820839
route: path
821840
})
822841

823-
824842
const encoded = encodePath(loosePath)
825843
if (loosePath !== encoded)
826844
this.router.dynamic.add(method, loosePath, {
@@ -3823,7 +3841,7 @@ export default class Elysia<
38233841
prefix: Prefix,
38243842
run: (
38253843
group: Elysia<
3826-
JoinPath<BasePath, Prefix>,
3844+
Prefix extends '' ? BasePath : JoinPath<BasePath, Prefix>,
38273845
Singleton,
38283846
Definitions,
38293847
{
@@ -8154,13 +8172,8 @@ export {
81548172
type TraceStream
81558173
} from './trace'
81568174

8157-
export {
8158-
getSchemaValidator,
8159-
getResponseSchemaValidator,
8160-
} from './schema'
8161-
export {
8162-
replaceSchemaTypeFromManyOptions as replaceSchemaType
8163-
} from './replace-schema'
8175+
export { getSchemaValidator, getResponseSchemaValidator } from './schema'
8176+
export { replaceSchemaTypeFromManyOptions as replaceSchemaType } from './replace-schema'
81648177

81658178
export {
81668179
mergeHook,

test/types/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,3 +2922,16 @@ type a = keyof {}
29222922
? true
29232923
: false = true
29242924
}
2925+
2926+
// group empty prefix
2927+
{
2928+
const app = new Elysia()
2929+
.group('', (app) => {
2930+
return app.get('/ok', () => 'Hello World')
2931+
})
2932+
.listen(3000)
2933+
2934+
type Routes = keyof (typeof app)['~Routes']
2935+
2936+
expectTypeOf<Routes>().toEqualTypeOf<'ok'>()
2937+
}

0 commit comments

Comments
 (0)