Skip to content

Commit

Permalink
feat: Add TS support to generate data
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Apr 6, 2024
1 parent 2a90c1a commit 62e84cb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/Commands/Generator/MakeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Spatie\TypeScriptTransformer\Attributes\Optional as TypeScriptOptional;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use XtendPackages\RESTPresenter\Concerns\WithTypeScriptGenerator;
use XtendPackages\RESTPresenter\Contracts\TypeScriptGeneratorContract;

use function Laravel\Prompts\select;

#[AsCommand(name: 'rest-presenter:make-data')]
class MakeData extends GeneratorCommand
class MakeData extends GeneratorCommand implements TypeScriptGeneratorContract
{
use WithTypeScriptGenerator;

protected $name = 'rest-presenter:make-data';

protected $description = 'Create a new data class';
Expand Down Expand Up @@ -84,20 +89,23 @@ protected function buildResourceReplacements(): array
protected function transformFieldProperties(array $fields): string
{
return collect($fields)->map(function (array $fieldProperties, string $field) {
$propertyType = match ($fieldProperties['type']) {
$fieldType = strtolower($fieldProperties['type']);
$propertyType = match ($fieldType) {
'int', 'integer', 'bigint' => 'int',
'tinyint' => 'bool',
'timestamp', 'datetime' => 'Carbon | Optional | null',
'timestamp', 'datetime' => 'Carbon',
'json' => 'array',
default => 'string',
};

$nullable = ! ($fieldProperties['notnull'] === 1);
if ($nullable && $propertyType !== 'Carbon | Optional | null') {

if ($nullable) {
$propertyType = '?' . $propertyType;
}
$tsOptional = $nullable ? "#[TypeScriptOptional]\n\t\t" : '';

return "public {$propertyType} \${$field}";
return $tsOptional . "public {$propertyType} \${$field}";
})->implode(",\n\t\t");
}

Expand Down
3 changes: 2 additions & 1 deletion src/Commands/Generator/stubs/new/data.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ namespace {{ namespace }};

use Carbon\Carbon;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Optional;
use Spatie\TypeScriptTransformer\Attributes\Optional as TypeScriptOptional;

/** @typescript */
class {{ class }} extends Data
{
public function __construct(
Expand Down
8 changes: 8 additions & 0 deletions src/Concerns/WithTypeScriptGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace XtendPackages\RESTPresenter\Concerns;

trait WithTypeScriptGenerator
{

}
7 changes: 7 additions & 0 deletions src/Contracts/TypeScriptGeneratorContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace XtendPackages\RESTPresenter\Contracts;

interface TypeScriptGeneratorContract
{
}

0 comments on commit 62e84cb

Please sign in to comment.