Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ import type {
UnknownRouteSchema,
MaybeFunction,
InlineHandlerNonMacro,
Router,
Router
} from './types'
import {
coercePrimitiveRoot,
Expand Down Expand Up @@ -4703,7 +4703,12 @@ export default class Elysia<
} = hook

const hasStandaloneSchema =
body || headers || query || params || cookie || response
body ||
headers ||
query ||
params ||
cookie ||
response

const startIndex = processedUntil
processedUntil = instance.router.history.length
Expand Down Expand Up @@ -4731,11 +4736,13 @@ export default class Elysia<
: Array.isArray(localHook.error)
? [
...(localHook.error ?? []),
...(sandbox.event.error ?? [])
...(sandbox.event.error ??
[])
]
: [
localHook.error,
...(sandbox.event.error ?? [])
...(sandbox.event.error ??
[])
],
standaloneValidator: !hasStandaloneSchema
? localHook.standaloneValidator
Expand Down Expand Up @@ -5655,8 +5662,20 @@ export default class Elysia<
throw new Error('Invalid handler')
})()

const prefix = this.config.prefix
const prefixLength = prefix?.length ?? 0
const handler: Handler = ({ request, path }) =>
run(new Request(replaceUrlPath(request.url, path), request))
run(
new Request(
replaceUrlPath(
request.url,
prefixLength
? path.slice(prefixLength) || '/'
: path
),
request
)
)

this.route('ALL', '/*', handler as any, {
parse: 'none',
Expand Down
30 changes: 30 additions & 0 deletions test/core/mount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,34 @@ describe('Mount', () => {
expect(response.status).toBe(302)
expect(response.headers.get('location')).toBe('/redirect')
})

// https://github.com/elysiajs/elysia/issues/1806
it('mount(handler) with prefix should strip prefix from path', async () => {
const plugin = new Elysia({ prefix: '/api' }).mount((request) => {
return Response.json({ path: new URL(request.url).pathname })
})

const app = new Elysia().use(plugin)

const response = await app
.handle(new Request('http://localhost/api/auth/dash/users'))
.then((x) => x.json() as Promise<{ path: string }>)

expect(response.path).toBe('/auth/dash/users')
})

// https://github.com/elysiajs/elysia/issues/1806
it('mount(handler) with prefix should handle root path', async () => {
const plugin = new Elysia({ prefix: '/api' }).mount((request) => {
return Response.json({ path: new URL(request.url).pathname })
})

const app = new Elysia().use(plugin)

const response = await app
.handle(new Request('http://localhost/api/'))
.then((x) => x.json() as Promise<{ path: string }>)

expect(response.path).toBe('/')
})
})