refactor: request param (#2088)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Aris Kemper · committed by GitHub · parent 8c62c64 · Browse files · 2 files·+46 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+46 added−44 removed
- other:+46 added−44 removed
Provenance
- Commit
b0cac62- Tree
8736eec- Author
Aris Kemper <aris.github@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
8c62c64
2 files changed+46 added−44 removed
modified:deno_dist/request.ts+23 added-22 removed
49 unmodified lines50 key: RemoveQuestion<ParamKeys<P2>>51 ): UndefinedIfHavingQuestion<ParamKeys<P2>>52 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>53 param(key?: string): unknown {54- if (key) {55- const param = (56- this.#matchResult[1]57- ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any]58- : this.#matchResult[0][this.routeIndex][1][key]59- ) as string | undefined60- return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined61- } else {62- const decoded: Record<string, string> = {}54+ return key ? this.getDecodedParam(key) : this.getAllDecodedParams()55+ }56 64- const keys = Object.keys(this.#matchResult[0][this.routeIndex][1])65- for (let i = 0, len = keys.length; i < len; i++) {66- const key = keys[i]67- const value = (68- this.#matchResult[1]69- ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any]70- : this.#matchResult[0][this.routeIndex][1][key]71- ) as string | undefined72- if (value && typeof value === 'string') {73- decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value74- }75- }57+ private getDecodedParam(key: string): string | undefined {58+ const paramKey = this.#matchResult[0][this.routeIndex][1][key]59+ const param = this.getParamValue(paramKey)60 77- return decoded61+ return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined62+ }63+64+ private getAllDecodedParams(): Record<string, string> {65+ const decoded: Record<string, string> = {}66+67+ const keys = Object.keys(this.#matchResult[0][this.routeIndex][1])68+ for (const key of keys) {69+ const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key])70+ if (value && typeof value === 'string') {71+ decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value72+ }73 }74+75+ return decoded76 }77 78+ private getParamValue(paramKey: any): string | undefined {79+ return this.#matchResult[1] ? this.#matchResult[1][paramKey as any] : paramKey80+ }81+82 query(key: string): string | undefined83 query(): Record<string, string>84 query(key?: string) {85 return getQueryParam(this.url, key)178 unmodified linesmodified:src/request.ts+23 added-22 removed
49 unmodified lines50 key: RemoveQuestion<ParamKeys<P2>>51 ): UndefinedIfHavingQuestion<ParamKeys<P2>>52 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>53 param(key?: string): unknown {54- if (key) {55- const param = (56- this.#matchResult[1]57- ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any]58- : this.#matchResult[0][this.routeIndex][1][key]59- ) as string | undefined60- return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined61- } else {62- const decoded: Record<string, string> = {}54+ return key ? this.getDecodedParam(key) : this.getAllDecodedParams()55+ }56 64- const keys = Object.keys(this.#matchResult[0][this.routeIndex][1])65- for (let i = 0, len = keys.length; i < len; i++) {66- const key = keys[i]67- const value = (68- this.#matchResult[1]69- ? this.#matchResult[1][this.#matchResult[0][this.routeIndex][1][key] as any]70- : this.#matchResult[0][this.routeIndex][1][key]71- ) as string | undefined72- if (value && typeof value === 'string') {73- decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value74- }75- }57+ private getDecodedParam(key: string): string | undefined {58+ const paramKey = this.#matchResult[0][this.routeIndex][1][key]59+ const param = this.getParamValue(paramKey)60 77- return decoded61+ return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined62+ }63+64+ private getAllDecodedParams(): Record<string, string> {65+ const decoded: Record<string, string> = {}66+67+ const keys = Object.keys(this.#matchResult[0][this.routeIndex][1])68+ for (const key of keys) {69+ const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key])70+ if (value && typeof value === 'string') {71+ decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value72+ }73 }74+75+ return decoded76 }77 78+ private getParamValue(paramKey: any): string | undefined {79+ return this.#matchResult[1] ? this.#matchResult[1][paramKey as any] : paramKey80+ }81+82 query(key: string): string | undefined83 query(): Record<string, string>84 query(key?: string) {85 return getQueryParam(this.url, key)178 unmodified lines