File tree Expand file tree Collapse file tree 6 files changed +63
-6
lines changed
Expand file tree Collapse file tree 6 files changed +63
-6
lines changed Original file line number Diff line number Diff line change 11# 1.4.22
22Bug fix:
3+ - [ #1671 ] ( https://github.com/elysiajs/elysia/issues/1671 ) mount() produces incorrect URL path when Elysia instance has prefix option
34- ValueError with summary missing types
45- Elysia not using Bun.routes
56
Original file line number Diff line number Diff line change 11import { Elysia } from '../src'
22
3- const app = new Elysia ( ) . get ( '/' , ( ) => 'a' ) . listen ( 3000 )
3+ const sdkApp = new Elysia ( { prefix : '/sdk' } ) . mount (
4+ '/problems-domain' ,
5+ ( request ) => {
6+ console . log ( request . url )
7+
8+ return Response . json ( { path : new URL ( request . url ) . pathname } )
9+ }
10+ )
11+
12+ const app = new Elysia ( ) . use ( sdkApp )
13+
14+ const response = await app
15+ . handle ( new Request ( 'http://localhost/sdk/problems-domain/problems' ) )
16+ . then ( ( x ) => x . text ( ) )
17+
18+ console . log ( response )
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ const createContext = (
3333 const getQi =
3434 `const u=request.url,` +
3535 `s=u.indexOf('/',${ standardHostname ? 11 : 7 } ),` +
36- `qi=u.indexOf('?', s + 1)\n`
36+ `qi=u.indexOf('?',s+ 1)\n`
3737
3838 const needsQuery =
3939 inference . query ||
Original file line number Diff line number Diff line change @@ -5613,8 +5613,12 @@ export default class Elysia<
56135613 throw new Error ( 'Invalid handler' )
56145614 } ) ( )
56155615
5616- const length = path . length - ( path . endsWith ( '*' ) ? 1 : 0 )
5616+ const fullPath =
5617+ typeof path === 'string' && this . config . prefix
5618+ ? this . config . prefix + path
5619+ : path
56175620
5621+ const length = fullPath . length - ( path . endsWith ( '*' ) ? 1 : 0 )
56185622 const handler : Handler = ( { request, path } ) =>
56195623 handle (
56205624 new Request (
Original file line number Diff line number Diff line change @@ -28,9 +28,13 @@ import { ElysiaFile } from './universal/file'
2828export const hasHeaderShorthand = 'toJSON' in new Headers ( )
2929
3030export const replaceUrlPath = ( url : string , pathname : string ) => {
31- const urlObject = new URL ( url )
32- urlObject . pathname = pathname
33- return urlObject . toString ( )
31+ const pathStartIndex = url . indexOf ( '/' , 11 )
32+ const queryIndex = url . indexOf ( '?' , pathStartIndex )
33+
34+ if ( queryIndex === - 1 )
35+ return `${ url . slice ( 0 , pathStartIndex ) } ${ pathname . charCodeAt ( 0 ) === 47 ? '' : '/' } ${ pathname } `
36+
37+ return `${ url . slice ( 0 , pathStartIndex ) } ${ pathname . charCodeAt ( 0 ) === 47 ? '' : '/' } ${ pathname } ${ url . slice ( queryIndex ) } `
3438}
3539
3640export const isClass = ( v : Object ) =>
Original file line number Diff line number Diff line change @@ -137,4 +137,37 @@ describe('Mount', () => {
137137 'x-test' : 'test'
138138 } )
139139 } )
140+
141+ it ( 'mount without prefix - strips mount path' , async ( ) => {
142+ const app = new Elysia ( ) . mount ( '/sdk/problems-domain' , ( request ) => {
143+ return Response . json ( { path : new URL ( request . url ) . pathname } )
144+ } )
145+
146+ const response = await app
147+ . handle (
148+ new Request ( 'http://localhost/sdk/problems-domain/problems' )
149+ )
150+ . then ( ( x ) => x . json ( ) as Promise < { path : string } > )
151+
152+ expect ( response . path ) . toBe ( '/problems' )
153+ } )
154+
155+ it ( 'mount with prefix - should strip both prefix and mount path' , async ( ) => {
156+ const sdkApp = new Elysia ( { prefix : '/sdk' } ) . mount (
157+ '/problems-domain' ,
158+ ( request ) => {
159+ return Response . json ( { path : new URL ( request . url ) . pathname } )
160+ }
161+ )
162+
163+ const app = new Elysia ( ) . use ( sdkApp )
164+
165+ const response = await app
166+ . handle (
167+ new Request ( 'http://localhost/sdk/problems-domain/problems' )
168+ )
169+ . then ( ( x ) => x . json ( ) as Promise < { path : string } > )
170+
171+ expect ( response . path ) . toBe ( '/problems' )
172+ } )
140173} )
You can’t perform that action at this time.
0 commit comments