mirror of
https://github.com/elysiajs/elysia
synced 2026-08-09 12:35:48 +00:00
🔧 fix: #1671 mount produce incorrect url
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# 1.4.22
|
||||
Bug fix:
|
||||
- [#1671](https://github.com/elysiajs/elysia/issues/1671) mount() produces incorrect URL path when Elysia instance has prefix option
|
||||
- ValueError with summary missing types
|
||||
- Elysia not using Bun.routes
|
||||
|
||||
|
||||
+16
-1
@@ -1,3 +1,18 @@
|
||||
import { Elysia } from '../src'
|
||||
|
||||
const app = new Elysia().get('/', () => 'a').listen(3000)
|
||||
const sdkApp = new Elysia({ prefix: '/sdk' }).mount(
|
||||
'/problems-domain',
|
||||
(request) => {
|
||||
console.log(request.url)
|
||||
|
||||
return Response.json({ path: new URL(request.url).pathname })
|
||||
}
|
||||
)
|
||||
|
||||
const app = new Elysia().use(sdkApp)
|
||||
|
||||
const response = await app
|
||||
.handle(new Request('http://localhost/sdk/problems-domain/problems'))
|
||||
.then((x) => x.text())
|
||||
|
||||
console.log(response)
|
||||
|
||||
@@ -33,7 +33,7 @@ const createContext = (
|
||||
const getQi =
|
||||
`const u=request.url,` +
|
||||
`s=u.indexOf('/',${standardHostname ? 11 : 7}),` +
|
||||
`qi=u.indexOf('?', s + 1)\n`
|
||||
`qi=u.indexOf('?',s+1)\n`
|
||||
|
||||
const needsQuery =
|
||||
inference.query ||
|
||||
|
||||
+5
-1
@@ -5613,8 +5613,12 @@ export default class Elysia<
|
||||
throw new Error('Invalid handler')
|
||||
})()
|
||||
|
||||
const length = path.length - (path.endsWith('*') ? 1 : 0)
|
||||
const fullPath =
|
||||
typeof path === 'string' && this.config.prefix
|
||||
? this.config.prefix + path
|
||||
: path
|
||||
|
||||
const length = fullPath.length - (path.endsWith('*') ? 1 : 0)
|
||||
const handler: Handler = ({ request, path }) =>
|
||||
handle(
|
||||
new Request(
|
||||
|
||||
+7
-3
@@ -28,9 +28,13 @@ import { ElysiaFile } from './universal/file'
|
||||
export const hasHeaderShorthand = 'toJSON' in new Headers()
|
||||
|
||||
export const replaceUrlPath = (url: string, pathname: string) => {
|
||||
const urlObject = new URL(url)
|
||||
urlObject.pathname = pathname
|
||||
return urlObject.toString()
|
||||
const pathStartIndex = url.indexOf('/', 11)
|
||||
const queryIndex = url.indexOf('?', pathStartIndex)
|
||||
|
||||
if (queryIndex === -1)
|
||||
return `${url.slice(0, pathStartIndex)}${pathname.charCodeAt(0) === 47 ? '' : '/'}${pathname}`
|
||||
|
||||
return `${url.slice(0, pathStartIndex)}${pathname.charCodeAt(0) === 47 ? '' : '/'}${pathname}${url.slice(queryIndex)}`
|
||||
}
|
||||
|
||||
export const isClass = (v: Object) =>
|
||||
|
||||
@@ -137,4 +137,37 @@ describe('Mount', () => {
|
||||
'x-test': 'test'
|
||||
})
|
||||
})
|
||||
|
||||
it('mount without prefix - strips mount path', async () => {
|
||||
const app = new Elysia().mount('/sdk/problems-domain', (request) => {
|
||||
return Response.json({ path: new URL(request.url).pathname })
|
||||
})
|
||||
|
||||
const response = await app
|
||||
.handle(
|
||||
new Request('http://localhost/sdk/problems-domain/problems')
|
||||
)
|
||||
.then((x) => x.json() as Promise<{ path: string }>)
|
||||
|
||||
expect(response.path).toBe('/problems')
|
||||
})
|
||||
|
||||
it('mount with prefix - should strip both prefix and mount path', async () => {
|
||||
const sdkApp = new Elysia({ prefix: '/sdk' }).mount(
|
||||
'/problems-domain',
|
||||
(request) => {
|
||||
return Response.json({ path: new URL(request.url).pathname })
|
||||
}
|
||||
)
|
||||
|
||||
const app = new Elysia().use(sdkApp)
|
||||
|
||||
const response = await app
|
||||
.handle(
|
||||
new Request('http://localhost/sdk/problems-domain/problems')
|
||||
)
|
||||
.then((x) => x.json() as Promise<{ path: string }>)
|
||||
|
||||
expect(response.path).toBe('/problems')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user