codexsoft/transmission-ts-converter

Typescript converter for CodexSoft Transmission library for building HTTP API

v1.0.0 2022-02-14 21:34 UTC

This package is auto-updated.

Last update: 2024-10-15 03:08:03 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