From 2d0d1a22e18e7ade6b1902931eec84b88e35455e Mon Sep 17 00:00:00 2001 From: Anoesj Date: Tue, 18 Mar 2025 11:06:08 +0100 Subject: [PATCH] feat: allow `filePath` to be `null` on a route added in an editable tree --- src/core/extendRoutes.spec.ts | 9 +++++++++ src/core/extendRoutes.ts | 2 +- src/core/tree.ts | 6 +++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/core/extendRoutes.spec.ts b/src/core/extendRoutes.spec.ts index 730a0e647..101fae102 100644 --- a/src/core/extendRoutes.spec.ts +++ b/src/core/extendRoutes.spec.ts @@ -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) diff --git a/src/core/extendRoutes.ts b/src/core/extendRoutes.ts index 8692d73e1..fe702e302 100644 --- a/src/core/extendRoutes.ts +++ b/src/core/extendRoutes.ts @@ -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('/')) { diff --git a/src/core/tree.ts b/src/core/tree.ts index 501a7c3c3..3c09203fb 100644 --- a/src/core/tree.ts +++ b/src/core/tree.ts @@ -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( {