From b9025907175d31879e5f3648661d60536af1a787 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 12 May 2024 17:54:43 +0900 Subject: [PATCH] don't use `dynamicClass` --- deno_dist/hono-base.ts | 40 +++++++++++++++++++--------------------- src/hono-base.ts | 40 +++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts index 5a69e8055..508b46234 100644 --- a/deno_dist/hono-base.ts +++ b/deno_dist/hono-base.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { compose } from './compose.ts' import { Context } from './context.ts' import type { ExecutionContext } from './context.ts' @@ -25,20 +26,6 @@ import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/ur export const COMPOSED_HANDLER = Symbol('composedHandler') -type Methods = (typeof METHODS)[number] | typeof METHOD_NAME_ALL_LOWERCASE - -function defineDynamicClass(): { - new (): { - [M in Methods]: HandlerInterface - } & { - on: OnHandlerInterface - } & { - use: MiddlewareHandlerInterface - } -} { - return class {} as never -} - const notFoundHandler = (c: Context) => { return c.text('404 Not Found', 404) } @@ -89,11 +76,28 @@ export type HonoOptions = { getPath?: GetPath } +abstract class SuperClass< + E extends Env = Env, + S extends Schema = {}, + BasePath extends string = '/' +> { + // The implementations of these methods are fake. + get: HandlerInterface = () => new Hono() + post: HandlerInterface = () => new Hono() + put: HandlerInterface = () => new Hono() + delete: HandlerInterface = () => new Hono() + options: HandlerInterface = () => new Hono() + patch: HandlerInterface = () => new Hono() + all: HandlerInterface = () => new Hono() + on: OnHandlerInterface = () => new Hono() + use: MiddlewareHandlerInterface = () => new Hono() +} + class Hono< E extends Env = Env, S extends Schema = {}, BasePath extends string = '/' -> extends defineDynamicClass() { +> extends SuperClass { /* This class is like an abstract class and does not have a router. To use it, inherit the class and implement router in the constructor. @@ -123,7 +127,6 @@ class Hono< this.addRoute(method, this.#path, handler) } }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } }) @@ -141,12 +144,10 @@ class Hono< }) } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } // Implementation of app.use(...handlers[]) or app.use(path, ...handlers[]) - // eslint-disable-next-line @typescript-eslint/no-explicit-any this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => { if (typeof arg1 === 'string') { this.#path = arg1 @@ -157,7 +158,6 @@ class Hono< handlers.forEach((handler) => { this.addRoute(METHOD_NAME_ALL, this.#path, handler) }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } @@ -201,7 +201,6 @@ class Hono< } else { handler = async (c: Context, next: Next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res - // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(handler as any)[COMPOSED_HANDLER] = r.handler } @@ -254,7 +253,6 @@ class Hono< mount( path: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any applicationHandler: (request: Request, ...args: any) => Response | Promise, optionHandler?: (c: Context) => unknown ): Hono { diff --git a/src/hono-base.ts b/src/hono-base.ts index 293d3874f..673fe58d8 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { compose } from './compose' import { Context } from './context' import type { ExecutionContext } from './context' @@ -25,20 +26,6 @@ import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/ur export const COMPOSED_HANDLER = Symbol('composedHandler') -type Methods = (typeof METHODS)[number] | typeof METHOD_NAME_ALL_LOWERCASE - -function defineDynamicClass(): { - new (): { - [M in Methods]: HandlerInterface - } & { - on: OnHandlerInterface - } & { - use: MiddlewareHandlerInterface - } -} { - return class {} as never -} - const notFoundHandler = (c: Context) => { return c.text('404 Not Found', 404) } @@ -89,11 +76,28 @@ export type HonoOptions = { getPath?: GetPath } +abstract class SuperClass< + E extends Env = Env, + S extends Schema = {}, + BasePath extends string = '/' +> { + // The implementations of these methods are fake. + get: HandlerInterface = () => new Hono() + post: HandlerInterface = () => new Hono() + put: HandlerInterface = () => new Hono() + delete: HandlerInterface = () => new Hono() + options: HandlerInterface = () => new Hono() + patch: HandlerInterface = () => new Hono() + all: HandlerInterface = () => new Hono() + on: OnHandlerInterface = () => new Hono() + use: MiddlewareHandlerInterface = () => new Hono() +} + class Hono< E extends Env = Env, S extends Schema = {}, BasePath extends string = '/' -> extends defineDynamicClass() { +> extends SuperClass { /* This class is like an abstract class and does not have a router. To use it, inherit the class and implement router in the constructor. @@ -123,7 +127,6 @@ class Hono< this.addRoute(method, this.#path, handler) } }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } }) @@ -141,12 +144,10 @@ class Hono< }) } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } // Implementation of app.use(...handlers[]) or app.use(path, ...handlers[]) - // eslint-disable-next-line @typescript-eslint/no-explicit-any this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => { if (typeof arg1 === 'string') { this.#path = arg1 @@ -157,7 +158,6 @@ class Hono< handlers.forEach((handler) => { this.addRoute(METHOD_NAME_ALL, this.#path, handler) }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any return this as any } @@ -201,7 +201,6 @@ class Hono< } else { handler = async (c: Context, next: Next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res - // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(handler as any)[COMPOSED_HANDLER] = r.handler } @@ -254,7 +253,6 @@ class Hono< mount( path: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any applicationHandler: (request: Request, ...args: any) => Response | Promise, optionHandler?: (c: Context) => unknown ): Hono {