mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 10:51:01 +00:00
refactor: shorten hono.ts
(#162)
This commit is contained in:
parent
d4eb8071db
commit
e288ac2983
@ -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()
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { app } from './index'
|
||||
import type { Post } from './model'
|
||||
import { app } from './index'
|
||||
|
||||
describe('Root', () => {
|
||||
it('GET /', async () => {
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import type { Context } from '@/context'
|
||||
import { Hono } from '@/hono'
|
||||
|
||||
describe('GET Request', () => {
|
||||
|
95
src/hono.ts
95
src/hono.ts
@ -17,7 +17,7 @@ declare global {
|
||||
}
|
||||
|
||||
export type Handler<RequestParamKeyType = string> = (
|
||||
c: Context<RequestParamKeyType>,
|
||||
c: Context<RequestParamKeyType | string>,
|
||||
next?: Next
|
||||
) => Response | Promise<Response>
|
||||
export type MiddlewareHandler = (c: Context, next: Next) => Promise<void>
|
||||
@ -38,7 +38,32 @@ type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}`
|
||||
? ParamKey<Component> | ParamKeys<Rest>
|
||||
: ParamKey<Path>
|
||||
|
||||
export class Hono {
|
||||
function defineDynamicClass<T extends string[]>(
|
||||
...methods: T
|
||||
): {
|
||||
new (): {
|
||||
[K in T[number]]: <Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>) => 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<any> } = TrieRouter
|
||||
strict: boolean = true // strict routing - default is true
|
||||
router: Router<Handler>
|
||||
@ -46,6 +71,14 @@ export class Hono {
|
||||
tempPath: string
|
||||
|
||||
constructor(init: Partial<Pick<Hono, 'routerClass' | 'strict'>> = {}) {
|
||||
super()
|
||||
|
||||
this.methods.map((method) => {
|
||||
this[method] = <Path extends string>(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 extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
get(path: string, handler: Handler<string>): Hono
|
||||
get(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('get', path, handler)
|
||||
}
|
||||
|
||||
post<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
post(path: string, handler: Handler<string>): Hono
|
||||
post(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('post', path, handler)
|
||||
}
|
||||
|
||||
put<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
put(path: string, handler: Handler<string>): Hono
|
||||
put(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('put', path, handler)
|
||||
}
|
||||
|
||||
head<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
head(path: string, handler: Handler<string>): Hono
|
||||
head(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('head', path, handler)
|
||||
}
|
||||
|
||||
delete<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
delete(path: string, handler: Handler<string>): Hono
|
||||
delete(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('delete', path, handler)
|
||||
}
|
||||
|
||||
options<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
options(path: string, handler: Handler<string>): Hono
|
||||
options(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('options', path, handler)
|
||||
}
|
||||
|
||||
patch<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
patch(path: string, handler: Handler<string>): Hono
|
||||
patch(path: string, handler: Handler): Hono {
|
||||
return this.addRoute('patch', path, handler)
|
||||
}
|
||||
|
||||
/* Any methods */
|
||||
all<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono
|
||||
all(path: string, handler: Handler<string>): 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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user