prsw/typeapi-bundle

TypeApi Impelementation For Symfony

dev-main 2024-09-29 13:25 UTC

This package is auto-updated.

Last update: 2024-09-29 13:25:44 UTC


README

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require prsw/typeapi-bundle

Applications that don't use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require prsw/typeapi-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    PRSW\TypeApiBundle\TypeApiBundle::class => ['all' => true],
];

Usage

Add TypeApi routes in config/routes.yml

typeapi:
    resource:
        path: .
    type: typeapi

Now you can use TypeApi attribute to define your API definition in your class, you mush add TypeApi attribute in your class in order to autoload them in symfony routes.

<?php
declare(strict_types=1);

namespace App\Api;

use App\Dto\Hello;
use App\Dto\Payload;
use PRSW\TypeApiBundle\Attributes\Path;
use PRSW\TypeApiBundle\Attributes\TypeApi;
use PSX\Api\Attribute\Body;
use PSX\Api\Attribute\Post;

#[TypeApi]
final class PostCollection
{
    #[Post]
    #[Path('/hello')]
    public function hello(
        #[Body]
        Payload $payload
    ): Hello {
        return Hello::create($payload->name, '', $payload->id);
    }
}

All is setup to verify route already registered or not you check via ./bin/console debug:router