Skip to content

Commit

Permalink
Merge pull request #546 from aleclarson/fix/elysia-use
Browse files Browse the repository at this point in the history
fix: support for module promise in `Elysia#use`
  • Loading branch information
SaltyAom committed Mar 18, 2024
2 parents 2fac801 + 6a5ae4b commit 92f9afb
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/index.ts
Expand Up @@ -2504,7 +2504,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 @@ -2542,25 +2542,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)
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

0 comments on commit 92f9afb

Please sign in to comment.