Skip to content

Commit dac5d57

Browse files
shk0Abdullahyusukebeusualoma
authored
refactor(on-handler): use forEach for consistent handler iteration (#5326)
* refactor(on-handler): use forEach for consistent handler iteration * refactor(hono): optimize handler iteration and normalization --------- Co-authored-by: Yusuke Wada <[email protected]> Co-authored-by: Taku Amano <[email protected]>
1 parent ec648d6 commit dac5d57

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎src/hono-base.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ class Hono<
129129
const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE]
130130
allMethods.forEach((method) => {
131131
this[method] = (args1: string | H, ...args: H[]) => {
132+
const methodName = method.toUpperCase()
132133
if (typeof args1 === 'string') {
133134
this.#path = args1
134135
} else {
135-
this.#addRoute(method, this.#path, args1)
136+
this.#addRoute(methodName, this.#path, args1)
136137
}
137138
args.forEach((handler) => {
138-
this.#addRoute(method, this.#path, handler)
139+
this.#addRoute(methodName, this.#path, handler)
139140
})
140141
return this as any
141142
}
@@ -146,9 +147,10 @@ class Hono<
146147
for (const p of [path].flat()) {
147148
this.#path = p
148149
for (const m of [method].flat()) {
149-
handlers.map((handler) => {
150-
this.#addRoute(m.toUpperCase(), this.#path, handler)
151-
})
150+
const methodName = m.toUpperCase()
151+
for (const handler of handlers) {
152+
this.#addRoute(methodName, this.#path, handler)
153+
}
152154
}
153155
}
154156
return this as any
@@ -384,7 +386,6 @@ class Hono<
384386
}
385387

386388
#addRoute(method: string, path: string, handler: H, baseRoutePath?: string): void {
387-
method = method.toUpperCase()
388389
path = mergePath(this._basePath, path)
389390
const r: RouterRoute = {
390391
basePath:

0 commit comments

Comments
 (0)