From f645e1cebc0b4aa3fd8285b34b3665064f7925b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller?= Date: Mon, 1 Jul 2019 14:35:30 +0200 Subject: [PATCH 1/3] update dependencies --- composer.json | 18 +++++++++--------- tests/ApiPaginatedTransformerTest.php | 2 +- tests/ApiRequestTest.php | 2 +- tests/ApiTransformerTest.php | 2 +- tests/TestCase.php | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 21991e0..195e9e0 100644 --- a/composer.json +++ b/composer.json @@ -25,14 +25,14 @@ "require": { "php" : ">=7.1.3", "ext-json": "*", - "illuminate/container": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/contracts": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/database": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/http": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/routing": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/support": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/validation": "~5.4.0|~5.5.0|~5.6.0", - "illuminate/pagination": "~5.4.0|~5.5.0|~5.6.0" + "illuminate/container": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/contracts": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/database": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/http": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/routing": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/support": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/validation": "~5.6.0|~5.7.0|~5.8.0", + "illuminate/pagination": "~5.6.0|~5.7.0|~5.8.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.7", @@ -41,7 +41,7 @@ "phpunit/phpcov": "^5.0", "squizlabs/php_codesniffer": "^3.1", "orchestra/testbench": "^3.6", - "orchestra/database": "^3.6", + "orchestra/database": "^3.7", "fzaninotto/faker": "^1.7" }, "extra": { diff --git a/tests/ApiPaginatedTransformerTest.php b/tests/ApiPaginatedTransformerTest.php index 07b4c48..11c1a73 100644 --- a/tests/ApiPaginatedTransformerTest.php +++ b/tests/ApiPaginatedTransformerTest.php @@ -22,7 +22,7 @@ class ApiPaginatedTransformerTest extends TestCase */ protected $faker; - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/ApiRequestTest.php b/tests/ApiRequestTest.php index 6d06e0e..6c48fe4 100644 --- a/tests/ApiRequestTest.php +++ b/tests/ApiRequestTest.php @@ -15,7 +15,7 @@ class ApiRequestTest extends TestCase */ protected $request; - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/ApiTransformerTest.php b/tests/ApiTransformerTest.php index b6ce537..149809a 100644 --- a/tests/ApiTransformerTest.php +++ b/tests/ApiTransformerTest.php @@ -19,7 +19,7 @@ class ApiTransformerTest extends TestCase */ protected $transformer; - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/TestCase.php b/tests/TestCase.php index a87af57..039c25d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,7 +17,7 @@ class TestCase extends \Orchestra\Testbench\TestCase * * @return void */ - public function setUp() + public function setUp(): void { parent::setUp(); From 50fc82d8d432e5a76917193bb51b321227a8f68d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller?= Date: Tue, 2 Jul 2019 08:34:46 +0200 Subject: [PATCH 2/3] update dependencies --- .travis.yml | 3 ++- composer.json | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index ff351e1..fef8c41 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: php php: - - 7.1 + - 7.2 + - 7.3 before_script: - travis_retry composer self-update diff --git a/composer.json b/composer.json index 195e9e0..e81fbdd 100644 --- a/composer.json +++ b/composer.json @@ -25,24 +25,24 @@ "require": { "php" : ">=7.1.3", "ext-json": "*", - "illuminate/container": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/contracts": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/database": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/http": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/routing": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/support": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/validation": "~5.6.0|~5.7.0|~5.8.0", - "illuminate/pagination": "~5.6.0|~5.7.0|~5.8.0" + "illuminate/container": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/contracts": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/database": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/http": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/routing": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/support": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/validation": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "illuminate/pagination": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.7", + "friendsofphp/php-cs-fixer": "^2.15", "mockery/mockery": "^1.0", - "phpunit/phpunit" : "^7.0", - "phpunit/phpcov": "^5.0", - "squizlabs/php_codesniffer": "^3.1", - "orchestra/testbench": "^3.6", - "orchestra/database": "^3.7", - "fzaninotto/faker": "^1.7" + "phpunit/phpunit" : "^8.0", + "phpunit/phpcov": "^6.0", + "squizlabs/php_codesniffer": "^3.4", + "orchestra/testbench": "^3.8", + "orchestra/database": "^3.8", + "fzaninotto/faker": "^1.8" }, "extra": { "laravel": { From fbd54372cd1f8b64f19488175311c4049e0f574c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller?= Date: Tue, 2 Jul 2019 09:15:16 +0200 Subject: [PATCH 3/3] add readme --- README.md | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3544d9a..8a0d278 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,183 @@ [![Code Coverage](https://scrutinizer-ci.com/g/Napp/apicore/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Napp/apicore/?branch=master) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) -*Still in development* +Use as a foundation for APIs. + +## Features + +* Full request cycle + * APU auth guard + * Transform request input based on model api mapping + * Validate transformed data + * Transform response output (with support for nested relationships using `TransformAware`) + * Correct HTTP responses backed into ApiController +* Exception handling with two renderers (dev and prod) +* Standard Exceptions +* ETag middleware for cache responses (Not Modified 304) +* Internal Router for internal api requests +* API Proxy to use easy request handling + + +## Usage + +### Transform Mapping + +Being able to hide database fields from the outside exposed API. With auto type casting. + +```php + ['newName' => 'id', 'dataType' => 'int'], + 'image' => ['newName' => 'image', 'dataType' => 'string'], + 'title' => ['newName' => 'name', 'dataType' => 'string'], + 'description' => ['newName' => 'desc', 'dataType' => 'string'], + 'created_at' => ['newName' => 'createdAt', 'dataType' => 'datetime'], + 'published' => ['newName' => 'published', 'dataType' => 'boolean'], + ]; +} + +``` + + +### Factory + +Using a factory pattern. + +```php +postRepository->find($id); + if (null === $post) { + return $this->responseNotFound(); + } + + return $this->respond($transformer->transformOutput($post)); + } + + public function store(StorePostRequest $request, PostTransformer $transformer): JsonResponse + { + if (/* some logic */) { + return $this->responseUnauthorized(); + } + + $post = PostFactory::create($request->validated(), false); + + return $this->responseCreated($transformer->transformOutput($post)); + } +} + +``` + + +### Internal router + +Using the Internal router to request APIs. + +```php +get('/api/v1/some/route'); + $stuff = $this->post('/api/v1/new/stuff', $data); + + return view('my.view', compact('stuff')); + } +} + +```