/** * Route registration helper. Installed as a router macro by the provider: * * ```ts * router * .group(() => { * router.jsonApiResource('articles', { * resource: () => import('#controllers/articles_controller'), * relationships: () => import('#controllers/article_relationships_controller'), * }) * }) * .prefix('/api/v1') * .as('api.v1') * ``` * * Routes are named `.` (prefixed by the enclosing groups' * `.as()` names), which is the convention the LinkBuilder uses to generate * version-correct URLs and to omit links for unregistered routes. */ /** * Resource controller actions, matching its method names. Selected with the * `only` option. */ export type ResourceActions = 'index' | 'show' | 'store' | 'update' | 'destroy'; /** * Relationship controller actions, matching its method names. Selected with * the `relationshipsOnly` option. `show`/`replace`/`add`/`remove` serve * `/:id/relationships/:relation`; `related` serves `/:id/:relation`. */ export type RelationshipActions = 'show' | 'replace' | 'add' | 'remove' | 'related'; export type JsonApiResourceControllers = { /** * Controller with index/show/store/update/destroy actions (any subset, * limited via `only`). */ resource?: LazyController; /** * Controller with show/replace/add/remove/related actions serving * `/:id/relationships/:relation` and `/:id/:relation` routes (any subset, * limited via `relationshipsOnly`). */ relationships?: LazyController; }; export type JsonApiResourceOptions = { /** * Register only the listed resource routes. Omit to register every action * the `resource` controller supports. */ only?: ResourceActions[]; /** * Register only the listed relationship routes. Omit to register every * action the `relationships` controller supports. */ relationshipsOnly?: RelationshipActions[]; }; type ControllerConstructor = new (...args: never[]) => unknown; type LazyController = () => Promise<{ default: ControllerConstructor; }>; /** * Structural slice of an AdonisJS route: naming it and attaching * middleware, which is all the helper and the returned routes need. */ type RouteLike = { as(name: string): RouteLike; use(middleware: never): unknown; }; /** * Structural slice of the AdonisJS router used by the helper. The handler * is typed loosely (unknown) because the real router accepts a wide union; * this helper only ever passes [LazyController, actionName] tuples. */ type RouterLike = { get(pattern: string, handler: unknown): RouteLike; post(pattern: string, handler: unknown): RouteLike; patch(pattern: string, handler: unknown): RouteLike; delete(pattern: string, handler: unknown): RouteLike; }; /** * The routes one `jsonApiResource()` call registered, kept by action so * middleware can be attached to some of them. Modelled on AdonisJS's own * `RouteResource.use()`. Each call returns the same object, so calls * chain. Naming an action that was not registered (left out with `only` * or `relationshipsOnly`) does nothing. * * Middleware attached here is route middleware, so it runs after the * middleware of the groups the resource sits in. * * ```ts * router * .jsonApiResource('articles', { resource, relationships }) * .use(['store', 'update', 'destroy'], middleware.auth()) * .useRelationships(['replace', 'add', 'remove'], middleware.auth()) * ``` */ export declare class JsonApiResourceRoutes { #private; /** @internal Called by registerJsonApiResource as it registers each route. */ addResource(action: ResourceActions, route: RouteLike): void; /** @internal Called by registerJsonApiResource as it registers each route. */ addRelationship(action: RelationshipActions, route: RouteLike): void; /** Attaches middleware to the listed resource routes, or all of them with '*'. */ use(actions: ResourceActions[] | '*', middleware: Middleware): this; /** Attaches middleware to the listed relationship routes, or all of them with '*'. */ useRelationships(actions: RelationshipActions[] | '*', middleware: Middleware): this; } export declare function registerJsonApiResource(router: RouterLike, type: string, controllers: JsonApiResourceControllers, options?: JsonApiResourceOptions): JsonApiResourceRoutes; export {};