Skip to content

[Feature Request] Nested routes in the RestApiHandler #2453

@lsmith77

Description

@lsmith77

This relates to this discussion thread on Discord.

Is your feature request related to a problem? Please describe.

RestApiHandler only supports flat routes (/post, /post/42). There is no way to express parent-scoped access in the URL structure. To fetch only a user's posts, clients must manually filter by parent ID — which is error-prone and produces URLs that don't reflect the data hierarchy.

** Describe the solution you'd like **
Add explicit nested route configuration to RestApiHandler that enables hierarchical URL patterns (/user/:id/post, /user/:id/post/:postId) for all five HTTP methods, with automatic parent scoping enforced server-side:

new RestApiHandler({
    schema: client.$schema,
    endpoint: 'http://localhost/api',
    nestedRoutes: {
        User: {
            Post: {
                relation: 'author',            // relation field on child pointing to parent
                requireOrphanProtection: true, // optional: enforce safe onDelete at startup
            },
        },
    },
});

Key behaviors:

  • 404 if parent does not exist, or child does not belong to the requested parent.
  • POST automatically binds the created child to the parent; returns 400 if the payload also sets that relation.
  • Flat routes remain fully functional. Non-configured 3-segment paths fall through to fetchRelated unchanged.
  • JSON:API self links in responses use the nested URL.
  • requireOrphanProtection: true throws at startup if the configured relation does not have onDelete: Cascade, Restrict, or NoAction in the schema.
  • All configuration errors (unknown models, missing relation fields, mismatched targets) are caught at construction time.

Describe alternatives you've considered

  • Implicit route generation from schema relations (aka auto discovery of the route nesting) — rejected because it creates unexpected route surface area and is hard to audit. Explicit-only is safer.
  • Client-side filtering via filter[authorId]=u1 on flat routes — works but is not server-enforced and doesn't produce hierarchical URLs.

Additional context

  • modelNameMapping and externalIdMapping are both respected on nested routes.
  • Multi-level nesting (/a/:id/b/:id/c) could be done later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions