Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Generate TypeScript interfaces from Laravel Models & Resources

This resource:
```PHP

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/** @mixin \App\Models\Product */
Expand All @@ -30,10 +33,12 @@ class ProductResource extends JsonResource
will be converted into this interface:

```typescript
export interface ProductResourceType {
declare namespace App.Http.Resources {
export interface ProductResourceType {
id: number;
name: string;
hidden?: boolean;
}
}
```

Expand Down
29 changes: 22 additions & 7 deletions src/Actions/TranspileToTypescript.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,45 @@ public function __construct()
/** @param Collection<TypescriptType> $types */
public function handle(Collection $types)
{
$types->each(fn (TypescriptType $type) => $this->processType($type));
$lines = $this->lines;

$this->groupTypes($types)->each(function (Collection $groupedTypes, string $group) use (&$lines) {
$lines->push("declare namespace {$group} {");
$groupedTypes->each(fn (TypescriptType $type) => $this->processType($type));
$lines->push('}'.PHP_EOL);
});

return $this->lines->join(PHP_EOL);
}

private function processType(TypescriptType $type): void
/**
* @param Collection<TypescriptType> $types
* @return Collection<string, Collection<int, TypescriptType>>
*/
private function groupTypes(Collection $types): Collection
{
$this->lines->push("// {$type->getClass()}");
return $types->mapToGroups(function (TypescriptType $type) {
return [TypescriptType::determineNamespace($type->getClass()) => $type];
});
}

private function processType(TypescriptType $type): void
{
if ($type->listProperties()->isEmpty()) {
$this->lines->push("export type {$type->getName()} = any");
$this->lines->push("\texport type {$type->getName()} = any");

return;
}

$this->lines->push("export type {$type->getName()} = {");
$this->lines->push("\texport type {$type->getName()} = {");

$type->listProperties()->each(fn (TypescriptProperty $property) => $this->processProperty($property));

$this->lines->push('}');
$this->lines->push("\t}");
}

private function processProperty(TypescriptProperty $property): void
{
$this->lines->push("{$property->getName()}:{$property->getType()}");
$this->lines->push("\t\t{$property->getName()}: {$property->getType()};");
}
}
4 changes: 2 additions & 2 deletions src/Types/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function determineType(mixed $value)
is_float($value) => self::NUMBER,
is_array($value) => $this->processArray($value),
$value instanceof \BackedEnum => $this->processEnum($value),
$value instanceof ResourceCollection => TypescriptType::determineName($value->collects).'[]',
$value instanceof JsonResource => TypescriptType::determineName(get_class($value)),
$value instanceof ResourceCollection => TypescriptType::determineNamespace($value->collects).'.'.TypescriptType::determineName($value->collects).'[]',
$value instanceof JsonResource => TypescriptType::determineNamespace(get_class($value)).'.'.TypescriptType::determineName(get_class($value)),
$value instanceof Arrayable => $this->processArray($value->toArray()), // TODO: Test for this
is_object($value) => $this->processArray((array) $value),
default => self::UNKNOWN,
Expand Down
12 changes: 10 additions & 2 deletions src/Values/TypescriptType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ public function merge(TypescriptType $type, Collection $originalProperties): sel
return $this;
}

public static function determineName(string $class): string
public static function determineNamespace(string $className): string
{
$class = new ReflectionClass($class);
$namespace = explode('\\', $className);
array_pop($namespace);

return implode('.', $namespace);
}

public static function determineName(string $className): string
{
$class = new ReflectionClass($className);

return $class->getShortName().'Type';
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Actions/TranspileToTypescriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
$generated = (new TranspileToTypescript)->handle(collect([$type]));
$lines = explode(PHP_EOL, $generated);

expect($lines)->toContain('export type ProductResourceType = {');
expect($lines)->toContain('}');
expect($lines)->toContain("\texport type ProductResourceType = {");
expect($lines)->toContain("\t}");

$type->listProperties()->each(function (TypescriptProperty $property) use ($lines) {
expect($lines)->toContain("{$property->getName()}:{$property->getType()}");
expect($lines)->toContain("\t\t{$property->getName()}: {$property->getType()};");
});
});

Expand All @@ -35,5 +35,5 @@
$generated = (new TranspileToTypescript)->handle(collect([$type]));
$lines = explode(PHP_EOL, $generated);

expect($lines)->toContain('export type ProductResourceType = any');
expect($lines)->toContain("\texport type ProductResourceType = any");
});
2 changes: 1 addition & 1 deletion tests/Converters/ResourceConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
$property = $processed->listProperties()->first();

expect($property->getName())->toBe('data');
expect($property->getType())->toBe('ProductResourceType[]');
expect($property->getType())->toBe('Vagebond.Runtype.Tests.Fakes.Resources.ProductResourceType[]');
});

it('can convert resources that use the user bound to the request', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Types/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[-1, 'number'],
[1.1, 'number'],
[now(), 'string'],
[new ProductResource(new Product), 'ProductResourceType'],
[new ProductResource(new Product), 'Vagebond.Runtype.Tests.Fakes.Resources.ProductResourceType'],
[[1, 2, 3], 'number[]'],
[['name' => 'name', 'value' => 1], '{name:string,value:number}'],
[['name' => 'name', 'values' => ['name' => 'value']], '{name:string,values:{name:string}}'],
Expand Down