Skip to content

Commit

Permalink
Generate url with nested query params
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Nov 1, 2021
1 parent 8e2969b commit d9582e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/route-recognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ function recognizeChar<THandler>(
}

export interface QueryParams {
[param: string]: string[] | string | null | undefined;
[param: string]: Record<string, any> | string[] | string | null | undefined;
}

export interface Result<THandler> {
Expand Down
16 changes: 15 additions & 1 deletion tests/recognizer-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,16 @@ QUnit.test(
}
);

QUnit.test("Deserialize query param nested object", (assert: Assert) => {
const handler = {};
const router = new RouteRecognizer<{}>();
router.add([{ path: "/foo/bar", handler }]);

const results = router.recognize("/foo/bar?filter=[user][name][$contains]=nick");
const p = results && results.queryParams;
assert.deepEqual(p, { filter: { user: { name: { $contains: 'scoot' } } } });
});

QUnit.test("Multiple `/` routes recognize", (assert: Assert) => {
const handler1 = { handler: 1 };
const handler2 = { handler: 2 };
Expand Down Expand Up @@ -1637,7 +1647,7 @@ QUnit.module("Route Generation", hooks => {
}
);

QUnit.test("Generation works with query params", (assert: Assert) => {
QUnit.only("Generation works with query params", (assert: Assert) => {
assert.equal(
router.generate("index", { queryParams: { filter: "date" } }),
"/?filter=date"
Expand Down Expand Up @@ -1721,6 +1731,10 @@ QUnit.module("Route Generation", hooks => {
router.generate("index", { queryParams: { filter: "date", sort: 0 } }),
"/?filter=date&sort=0"
);
assert.equal(
router.generate("index", { queryParams: { filter: { user: { name: { $contains: 'scoot' } } }, sort: 0 } }),
"/?filter=[user][name][$contains]=scoot"
);
});

QUnit.test("Generation works with array query params", (assert: Assert) => {
Expand Down

0 comments on commit d9582e0

Please sign in to comment.