scrumble-nl / laravel-model-ts-type
This package makes it possible to generate TypeScript types based on your models
Installs: 33 277
Dependents: 0
Suggesters: 0
Security: 0
Stars: 61
Watchers: 1
Forks: 13
Open Issues: 3
Requires
- php: ^8.1
- ext-pdo: *
- laravel/framework: ^10.0|^11.0
- laravel/helpers: ^1.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- nunomaduro/larastan: ^2.4
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-25 10:00:42 UTC
README
Generate TypeScript types based on your models.
Usage
Installation
Install the package using composer:
composer require --dev scrumble-nl/laravel-model-ts-type
Generating types
php artisan types:generate {--modelDir=} {--outputDir=} {--model=} {--noKebabCase}
Additional options:
If you want to change the default values you can publish the config file and change it to you liking.
NOTE: Do not forget to add the directory to your typeroots in tsconfig.json
What does it do?
This package takes (almost) all off Laravel's magic into account. It follows these steps for generating a Type:
- Retrieve all fields from te database (only MySQL/MariaDB supported) and map them to default types (string, number, etc.)
- Add relations for the Model, they will point to the related generated Type
- Add the attribute getters for the Model
- Check the
casts
attribute - Remove all fields that are in the
hidden
attribute
Example output
type Company = { id: number; name: string; created_at: string /* Date */ | null; updated_at: string /* Date */ | null; slug: string; welcome_message: string | null; contact_information: string | null; main_color: string | null; logo_src: string | null; user_field: any[]; language: any[]; team_site: any[]; is_api_enabled: boolean; kaizen_user_field: string; faqs?: Faq[] | null; users?: User[] | null; team_properties: TeamProperty[] | null; editor_images: EditorImage[] | null; meta_data?: any[]; };
Roadmap
- Add tests (in progress)
- Generate types for packagized models
- Create command to generate type for 1 model
- Implement unqualified name for relation doc blocks
Contributing
If you would like to see additions/changes to this package you are always welcome to add some code or improve it.
Scrumble
This product has been originally developed by Scrumble for internal use. As we have been using lots of open source packages we wanted to give back to the community. We hope this helps you getting forward as much as other people helped us!