citizen63000/easy-api-bundle

Complete API bundle to create easily your API

Installs: 1 382

Dependents: 1

Suggesters: 0

Security: 0

Stars: 5

Watchers: 1

Forks: 3

Open Issues: 10

Type:symfony-bundle


README

Symfony bundle to easily make api, inspired by work of DarwinOnLine

CRUD controllers :

Exemple :

/**
 * @Route("/my-path", name="my_path")
 * @OA\Tag(name="MyEntity")
 */
class MyEntityCrudController extends AbstractApiController
{
    public const entityClass = MyEntity::class;
    public const entityCreateTypeClass = MyEntityType::class;
    public const entityUpdateTypeClass = MyEntityType::class;
    public const serializationGroups = ['my_entity_full'];
    public const listSerializationGroups = ['my_entity_light'];
    public const filterFields = [];
    public const filterSortFields = [];
    
    use CrudControllerTrait;
}

CRUD routing :

Example:

api_my_path:
  resource: "@AppBundle/Controller/MyContext/MyController.php"
  type: annotation

Configuration (everything is optionnal) :

easy_api:
  authentication: true
  user_class: AppBundle\Entity\User\User
  user_tracking :
    enable: false
    connection_history_class: AppBundle\Entity\User\ConnectionHistory
  inheritance:
    entity: 'App\Entity\AbstractEntity'
    entityReferential: 'App\Entity\AbstractReferential'
    form: 'App\Form\Type\AbstractCoreType'
    repository: 'App\Form\Type\AbstractRepository'
    controller: 'App\Controller\AbstractApiController'
    serialized_form: 'App\Form\Model\SerializedForm'
  tests:
    debug: true # true by default
    datetime_format: 'Y-m-d H:i:s' # DateTimeInterface::ATOM format by default

Form options for frontend apps :

You can specify some options in attr field of form fields:

  • group : to group fields
  • discriminator
  • other options you want to pass

API-DOC Annotations

You can use all annotations of the nelmio api bundle. The DoctrineAnnorationReader cannot read self and static constants but with this bundle you can use it as strings in two cases as long as the issue isn't closed :

In Nelmio model annotation:

    /**
     * Get entity.
     *
     * @OA\Annotations\Response(
     *     response=200,
     *     description="Successful operation",
     *     @OA\Annotations\Schema(
     *          ref=@Nelmio\ApiDocBundle\Annotation\Model(
     *              type="static::entityClass",
     *              groups={"static::serializationGroups"}
     *          )
     *      )
     * ),
     * @param Request $request
     *
     * @return Response
     */
    public function getAction(Request $request)
    {
        ...
    }

And in an annotation named "GetFormParameter" which allows you to generate api-doc of form fields as get parameters :

     /**
      * List entities.
      *
      * @EasyApiBundle\Annotation\GetFormParameter(type="static::entitySearchTypeClass")
      * ...
      */
    public function listAction(Request $request)
    {
        ...       
    }

Test framework

User assertion:

Define it in php static::$additionalAssessableFunctions and implement it, with good parameters :

static::$additionalAssessableFunctions = ['assertMyAssertion'];
...
/**
* @param $key The key in response
* @param $parameter The parameter passed (optionnal)
* @param $value The value in response
*/
protected static function assertMyAssertion($key, $parameter, $value): void
{
    $parameter = $parameter ?? 'John';
    $expected = "hello world $parameter";
    $errorMessage = "Invalid value for {$key} field: expected {$expected}, get {$value}";
    static::assertTrue($expected === $value, $errorMessage);
}

Performance optimization