https://github.com/multividas/api-responser
composer package to facilitates the process of structuring and generating API responses
https://github.com/multividas/api-responser
api-responser
Last synced: 5 months ago
JSON representation
composer package to facilitates the process of structuring and generating API responses
- Host: GitHub
- URL: https://github.com/multividas/api-responser
- Owner: multividas
- License: mit
- Created: 2023-05-02T12:42:53.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-13T16:49:36.000Z (over 1 year ago)
- Last Synced: 2025-07-14T00:09:39.584Z (11 months ago)
- Topics: api-responser
- Language: PHP
- Homepage: https://packagist.org/packages/multividas/api-responser
- Size: 32.2 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# API Responser
[](https://github.com/multividas/api-responser/actions/workflows/tests.yml)
[](https://packagist.org/packages/multividas/api-responser)
[](https://github.com/multividas/api-responser/blob/main/LICENSE)
Composer package to facilitates the process of structuring and generating API responses
## Installation
Require this package with composer.
```shell
composer require multividas/api-responser
```
## ServiceProvider:
**[Optional]** Adding the **ApiResponserServiceProvider** to the providers array in **config/app.php**
```php
\Multividas\ApiResponser\Providers\ApiResponserServiceProvider::class,
```
**[Optional]** To get **X-Application-Name** http response header, Copy the package config to your local config with the publish command:
```sh
php artisan vendor:publish --tag=api-responser-config
```
## Usage
```php
use \Multividas\ApiResponser\Traits\ApiResponser;
class Controller extends BaseController
{
use ApiResponser;
}
```
### ApiResponser Interface
Using the `ApiResponser` interface methods.
- `showAll()` method receives **`Collection|JsonResource`** as its param.
- `showOne()` method receives **`Model|JsonResource $instance`** as its param.
```php
use \Multividas\ApiResponser\Interfaces\ApiRepositoryInterface;
class PostsController extends Controller
{
public function __construct(
public ApiRepositoryInterface $apiRepository
) {
}
public function index(): JsonResponse
{
return $this->apiRepository->showAll(Post::all());
}
public function show(Post $post): JsonResponse
{
if (!$post instanceof Post) {
return $this->infoResponse('Item Not Found', 404, []);
}
return $this->apiRepository->showOne($post);
}
}
```
### ApiResponser Facade
Using the `ApiResponser` facade design pattern.
```php
use Multividas\ApiResponser\Facades\ApiResponser;
class PostsController extends Controller
{
public function index(): JsonResponse
{
return ApiResponser::showAll(Post::all());
}
public function show(string $postId): JsonResponse
{
$post = Post::find($postId);
if (!$post instanceof Post) {
return $this->infoResponse('Post Not Found', 404, (object)[]);
}
return ApiResponser::showOne($post);
}
}
```
This approach provides a cleaner and more organized way to interact with the `ApiRepositoryInterface` instance in your controller methods.
### Success Response
Successful response containing the requested data and an appropriate status code.
```json
{
"data": [],
"code": 200,
"meta": {}
}
```
Learn more: [Multividas API Responser](https://developers.multividas.com/rest/introduction/api-responser)
---
### Run PHPUnit tests
```sh
composer test
```
## 🤝 Contributing
Please read the [contributing guide](https://github.com/multividas/.github/blob/main/CONTRIBUTING.md).
## 🛡️ Security Issues
If you discover a security vulnerability within Multividas, we would appreciate your help in disclosing it to us responsibly, please check out our [security issues guidelines](https://github.com/multividas/.github/blob/main/SECURITY.md).
## 🛡️ License
Licensed under the [MIT license](https://github.com/multividas/.github/blob/main/LICENSE).
---
> Email: multividasdotcom@gmail.com