I am modifying an application, and i intend to remove /app from the url path. I want
http://localhost:3000/app/users
http://localhost:3000/app/admin/add-product
to become something like this
http://localhost:3000/users
http://localhost:3000/admin/add-product
here’s my code snippet
export const LOGIN_ENTRY = "login";
export const APP_ENTRY = "app";
const config: BasicConfig = [
{ name: LOGIN_ENTRY, container: containerKeys.LOGIN },
{
name: APP_ENTRY,
container: containerKeys.LAYOUT,
role: "user",
isPrivate: true,
children: [
{ name: "home", role: "user", container: containerKeys.HOME },
...
]
}
]
const getPaths = (config: BasicConfig, parentPath: string = ""): Paths =>
config.reduce((paths, configItem) => {
const currentPath = `${parentPath}/${configItem.name}`;
return {
...paths,
...(configItem.container ? { [configItem.container]: currentPath } : {}),
...{ [currentPath]: currentPath },
...(configItem.name === LOGIN_ENTRY ? { LOGIN_ENTRY: currentPath } : {}),
...(configItem.name === APP_ENTRY ? { APP_ENTRY: currentPath } : {}),
...(configItem.children ? getPaths(configItem.children, currentPath) : {})
};
}, {} as Paths);
export const fillPathWithParams = (
path: string,
params: { [key: string]: string }
) =>
Object.entries(params).reduce((acc, param) => {
return acc.replace(`:${param[0]}`, param[1]);
}, path);
export const findByPath = (
config: Config,
targetPath: string,
parentPath: string = ""
): Config | undefined => {
for (let i = 0; i < config.length; i++) {
const currentPath = `${parentPath}/${config[i].name}`;
if (`${parentPath}/${config[i].name}` === targetPath) {
return config[i].children;
}
if (config[i].children) {
const result = findByPath(config[i].children!, targetPath, currentPath);
if (result) {
return result;
}
}
}
return;
};
const extendBasicConfig = (
config: BasicConfig,
parentKeys: string[] = []
): Config => {
return config.map(item => {
return {
...item,
key: `/${[...parentKeys, item.name].join("/")}`,
path: item.container ? paths[item.container] : undefined,
children: item.children
? extendBasicConfig(item.children, [...parentKeys, item.name])
: undefined
};
});
};
export const paths = getPaths(config);
I have tried initialising APP_ENTRY = ''
It doesn’t work at all, the application does not work on any url, but if i update APP_ENTRY = 'somethingelse'
, my url path changes to http://localhost:3000/somethingelse/users
I have also tried to see if i can update it manually, but I am unable to.