Skip to content

Commit

Permalink
fix: get full path for nested routes (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeya authored Aug 28, 2023
1 parent 361eb24 commit 2620adb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/utils/__tests__/data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getFullPath } from '../data';

describe('getFullPath', () => {
test('should return just the path if parentId is root', () => {
const path = getFullPath('admin.users.$id', {
root: {
path: ''
},
'admin.users.$id': {
parentId: 'root',
path: 'admin/users/:id'
}
});

expect(path).toBe('admin/users/:id');
});

test('should return the full path of a route with multiple parents', () => {
const path = getFullPath('blog.post.$id', {
root: {
path: ''
},
blog: {
parentId: 'root',
path: 'blog'
},
'blog.post': {
parentId: 'blog',
path: 'post'
},
'blog.post.$id': {
parentId: 'blog.post',
path: ':id'
}
});

expect(path).toBe('blog/post/:id');
});
});
14 changes: 12 additions & 2 deletions src/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export function getRouteData(route: string, context: EntryContext) {
}
: undefined;

const path =
!manifest.path && manifest.parentId === 'root' ? '' : manifest?.path;
const path = getFullPath(route, context.manifest.routes);

const parents = manifest.id?.split('/').slice(0, -1);

Expand All @@ -44,6 +43,17 @@ export function getRouteData(route: string, context: EntryContext) {
};
}

export function getFullPath(
route: string,
routes: Record<string, { parentId?: string; path?: string }>
): string | undefined {
const manifest = routes[route];

if (!manifest.parentId || manifest.parentId === 'root') return manifest.path;

return `${getFullPath(manifest.parentId, routes)}/${manifest.path}`;
}

type GetOptionalSegmentDataParams = {
route: string;
context: EntryContext;
Expand Down

0 comments on commit 2620adb

Please sign in to comment.