Looking to hire Laravel developers? Try LaraJobs

laravel-wayfinder-forge maintained by illuma-law

Description
Auto-generate strongly typed frontend SDKs from Laravel Wayfinder routes.
Author
Last update
2026/04/20 20:50 (dev-main)
License
Downloads
1

Comments
comments powered by Disqus

Laravel Wayfinder Forge

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Auto-generate strongly typed frontend SDKs from your Laravel backend routes and FormRequests.

Wayfinder Forge ensures your frontend client code stays perfectly in sync with your backend validation rules. It parses your Laravel routes, reflects on their FormRequest or Spatie\Data classes, and automatically generates a ready-to-use TypeScript SDK.

Features

  • TypeScript Interface Generation: Automatically converts Laravel validation rules (required, string, nullable, array) into accurate TypeScript interfaces.
  • Spatie Laravel Data Support: First-class support for spatie/laravel-data DTOs.
  • Multiple Client Support: Generates SDKs formatted for axios, standard fetch, or inertia.
  • Customizable Output: Easily define which routes to include/exclude via prefixes and middleware filters.

Installation

You can install the package via composer:

composer require illuma-law/laravel-wayfinder-forge --dev

Publish the config file:

php artisan vendor:publish --tag="laravel-wayfinder-forge-config"

Configuration

The published config/wayfinder-forge.php allows you to customize the output behavior:

return [
    /**
     * The output path for the generated TypeScript SDK file.
     */
    'output_path' => resource_path('js/api/sdk.ts'),

    /**
     * Define which routes should be processed.
     */
    'routes' => [
        // Only include routes matching these prefixes
        'include_prefixes' => [
            'api/*',
        ],

        // Exclude routes with specific middleware (like internal debug tools)
        'exclude_middlewares' => [
            'debugbar',
        ],
    ],

    /**
     * The HTTP client template to use for the generated SDK.
     * Supported: 'axios', 'fetch', 'inertia'
     */
    'client' => 'axios',

    /**
     * Enable support for spatie/laravel-data objects.
     */
    'use_spatie_data' => true,
];

Usage & Integration

Generate your TypeScript SDK by running the forge command:

php artisan wayfinder:forge

Tip: Add this command to your package.json build scripts so it runs automatically during development.

Example: FormRequests

Laravel Controller & Request

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'content' => 'required|string',
            'is_published' => 'boolean', // Optional by default unless 'required' is present
        ];
    }
}

// routes/api.php
Route::post('api/posts', [PostController::class, 'store'])->name('posts.store');

Generated TypeScript SDK (resources/js/api/sdk.ts)

export interface ApiPostsRequest {
    title: string;
    content: string;
    is_published?: boolean;
}

export const postsStore = (data: ApiPostsRequest) => {
    return axios.post(`/api/posts`, data);
};

Example: Spatie Laravel Data

If you are using spatie/laravel-data to type-hint your controllers instead of FormRequests, Forge handles that automatically.

Laravel Controller & Data Object

namespace App\Data;

use Spatie\LaravelData\Data;

class UserData extends Data
{
    public string $name;
    public ?int $age;
}

// routes/api.php
Route::post('api/users', function (UserData $userData) { 
    // ...
});

Generated TypeScript SDK

export interface UserData {
    name: string;
    age?: number;
}

export const apiUsers = (data: UserData) => {
    return axios.post(`/api/users`, data);
};

Testing

Run the package tests:

composer test

License

The MIT License (MIT). Please see License File for more information.