fumeapp / modeltyper

Generate TypeScript interfaces from Laravel Models
MIT License
136 stars 16 forks source link

Fix: incorrect logic in PR #75 merge #77

Closed starter-dev closed 4 months ago

starter-dev commented 4 months ago

Sorry about the incorrect logic on the previous PR #75. The logic has now been corrected so that the columns, mutators, and relationships will be properly overridden by custom interfaces.

In previous PR #75, if matched is found in columns, mutators or relationships, the custom interfaces collection will be copied and will forget its own item rather than forgetting the matched column, mutator, or relationship. In this update, the custom interfaces collection will not forget its own item so it will properly provide a typescript output with comment // overrides

Example:

<?php
class Form extends Model
{
    use HasFactory;

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'id' => 'integer',
        'note' => 'string',
        'is_approved' => 'boolean',
        'is_declined' => 'boolean',
    ];

    /**
     * Custom interfaces for  model typer (fume/modeltyper).
     */
    public array $interfaces = [
        'status' => [
            'type' => "'approved' | 'declined' | 'pending'",
        ],
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'status',
    ];

    /**
     * Get the status of the request.
     */
    protected function status(): Attribute
    {
        return new Attribute(
            get: function () {
                if ($this->is_approved) {
                    return 'approved';
                }

                if ($this->is_declined) {
                    return 'declined';
                }

                return 'pending';
            },
        );
    }
}

Before: before

After: after