Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow filePath to be null on a route added in an editable tree #607

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core/extendRoutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('EditableTreeNode', () => {
expect(tree.children.get('foo')?.path).toBe('/foo')
})

it('allows filePath to be null on a route added in the editable tree', () => {
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert('foo', null)
expect(editable.children).toHaveLength(1)
expect(editable.children[0]?.path).toBe('/foo')
})

it('keeps nested routes flat', () => {
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)
Expand Down
2 changes: 1 addition & 1 deletion src/core/extendRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class EditableTreeNode {
* @param filePath - file path
* @returns the new editable route node
*/
insert(path: string, filePath: string) {
insert(path: string, filePath: string | null) {
// adapt paths as they should match a file system
let addBackLeadingSlash = false
if (path.startsWith('/')) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export class TreeNode {
* @param path - path segment to insert, already parsed (e.g. users/:id)
* @param filePath - file path, defaults to path for convenience and testing
*/
insertParsedPath(path: string, filePath: string = path): TreeNode {
// TODO: allow null filePath?
const isComponent = true
insertParsedPath(path: string, filePath: string | null = path): TreeNode {
// Allow null filePath to be handled
const isComponent = filePath !== null

const node = new TreeNode(
{
Expand Down