diff --git a/deno_dist/request.ts b/deno_dist/request.ts index 44802ddb3..12194e57a 100644 --- a/deno_dist/request.ts +++ b/deno_dist/request.ts @@ -51,31 +51,32 @@ export class HonoRequest

{ ): UndefinedIfHavingQuestion> param(): UnionToIntersection>> param(key?: string): unknown { - if (key) { - const param = ( - this.#matchResult[1] - ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any] - : this.#matchResult[0][this.routeIndex][1][key] - ) as string | undefined - return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined - } else { - const decoded: Record = {} - - const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]) - for (let i = 0, len = keys.length; i < len; i++) { - const key = keys[i] - const value = ( - this.#matchResult[1] - ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any] - : this.#matchResult[0][this.routeIndex][1][key] - ) as string | undefined - if (value && typeof value === 'string') { - decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value - } - } + return key ? this.getDecodedParam(key) : this.getAllDecodedParams() + } + + private getDecodedParam(key: string): string | undefined { + const paramKey = this.#matchResult[0][this.routeIndex][1][key] + const param = this.getParamValue(paramKey) + + return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined + } + + private getAllDecodedParams(): Record { + const decoded: Record = {} - return decoded + const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]) + for (const key of keys) { + const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key]) + if (value && typeof value === 'string') { + decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value + } } + + return decoded + } + + private getParamValue(paramKey: any): string | undefined { + return this.#matchResult[1] ? this.#matchResult[1][paramKey as any] : paramKey } query(key: string): string | undefined diff --git a/src/request.ts b/src/request.ts index 629b30940..3c799dbd0 100644 --- a/src/request.ts +++ b/src/request.ts @@ -51,31 +51,32 @@ export class HonoRequest

{ ): UndefinedIfHavingQuestion> param(): UnionToIntersection>> param(key?: string): unknown { - if (key) { - const param = ( - this.#matchResult[1] - ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any] - : this.#matchResult[0][this.routeIndex][1][key] - ) as string | undefined - return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined - } else { - const decoded: Record = {} - - const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]) - for (let i = 0, len = keys.length; i < len; i++) { - const key = keys[i] - const value = ( - this.#matchResult[1] - ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any] - : this.#matchResult[0][this.routeIndex][1][key] - ) as string | undefined - if (value && typeof value === 'string') { - decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value - } - } + return key ? this.getDecodedParam(key) : this.getAllDecodedParams() + } + + private getDecodedParam(key: string): string | undefined { + const paramKey = this.#matchResult[0][this.routeIndex][1][key] + const param = this.getParamValue(paramKey) + + return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined + } + + private getAllDecodedParams(): Record { + const decoded: Record = {} - return decoded + const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]) + for (const key of keys) { + const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key]) + if (value && typeof value === 'string') { + decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value + } } + + return decoded + } + + private getParamValue(paramKey: any): string | undefined { + return this.#matchResult[1] ? this.#matchResult[1][paramKey as any] : paramKey } query(key: string): string | undefined