> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kit-rs.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Generators

> Generate controllers, actions, middleware, errors, and more

Kit provides generator commands to scaffold common application components with proper structure and boilerplate code.

## make:controller

Generate a new controller for handling HTTP requests.

```bash theme={null}
kit make:controller <name>
```

### Examples

```bash theme={null}
kit make:controller User
kit make:controller OrderItem
kit make:controller api/User
```

### Generated File

```rust theme={null}
// src/controllers/user.rs
use kit::{handler, json_response, Request, Response};

#[handler]
pub async fn invoke(_req: Request) -> Response {
    json_response!({
        "controller": "User"
    })
}
```

### What It Does

1. Creates `src/controllers/<name>.rs` with `#[handler]` attribute
2. Updates `src/controllers/mod.rs` to export the controller

***

## make:action

Generate a new action for encapsulating business logic.

```bash theme={null}
kit make:action <name>
```

### Examples

```bash theme={null}
kit make:action CreateUser
kit make:action SendNotification
kit make:action ProcessPayment
```

### Generated File

```rust theme={null}
// src/actions/create_user.rs
use kit::injectable;

#[injectable]
pub struct CreateUserAction {
    // Dependencies injected via container
}

impl CreateUserAction {
    pub fn execute(&self) {
        // TODO: Implement action logic
    }
}
```

### What It Does

1. Creates `src/actions/<name>.rs`
2. Updates `src/actions/mod.rs` to export the action
3. Action is automatically registered in the DI container

***

## make:middleware

Generate a new middleware for request/response processing.

```bash theme={null}
kit make:middleware <name>
```

### Examples

```bash theme={null}
kit make:middleware Auth
kit make:middleware RateLimit
kit make:middleware Cors
```

### Generated File

```rust theme={null}
// src/middleware/auth.rs
use kit::middleware::{Middleware, Next};
use kit::{Request, Response};
use async_trait::async_trait;

pub struct AuthMiddleware;

#[async_trait]
impl Middleware for AuthMiddleware {
    async fn handle(&self, req: Request, next: Next) -> Response {
        // Before request handling

        let response = next.run(req).await;

        // After request handling

        response
    }
}
```

### What It Does

1. Creates `src/middleware/<name>.rs`
2. Updates `src/middleware/mod.rs` to export the middleware

***

## make:error

Generate a custom domain error with HTTP response conversion.

```bash theme={null}
kit make:error <name>
```

### Examples

```bash theme={null}
kit make:error UserNotFound
kit make:error PaymentFailed
kit make:error InsufficientStock
```

### Generated File

```rust theme={null}
// src/errors/user_not_found.rs
use kit::domain_error;

#[domain_error(status = 500, message = "User not found")]
pub struct UserNotFound;
```

### What It Does

1. Creates `src/errors/<name>.rs`
2. Creates or updates `src/errors/mod.rs`
3. Generates a domain error with automatic HTTP response conversion

### Usage

```rust theme={null}
use crate::errors::user_not_found::UserNotFound;

pub async fn show(req: Request) -> Response {
    let user = find_user(id).await
        .ok_or(UserNotFound)?;  // Returns 500 response

    json_response!({ "user": user })
}
```

***

## make:task

Generate a new scheduled task for background processing.

```bash theme={null}
kit make:task <name>
```

### Examples

```bash theme={null}
kit make:task CleanupLogs
kit make:task SendReminders
kit make:task BackupDatabase
```

### Generated File

```rust theme={null}
// src/tasks/cleanup_logs_task.rs
use async_trait::async_trait;
use kit::{Task, TaskResult};

/// CleanupLogsTask - A scheduled task
///
/// Implement your task logic in the `handle()` method.
/// Register this task in `src/schedule.rs` with the fluent API.
pub struct CleanupLogsTask;

impl CleanupLogsTask {
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl Task for CleanupLogsTask {
    async fn handle(&self) -> TaskResult {
        // TODO: Implement your task logic here
        println!("Running CleanupLogsTask...");
        Ok(())
    }
}
```

### What It Does

1. Creates `src/tasks/<name>.rs` with `Task` trait implementation
2. Creates or updates `src/tasks/mod.rs` to export the task
3. Creates `src/schedule.rs` for registering tasks (if not exists)
4. Creates `src/bin/schedule.rs` scheduler binary (if not exists)

### Next Steps

After generating, register your task in `src/schedule.rs`:

```rust theme={null}
use crate::tasks::cleanup_logs_task;

pub fn register(schedule: &mut Schedule) {
    schedule.add(
        schedule.task(cleanup_logs_task::CleanupLogsTask::new())
            .daily()
            .at("03:00")
            .name("cleanup_logs_task")
            .description("TODO: Add task description")
    );
}
```

Then run the scheduler:

```bash theme={null}
kit schedule:work  # Daemon mode
kit schedule:run   # Run once
```

***

## make:inertia

Generate an Inertia.js page component.

```bash theme={null}
kit make:inertia <name>
```

### Examples

```bash theme={null}
kit make:inertia About
kit make:inertia UserProfile
kit make:inertia Dashboard
```

### Generated Files

Creates a React component in `frontend/src/pages/`:

```tsx theme={null}
// frontend/src/pages/About.tsx
export default function About() {
    return (
        <div>
            <h1>About</h1>
        </div>
    );
}
```

***

## generate-types

Generate TypeScript types from Rust `InertiaProps` structs.

```bash theme={null}
kit generate-types [options]
```

### Options

| Option                | Description                                                       |
| --------------------- | ----------------------------------------------------------------- |
| `-o, --output <PATH>` | Output file path (default: `frontend/src/types/inertia-props.ts`) |
| `-w, --watch`         | Watch for changes and regenerate                                  |

### Examples

```bash theme={null}
# Generate types once
kit generate-types

# Watch mode
kit generate-types --watch

# Custom output path
kit generate-types --output frontend/src/types/props.ts
```

### How It Works

Scans your Rust code for structs implementing `InertiaProps` and generates TypeScript interfaces:

```rust theme={null}
// Rust
#[derive(InertiaProps)]
pub struct UserPageProps {
    pub user: User,
    pub posts: Vec<Post>,
}
```

```typescript theme={null}
// Generated TypeScript
export interface UserPageProps {
    user: User;
    posts: Post[];
}
```

***

## Summary

| Command                  | Creates          | Location              |
| ------------------------ | ---------------- | --------------------- |
| `make:controller <name>` | Controller       | `src/controllers/`    |
| `make:action <name>`     | Action           | `src/actions/`        |
| `make:middleware <name>` | Middleware       | `src/middleware/`     |
| `make:error <name>`      | Domain Error     | `src/errors/`         |
| `make:task <name>`       | Scheduled Task   | `src/tasks/`          |
| `make:inertia <name>`    | Page Component   | `frontend/src/pages/` |
| `make:migration <name>`  | Migration        | `migrations/`         |
| `generate-types`         | TypeScript Types | `frontend/src/types/` |
