Repository navigation
Return chainable resource routes from jsonApiResource - #23
Merged
Merged
Conversation
router.jsonApiResource() now returns the routes it registered, so middleware can be attached to some actions only. use() covers resource actions and useRelationships() covers relationship actions, each taking a list of action names or '*', modelled on AdonisJS RouteResource.use(). Middleware types come from the AdonisJS route's own use(), and actions that were not registered are ignored. Existing callers are unaffected.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
router.jsonApiResource()now returns the routes it registered, so middleware can be attached to some actions only. Before this it returned nothing, and the only way to add middleware was a group around the whole resource, which reached every action.What changed
registerJsonApiResource()keeps each route it creates, keyed by action, and returns aJsonApiResourceRoutes. It is modelled on AdonisJSRouteResource.use().use(actions | '*', middleware)attaches to resource routes, with the same tokens asonly.useRelationships(actions | '*', middleware)attaches to relationship routes, with the same tokens asrelationshipsOnly. It is a separate method because both sets have ashow.onlyorrelationshipsOnly) is ignored.JsonApiResourceRoutes<RouteMiddleware>, whereRouteMiddlewareisParameters<Route['use']>[0], so it accepts exactly what an AdonisJS route's ownuse()accepts.JsonApiResourceRoutesis exported.docs/reference.mdhas a new "Route middleware" section.The change is additive. Callers that ignore the return value behave as before.
Why
Ekto's server needs per-action middleware on resources registered this way, to mark which routes a scoped API token may use (ekto monorepo #159).
Testing
tests/unit/register_resource.spec.tscover attaching to chosen actions,'*', keeping the resource and relationshipshowapart, ignoring unregistered actions, and middleware order across calls.pnpm buildare clean.Routeis checked against the new route type through the provider's typecheck.🤖 Generated with Claude Code