Automatically generate Frontend type definitions from your Laravel Backend structure. Bridge the gap between your Laravel models, validations, and migrations with TypeScript interfaces for seamless full-stack development.
- π Multi-Source Analysis - Extracts data from Models, Controller validations, and Migrations
- π― TypeScript Generation - Creates clean TypeScript interfaces for your frontend
- π JSON Schema Export - Generates JSON schema for API documentation
- π CLI Only - Secure generation through Artisan commands only
- π Smart Type Mapping - Intelligently converts PHP/Laravel types to TypeScript
- π Organized Output - Creates structured files in
resources/js/docs-api/
composer require andrazero121/docs-horizonThe package will automatically register its service provider.
Run the Artisan command to analyze your Laravel application and generate type definitions:
php artisan docs:horizonThis command will:
- π Analyze all Models in
app/Models/ - π Extract validation rules from Controllers
- π Read column types from Migration files
- π Merge and convert data to frontend-friendly format
- π Generate TypeScript and JSON files
After running the command, you'll find generated files in:
resources/js/docs-api/
βββ types.ts # TypeScript interface definitions
βββ schema.json # JSON schema for API documentation
βββ index.ts # Barrel export file
// Auto-generated by Docs Horizon
// Do not edit this file manually
export interface Users {
id: number;
name: string;
email: string;
email_verified_at: string | null;
password: string;
remember_token: string | null;
created_at: string;
updated_at: string;
}
export interface Posts {
id: number;
title: string;
content: string;
excerpt: string | null;
user_id: number;
published: boolean;
published_at: string | null;
created_at: string;
updated_at: string;
}
export interface Categories {
id: number;
name: string;
slug: string;
description: string | null;
created_at: string;
updated_at: string;
}{
"users": {
"id": "number",
"name": "string",
"email": "string",
"email_verified_at": "string",
"password": "string",
"remember_token": "string",
"created_at": "string",
"updated_at": "string"
},
"posts": {
"id": "number",
"title": "string",
"content": "string",
"excerpt": "string",
"user_id": "number",
"published": "boolean",
"published_at": "string",
"created_at": "string",
"updated_at": "string"
}
}import { Users, Posts, Categories } from '@/docs-api';
// Type-safe object creation
const user: Users = {
id: 1,
name: 'John Doe',
email: 'john@example.com',
// TypeScript will ensure all required fields are present
};
// API response typing
async function fetchUser(id: number): Promise<Users> {
const response = await fetch(`/api/users/${id}`);
return response.json(); // Typed as Users interface
}
// Form validation
function validateUserForm(data: Partial<Users>): boolean {
// Your validation logic with type safety
return data.name !== undefined && data.email !== undefined;
}<script setup lang="ts">
import { ref } from 'vue';
import type { Users, Posts } from '@/docs-api';
const user = ref<Users>({
id: 0,
name: '',
email: '',
// All properties are typed and autocompleted
});
const posts = ref<Posts[]>([]);
</script>import React, { useState } from 'react';
import type { Users, Posts } from '@/docs-api';
export default function UserProfile() {
const [user, setUser] = useState<Users | null>(null);
const [posts, setPosts] = useState<Posts[]>([]);
// Component logic with full type safety
}Docs Horizon uses a sophisticated multi-layer analysis approach:
- Scans Controller validation rules
- Parses Migration files for column definitions
- Maps Laravel validation types to TypeScript types
- Analyzes Eloquent Models
- Extracts fillable fields
- Determines table relationships
- Merges data from all sources
- Resolves conflicts with smart prioritization
- Normalizes field names and types
- Creates clean TypeScript interfaces
- Generates comprehensive JSON schemas
- Maintains consistent formatting
| Laravel/PHP Type | TypeScript Type | Notes |
|---|---|---|
string |
string |
Text fields |
integer, bigInteger |
number |
Numeric fields |
boolean |
boolean |
True/false values |
json |
object |
JSON objects |
array |
any[] |
Array types |
text, longText |
string |
Text content |
timestamp, date |
string |
ISO date strings |
decimal, float |
number |
Decimal numbers |
When multiple sources define the same field, Docs Horizon uses this priority order:
- Migration files (highest priority) - Most accurate column definitions
- Model fillable fields - Business logic constraints
- Controller validation rules (lowest priority) - Request validation
You can extend the type mapping by modifying the generated files after creation, but remember they will be overwritten on the next generation.
Add the generation command to your build process:
{
"scripts": {
"build": "php artisan docs:horizon && npm run build",
"dev": "php artisan docs:horizon && npm run dev"
}
}php artisan docs:horizon --watchNo types generated?
- Ensure you have Models in
app/Models/ - Check that your Models extend
Illuminate\Database\Eloquent\Model - Verify Migration files exist in
database/migrations/
Missing fields in output?
- Check that fields are defined in
$fillablearray in your Models - Ensure Migration files use standard Laravel column types
- Verify Controller validation rules use supported validation types
TypeScript errors?
- Make sure your TypeScript configuration includes the
resources/jsdirectory - Update your path aliases to include
@/docs-api
Run with verbose output to see what's being processed:
php artisan docs:horizon -vWe welcome contributions! Please see our Contributing Guide for details.
- Clone the repository
- Install dependencies:
composer install - Run tests:
composer test - Check code style:
composer cs-fix
This package is licensed under the MIT License. See the LICENSE file for details.
- π Documentation
- π Issue Tracker
- π¬ Discussions
- v1.1 - Relationship mapping support
- v1.2 - Watch mode for auto-regeneration
- v1.3 - Custom configuration file
- v1.4 - Support for Enum types
- v1.5 - GraphQL schema generation
- v2.0 - Plugin system for custom generators
- Inspired by the Laravel and TypeScript communities
- Built with love for full-stack developers
- Special thanks to all contributors and users
Made with β€οΈ by andrazero121
β Star this repo if it helped you! β