Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jun 21, 2024
1 parent e987b0e commit 0ee5696
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 297 deletions.

This file was deleted.

15 changes: 14 additions & 1 deletion src/Laravel/LaravelDataTypeScriptTransformerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@

class LaravelDataTypeScriptTransformerExtension implements TypeScriptTransformerExtension
{
public function __construct(
protected array $customLazyTypes = [],
protected array $customDataCollections = [],
) {
}

public function enrich(TypeScriptTransformerConfigFactory $factory): void
{
$factory->transformer(DataClassTransformer::class);
$factory->extension(new LaravelTypeScriptTransformerExtension());

$factory->prependTransformer(new DataClassTransformer(
customLazyTypes: $this->customLazyTypes,
customDataCollections: $this->customDataCollections,
));

$factory->typesProvider(LaravelDataTypesProvider::class);
}
}
60 changes: 60 additions & 0 deletions src/Laravel/LaravelDataTypesProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Spatie\TypeScriptTransformer\Laravel;

use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Spatie\LaravelData\CursorPaginatedDataCollection;
use Spatie\LaravelData\PaginatedDataCollection;
use Spatie\TypeScriptTransformer\References\ClassStringReference;
use Spatie\TypeScriptTransformer\Support\TransformedCollection;
use Spatie\TypeScriptTransformer\Transformed\Transformed;
use Spatie\TypeScriptTransformer\TypeProviders\TypesProvider;
use Spatie\TypeScriptTransformer\TypeScript\TypeReference;
use Spatie\TypeScriptTransformer\TypeScript\TypeScriptAlias;
use Spatie\TypeScriptTransformer\TypeScript\TypeScriptGeneric;
use Spatie\TypeScriptTransformer\TypeScript\TypeScriptIdentifier;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

class LaravelDataTypesProvider implements TypesProvider
{
public function provide(TypeScriptTransformerConfig $config, TransformedCollection $types): void
{
$types->add(
$this->paginatedCollection(),
$this->cursorPaginatedCollection(),
);
}

protected function paginatedCollection(): Transformed
{
return new Transformed(
new TypeScriptAlias(
new TypeScriptGeneric(
new TypeScriptIdentifier('PaginatedDataCollection'),
[new TypeScriptIdentifier('TKey'), new TypeScriptIdentifier('TValue')],
),
new TypeReference(new ClassStringReference(LengthAwarePaginator::class))
),
new ClassStringReference(PaginatedDataCollection::class),
['Spatie', 'LaravelData'],
true,
);
}

protected function cursorPaginatedCollection(): Transformed
{
return new Transformed(
new TypeScriptAlias(
new TypeScriptGeneric(
new TypeScriptIdentifier('CursorPaginatedDataCollection'),
[new TypeScriptIdentifier('TKey'), new TypeScriptIdentifier('TValue')],
),
new TypeReference(new ClassStringReference(CursorPaginator::class))
),
new ClassStringReference(CursorPaginatedDataCollection::class),
['Spatie', 'LaravelData'],
true,
);
}
}
9 changes: 6 additions & 3 deletions src/Laravel/LaravelTypeScriptTransformerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Spatie\TypeScriptTransformer\Laravel;

use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use Spatie\TypeScriptTransformer\Laravel\Transformers\LaravelAttributedClassTransformer;
use Spatie\TypeScriptTransformer\Support\Extensions\TypeScriptTransformerExtension;
use Spatie\TypeScriptTransformer\TypeScript\TypeScriptIdentifier;
use Spatie\TypeScriptTransformer\Transformers\AttributedClassTransformer;
use Spatie\TypeScriptTransformer\TypeScript\TypeScriptString;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfigFactory;

Expand All @@ -14,8 +14,11 @@ class LaravelTypeScriptTransformerExtension implements TypeScriptTransformerExte
public function enrich(TypeScriptTransformerConfigFactory $factory): void
{
$factory
->replaceTransformer(
AttributedClassTransformer::class,
LaravelAttributedClassTransformer::class
)
->typesProvider(LaravelTypesProvider::class)
->replaceType(Collection::class, new TypeScriptIdentifier('Array'))
->replaceType(CarbonInterface::class, new TypeScriptString());
}
}
116 changes: 72 additions & 44 deletions src/Laravel/LaravelTypesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Spatie\TypeScriptTransformer\Laravel;

use Illuminate\Contracts\Pagination\CursorPaginator as CursorPaginatorInterface;
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorInterface;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Spatie\TypeScriptTransformer\References\ClassStringReference;
use Spatie\TypeScriptTransformer\Support\TransformedCollection;
use Spatie\TypeScriptTransformer\Transformed\Transformed;
Expand All @@ -27,66 +27,99 @@ class LaravelTypesProvider implements TypesProvider
{
public function provide(TypeScriptTransformerConfig $config, TransformedCollection $types): void
{
/** @todo We should only keep these types if they are referenced otherwise they arent't required to be transformed */
/** @todo writing types in phpdoc syntax would be a lot easier here */
$types->add(
$this->collection(),
$this->eloquentCollection(),
$this->lengthAwarePaginator(),
$this->lengthAwarePaginatorInterface(),
$this->cursorPaginator(),
$this->cursorPaginatorInterface(),
);
}

protected function collection(): Transformed
protected function lengthAwarePaginator(): Transformed
{
return new Transformed(
new TypeScriptAlias(
new TypeScriptGeneric(
new TypeScriptIdentifier('Collection'),
[new TypeScriptIdentifier('T')],
),
new TypeScriptGeneric(
new TypeScriptIdentifier('Array'),
[new TypeScriptIdentifier('T')],
new TypeScriptIdentifier('LengthAwarePaginator'),
[new TypeScriptIdentifier('TKey'), new TypeScriptIdentifier('TValue')],
),
new TypeScriptObject([
new TypeScriptProperty('data', new TypeScriptGeneric(
new TypeScriptIdentifier('Record'),
[new TypeScriptIdentifier('TKey'), new TypeScriptIdentifier('TValue')],
), ),
new TypeScriptProperty('links', new TypeScriptObject([
new TypeScriptProperty('url', new TypeScriptUnion([
new TypeScriptIdentifier('string'),
new TypeScriptIdentifier('null'),
])),
new TypeScriptProperty('label', new TypeScriptString()),
new TypeScriptProperty('active', new TypeScriptBoolean()),
])),
new TypeScriptProperty('meta', new TypeScriptObject([
new TypeScriptProperty('total', new TypeScriptNumber()),
new TypeScriptProperty('current_page', new TypeScriptNumber()),
new TypeScriptProperty('first_page_url', new TypeScriptString()),
new TypeScriptProperty('from', new TypeScriptUnion([
new TypeScriptNumber(),
new TypeScriptNull(),
])),
new TypeScriptProperty('last_page', new TypeScriptNumber()),
new TypeScriptProperty('last_page_url', new TypeScriptString()),
new TypeScriptProperty('next_page_url', new TypeScriptUnion([
new TypeScriptString(),
new TypeScriptNull(),
])),
new TypeScriptProperty('path', new TypeScriptString()),
new TypeScriptProperty('per_page', new TypeScriptNumber()),
new TypeScriptProperty('prev_page_url', new TypeScriptUnion([
new TypeScriptString(),
new TypeScriptNull(),
])),
new TypeScriptProperty('to', new TypeScriptUnion([
new TypeScriptNumber(),
new TypeScriptNull(),
])),
])),
]),
),
new ClassStringReference(Collection::class),
['Illuminate', 'Support'],
new ClassStringReference(LengthAwarePaginator::class),
['Illuminate'],
true,
);
}

protected function eloquentCollection(): Transformed
protected function lengthAwarePaginatorInterface(): Transformed
{
return new Transformed(
new TypeScriptAlias(
new TypeScriptGeneric(
new TypeScriptIdentifier('Collection'),
new TypeScriptIdentifier('LengthAwarePaginatorInterface'),
[new TypeScriptIdentifier('T')],
),
new TypeScriptGeneric(
new TypeReference(new ClassStringReference(Collection::class)),
new TypeReference(new ClassStringReference(LengthAwarePaginator::class)),
[new TypeScriptIdentifier('T')],
),
),
new ClassStringReference(EloquentCollection::class),
['Illuminate', 'Database', 'Eloquent', 'Collection'],
new ClassStringReference(LengthAwarePaginatorInterface::class),
['Illuminate'],
true,
);
}

protected function lengthAwarePaginator(): Transformed
protected function cursorPaginator(): Transformed
{
return new Transformed(
new TypeScriptAlias(
new TypeScriptGeneric(
new TypeScriptIdentifier('LengthAwarePaginator'),
[new TypeScriptIdentifier('T')],
new TypeScriptIdentifier('CursorPaginator'),
[new TypeScriptIdentifier('TKey'), new TypeScriptIdentifier('TValue')],
),
new TypeScriptObject([
new TypeScriptProperty('data', new TypeScriptGeneric(
new TypeScriptIdentifier('Array'),
[new TypeScriptIdentifier('T')],
new TypeScriptIdentifier('Record'),
[new TypeScriptIdentifier('TKey'), new TypeScriptIdentifier('TValue')],
), ),
new TypeScriptProperty('links', new TypeScriptObject([
new TypeScriptProperty('url', new TypeScriptUnion([
Expand All @@ -97,53 +130,48 @@ protected function lengthAwarePaginator(): Transformed
new TypeScriptProperty('active', new TypeScriptBoolean()),
])),
new TypeScriptProperty('meta', new TypeScriptObject([
new TypeScriptProperty('total', new TypeScriptNumber()),
new TypeScriptProperty('current_page', new TypeScriptNumber()),
new TypeScriptProperty('first_page_url', new TypeScriptString()),
new TypeScriptProperty('from', new TypeScriptUnion([
new TypeScriptNumber(),
new TypeScriptProperty('path', new TypeScriptString()),
new TypeScriptProperty('per_page', new TypeScriptNumber()),
new TypeScriptProperty('next_cursor', new TypeScriptUnion([
new TypeScriptString(),
new TypeScriptNull(),
])),
new TypeScriptProperty('last_page', new TypeScriptNumber()),
new TypeScriptProperty('last_page_url', new TypeScriptString()),
new TypeScriptProperty('next_page_url', new TypeScriptUnion([
new TypeScriptString(),
new TypeScriptNull(),
])),
new TypeScriptProperty('path', new TypeScriptString()),
new TypeScriptProperty('per_page', new TypeScriptNumber()),
new TypeScriptProperty('prev_page_url', new TypeScriptUnion([
new TypeScriptProperty('prev_cursor', new TypeScriptUnion([
new TypeScriptString(),
new TypeScriptNull(),
])),
new TypeScriptProperty('to', new TypeScriptUnion([
new TypeScriptNumber(),
new TypeScriptProperty('prev_page_url', new TypeScriptUnion([
new TypeScriptString(),
new TypeScriptNull(),
])),
])),
]),
),
new ClassStringReference(LengthAwarePaginator::class),
['Illuminate', 'Pagination'],
new ClassStringReference(CursorPaginator::class),
['Illuminate'],
true,
);
}

protected function lengthAwarePaginatorInterface(): Transformed
protected function cursorPaginatorInterface(): Transformed
{
return new Transformed(
new TypeScriptAlias(
new TypeScriptGeneric(
new TypeScriptIdentifier('LengthAwarePaginator'),
new TypeScriptIdentifier('CursorPaginatorInterface'),
[new TypeScriptIdentifier('T')],
),
new TypeScriptGeneric(
new TypeReference(new ClassStringReference(LengthAwarePaginator::class)),
new TypeReference(new ClassStringReference(CursorPaginator::class)),
[new TypeScriptIdentifier('T')],
),
),
new ClassStringReference(LengthAwarePaginatorInterface::class),
['Illuminate', 'Contracts', 'Pagination'],
new ClassStringReference(CursorPaginatorInterface::class),
['Illuminate'],
true,
);
}
Expand Down
Loading

0 comments on commit 0ee5696

Please sign in to comment.