Skip to content

Commit

Permalink
fix(stark-core): fix support for URL query parameters
Browse files Browse the repository at this point in the history
ISSUE CLOSED: #3766
  • Loading branch information
SuperITMan committed Jun 5, 2024
1 parent a1fb423 commit b2482b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <string>statesConfig[1].url + "?test=test1";

const stateDeclarations: StarkStateConfigWithParams = <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 = <string>statesConfig[1].url + "?test=test1#some-hash";

const stateDeclarations: StarkStateConfigWithParams = <StarkStateConfigWithParams>routingService.getStateConfigByUrlPath(url);
expect(stateDeclarations.state).toBe(statesConfig[1]);
expect(stateDeclarations.paramValues["test"]).toBe("test1");
expect(stateDeclarations.paramValues["#"]).toBe("some-hash");
});
});

describe("getStateDeclarationByStateName", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (<Function>state.$$state)().url && (<Function>state.$$state)().url.exec(path, undefined, hash)
Expand All @@ -167,7 +173,10 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {
if (matchedState.length) {
targetRoute = {
state: matchedState[0],
paramValues: (<Function>matchedState[0].$$state)().url.exec(path, undefined, hash)
paramValues: {
...(<Function>matchedState[0].$$state)().url.exec(path, undefined, hash),
...paramValues
}
};
}

Expand Down

0 comments on commit b2482b3

Please sign in to comment.