fumeapp / modeltyper

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

Feature: Allow user to override or expand package mappings #74

Closed ayrtonandino closed 6 months ago

ayrtonandino commented 6 months ago

Add a new key custom_mappings to the config, allows the user to override existing mappings, or add new ones like Point from geometry, or missing ones like character from #73.

For this, a new Action was created GetMappings, This action takes the package mappings, merges them with the ones in config, and set all the keys to lowercase (because MapReturnType::class expects them to be lowercase).

public function __invoke(bool $setTimestampsToDate = false): array
{
      $mappings = TypescriptMappings::$mappings;

      if ($setTimestampsToDate) {
          $mappings['datetime'] = 'Date';
          $mappings['date'] = 'Date';
          $mappings['timestamp'] = 'Date';
      }

      return array_change_key_case(array_merge(
          $mappings,
          config('modeltyper.custom_mappings', []),
      ), CASE_LOWER);
}

It's initialized in Generator class, passed down to GenerateJsonOutput/GenerateCliOutput, then to WriteColumnAttribute and finally to MapReturnType.

It's not pretty, but it works, I would prefer to access those mappings and the commands arguments from a Facade.

A side effect is that you now can cast classes like Custom Casts globally, instead of per model.

Added some test, for GetMappings, and ComplexModel with Custom Casts casted from config.

Rename phpstan.neon to phpstan.neon.dist to allow per machine config, fix ignored error in phpstan.

Update github workflows, remove unsupported Laravel 9 from matrix, update actions (Warning: Node.js 16 actions are deprecated), use phpstan.neon.dist in phpstan.yml

Added character as string to TypescriptMappings, fixes #73.

Update readme.md

tcampbPPU commented 6 months ago

Thanks for the PR, looking into this now! (sorry for the delay)