From 6b3241bf5888d292ccc534953314899b2d4c9cd4 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 17 Mar 2024 14:24:36 -0400 Subject: [PATCH 1/2] chore(types): fix `Elysia#use` internal param type --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index dfd29c1e..6f5cfd3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2496,7 +2496,7 @@ export default class Elysia< */ use( plugin: - | Elysia + | MaybePromise> | Elysia[] | MaybePromise< ( From 6a5ae4b204c6e7d8654c6018c08c21edf77d46a8 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 17 Mar 2024 14:30:47 -0400 Subject: [PATCH 2/2] fix: support for module promise in `Elysia#use` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/index.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6f5cfd3b..e5df893a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(