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
  • Loading branch information
SuperITMan committed Mar 19, 2024
1 parent c32c323 commit 164621c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <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");
});
});

describe("getStateDeclarationByStateName", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 hash: string | undefined;
const paramValues: RawParams = {};

// When there is no match, null is set to each variable.
// 3 different RegEx are required to extract the path, the params and the hash.
if (urlPath.includes("?")) {
let params: string;
[, path = urlPath, params, hash] = (urlPath.includes("#") ? urlPath.match(/(.*)\?(.*)#(.*)/) : urlPath.match(/(.*)\?(.*)/)) || [];
params.split("&").forEach((param: string) => {
const keyValue = param.split("=");
paramValues[keyValue[0]] = keyValue[1];
});
} else {
[, path = urlPath, hash] = urlPath.match(/(.*)#(.*)/) || [];
}

const matchedState: StateDeclaration[] = this.getStatesConfig().filter((state: StateDeclaration) => {
return (<Function>state.$$state)().url && (<Function>state.$$state)().url.exec(path, undefined, hash);
});

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
4 changes: 2 additions & 2 deletions packages/stark-core/src/modules/session/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ export function resolveTargetRoute(
* @param targetRoute - Returned value of `resolveTargetRoute` method
*/
export function resolveTargetState(targetRoute?: StarkStateConfigWithParams): Promise<string | undefined> {
return of(typeof targetRoute !== "undefined" ? targetRoute.state.name : undefined).toPromise();
return of(targetRoute?.state.name).toPromise();
}

/**
* Check if targetRoute is defined and returns the params of the state OR `undefined`.
* @param targetRoute - Returned value of `resolveTargetRoute` method
*/
export function resolveTargetStateParams(targetRoute?: StarkStateConfigWithParams): Promise<RawParams | undefined> {
return of(typeof targetRoute !== "undefined" ? targetRoute.paramValues : undefined).toPromise();
return of(targetRoute?.paramValues).toPromise();
}

/**
Expand Down

0 comments on commit 164621c

Please sign in to comment.