gigerit / laravel-morphmap-js-generator
Generate frontend JavaScript/TypeScript constants from Laravel morph map configuration
Package info
github.com/gigerIT/laravel-morphmap-js-generator
pkg:composer/gigerit/laravel-morphmap-js-generator
Requires
- php: ^8.0
- laravel/framework: ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Generate frontend JavaScript/TypeScript constants from your Laravel application's morph map configuration. Keep your frontend morph types in sync with your Laravel models automatically.
Installation
You can install the package via composer:
composer require gigerit/laravel-morphmap-js-generator --dev
The package will automatically register itself with Laravel.
Usage
After installation, you can generate the JavaScript/TypeScript constants using the artisan command:
# Generate JavaScript file php artisan morphmap:generate-js # Generate TypeScript file php artisan morphmap:generate-js --ts # Specify custom output path php artisan morphmap:generate-js --path=resources/js/constants
Example
If your Laravel application has this morph map configuration:
// App/Providers/AppServiceProvider.php use Illuminate\Database\Eloquent\Relations\Relation; class AppServiceProvider extends ServiceProvider { public function boot() { Relation::morphMap([ 'tenant' => Tenant::class, 'user' => User::class, 1 => Client::class, 2 => Contact::class, ]); } }
With --ts, the command generates these TypeScript constants:
// morphMap.ts export const MORPH_MAP = { TENANT: 'tenant', USER: 'user', CLIENT: 1, CONTACT: 2 } as const; export const MORPH_MAP_MODELS = { [MORPH_MAP.TENANT]: 'Tenant', [MORPH_MAP.USER]: 'User', [MORPH_MAP.CLIENT]: 'Client', [MORPH_MAP.CONTACT]: 'Contact' } as const; export const getMorphMapModel = (morphMap: MorphMapValue): string => { return MORPH_MAP_MODELS[morphMap] || 'Unknown'; }; export type MorphMapValue = typeof MORPH_MAP[keyof typeof MORPH_MAP];
The helper accepts morph values such as 'user' or 1. Constant names such as
'USER' are only valid arguments if they are also morph values. Untyped callers
passing an unknown value retain the 'Unknown' runtime fallback.
Using in Your Frontend
import { MORPH_MAP, getMorphMapModel, type MorphMapValue } from './morphMap'; // Use constants if (type === MORPH_MAP.TENANT) { // Handle tenant case } // Get model name const modelName = getMorphMapModel(MORPH_MAP.CLIENT); // Returns 'Client' // TypeScript support function handleMorphable(type: MorphMapValue) { // Type-safe handling of morph types }
Features
- 🔄 Automatically syncs with Laravel's morph map configuration
- 📝 Generates JavaScript or TypeScript files
- 🎯 Type-safe with TypeScript support
- 🔍 Includes helper function for model name lookup
- 🎨 Converts numeric keys to readable constant names
- 📦 Zero configuration required
- 💻 Command output shows current mappings
Command Options
| Option | Description | Default |
|---|---|---|
--ts |
Generate TypeScript instead of JavaScript | false |
--path |
Custom output path for generated file | resources/js |
Best Practices
- Version Control: Add the generated file to your version control system to track changes.
- Build Process: Include the generation command in your build process to ensure sync:
{
"scripts": {
"prepare": "php artisan morphmap:generate-js --ts"
}
}
- Type Safety: Use TypeScript for better type checking and IDE support.
Development
Install PHP dependencies with composer install and the test compiler with
npm ci, then run npm test. The tests boot the package service provider,
execute morphmap:generate-js --ts, compile the generated files under strict
TypeScript settings, and execute the compiled consumers to verify lookups and
the runtime fallback. See the test workflow for
the tested PHP and Laravel versions.
Security
If you discover any security related issues, please email security@example.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.