Skip to content

Conversation

MichaelDeBoey
Copy link
Member

With this PR

  let books = {
    ...resources('books'),
    author: 'books/:id/author',
    reviews: resources('books/:id/reviews', { param: 'reviewId' }),
  }
  let profile = {
    ...resource('profile'),
    admin: '/profile/admin',
    todos: resource('profile/todos'),
  }

now becomes

let books = resources('books', {
  children: { author: 'author', reviews: resources('reviews', { param: 'reviewId' }) },
})
let profile = resource('profile', { children: { admin: 'admin', todos: resource('todos') } })

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for nested routes in createResource and createResources functions through a new children option, allowing developers to define child routes inline instead of using object spread syntax.

  • Adds children option to both ResourceOptions and ResourcesOptions interfaces
  • Implements addParamToPatterns utility function to prepend parameters to child route patterns
  • Updates type definitions to handle nested route structures with proper parameter inheritance

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/fetch-router/src/lib/resource.ts Adds children option support, implements parameter prefixing logic, and updates type definitions
packages/fetch-router/src/lib/resource.test.ts Adds comprehensive tests for the new children functionality with various route pattern types
packages/fetch-router/CHANGELOG.md Documents the new nested routes feature with before/after examples

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +134 to +148
const addParamToPatterns = (defs: RouteDefs, param: string): RouteDefs =>
Object.fromEntries(
Object.entries(defs).map(([key, value]) => {
let updatedValue =
value instanceof Route
? new Route(value.method, new RoutePattern(`:${param}`).join(value.pattern))
: typeof value === 'string' || value instanceof RoutePattern
? new RoutePattern(`:${param}`).join(value)
: typeof value === 'object' && 'pattern' in value
? { ...value, pattern: new RoutePattern(`:${param}`).join((value as any).pattern) }
: addParamToPatterns(value, param)

return [key, updatedValue]
}),
)
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type assertion (value as any).pattern on line 143 bypasses TypeScript's type checking. Consider using a more specific type guard or conditional type checking to ensure type safety.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also done in a couple of other places, hence why I took it over.

If needed, I'm happy to add better typing here

@MichaelDeBoey MichaelDeBoey force-pushed the support-nested-routes-in-resources branch from 53c7d1b to 71c4820 Compare October 13, 2025 15:40
@MichaelDeBoey MichaelDeBoey changed the title fix(fetch-router): support nested routes in createResource & createResources feat(fetch-router): support nested routes in createResource & createResources Oct 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant