Skip to content
Myr

refactor(on-handler): use forEach for consistent handler iteration (#5326)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor(on-handler): use forEach for consistent handler iteration * refactor(hono): optimize handler iteration and normalization ---------

authored by ABDULLAH AMJAD · committed by GitHub · parent ec648d6 · Browse files · 1 file+7 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

1 file+7 added−6 removed

  • other:+7 added−6 removed

1 file+7 added−6 removed

modified:src/hono-base.ts+7 added-6 removed
127 unmodified lines
128 // Implementation of app.get(...handlers[]) or app.get(path, ...handlers[])129 const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE]130 allMethods.forEach((method) => {131 this[method] = (args1: string | H, ...args: H[]) => {132 const methodName = method.toUpperCase()133 if (typeof args1 === 'string') {134 this.#path = args1135 } else {135 this.#addRoute(method, this.#path, args1)136 this.#addRoute(methodName, this.#path, args1)137 }138 args.forEach((handler) => {138 this.#addRoute(method, this.#path, handler)139 this.#addRoute(methodName, this.#path, handler)140 })141 return this as any142 }143 })
2 unmodified lines
146 this.on = (method: string | string[], path: string | string[], ...handlers: H[]) => {147 for (const p of [path].flat()) {148 this.#path = p149 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 }154 }155 }156 return this as any157 }
227 unmodified lines
385 return this386 }387388 #addRoute(method: string, path: string, handler: H, baseRoutePath?: string): void {387 method = method.toUpperCase()389 path = mergePath(this._basePath, path)390 const r: RouterRoute = {391 basePath:392 baseRoutePath !== undefined ? mergePath(this._basePath, baseRoutePath) : this._basePath,
155 unmodified lines