Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support for module promise in Elysia#use #546

Merged
merged 2 commits into from Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/index.ts
Expand Up @@ -2496,7 +2496,7 @@ export default class Elysia<
*/
use(
plugin:
| Elysia<any, any, any, any, any, any, any, any>
| MaybePromise<Elysia<any, any, any, any, any, any, any, any>>
| Elysia<any, any, any, any, any, any, any, any>[]
| MaybePromise<
(
Expand Down 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 (plugin instanceof Elysia) {
return this._use(plugin)
}

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

return this._use(plugin as any)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was that plugin.default was not being referenced here, i.e. plugin.default || plugin. But also, I decided to clean up this area of the code, removing unnecessary type casts and adding instanceof checks for proper type narrowing.

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

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