### Install Girouette CLI Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Installs the Girouette package into your AdonisJS project using the AdonisJS CLI. This is the primary method for adding Girouette to your application. ```bash node ace add @adonisjs-community/girouette ``` -------------------------------- ### Girouette Resource Actions Picking Example (@Except) Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Shows how to exclude specific resource actions using the @Except decorator. This example defines a resource for products but excludes 'create', 'store', 'edit', 'update', and 'destroy' actions. ```typescript import { Resource, Except } from '@adonisjs-community/girouette' import HttpContext from '@ioc:Adonis/Core/HttpContext' @Resource({ pattern: '/products', name: 'shop.products' }) @Except(['create', 'store', 'edit', 'update', 'destroy']) export default class ProductsController { async index() { // GET /products return 'Listing all products' } async show({ params }: HttpContext) { // GET /products/:id return `Showing product with ID: ${params.id}` } // 'create', 'store', 'edit', 'update', 'destroy' actions are excluded. } ``` -------------------------------- ### Girouette Resource Middleware Example Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Illustrates how to apply middleware selectively to specific actions of a resource using the @ResourceMiddleware decorator. In this example, authentication is applied only to 'store', 'update', and 'destroy' actions. ```typescript import { Resource, ResourceMiddleware } from '@adonisjs-community/girouette' import { middleware } from '#start/kernel' @Resource({ pattern: '/admin/posts', name: 'admin.posts' }) @ResourceMiddleware(['store', 'update', 'destroy'], [middleware.auth()]) export default class AdminPostsController { // Methods for 'store', 'update', 'destroy' will have auth middleware applied. // Other methods like 'index', 'show', 'create', 'edit' will not. async index() {} async show() {} async create() {} async store() {} async edit() {} async update() {} async destroy() {} } ``` -------------------------------- ### Combining Multiple Girouette Decorators Example Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Demonstrates how to combine multiple Girouette decorators like @Group, @Get, @Middleware, and @Where to create a protected and validated API route for fetching articles. ```typescript import { Get, Middleware, Where, Group } from '@adonisjs-community/girouette' import { middleware } from '#start/kernel' import HttpContext from '@ioc:Adonis/Core/HttpContext' @Group('/api') export default class ArticlesController { @Get('/articles/:id') @Middleware([middleware.auth()]) @Where('id', /^\d+$/) async show({ params }: HttpContext) { // This route is protected by authentication middleware // and the 'id' parameter must be a digit. return `Showing article with ID: ${params.id}` } } ``` -------------------------------- ### Basic HTTP Method Decorators Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Demonstrates how to use basic HTTP method decorators like @Get, @Post, @Put, @Patch, and @Delete to define routes in an AdonisJS controller. These decorators map HTTP requests to controller methods. ```typescript import { Get, Post, Put, Patch, Delete, Any, } from '@adonisjs-community/girouette' import { HttpContext } from '@adonisjs/core/http' export default class UsersController { @Get('/users') async index({ response }: HttpContext) { // Handle GET request for /users } @Post('/users') async store({ request }: HttpContext) { // Handle POST request for /users } @Put('/users/:id') async update({ params }: HttpContext) { // Handle PUT request for /users/:id } @Patch('/users/:id') async patch({ params }: HttpContext) { // Handle PATCH request for /users/:id } @Delete('/users/:id') async destroy({ params }: HttpContext) { // Handle DELETE request for /users/:id } @Any('/users/any') async handleAny() { // Handle any HTTP method for /users/any } } ``` -------------------------------- ### Girouette Resource Actions Picking Example (@ApiOnly) Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Demonstrates using the @ApiOnly decorator to include only standard API actions (index, show, store, update, destroy) for a resource. This automatically excludes web-form related actions like 'create' and 'edit'. ```typescript import { Resource, ApiOnly } from '@adonisjs-community/girouette' import HttpContext from '@ioc:Adonis/Core/HttpContext' @Resource({ pattern: '/products', name: 'shop.products' }) @ApiOnly() export default class ProductsController { async index() { // GET /products return 'Listing all products' } async show() { // GET /products/:id return 'Showing a product' } async store() { // POST /products return 'Creating a new product' } async update() { // PUT/PATCH /products/:id return 'Updating a product' } async destroy() { // DELETE /products/:id return 'Deleting a product' } // 'create' and 'edit' actions are automatically excluded. } ``` -------------------------------- ### Resource Controller Routing Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Utilizes the @Resource decorator to automatically define conventional RESTful routes for a controller. This simplifies the setup for CRUD operations. ```typescript import { Resource, } from '@adonisjs-community/girouette' import { HttpContext } from '@adonisjs/core/http' @Resource({ pattern: '/posts', name: 'blog.posts' }) export default class PostsController { async index() {} // GET /posts -> blog.posts.index async create() {} // GET /posts/create -> blog.posts.create async store() {} // POST /posts -> blog.posts.store async show() {} // GET /posts/:id -> blog.posts.show async edit() {} // GET /posts/:id/edit -> blog.posts.edit async update() {} // PUT/PATCH /posts/:id -> blog.posts.update async destroy() {} // DELETE /posts/:id -> blog.posts.destroy } ``` -------------------------------- ### Girouette Route Configuration Decorators Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Provides decorators for configuring route groups, domains, middleware, and parameter constraints. These decorators help organize and apply common settings to multiple routes. ```APIDOC Route Configuration Decorators: - @Group(name?: string) - Defines an optional route name prefix for nested routes. - @GroupDomain(domain: string) - Restricts routes within the group to a specific domain. - @GroupMiddleware(middleware: Middleware[]) - Applies middleware to all routes within the group. - @Middleware(middleware: Middleware[]) - Applies middleware to a single route. - @Resource({pattern: string, name?: string, params?: { [resource: string]: string } }) - Creates RESTful resource routes (index, show, create, store, edit, update, destroy) with a base pattern and optional name/params. - @ResourceMiddleware(actions: string | string[], middleware: Middleware[]) - Applies middleware to specific actions of a resource route. - @Where(param: string, matcher: string | RegExp | Function) - Adds constraints to route parameters, ensuring they match a specific pattern or condition. ``` -------------------------------- ### Girouette HTTP Method Decorators Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Defines decorators for mapping HTTP methods to controller actions. Each decorator accepts a route pattern and an optional route name. ```APIDOC HTTP Method Decorators: - @Get(pattern: string, name?: string) - Maps GET requests to a specific route pattern. - @Post(pattern: string, name?: string) - Maps POST requests to a specific route pattern. - @Put(pattern: string, name?: string) - Maps PUT requests to a specific route pattern. - @Patch(pattern: string, name?: string) - Maps PATCH requests to a specific route pattern. - @Delete(pattern: string, name?: string) - Maps DELETE requests to a specific route pattern. - @Any(pattern: string, name?: string) - Maps any HTTP method to a specific route pattern. ``` -------------------------------- ### Configure Girouette Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Generates the Girouette configuration file for your AdonisJS project. This allows customization of controller scanning paths and other settings. ```bash node ace configure @adonisjs-community/girouette ``` -------------------------------- ### Resource Controller with Custom Parameters Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Demonstrates how to customize resource parameter names using the `params` option in the @Resource decorator. This allows using slugs or other identifiers instead of default IDs. ```typescript import { Resource, } from '@adonisjs-community/girouette' import { HttpContext } from '@adonisjs/core/http' @Resource({ pattern: '/articles', params: { article: 'slug' } }) export default class ArticlesController { async index() {} // GET /articles -> blog.articles.index async create() {} // GET /articles/create -> blog.articles.create async store() {} // POST /articles -> blog.articles.store async show({ params }: HttpContext) {} // GET /articles/:slug -> blog.articles.show (params.slug will be the slug) async edit({ params }: HttpContext) {} // GET /articles/:slug/edit -> blog.articles.edit (params.article will be the slug) async update({ params }: HttpContext) {} // PUT/PATCH /articles/:slug -> blog.articles.update (params.article will be the slug) async destroy({ params }: HttpContext) {} // DELETE /articles/:slug -> blog.articles.destroy (params.article will be the slug) } ``` -------------------------------- ### Route Grouping and Middleware Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Illustrates how to group routes using @Group, apply shared middleware with @GroupMiddleware, and set domain restrictions with @GroupDomain. This helps organize related routes and apply common configurations. ```typescript import { Get, Group, GroupMiddleware, GroupDomain, } from '@adonisjs-community/girouette' import { middleware } from '#start/kernel' import { HttpContext } from '@adonisjs/core/http' @Group({ name: 'admin', prefix: '/admin' }) // Name prefix 'admin', URL prefix '/admin' @GroupMiddleware([middleware.auth()]) // Apply auth middleware to all routes in this group @GroupDomain('admin.example.com') // Restrict routes to this domain export default class AdminController { @Get('/dashboard', 'admin.dashboard') // Final URL: /admin/dashboard, Route name: admin.dashboard async index({ auth }: HttpContext) { // This route is protected by auth middleware and only accessible via admin.example.com } @Get('/users', 'admin.users.list') // Final URL: /admin/users, Route name: admin.users.list async listUsers() { // Another admin route } } // Example of @Group with only name prefix @Group({ name: 'api' }) export class ApiController {} // Example of @Group with only URL prefix @Group({ prefix: '/api' }) export class ApiControllerWithPrefix {} // Example of @Group with both name and URL prefix @Group({ name: 'api', prefix: '/api/v1' }) export class ApiControllerV1 {} ``` -------------------------------- ### Applying Route Middleware Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Shows how to apply middleware to specific routes using the @Middleware decorator. This allows fine-grained control over which requests are processed by which middleware. ```typescript import { Get, Middleware, } from '@adonisjs-community/girouette' import { middleware } from '#start/kernel' import { HttpContext } from '@adonisjs/core/http' export default class UsersController { @Get('/profile') @Middleware([middleware.auth()]) // Apply auth middleware to this specific route async show({ auth }: HttpContext) { // Only authenticated users can access this route } @Get('/public') async publicInfo() { // This route does not require authentication } } ``` -------------------------------- ### Route Parameter Constraints Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Explains how to use the @Where decorator to add constraints to route parameters. This ensures that route segments match specific patterns, improving route matching accuracy. ```typescript import { Get, Where, } from '@adonisjs-community/girouette' import { HttpContext } from '@adonisjs/core/http' export default class PostsController { @Get('/posts/:slug') @Where('slug', /^[a-z0-9-]+$/) // Constraint for the 'slug' parameter async show({ params }: HttpContext) { // This route will only match if the slug contains lowercase letters, numbers, and hyphens. // Example: /posts/my-first-post matches, /posts/MyPost-123 does not. } @Get('/users/:id') @Where('id', /^[0-9]+$/) // Constraint for the 'id' parameter to be numeric async getUser({ params }: HttpContext) { // This route will only match if the id is a sequence of digits. // Example: /users/123 matches, /users/abc does not. } } ``` -------------------------------- ### Girouette Resource Action Decorators Source: https://github.com/adonisjs-community/girouette/blob/main/README.md Allows fine-grained control over which resource actions are included or excluded when using the @Resource decorator. This helps in customizing resource route generation. ```APIDOC Resource Action Decorators: - @Pick(actions: string | string[]) - Includes only the specified actions when generating resource routes. - @Except(actions: string | string[]) - Excludes the specified actions when generating resource routes. - @ApiOnly() - Includes only API-centric actions (index, show, store, update, destroy) for a resource, automatically excluding web-form related actions like 'create' and 'edit'. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.