Skip to content

Commit

Permalink
Moving headers to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSteveKing committed Aug 25, 2023
1 parent 920e57d commit bad2eb0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ composer require treblle/api-responses

This package is easy to use, it is designed to be used within your controllers to return API responses that are simple and standardized.

## Using the configuration

You can publish the configuration for this package using the following artisan command:

```bash
php artisan vendor:publish --tag=api-config
```

This will return the configuration file for this package. Currently, the configuration only covers headers used in responses.

```php
return [
'headers' => [
'default' => [
'Content-Type' => 'application/vnd.api+json',
],
'error' => [
'Content-Type' => 'application/problem+json',
],
],
];
```

The `HeaderFactory` that is used in the response classes will pull with `HeaderFactory::default()` or `HeaderFactory::error()` depending if you are returning an error or a response.

You can override the available headers using the configuration file. This is executed outside of any middleware you may be using - which will merge in relevant Headers as required, such as Rate Limiting and Cache headers you may have set.

## Returning a single model

Some API endpoints just need to return a single model, in this situation you should use the `ModelResponse` which accepts a `JsonResource` representation of your model.
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"phpstan/phpstan": "^1.10.32",
"roave/security-advisories": "dev-latest"
},
"extra": {
"laravel": {
"providers": [
"Treblle\\ApiResponses\\Providers\\PackageServiceProvider"
]
}
},
"scripts": {
"pint": [
"./vendor/bin/pint"
Expand Down
14 changes: 14 additions & 0 deletions config/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

return [
'headers' => [
'default' => [
'Content-Type' => 'application/vnd.api+json',
],
'error' => [
'Content-Type' => 'application/problem+json',
],
],
];
8 changes: 2 additions & 6 deletions src/Factories/HeaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class HeaderFactory
public static function default(array $headers = []): array
{
return array_merge(
[
'Content-Type' => 'application/vnd.api+json',
],
(array) config('api.headers.default'),
$headers,
);
}
Expand All @@ -32,9 +30,7 @@ public static function default(array $headers = []): array
public static function error(array $headers = []): array
{
return array_merge(
[
'Content-Type' => 'application/problem+json',
],
(array) config('api.headers.error'),
$headers,
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Providers/PackageServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Treblle\ApiResponses\Providers;

use Illuminate\Support\ServiceProvider;

final class PackageServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../../config/api.php' => config_path('api.php'),
], 'api-config');
}
}
}

0 comments on commit bad2eb0

Please sign in to comment.