owowagency/automated-api-docs

OWOW风格的自动化API文档。

0.3.6 2020-09-14 14:38 UTC

This package is auto-updated.

Last update: 2024-08-29 05:06:36 UTC


README

安装

安装和设置时间预计为5到10分钟。通过composer安装此包。

composer require owowagency/automated-api-docs

如果你使用Laravel >= 5.5,此包将自动添加到你的提供者列表中。如果使用较低版本,请将服务提供者添加到config/app.php中的providers数组中。

OwowAgency\AutomatedApiDocs\ServiceProvider::class,

你现在可以开始设置了。

此包附带一个配置文件。默认配置在大多数情况下应该很好。然而,你可以自由地更改它。要发布配置文件,请运行以下命令

php artisan vendor:publish --provider="OwowAgency\AutomatedApiDocs\ServiceProvider" --tag="config"

设置

安装和可选配置后,我们需要设置此包。该包通过功能测试在HTTP调用中使用了钩子,以监控所有请求和响应。

首先,你需要使用特质来启用监控钩子。

use OwowAgency\AutomatedApiDocs\DocsGenerator;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, DocsGenerator;
}

其次,你需要注册一个关闭函数,这样包就知道何时将文档解析成解析器可读的自定义格式。

protected function setUp(): void
{
    parent::setUp();
    

    $config = config('automated-api-docs');

    register_shutdown_function(function () use ($config) {
        $this->exportDocsToJson($config);
    });
}

接下来,你需要将此文件添加到Laravel应用程序的根目录。如果此文件已存在于你的应用程序中,你可能只需要复制documentation任务。

最后,确保在你的部署脚本中添加以下命令envoy run documentation。例如,在Laravel Forge上。

你现在可以注册所有监控钩子了。你可以在调用路由之前调用monitor()方法来做到这一点。

public function test_foo()
{
    $user = factory(User::class)->create();
    
    $this->actingAs($user)->monitor()->post('/v1/posts', [
        'title' => 'Foo bar',
    ]);
}