codexsoft / transmission-ts-converter
Typescript converter for CodexSoft Transmission library for building HTTP API
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
pkg:composer/codexsoft/transmission-ts-converter
Requires
- php: ^8.1
- codexsoft/transmission-schema: ^2
Requires (Dev)
- phpunit/phpunit: ^9
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2025-10-15 05:18:50 UTC
README
This library provides a way to convert Transmission elements into typescript interfaces (recursively). It supports references.
Installation
composer require codexsoft/transmission-ts-converter
Extending
By default, only basic element classes can be converted: CollectionElement, JsonElement, StringElement, NumberElement, BoolElement, ScalarElement. You can add your own converters (each needs to extend CodexSoft\Transmission\Typescript\Converters\AbstractElementTsConverter)
$toTs = (new TransmissionToTypescriptConverter()); $toTs->addKnownConverter(MyElement::class => MyElementTsConverter::class);
Usage
Using this utility you can generate SDK for typescript. For given directory with controllers that implement CodexSoft\Transmission\OpenApi3\OpenApi3OperationInterface generator of whole API can be easily implemented.
// ...preparing Symfony Finder or whatever $endpointReflections = []; foreach ($finder->getIterator() as $fileInfo) { $fqnClassName = (string) 'App'.(new \Stringy\Stringy($fileInfo->getRealPath())) ->removeRight('.php') ->replace('/', "\\"); $reflectionClass = new \ReflectionClass($fqnClassName); if ($reflectionClass->isAbstract()) { continue; } if (!$reflectionClass->implementsInterface(OpenApi3OperationInterface::class)) { continue; } $endpointReflections[] = $reflectionClass; $toTs = (new TransmissionToTypescriptConverter()); /** * Set ref interface name generator */ $toTs->setCreateRefClosure(function(string $class) { $reflection = new \ReflectionClass($class); return (string) (new \Stringy\Stringy('I'.$reflection->getShortName()))->removeRight('Transformer'); }); }
Testing
php ./vendor/bin/phpunit