Skip to content

Commit

Permalink
fix: support for module promise in Elysia#use
Browse files Browse the repository at this point in the history
The following use case was broken: Calling `app.use(import("./routes"))` where the "./routes" module has a default export that is an Elysia instance.

I‘m doing `instanceof Elysia` checks on the promise result and the module‘s "default" export, so a more informative error can be thrown, where before the app would crash with some obscure TypeError with a message of "Right side of assignment cannot be destructured" within the `Elysia#_use` method.
  • Loading branch information
aleclarson committed Mar 17, 2024
1 parent 6b3241b commit 6a5ae4b
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2534,25 +2534,31 @@ export default class Elysia<
plugin
.then((plugin) => {
if (typeof plugin === 'function') {
return plugin(
this as unknown as any
) as unknown as Elysia
return plugin(this)
}

if (typeof plugin.default === 'function')
return plugin.default(
this as unknown as any
) as unknown as Elysia
if (plugin instanceof Elysia) {
return this._use(plugin)
}

if (typeof plugin.default === 'function') {
return plugin.default(this)
}

if (plugin.default instanceof Elysia) {
return this._use(plugin.default)
}

return this._use(plugin as any)
throw new Error(
'Invalid plugin type. Expected Elysia instance, function, or module with "default" as Elysia instance or function that returns Elysia instance.'
)
})
.then((x) => x.compile())
)
return this
}

return this as unknown as any
} else return this._use(plugin)

return this
return this._use(plugin)
}

private _use(
Expand Down

0 comments on commit 6a5ae4b

Please sign in to comment.