Download the PHP package jpnut/laravel-typescript-codegen without Composer
On this page you can find all versions of the php package jpnut/laravel-typescript-codegen. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jpnut/laravel-typescript-codegen
More information about jpnut/laravel-typescript-codegen
Files in jpnut/laravel-typescript-codegen
Package laravel-typescript-codegen
Short Description Typescript Code Generation for Laravel
License MIT
Informations about the package laravel-typescript-codegen
Laravel Typescript Codegen
This package allows you to generate a typescript schema by getting type information from your Laravel routes and controllers.
For example, you can turn this:
into this:
Installation
You can install the package via composer:
Usage
To generate the schema, use the generate command with the desired file location as the only argument:
In order to generate a function for a given route, you must add the @code-gen
tag to the doc-block of the controller method which is used by the route.
This will attempt to get return type information from the doc-block @return
tag, or, if that is not present, the return type of the method. If there is no type information present, the return type defaults to any
.
Scalar types (e.g. string
, int
, etc.) will be transformed into the appropriate typescript scalar (e.g. string
, number
, etc.). If the method returns a class, then the class will be transformed into an interface containing all the public properties of the class.
For example, given the following class:
the generated interfaces will look as follows:
Working with Requests
Generating types for request properties (e.g. the request body) is slightly more involved. The request must implement JPNut\CodeGen\Contracts\CodeGenRequest
, and should be resolvable by Laravel's Service Container (typically this means extending Illuminate\Foundation\Http\FormRequest
). You should then create and tag methods which create a mapping from the request to the desired type. For example:
In this case the generated interfaces and methods would look similar to the following
Note that the body parameter is serialised for the request automatically. You can change this or serialise other properties by modifying the request_properties
config property.
Ignoring Properties
You may have public properties which you do not wish to generate types for. Properties can be ignored by including the @code-gen-ignore
tag:
Using a custom stub
By default, this package will generate the schema using a stub. This stub contains a request
method which is a wrapper for the fetch api and returns a promise containing the deserialised response data.
If you wish to customise this stub, you should first publish the config file
This will also create a copy of the default stub file located at resource_path('stubs/typescript-codegen-schema.stub')
. This file will then be used to generate the schema.
Using a custom interface/method writer
You may wish to change the generated output for interfaces and methods. For example, you might decide to rename the request
method within the stub. In this case, you should create your own Interface or Method writer and replace the included writer(s) with your version in the writers
array of the config. The writer(s) should implement the JPNut\CodeGen\Contracts\InterfaceWriterContract
and JPNut\CodeGen\Contracts\MethodWriterContract
interfaces respectively.
Override class resolution
You may wish to resolve certain classes into literals. This is particularly useful for classes which are not part of your codebase. To achieve this, include the class in the default_literals
array of the config. The class name should be the key, and the value should be an array of php literal types (e.g. string, int etc.)
For example, by default this package converts the Carbon\Carbon
class into a string. This is necessary since the return type of the jsonSerialize
method of the class is string|array
.
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-typescript-codegen with dependencies
ext-json Version *
illuminate/contracts Version ^8.0
illuminate/routing Version ^8.0
illuminate/support Version ^8.0
phpdocumentor/reflection-docblock Version ^5.0