From f7d4b98f555286c712666aba47798cd05e5b2deb Mon Sep 17 00:00:00 2001 From: Horu <73709188+HigherOrderLogic@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:39:59 +0700 Subject: [PATCH 1/6] feat: decode param --- src/utils/request.ts | 13 +++++++++++-- test/router.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index f72df198..3c9d3de2 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -29,16 +29,25 @@ export function getValidatedQuery< export function getRouterParams( event: H3Event, + opts: { decode?: boolean } = {}, ): NonNullable { // Fallback object needs to be returned in case router is not used (#149) - return event.context.params || {}; + const params = event.context.params || {}; + if (opts.decode === true) { + for (const key in params) { + params[key] = decodeURIComponent(params[key]); + } + } + + return params; } export function getRouterParam( event: H3Event, name: string, + opts: { decode?: boolean } = {}, ): string | undefined { - const params = getRouterParams(event); + const params = getRouterParams(event, opts); return params[name]; } diff --git a/test/router.test.ts b/test/router.test.ts index 65063555..f748b90e 100644 --- a/test/router.test.ts +++ b/test/router.test.ts @@ -189,6 +189,22 @@ describe("getRouterParams", () => { expect(result.text).toBe("200"); }); + + it("can decode router params", async () => { + const router = createRouter().get( + "/test/params/:name", + eventHandler((event) => { + expect(getRouterParams(event, { decode: true })).toMatchObject({ + name: "string with space", + }); + return "200"; + }), + ); + app.use(router); + const result = await request.get("/test/params/string with space"); + + expect(result.text).toBe("200"); + }); }); describe("without router", () => { @@ -230,6 +246,22 @@ describe("getRouterParam", () => { expect(result.text).toBe("200"); }); + + it("can decode a value of router params corresponding to the given name", async () => { + const router = createRouter().get( + "/test/params/:name", + eventHandler((event) => { + expect(getRouterParam(event, "name", { decode: true })).toEqual( + "string with space", + ); + return "200"; + }), + ); + app.use(router); + const result = await request.get("/test/params/string with space"); + + expect(result.text).toBe("200"); + }); }); describe("without router", () => { From cb96a2e2805d03a0d92f7a11d3bb677eb22bd39b Mon Sep 17 00:00:00 2001 From: Horu <73709188+HigherOrderLogic@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:45:04 +0700 Subject: [PATCH 2/6] docs: new opt --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e6c0977..0f52627d 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,8 @@ H3 has a concept of composable utilities that accept `event` (from `eventHandler - `getQuery(event)` - `getValidatedQuery(event, validate)` -- `getRouterParams(event)` +- `getRouterParams(event, opts)` +- `getRouterParams(event, name, opts)` - `getMethod(event, default?)` - `isMethod(event, expected, allowHead?)` - `assertMethod(event, expected, allowHead?)` From 65d71cf75cc5d114cd83daa5dce86584659294fe Mon Sep 17 00:00:00 2001 From: Horu <73709188+HigherOrderLogic@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:45:43 +0700 Subject: [PATCH 3/6] chore: best commit ever permalink: http://whatthecommit.com/fd1e76a61e8173b57334cb93c79300f3 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f52627d..e53e8646 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ H3 has a concept of composable utilities that accept `event` (from `eventHandler - `getQuery(event)` - `getValidatedQuery(event, validate)` - `getRouterParams(event, opts)` -- `getRouterParams(event, name, opts)` +- `getRouterParam(event, name, opts)` - `getMethod(event, default?)` - `isMethod(event, expected, allowHead?)` - `assertMethod(event, expected, allowHead?)` From 52e5f485d448f8649da890046bc34ae271577920 Mon Sep 17 00:00:00 2001 From: Horu <73709188+HigherOrderLogic@users.noreply.github.com> Date: Mon, 16 Oct 2023 23:07:26 +0700 Subject: [PATCH 4/6] feat: use func from `ufo` --- src/utils/request.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index 3c9d3de2..13774bd0 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,4 +1,4 @@ -import { getQuery as _getQuery } from "ufo"; +import { getQuery as _getQuery, decode as decodeURI } from "ufo"; import { createError } from "../error"; import type { HTTPHeaderName, @@ -35,7 +35,7 @@ export function getRouterParams( const params = event.context.params || {}; if (opts.decode === true) { for (const key in params) { - params[key] = decodeURIComponent(params[key]); + params[key] = decodeURI(params[key]); } } From 43f9c264522709d6f9a77b53361dd298d3013e72 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 20 Nov 2023 03:38:43 +0100 Subject: [PATCH 5/6] support for getValidatedRouterParams --- README.md | 6 +++--- src/utils/request.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 20486920..784f23d8 100644 --- a/README.md +++ b/README.md @@ -214,9 +214,9 @@ H3 has a concept of composable utilities that accept `event` (from `eventHandler - `getQuery(event)` - `getValidatedQuery(event, validate)` -- `getRouterParams(event, opts)` -- `getRouterParam(event, name, opts)` -- `getValidatedRouterParams(event, validate)` +- `getRouterParams(event, { decode? })` +- `getRouterParam(event, name, { decode? })` +- `getValidatedRouterParams(event, validate, { decode? })` - `getMethod(event, default?)` - `isMethod(event, expected, allowHead?)` - `assertMethod(event, expected, allowHead?)` diff --git a/src/utils/request.ts b/src/utils/request.ts index 9649bd7d..359826df 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -46,8 +46,12 @@ export function getValidatedRouterParams< T, Event extends H3Event = H3Event, _T = InferEventInput<"routerParams", Event, T>, ->(event: Event, validate: ValidateFunction<_T>): Promise<_T> { - const routerParams = getRouterParams(event); +>( + event: Event, + validate: ValidateFunction<_T>, + opts: { decode?: boolean } = {}, +): Promise<_T> { + const routerParams = getRouterParams(event, opts); return validateData(routerParams, validate); } From 52a7d02948809f71262c6e2560e03d64000ecb0e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 20 Nov 2023 03:40:46 +0100 Subject: [PATCH 6/6] avoid muating original context --- src/utils/request.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/request.ts b/src/utils/request.ts index 359826df..f97c790b 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -32,8 +32,9 @@ export function getRouterParams( opts: { decode?: boolean } = {}, ): NonNullable { // Fallback object needs to be returned in case router is not used (#149) - const params = event.context.params || {}; - if (opts.decode === true) { + let params = event.context.params || {}; + if (opts.decode) { + params = { ...params }; for (const key in params) { params[key] = decodeURI(params[key]); }