Skip to content

Commit 1c3fc54

Browse files
committed
laravel data & typescript: Add UserData class and configuration for TypeScript transformer
1 parent 557d9f6 commit 1c3fc54

File tree

3 files changed

+302
-0
lines changed

3 files changed

+302
-0
lines changed

app/Data/UserData.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Data;
4+
5+
use Spatie\LaravelData\Data;
6+
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
7+
8+
#[TypeScript]
9+
class UserData extends Data
10+
{
11+
public function __construct(
12+
public string $name,
13+
public string $email,
14+
public ?string $avatar,
15+
public ?string $email_verified_at,
16+
public string $created_at,
17+
public string $updated_at
18+
) {}
19+
}

config/data.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* The package will use this format when working with dates. If this option
6+
* is an array, it will try to convert from the first format that works,
7+
* and will serialize dates using the first format from the array.
8+
*/
9+
'date_format' => DATE_ATOM,
10+
11+
/*
12+
* When transforming or casting dates, the following timezone will be used to
13+
* convert the date to the correct timezone. If set to null no timezone will
14+
* be passed.
15+
*/
16+
'date_timezone' => null,
17+
18+
/*
19+
* It is possible to enable certain features of the package, these would otherwise
20+
* be breaking changes, and thus they are disabled by default. In the next major
21+
* version of the package, these features will be enabled by default.
22+
*/
23+
'features' => [
24+
'cast_and_transform_iterables' => false,
25+
26+
/*
27+
* When trying to set a computed property value, the package will throw an exception.
28+
* You can disable this behaviour by setting this option to true, which will then just
29+
* ignore the value being passed into the computed property and recalculate it.
30+
*/
31+
'ignore_exception_when_trying_to_set_computed_property_value' => false,
32+
],
33+
34+
/*
35+
* Global transformers will take complex types and transform them into simple
36+
* types.
37+
*/
38+
'transformers' => [
39+
DateTimeInterface::class => \Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer::class,
40+
\Illuminate\Contracts\Support\Arrayable::class => \Spatie\LaravelData\Transformers\ArrayableTransformer::class,
41+
BackedEnum::class => Spatie\LaravelData\Transformers\EnumTransformer::class,
42+
],
43+
44+
/*
45+
* Global casts will cast values into complex types when creating a data
46+
* object from simple types.
47+
*/
48+
'casts' => [
49+
DateTimeInterface::class => Spatie\LaravelData\Casts\DateTimeInterfaceCast::class,
50+
BackedEnum::class => Spatie\LaravelData\Casts\EnumCast::class,
51+
// Enumerable::class => Spatie\LaravelData\Casts\EnumerableCast::class,
52+
],
53+
54+
/*
55+
* Rule inferrers can be configured here. They will automatically add
56+
* validation rules to properties of a data object based upon
57+
* the type of the property.
58+
*/
59+
'rule_inferrers' => [
60+
Spatie\LaravelData\RuleInferrers\SometimesRuleInferrer::class,
61+
Spatie\LaravelData\RuleInferrers\NullableRuleInferrer::class,
62+
Spatie\LaravelData\RuleInferrers\RequiredRuleInferrer::class,
63+
Spatie\LaravelData\RuleInferrers\BuiltInTypesRuleInferrer::class,
64+
Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
65+
],
66+
67+
/*
68+
* Normalizers return an array representation of the payload, or null if
69+
* it cannot normalize the payload. The normalizers below are used for
70+
* every data object, unless overridden in a specific data object class.
71+
*/
72+
'normalizers' => [
73+
Spatie\LaravelData\Normalizers\ModelNormalizer::class,
74+
// Spatie\LaravelData\Normalizers\FormRequestNormalizer::class,
75+
Spatie\LaravelData\Normalizers\ArrayableNormalizer::class,
76+
Spatie\LaravelData\Normalizers\ObjectNormalizer::class,
77+
Spatie\LaravelData\Normalizers\ArrayNormalizer::class,
78+
Spatie\LaravelData\Normalizers\JsonNormalizer::class,
79+
],
80+
81+
/*
82+
* Data objects can be wrapped into a key like 'data' when used as a resource,
83+
* this key can be set globally here for all data objects. You can pass in
84+
* `null` if you want to disable wrapping.
85+
*/
86+
'wrap' => null,
87+
88+
/*
89+
* Adds a specific caster to the Symphony VarDumper component which hides
90+
* some properties from data objects and collections when being dumped
91+
* by `dump` or `dd`. Can be 'enabled', 'disabled' or 'development'
92+
* which will only enable the caster locally.
93+
*/
94+
'var_dumper_caster_mode' => 'development',
95+
96+
/*
97+
* It is possible to skip the PHP reflection analysis of data objects
98+
* when running in production. This will speed up the package. You
99+
* can configure where data objects are stored and which cache
100+
* store should be used.
101+
*
102+
* Structures are cached forever as they'll become stale when your
103+
* application is deployed with changes. You can set a duration
104+
* in seconds if you want the cache to clear after a certain
105+
* timeframe.
106+
*/
107+
'structure_caching' => [
108+
'enabled' => true,
109+
'directories' => [app_path('Data')],
110+
'cache' => [
111+
'store' => env('CACHE_STORE', env('CACHE_DRIVER', 'file')),
112+
'prefix' => 'laravel-data',
113+
'duration' => null,
114+
],
115+
'reflection_discovery' => [
116+
'enabled' => true,
117+
'base_path' => base_path(),
118+
'root_namespace' => null,
119+
],
120+
],
121+
122+
/*
123+
* A data object can be validated when created using a factory or when calling the from
124+
* method. By default, only when a request is passed the data is being validated. This
125+
* behaviour can be changed to always validate or to completely disable validation.
126+
*/
127+
'validation_strategy' => \Spatie\LaravelData\Support\Creation\ValidationStrategy::OnlyRequests->value,
128+
129+
/*
130+
* A data object can map the names of its properties when transforming (output) or when
131+
* creating (input). By default, the package will not map any names. You can set a
132+
* global strategy here, or override it on a specific data object.
133+
*/
134+
'name_mapping_strategy' => [
135+
'input' => null,
136+
'output' => null,
137+
],
138+
139+
/*
140+
* When using an invalid include, exclude, only or except partial, the package will
141+
* throw an exception. You can disable this behaviour by setting this option to true.
142+
*/
143+
'ignore_invalid_partials' => false,
144+
145+
/*
146+
* When transforming a nested chain of data objects, the package can end up in an infinite
147+
* loop when including a recursive relationship. The max transformation depth can be
148+
* set as a safety measure to prevent this from happening. When set to null, the
149+
* package will not enforce a maximum depth.
150+
*/
151+
'max_transformation_depth' => null,
152+
153+
/*
154+
* When the maximum transformation depth is reached, the package will throw an exception.
155+
* You can disable this behaviour by setting this option to true which will return an
156+
* empty array.
157+
*/
158+
'throw_when_max_transformation_depth_reached' => true,
159+
160+
/*
161+
* When using the `make:data` command, the package will use these settings to generate
162+
* the data classes. You can override these settings by passing options to the command.
163+
*/
164+
'commands' => [
165+
166+
/*
167+
* Provides default configuration for the `make:data` command. These settings can be overridden with options
168+
* passed directly to the `make:data` command for generating single Data classes, or if not set they will
169+
* automatically fall back to these defaults. See `php artisan make:data --help` for more information
170+
*/
171+
'make' => [
172+
173+
/*
174+
* The default namespace for generated Data classes. This exists under the application's root namespace,
175+
* so the default 'Data` will end up as '\App\Data', and generated Data classes will be placed in the
176+
* app/Data/ folder. Data classes can live anywhere, but this is where `make:data` will put them.
177+
*/
178+
'namespace' => 'Data',
179+
180+
/*
181+
* This suffix will be appended to all data classes generated by make:data, so that they are less likely
182+
* to conflict with other related classes, controllers or models with a similar name without resorting
183+
* to adding an alias for the Data object. Set to a blank string (not null) to disable.
184+
*/
185+
'suffix' => 'Data',
186+
],
187+
],
188+
189+
/*
190+
* When using Livewire, the package allows you to enable or disable the synths
191+
* these synths will automatically handle the data objects and their
192+
* properties when used in a Livewire component.
193+
*/
194+
'livewire' => [
195+
'enable_synths' => false,
196+
],
197+
];

config/typescript-transformer.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* The paths where typescript-transformer will look for PHP classes
6+
* to transform, this will be the `app` path by default.
7+
*/
8+
9+
'auto_discover_types' => [
10+
app_path(),
11+
],
12+
13+
/*
14+
* Collectors will search for classes in the `auto_discover_types` paths and choose the correct
15+
* transformer to transform them. By default, we include a DefaultCollector which will search
16+
* for @typescript annotated and #[TypeScript] attributed classes to transform.
17+
*/
18+
19+
'collectors' => [
20+
Spatie\TypeScriptTransformer\Collectors\DefaultCollector::class,
21+
Spatie\TypeScriptTransformer\Collectors\EnumCollector::class,
22+
],
23+
24+
/*
25+
* Transformers take PHP classes(e.g., enums) as an input and will output
26+
* a TypeScript representation of the PHP class.
27+
*/
28+
29+
'transformers' => [
30+
Spatie\LaravelTypeScriptTransformer\Transformers\SpatieStateTransformer::class,
31+
Spatie\TypeScriptTransformer\Transformers\EnumTransformer::class,
32+
// Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
33+
Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer::class,
34+
],
35+
36+
/*
37+
* In your classes, you sometimes have types that should always be replaced
38+
* by the same TypeScript representations. For example, you can replace a
39+
* Datetime always with a string. You define these replacements here.
40+
*/
41+
42+
'default_type_replacements' => [
43+
DateTime::class => 'string',
44+
DateTimeImmutable::class => 'string',
45+
Carbon\CarbonInterface::class => 'string',
46+
Carbon\CarbonImmutable::class => 'string',
47+
Carbon\Carbon::class => 'string',
48+
],
49+
50+
/*
51+
* The package will write the generated TypeScript to this file.
52+
*/
53+
54+
'output_file' => resource_path('js/types/generated.d.ts'),
55+
56+
/*
57+
* When the package is writing types to the output file, a writer is used to
58+
* determine the format. By default, this is the `TypeDefinitionWriter`.
59+
* But you can also use the `ModuleWriter` or implement your own.
60+
*/
61+
62+
'writer' => Spatie\TypeScriptTransformer\Writers\TypeDefinitionWriter::class,
63+
64+
/*
65+
* The generated TypeScript file can be formatted. We ship a Prettier formatter
66+
* out of the box: `PrettierFormatter` but you can also implement your own one.
67+
* The generated TypeScript will not be formatted when no formatter was set.
68+
*/
69+
70+
'formatter' => Spatie\TypeScriptTransformer\Formatters\PrettierFormatter::class,
71+
72+
/*
73+
* Enums can be transformed into types or native TypeScript enums, by default
74+
* the package will transform them to types.
75+
*/
76+
77+
'transform_to_native_enums' => false,
78+
79+
/*
80+
* By default, this package will convert PHP nullable properties to TypeScript
81+
* types using a `null` type union. Setting `transform_null_to_optional` will
82+
* make them optional instead.
83+
*/
84+
85+
'transform_null_to_optional' => false,
86+
];

0 commit comments

Comments
 (0)