diff --git a/framework/framework/server.ts b/framework/framework/server.ts index 1899bc7b4..0745bb3f4 100644 --- a/framework/framework/server.ts +++ b/framework/framework/server.ts @@ -752,8 +752,9 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`); this.captureAllRoutes[prefix] = cb; } - private _applyMixin(Target: any, MixinClass: Partial) { + private _applyMixin(Target: any, MixinClass: Partial | ((t: Partial) => Partial)) { if (!('prototype' in Target)) throw new Error('Target must be a class.'); + if (typeof MixinClass === 'function') MixinClass = MixinClass(Target.prototype); this.ctx.effect(() => { let oldValue = null; for (const val of Object.getOwnPropertyNames(MixinClass)) { @@ -782,15 +783,15 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`); }); } - public handlerMixin(MixinClass: Partial>) { + public handlerMixin(MixinClass: Partial> | ((s: HandlerCommon) => Partial>)) { return this._applyMixin(HandlerCommon, MixinClass); } - public httpHandlerMixin(MixinClass: Partial>) { + public httpHandlerMixin(MixinClass: Partial> | ((s: Handler) => Partial>)) { return this._applyMixin(Handler, MixinClass); } - public wsHandlerMixin(MixinClass: Partial>) { + public wsHandlerMixin(MixinClass: Partial> | ((s: ConnectionHandler) => Partial>)) { return this._applyMixin(ConnectionHandler, MixinClass); } diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index 118025f3d..416fdabd1 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -222,15 +222,15 @@ export async function apply(ctx: Context) { } }, }); - server.wsHandlerMixin({ + server.wsHandlerMixin((s) => ({ async onerror(err: HydroError) { if (![NotFoundError, PrivilegeError, PermissionError].some((i) => err instanceof i) || this.user?._id !== 0) { logger.error(`Path:${this.request.path}, User:${this.user?._id}(${this.user?.uname})`); logger.error(err); } - await super.onerror(err); + await s.onerror(err); }, - }); + })); on('handler/create', async (h) => { h.user = h.context.HydroContext.user as any; diff --git a/packages/hydrooj/src/settings.ts b/packages/hydrooj/src/settings.ts index 2f7194178..1435c3baa 100644 --- a/packages/hydrooj/src/settings.ts +++ b/packages/hydrooj/src/settings.ts @@ -74,7 +74,7 @@ export class ConfigService extends Service { await this.saveConfig(newConfig); } - requestConfig(s: Schema, dynamic = true): ReturnType> { + requestConfig(s: Schema, dynamic = true): S { if (dynamic) { this.ctx.effect(() => { this.settings.push(s);