codexsoft/transmission-ts-converter

适用于CodexSoft Transmission库构建HTTP API的TypeScript转换器

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

This package is auto-updated.

Last update: 2024-09-15 02:57:49 UTC


README

此库提供了一种将Transmission元素转换为typescript接口(递归)的方法。它支持引用。

安装

composer require codexsoft/transmission-ts-converter

扩展

默认情况下,只能转换基本元素类:CollectionElementJsonElementStringElementNumberElementBoolElementScalarElement。您可以添加自己的转换器(每个都需要扩展CodexSoft\Transmission\Typescript\Converters\AbstractElementTsConverter

$toTs = (new TransmissionToTypescriptConverter());
$toTs->addKnownConverter(MyElement::class => MyElementTsConverter::class);

使用方法

使用此实用程序,您可以生成typescript的SDK。对于实现了CodexSoft\Transmission\OpenApi3\OpenApi3OperationInterface的控制器所在的目录,可以轻松实现整个API的生成器。

// ...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');
    });
}
    

测试

php ./vendor/bin/phpunit