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 dc82cb2787..d76802cb28 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 @@ -549,6 +549,16 @@ 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"); + }); }); 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 34dd43e555..cca7168828 100644 --- a/packages/stark-core/src/modules/routing/services/routing.service.ts +++ b/packages/stark-core/src/modules/routing/services/routing.service.ts @@ -154,17 +154,34 @@ 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(/(.*)#(.*)/) || []; + let path!: string; + let params: string | undefined; + let hash: string | undefined; + const paramValues: RawParams = {}; + + // tslint:disable-next-line:no-self-assignment no-non-null-assertion no-dead-store + [, path, params, hash, path = path, params = params, path = path, hash = hash, path = path] = urlPath.match( + /(.*)\?(.*)#(.*)|(.*)\?(.*)|(.*)#(.*)|(.*)/ + )!; + + if (typeof params === "string") { + params.split("&").forEach((param: string) => { + const keyValue = param.split("="); + paramValues[keyValue[0]] = keyValue[1]; + }); + } - const matchedState: StateDeclaration[] = this.getStatesConfig().filter((state: StateDeclaration) => { - return (state.$$state)().url && (state.$$state)().url.exec(path, undefined, hash); - }); + const matchedState: StateDeclaration[] = this.getStatesConfig().filter( + (state: StateDeclaration) => (state.$$state)().url && (state.$$state)().url.exec(path, undefined, hash) + ); 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 + } }; }