develings/api-x

Create a full-fledged API using a simple json file.

0.0.2 2022-02-18 08:22 UTC

README

Logo

Build Status Total Downloads Latest Stable Version License

BE CAREFUL

This package is still under heavy development and may contain breaking changes with every update.

About

Create a full-fledged API only using a simple JSON file.

Search

Search should be easy

{ "name": "string|search:like" },
{ "description": "string|search:like_left" },
{ "description": "string|search:like_right" },
{ "uuid": "string|search:equal" },

Simple example

{
    "name": "App",
    "version": "1.0",
    "description": "A demo application using the ApiX library",
    "endpoint": "/api/v1.0/",
    "authentication": "token:users,api_key",
    "events": true,
    "db": {
        "driver": "mariadb",
        "prefix": "app_test_"
    },
    "servers": [
        {
            "url": "http://app.test"
        },
        {
            "url": "https://someId.execute-api.eu-central-1.amazonaws.com/dev"
        }
    ],
    "api": [
        {
            "name": "device",
            "timestamps": true,
            "soft_deletes": true,
            "identifier": "uuid",
            "sort_key": "created_at",
            "per_page": 10,
            "fields": {
                "uuid": "string|primary|default:uuid",
                "device_id": "string:64|unique",
                "last_active_at": "datetime|index|on_update_fill:datetime",
                "device_user_id": "uuid|nullable",
                "api_key": "string|default:alphanumeric,36"
            },
            "relations": {
                "user": "belongsTo:users"
            }
        }
    ]
}

We currently support the normal laravel DB drivers.

This definition will create an OpenAPI specification route plus a migration for the device table including the endpoints for it.

All that needs to be done is to instantiate the API class.

Install

You just need to require the composer package, and you're done.

composer install develings/api-x

Create api.json in the root of your project.

php artisan api:make

Instantiate

// add this to config/app.php
'providers' => [
    ...
    \ApiX\ApiXServiceProvider::class,
],

// add the route (e.g. routes/web.php)
$api = new ApiX\ApiX(base_path('api.json'));
$api->setRoutes();

Fake data

Since we have all the definition we need from the api.json file, it's also possible to populate test data using the faker package. Coming soon

Planned features

Use route syntax to fetch code from PHP instead of api.json. Eg:

{
    "api": [
        "@App\\ApiX\\DeviceAPI",
        "@App\\ApiX\\UserAPI:getDefinition",
        {
            "name": "company",
            "fields": "@App\\ApiX\\CompanyAPI:getFields"
        }
    ]
}