-
-
Notifications
You must be signed in to change notification settings - Fork 41
Generate typescript definitions #102
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
Conversation
src/Support/TypeScriptCollector.php
Outdated
protected function shouldCollect(ReflectionClass $class): bool | ||
{ | ||
// Only collect classes that extend ValidatedDTO | ||
if (! $class->isSubclassOf(ValidatedDTO::class)) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the change, seems to work just as well with SimpleDTO.
@benbjurstrom can you just check if there's any code style issue to be fixed, please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the awesome contribution @benbjurstrom!!! 💪 🔥
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.Step 2:
Publish the
typescript-transformer
configphp artisan vendor:publish --provider="Spatie\LaravelTypeScriptTransformer\TypeScriptTransformerServiceProvider"
Step 3:
Register the packages TypeScriptCollector and TypeScriptTransformer in the
typescript-transformer
configStep 4:
Run the command to generate the TypeScript types.
By default the definitions are added here:
resources/types/generated.d.ts
.