From e288ac298370a5e222671270f833b3dd1a87dc04 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Thu, 21 Apr 2022 03:32:20 +0900 Subject: [PATCH] refactor: shorten `hono.ts` (#162) --- examples/basic/index.js | 8 +-- examples/blog/src/index.test.ts | 2 +- examples/compute-at-edge/src/index.ts | 2 +- examples/jsx-ssr/src/index.tsx | 2 +- src/hono.test.ts | 1 + src/hono.ts | 95 +++++++++++---------------- 6 files changed, 45 insertions(+), 65 deletions(-) diff --git a/examples/basic/index.js b/examples/basic/index.js index f38b8302..4051f3cd 100644 --- a/examples/basic/index.js +++ b/examples/basic/index.js @@ -1,9 +1,9 @@ import { Hono } from '../../dist' -import { poweredBy } from '../../src/middleware/powered-by/powered-by' -import { logger } from '../../src/middleware/logger/logger' -import { bodyParse } from '../../src/middleware/body-parse/body-parse' -import { basicAuth } from '../../src/middleware/basic-auth/basic-auth' +import { basicAuth } from '../../src/middleware/basic-auth' +import { bodyParse } from '../../src/middleware/body-parse' import { etag } from '../../src/middleware/etag/etag' +import { logger } from '../../src/middleware/logger' +import { poweredBy } from '../../src/middleware/powered-by' const app = new Hono() diff --git a/examples/blog/src/index.test.ts b/examples/blog/src/index.test.ts index 9e64a51c..200f7e91 100644 --- a/examples/blog/src/index.test.ts +++ b/examples/blog/src/index.test.ts @@ -1,5 +1,5 @@ -import { app } from './index' import type { Post } from './model' +import { app } from './index' describe('Root', () => { it('GET /', async () => { diff --git a/examples/compute-at-edge/src/index.ts b/examples/compute-at-edge/src/index.ts index 65e3cd98..58249db7 100644 --- a/examples/compute-at-edge/src/index.ts +++ b/examples/compute-at-edge/src/index.ts @@ -1,6 +1,6 @@ +import { SHA256 } from 'crypto-js' import { Hono } from 'hono' import { basicAuth } from 'hono/basic-auth' -import { SHA256 } from 'crypto-js' const app = new Hono() diff --git a/examples/jsx-ssr/src/index.tsx b/examples/jsx-ssr/src/index.tsx index 37e7c6fe..be3acae0 100644 --- a/examples/jsx-ssr/src/index.tsx +++ b/examples/jsx-ssr/src/index.tsx @@ -1,8 +1,8 @@ import { Hono } from 'hono' import Nano from 'nano-jsx' -import { render } from './renderer' import { Page } from './pages/page' import { Top } from './pages/top' +import { render } from './renderer' const app = new Hono() diff --git a/src/hono.test.ts b/src/hono.test.ts index 9982af26..7fc284a9 100644 --- a/src/hono.test.ts +++ b/src/hono.test.ts @@ -1,3 +1,4 @@ +import type { Context } from '@/context' import { Hono } from '@/hono' describe('GET Request', () => { diff --git a/src/hono.ts b/src/hono.ts index eea940f5..e7dafa82 100644 --- a/src/hono.ts +++ b/src/hono.ts @@ -17,7 +17,7 @@ declare global { } export type Handler = ( - c: Context, + c: Context, next?: Next ) => Response | Promise export type MiddlewareHandler = (c: Context, next: Next) => Promise @@ -38,7 +38,32 @@ type ParamKeys = Path extends `${infer Component}/${infer Rest}` ? ParamKey | ParamKeys : ParamKey -export class Hono { +function defineDynamicClass( + ...methods: T +): { + new (): { + [K in T[number]]: (path: Path, handler: Handler>) => Hono + } & { + methods: T + } +} { + return class { + get methods() { + return methods + } + } as any +} + +export class Hono extends defineDynamicClass( + 'get', + 'post', + 'put', + 'delete', + 'head', + 'options', + 'patch', + 'all' +) { routerClass: { new (): Router } = TrieRouter strict: boolean = true // strict routing - default is true router: Router @@ -46,6 +71,14 @@ export class Hono { tempPath: string constructor(init: Partial> = {}) { + super() + + this.methods.map((method) => { + this[method] = (path: Path, handler: Handler) => { + return this.addRoute(method, path, handler) + } + }) + Object.assign(this, init) this.router = new this.routerClass() @@ -64,56 +97,6 @@ export class Hono { return c.text(message, 500) } - /* HTTP METHODS */ - get(path: Path, handler: Handler>): Hono - get(path: string, handler: Handler): Hono - get(path: string, handler: Handler): Hono { - return this.addRoute('get', path, handler) - } - - post(path: Path, handler: Handler>): Hono - post(path: string, handler: Handler): Hono - post(path: string, handler: Handler): Hono { - return this.addRoute('post', path, handler) - } - - put(path: Path, handler: Handler>): Hono - put(path: string, handler: Handler): Hono - put(path: string, handler: Handler): Hono { - return this.addRoute('put', path, handler) - } - - head(path: Path, handler: Handler>): Hono - head(path: string, handler: Handler): Hono - head(path: string, handler: Handler): Hono { - return this.addRoute('head', path, handler) - } - - delete(path: Path, handler: Handler>): Hono - delete(path: string, handler: Handler): Hono - delete(path: string, handler: Handler): Hono { - return this.addRoute('delete', path, handler) - } - - options(path: Path, handler: Handler>): Hono - options(path: string, handler: Handler): Hono - options(path: string, handler: Handler): Hono { - return this.addRoute('options', path, handler) - } - - patch(path: Path, handler: Handler>): Hono - patch(path: string, handler: Handler): Hono - patch(path: string, handler: Handler): Hono { - return this.addRoute('patch', path, handler) - } - - /* Any methods */ - all(path: Path, handler: Handler>): Hono - all(path: string, handler: Handler): Hono - all(path: string, handler: Handler): Hono { - return this.addRoute('all', path, handler) - } - route(path: string): Hono { const newHono: Hono = new Hono() newHono.tempPath = path @@ -162,9 +145,7 @@ export class Hono { // Methods for Request object request.param = (key: string): string => { - if (result) { - return result.params[key] - } + if (result) return result.params[key] } request.header = (name: string): string => { return request.headers.get(name) @@ -180,9 +161,7 @@ export class Hono { for (const mr of this.middlewareRouters) { const mwResult = mr.match(METHOD_NAME_OF_ALL, path) - if (mwResult) { - middleware.push(mwResult.handler) - } + if (mwResult) middleware.push(mwResult.handler) } const wrappedHandler = async (context: Context, next: Next) => {