Skip to content

Commit fbd5437

Browse files
author
Mads Møller
committed
add readme
1 parent 50fc82d commit fbd5437

File tree

1 file changed

+179
-1
lines changed

1 file changed

+179
-1
lines changed

README.md

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,183 @@
55
[![Code Coverage](https://scrutinizer-ci.com/g/Napp/apicore/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Napp/apicore/?branch=master)
66
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
77

8-
*Still in development*
8+
Use as a foundation for APIs.
9+
10+
## Features
11+
12+
* Full request cycle
13+
* APU auth guard
14+
* Transform request input based on model api mapping
15+
* Validate transformed data
16+
* Transform response output (with support for nested relationships using `TransformAware`)
17+
* Correct HTTP responses backed into ApiController
18+
* Exception handling with two renderers (dev and prod)
19+
* Standard Exceptions
20+
* ETag middleware for cache responses (Not Modified 304)
21+
* Internal Router for internal api requests
22+
* API Proxy to use easy request handling
23+
24+
25+
## Usage
26+
27+
### Transform Mapping
28+
29+
Being able to hide database fields from the outside exposed API. With auto type casting.
30+
31+
```php
32+
<?php
33+
34+
use Illuminate\Database\Eloquent\Model;
35+
36+
class Post extends Model
37+
{
38+
/**
39+
* @var array
40+
*/
41+
public $apiMapping = [
42+
'id' => ['newName' => 'id', 'dataType' => 'int'],
43+
'image' => ['newName' => 'image', 'dataType' => 'string'],
44+
'title' => ['newName' => 'name', 'dataType' => 'string'],
45+
'description' => ['newName' => 'desc', 'dataType' => 'string'],
46+
'created_at' => ['newName' => 'createdAt', 'dataType' => 'datetime'],
47+
'published' => ['newName' => 'published', 'dataType' => 'boolean'],
48+
];
49+
}
50+
51+
```
52+
53+
54+
### Factory
55+
56+
Using a factory pattern.
57+
58+
```php
59+
<?php
60+
61+
namespace App\Posts\Factory;
62+
63+
use App\Posts\Models\Post;
64+
use Napp\Core\Api\Validation\ValidateTrait;
65+
66+
class PostFactory
67+
{
68+
use ValidateTrait;
69+
70+
/**
71+
* @param array $attributes
72+
* @param bool $validate
73+
* @return Post
74+
* @throws \Napp\Core\Api\Exceptions\Exceptions\ValidationException
75+
*/
76+
public static function create(array $attributes, $validate = true): Post
77+
{
78+
if (true === $validate) {
79+
static::validate($attributes, PostValidationRules::$createRules);
80+
}
81+
82+
return new Post($attributes);
83+
}
84+
}
85+
86+
87+
```
88+
89+
90+
### Requests
91+
92+
Extending the `ApiRequest` will automatically transform the input and validate it if Laravel rules are defined.
93+
94+
```php
95+
<?php
96+
97+
namespace App\Posts\Request;
98+
99+
use App\Posts\Factory\PostValidationRules;
100+
use App\Posts\Transformer\PostTransformer;
101+
use Napp\Core\Api\Requests\ApiRequest;
102+
use Napp\Core\Api\Transformers\TransformerInterface;
103+
104+
class StorePostRequest extends ApiRequest
105+
{
106+
/**
107+
* @return array
108+
*/
109+
public function rules(): array
110+
{
111+
return PostTransformer::$createRules;
112+
}
113+
114+
/**
115+
* @return TransformerInterface
116+
*/
117+
protected function getTransformer(): TransformerInterface
118+
{
119+
return app(PostTransformer::class);
120+
}
121+
}
122+
```
123+
124+
125+
### API Controllers
126+
127+
API Controllers can use the requests, the factory for creating a model, transforming the output and finally deliver the correct reponse.
128+
129+
```php
130+
<?php
131+
132+
namespace App\Posts\Controllers\Api;
133+
134+
use App\Posts\Factory\PostFactory;
135+
use App\Posts\Transformer\PostTransformer;
136+
use Napp\Core\Api\Controllers\ApiController;
137+
138+
class PostController extends ApiController
139+
{
140+
public function show(int $id, Request $request, PostTransformer $transformer): JsonResponse
141+
{
142+
$post = $this->postRepository->find($id);
143+
if (null === $post) {
144+
return $this->responseNotFound();
145+
}
146+
147+
return $this->respond($transformer->transformOutput($post));
148+
}
149+
150+
public function store(StorePostRequest $request, PostTransformer $transformer): JsonResponse
151+
{
152+
if (/* some logic */) {
153+
return $this->responseUnauthorized();
154+
}
155+
156+
$post = PostFactory::create($request->validated(), false);
157+
158+
return $this->responseCreated($transformer->transformOutput($post));
159+
}
160+
}
161+
162+
```
163+
164+
165+
### Internal router
166+
167+
Using the Internal router to request APIs.
168+
169+
```php
170+
<?php
171+
172+
use Napp\Core\Api\Controllers\ApiInternalController;
173+
174+
class MyController extends ApiInternalController
175+
{
176+
public function someImportantAction()
177+
{
178+
// using API get/post/put/delete
179+
$data = $this->get('/api/v1/some/route');
180+
$stuff = $this->post('/api/v1/new/stuff', $data);
181+
182+
return view('my.view', compact('stuff'));
183+
}
184+
}
185+
186+
```
9187

0 commit comments

Comments
 (0)