Skip to content

Commit 1546425

Browse files
authored
Merge pull request #1805 from MarcelOlsen/fix/guard-dynamic-import
Fix dynamic imports inside `.guard()` not registering routes
2 parents 651cc64 + 703de2d commit 1546425

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/index.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4685,6 +4685,80 @@ export default class Elysia<
46854685
}
46864686
)
46874687

4688+
// Handle dynamic imports (Promises) used inside guard callback
4689+
if (instance.promisedModules.size > 0) {
4690+
let processedUntil = instance.router.history.length
4691+
4692+
for (const promise of instance.promisedModules.promises) {
4693+
this.promisedModules.add(
4694+
promise.then(() => {
4695+
const {
4696+
body,
4697+
headers,
4698+
query,
4699+
params,
4700+
cookie,
4701+
response,
4702+
...guardHook
4703+
} = hook
4704+
4705+
const hasStandaloneSchema =
4706+
body || headers || query || params || cookie || response
4707+
4708+
const startIndex = processedUntil
4709+
processedUntil = instance.router.history.length
4710+
4711+
for (
4712+
let i = startIndex;
4713+
i < instance.router.history.length;
4714+
i++
4715+
) {
4716+
const {
4717+
method,
4718+
path,
4719+
handler,
4720+
hooks: localHook
4721+
} = instance.router.history[i]
4722+
4723+
this.add(
4724+
method,
4725+
path,
4726+
handler,
4727+
mergeHook(guardHook as AnyLocalHook, {
4728+
...((localHook || {}) as AnyLocalHook),
4729+
error: !localHook.error
4730+
? sandbox.event.error
4731+
: Array.isArray(localHook.error)
4732+
? [
4733+
...(localHook.error ?? []),
4734+
...(sandbox.event.error ?? [])
4735+
]
4736+
: [
4737+
localHook.error,
4738+
...(sandbox.event.error ?? [])
4739+
],
4740+
standaloneValidator: !hasStandaloneSchema
4741+
? localHook.standaloneValidator
4742+
: [
4743+
...(localHook.standaloneValidator ??
4744+
[]),
4745+
{
4746+
body,
4747+
headers,
4748+
query,
4749+
params,
4750+
cookie,
4751+
response
4752+
}
4753+
]
4754+
})
4755+
)
4756+
}
4757+
})
4758+
)
4759+
}
4760+
}
4761+
46884762
return this as any
46894763
}
46904764

test/core/modules.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,55 @@ describe('Modules', () => {
248248
const text = await response.text()
249249
expect(text).toEqual('GET /plugin')
250250
})
251+
252+
it('register dynamic import routes inside guard', async () => {
253+
const app = new Elysia().guard(
254+
{},
255+
(app) => app.use(import('../modules').then((m) => m.lazyInstance))
256+
)
257+
258+
await app.modules
259+
260+
const res = await app.handle(req('/lazy-instance'))
261+
262+
expect(res.status).toBe(200)
263+
expect(await res.text()).toBe('lazy-instance')
264+
})
265+
266+
it('register multiple dynamic import routes inside guard', async () => {
267+
const lazyA = Promise.resolve(new Elysia().get('/a', () => 'a'))
268+
const lazyB = Promise.resolve(new Elysia().get('/b', () => 'b'))
269+
270+
let hookCalls = 0
271+
272+
const app = new Elysia().guard(
273+
{ beforeHandle: () => { hookCalls++ } },
274+
(app) => app.use(lazyA).use(lazyB)
275+
)
276+
277+
await app.modules
278+
279+
expect((await app.handle(req('/a'))).status).toBe(200)
280+
expect((await app.handle(req('/b'))).status).toBe(200)
281+
expect(hookCalls).toBe(2)
282+
})
283+
284+
it('register dynamic import routes inside guard with hook', async () => {
285+
let called = false
286+
287+
const app = new Elysia().guard(
288+
{
289+
beforeHandle: () => {
290+
called = true
291+
}
292+
},
293+
(app) => app.use(import('../modules').then((m) => m.lazyInstance))
294+
)
295+
296+
await app.modules
297+
298+
await app.handle(req('/lazy-instance'))
299+
300+
expect(called).toBe(true)
301+
})
251302
})

test/modules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ import { Elysia } from '../src'
22

33
export const lazy = async (app: Elysia) => app.get('/lazy', () => 'lazy')
44

5+
export const lazyInstance = new Elysia().get('/lazy-instance', () => 'lazy-instance')
6+
57
export default lazy

0 commit comments

Comments
 (0)