ovrflo / api-platform-typescript-generator-bundle
这是一个用于为您的 API Platform 项目生成 TypeScript 类型的 Symfony 扩展包。它可以生成模型、端点、路由等。
dev-main
2024-09-27 13:07 UTC
Requires
- php: ^8.2
- api-platform/core: ^2.7 || ^3.1
- doctrine/orm: ^2.12
- psr/clock: ^1.0
- psr/container: ^1.0 || ^2.0
- symfony/console: ^6.0 || ^7.0
- symfony/event-dispatcher: ^6.0 || ^7.0
- symfony/filesystem: ^6.0 || ^7.0
- symfony/finder: ^6.0 || ^7.0
- symfony/framework-bundle: ^6.0 || ^7.0
- symfony/routing: ^6.0 || ^7.0
- symfony/string: ^6.0 || ^7.0
- symfony/translation: ^6.0 || ^7.0
This package is auto-updated.
Last update: 2024-09-27 13:07:37 UTC
README
此扩展包公开了一个命令,用于为您的 API Platform 资源生成 TypeScript 接口。它可以生成模型、枚举、API 端点、应用路由等的接口。
为什么?
在用 API Platform 启动新项目时,我发现自己编写了很多样板代码来让前端与 API 一起工作。寻找用于生成前端粘合代码的解决方案时,我发现没有东西可以表达 API Platform 提供的元数据的广度。另一个因素是现有解决方案往往专注于项目的启动,之后的使用有限。
此扩展包的目标是生成前端粘合代码,并在您开发应用程序时保持它与 API Platform 元数据的同步。
安装
composer require --dev ovrflo/api-platform-typescript-generator-bundle:dev-main
然后,将以下内容添加到 config/bundles.php
return [ // ... Ovrflo\ApiPlatformTypescriptGeneratorBundle\OvrfloApiPlatformTypescriptGeneratorBundle::class => ['dev' => true], ];
配置
# config/packages/ovrflo_api_platform_typescript_generator.yaml when@dev: ovrflo_api_platform_typescript_generator: output_dir: '%kernel.project_dir%/assets/api' model_metadata: namespaces: ['App\Entity']
使用方法
bin/console ovrflo:api-platform:typescript:generate
# or the watcher (requires nodejs and chokidar)
node ./vendor/.bin/generate_api_types_watch.js
生成的端点文件还依赖于项目中的一个名为 ApiMethods.ts
的文件,该文件导出一些用于执行 API 调用的函数。
Output --- When running the command, it will generate a bunch of files in the `config.output_dir` (default `assets/api`) directory. ```bash <output_dir>/interfaces/ApiTypes.ts # common interfaces for API platform <output_dir>/interfaces/Enum.ts # enum types <output_dir>/interfaces/<Entity|Resource>.ts # interfaces for the different entities discovered <output_dir>/endpoint/<Resource>.ts