Skip to content

Commit 60a7d58

Browse files
committed
fix: rollback util and add param
1 parent 1b61478 commit 60a7d58

1 file changed

Lines changed: 42 additions & 15 deletions

File tree

src/libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ import isDynamicRouteSuffix from './isDynamicRouteSuffix';
55
import splitPathAndQuery from './splitPathAndQuery';
66

77
/**
8-
* Merges two query strings into one. If both contain the same key, an error is thrown.
8+
* Merges two query strings into one. If both contain the same key,
9+
* the error is thrown.
10+
* @param baseQuery - The query string of the base path
11+
* @param suffixQuery - The query string of the suffix
12+
* @returns The merged query string or an empty string if both are empty
13+
*
14+
* @private - Internal helper. Do not export or use outside this file.
15+
*
16+
* @example
17+
* mergeQueryStrings('foo=bar', 'foo=baz') => '?foo=bar&baz=qux'
18+
* mergeQueryStrings('foo=bar', 'foo=baz') => throws an error
919
*/
1020
const mergeQueryStrings = (baseQuery = '', suffixQuery = ''): string => {
1121
if (!baseQuery && !suffixQuery) {
1222
return '';
1323
}
1424
const params = new URLSearchParams(baseQuery);
1525
const suffixParams = new URLSearchParams(suffixQuery);
16-
for (const [key, value] of suffixParams.entries()) {
26+
const suffixParamsEntries = suffixParams.entries();
27+
for (const [key, value] of suffixParamsEntries) {
1728
if (params.has(key)) {
1829
throw new Error(`[createDynamicRoute] Query param "${key}" exists in both base path and dynamic suffix. This is not allowed.`);
1930
}
@@ -23,7 +34,33 @@ const mergeQueryStrings = (baseQuery = '', suffixQuery = ''): string => {
2334
return result ? `?${result}` : '';
2435
};
2536

26-
/** Adds dynamic route name (with optional query params) to the current URL and returns it */
37+
/**
38+
* Combines a base path with a dynamic route suffix, merging their query parameters.
39+
*
40+
* @private - Internal helper. Do not export or use outside this file.
41+
*/
42+
const combinePathAndSuffix = (basePath: string, suffixWithQuery: string): Route => {
43+
const [normalizedBasePath, baseQuery] = splitPathAndQuery(basePath);
44+
const [suffixPath, suffixQuery] = splitPathAndQuery(suffixWithQuery);
45+
46+
if (!normalizedBasePath) {
47+
Log.warn('[createDynamicRoute.ts] Path is undefined or empty, returning suffix only', {basePath, suffixWithQuery});
48+
return suffixWithQuery as Route;
49+
}
50+
51+
const combinedPath = normalizedBasePath === '/' ? `/${suffixPath}` : `${normalizedBasePath}/${suffixPath}`;
52+
const mergedQuery = mergeQueryStrings(baseQuery, suffixQuery);
53+
54+
return `${combinedPath}${mergedQuery}` as Route;
55+
};
56+
57+
/** Adds dynamic route name (with optional query params) to the current URL and returns it
58+
*
59+
* @param dynamicRouteSuffixWithParams - The dynamic route suffix with optional query params
60+
* @param basePath - The base path to use for the dynamic route
61+
*
62+
* @returns The combined dynamic route path and query string
63+
*/
2764
const createDynamicRoute = (dynamicRouteSuffixWithParams: string, basePath?: string): Route => {
2865
const [suffixPath] = splitPathAndQuery(dynamicRouteSuffixWithParams);
2966

@@ -32,17 +69,7 @@ const createDynamicRoute = (dynamicRouteSuffixWithParams: string, basePath?: str
3269
}
3370

3471
const routePath = basePath ?? Navigation.getActiveRoute();
35-
const [normalizedBasePath, baseQuery] = splitPathAndQuery(routePath);
36-
const [normalizedSuffixPath, suffixQuery] = splitPathAndQuery(dynamicRouteSuffixWithParams);
37-
38-
if (!normalizedBasePath) {
39-
Log.warn('[createDynamicRoute.ts] Path is undefined or empty, returning suffix only', {basePath: routePath, suffixWithQuery: dynamicRouteSuffixWithParams});
40-
return dynamicRouteSuffixWithParams as Route;
41-
}
42-
43-
const combinedPath = normalizedBasePath === '/' ? `/${normalizedSuffixPath}` : `${normalizedBasePath}/${normalizedSuffixPath}`;
44-
const mergedQuery = mergeQueryStrings(baseQuery, suffixQuery);
45-
46-
return `${combinedPath}${mergedQuery}` as Route;
72+
return combinePathAndSuffix(routePath, dynamicRouteSuffixWithParams);
4773
};
74+
4875
export default createDynamicRoute;

0 commit comments

Comments
 (0)