From b2482b373fac4de9bf5f55ad06b13be64cb761c4 Mon Sep 17 00:00:00 2001 From: Alexis Georges Date: Tue, 19 Mar 2024 14:31:09 +0100 Subject: [PATCH] fix(stark-core): fix support for URL query parameters ISSUE CLOSED: #3766 --- .../routing/services/routing.service.spec.ts | 21 +++++++++++++++++++ .../routing/services/routing.service.ts | 15 ++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/stark-core/src/modules/routing/services/routing.service.spec.ts b/packages/stark-core/src/modules/routing/services/routing.service.spec.ts index 58dfd52c14..f38c0acc50 100644 --- a/packages/stark-core/src/modules/routing/services/routing.service.spec.ts +++ b/packages/stark-core/src/modules/routing/services/routing.service.spec.ts @@ -545,6 +545,27 @@ describe("Service: StarkRoutingService", () => { expect(stateDeclarations.state).toBe(statesConfig[1]); expect(stateDeclarations.paramValues["#"]).toBe("some-hash"); }); + + it("should return the state of the requested url with a query parameter", () => { + const statesConfig: StateDeclaration[] = $state.get(); + expect(statesConfig.length).toBe(numberOfMockStates); + const url: string = statesConfig[1].url + "?test=test1"; + + const stateDeclarations: StarkStateConfigWithParams = routingService.getStateConfigByUrlPath(url); + expect(stateDeclarations.state).toBe(statesConfig[1]); + expect(stateDeclarations.paramValues["test"]).toBe("test1"); + }); + + it("should return the state of the requested url with a query parameter and a hash", () => { + const statesConfig: StateDeclaration[] = $state.get(); + expect(statesConfig.length).toBe(numberOfMockStates); + const url: string = statesConfig[1].url + "?test=test1#some-hash"; + + const stateDeclarations: StarkStateConfigWithParams = routingService.getStateConfigByUrlPath(url); + expect(stateDeclarations.state).toBe(statesConfig[1]); + expect(stateDeclarations.paramValues["test"]).toBe("test1"); + expect(stateDeclarations.paramValues["#"]).toBe("some-hash"); + }); }); describe("getStateDeclarationByStateName", () => { diff --git a/packages/stark-core/src/modules/routing/services/routing.service.ts b/packages/stark-core/src/modules/routing/services/routing.service.ts index 7f61993c6f..eaf77359af 100644 --- a/packages/stark-core/src/modules/routing/services/routing.service.ts +++ b/packages/stark-core/src/modules/routing/services/routing.service.ts @@ -157,8 +157,14 @@ export class StarkRoutingServiceImpl implements StarkRoutingService { public getStateConfigByUrlPath(urlPath: string): StarkStateConfigWithParams | undefined { let targetRoute: StarkStateConfigWithParams | undefined; - // separate path from hash - const [, path = urlPath, hash]: string[] = urlPath.match(/(.*)#(.*)/) || []; + const [pathAndParams, hash] = urlPath.split("#"); + const [path, paramsString] = pathAndParams.split("?"); + const paramsObject = new URLSearchParams(paramsString); + const paramValues: RawParams = {}; + + for (const [key, value] of paramsObject.entries()) { + paramValues[key] = value; + } const matchedState: StateDeclaration[] = this.getStatesConfig().filter( (state: StateDeclaration) => (state.$$state)().url && (state.$$state)().url.exec(path, undefined, hash) @@ -167,7 +173,10 @@ export class StarkRoutingServiceImpl implements StarkRoutingService { if (matchedState.length) { targetRoute = { state: matchedState[0], - paramValues: (matchedState[0].$$state)().url.exec(path, undefined, hash) + paramValues: { + ...(matchedState[0].$$state)().url.exec(path, undefined, hash), + ...paramValues + } }; }