yauhenko/rest-bundle

为 Symfony 6 生成 REST 和 TypeScript

安装次数: 1,116

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

0.9.1 2022-07-13 14:05 UTC

README

步骤 1: 运行

composer req yauhenko/rest-bundle

步骤 2: 在 config/routes/annotations.yaml 中添加两行

rest_bundle:
    resource: '@RestBundle/config/routes.yaml'

这将注册 /docs 路由

步骤 3: 在 src/Types.php 中创建 Types 类

<?php

namespace App;

use Yauhenko\RestBundle\TypesInterface;
use Yauhenko\RestBundle\Service\TypeScript;

class Types implements TypesInterface {

    public static function registerTypes(TypeScript $ts): void {
        // register custom types here (optional)
        $ts->registerTypeOf('TBadge', ['notifications', 'shop']);
        
        // register interfaces
        $ts->registerInterfacesFromDir(__DIR__ . '/Entity');
        $ts->registerInterfacesFromDir(__DIR__ . '/Models');
    }
    
    public static function codePostProcessor(string $code): string {
        // change generated TypeScript code here (optional)	
        return $code;
    }

}

您可以通过在 config/services.yaml 中的 yauhenko.rest.types_class 参数更改 Types 类的路径

步骤 4(可选):配置 config/services.yaml

parameters:
    # Enable TypeScript generation (default: true)
    yauhenko.rest.ts_enabled: '%env(bool:API_TS_ENABLED)%'
    
    # Types class (default) 
    yauhenko.rest.types_class: 'App\Types'
    
    # Path to controllers (default) 
    yauhenko.rest.controllers_dir: '%kernel.project_dir%/src/Controller'