Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate typescript definitions #102

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

benbjurstrom
Copy link

@benbjurstrom benbjurstrom commented Feb 11, 2025

Related to #99

This pull request adds collector and transformer classes which can be used with the spatie/laravel-typescript-transformer package to generate typescript definitions from ValidatedDTO Classes.

This is especially useful when passing ValidatedDTOs through Inertia.js to a typescript based frontend.

Since this feature won't be used by everyone the spatie/typescript-transformer package is not added as a required dependency. Therefore to generate typescript definitions the user would need to complete the following steps.

Step 1:

Install the spatie/typescript-transformer package.

composer require spatie/typescript-transformer

Step 2:

Publish the typescript-transformer config

php artisan vendor:publish --provider="Spatie\LaravelTypeScriptTransformer\TypeScriptTransformerServiceProvider"

Step 3:

Register the packages TypeScriptCollector and TypeScriptTransformer in the typescript-transformer config

//config/typescript-transformer.php
   ...
    /*
     * Collectors will search for classes in the `auto_discover_types` paths and choose the correct
     * transformer to transform them. By default, we include a DefaultCollector which will search
     * for @typescript annotated and #[TypeScript] attributed classes to transform.
     */

    'collectors' => [
        Spatie\TypeScriptTransformer\Collectors\DefaultCollector::class,
        Spatie\TypeScriptTransformer\Collectors\EnumCollector::class,
+       WendellAdriel\ValidatedDTO\Support\TypeScriptCollector,
    ],

    /*
     * Transformers take PHP classes(e.g., enums) as an input and will output
     * a TypeScript representation of the PHP class.
     */

    'transformers' => [
        Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
        Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
        Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer::class,
+       WendellAdriel\ValidatedDTO\Support\TypeScriptTransformer,
    ],
   ...

Step 4:

Run the command to generate the TypeScript types.

 php artisan typescript:transform

By default the definitions are added here: resources/types/generated.d.ts.

protected function shouldCollect(ReflectionClass $class): bool
{
// Only collect classes that extend ValidatedDTO
if (! $class->isSubclassOf(ValidatedDTO::class)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make this work with any of the DTOs by checking if it's a subclass of the SimpleDTO instead?


protected function canTransform(ReflectionClass $class): bool
{
return $class->isSubclassOf(ValidatedDTO::class);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make this work with any of the DTOs by checking if it's a subclass of the SimpleDTO instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants