Skip to content

Commit

Permalink
Merge pull request #213 from apiato/API-1212-Create-default-Apiato-co…
Browse files Browse the repository at this point in the history
…nfig

feat(config): create publishable default configs
  • Loading branch information
Mohammad-Alavi authored Nov 30, 2024
2 parents c88dc83 + 5d7b7b7 commit a27848f
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 0 deletions.
205 changes: 205 additions & 0 deletions config/apiato.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Enable / Disable Hashed ID
|--------------------------------------------------------------------------
*/
'hash-id' => env('HASH_ID', true),

'api' => [
/*
|--------------------------------------------------------------------------
| API URL
|--------------------------------------------------------------------------
*/
'url' => env('API_URL', 'http://localhost'),

/*
|--------------------------------------------------------------------------
| API Prefix
|--------------------------------------------------------------------------
*/
'prefix' => env('API_PREFIX', '/'),

/*
|--------------------------------------------------------------------------
| API Version Prefix
|--------------------------------------------------------------------------
*/
'enable_version_prefix' => true,

/*
|--------------------------------------------------------------------------
| Access Token Expiration Time
|--------------------------------------------------------------------------
|
| In Minutes. Default to 1,440 minutes = 1 day
|
*/
'expires-in' => env('API_TOKEN_EXPIRES', 1440),

/*
|--------------------------------------------------------------------------
| Refresh Token Expiration Time
|--------------------------------------------------------------------------
|
| In Minutes. Default to 43,200 minutes = 30 days
|
*/
'refresh-expires-in' => env('API_REFRESH_TOKEN_EXPIRES', 43200),

/*
|--------------------------------------------------------------------------
| Enable Disable API Debugging
|--------------------------------------------------------------------------
|
| If enabled, the Error Exception trace will be injected in the JSON
| response, and it will be logged in the default Log file.
|
*/
'debug' => env('API_DEBUG', false),

/*
|--------------------------------------------------------------------------
| Enable/Disable Implicit Grant
|--------------------------------------------------------------------------
*/
'enabled-implicit-grant' => env('API_ENABLE_IMPLICIT_GRANT', true),

/*
|--------------------------------------------------------------------------
| Rate Limit (throttle)
|--------------------------------------------------------------------------
|
| Attempts per minutes.
| `attempts` is the number of attempts per `expires` in minutes.
|
*/
'throttle' => [
'enabled' => env('GLOBAL_API_RATE_LIMIT_ENABLED', true),
'attempts' => env('GLOBAL_API_RATE_LIMIT_ATTEMPTS_PER_MIN', '30'),
'expires' => env('GLOBAL_API_RATE_LIMIT_EXPIRES_IN_MIN', '1'),
],
],

'requests' => [
/*
|--------------------------------------------------------------------------
| Allow Roles to access all Routes
|--------------------------------------------------------------------------
|
| Define a list of roles that do not need to go through the "hasAccess"
| check in Requests. These roles automatically pass this check. This is
| useful, if you want to make all routes accessible for admin users.
|
| Usage: ['admin', 'editor']
| Default: []
|
*/
'allow-roles-to-access-all-routes' => [
env('ADMIN_ROLE', 'admin'),
],

/*
|--------------------------------------------------------------------------
| Force Request Header to Contain header
|--------------------------------------------------------------------------
|
| By default, users can send request without defining the accept header and
| setting it to [ accept = application/json ].
| To force the users to define that header, set this to true.
| When set to true, a PHP exception will be thrown preventing users from access
| When set to false, the header will contain a warning message.
|
*/
'force-accept-header' => false,

/*
|--------------------------------------------------------------------------
| Force Valid Request Include Parameters
|--------------------------------------------------------------------------
|
| By default, users can request to include additional resources into the
| response by using the ?include=... query parameter. The requested top-level
| resource also responds with all available includes. However, the user may
| still request an invalid (i.e., not available) include parameter. This flag
| determines, how to proceed in such a case:
| When set to true, a PHP Exception will be thrown (default)
| When set to false, this invalid include will be skipped
|
*/
'force-valid-includes' => true,

/*
|--------------------------------------------------------------------------
| Use ETags
|--------------------------------------------------------------------------
|
| This option appends an "ETag" HTTP Header to the Response. This ETag is a
| calculated hash of the content to be delivered.
| Clients can add an "If-None-Match" HTTP Header to the Request and submit
| an (old) ETag. These ETags are validated. If they match (are the same),
| an empty BODY with HTTP STATUS 304 (not modified) is returned!
|
*/
'use-etag' => false,

'params' => [
// TODO: BC: remove this after removing its usage in ResponseTrait in Core
'filter' => 'fieldset',
],

/*
|--------------------------------------------------------------------------
| Sparse Fieldsets
|--------------------------------------------------------------------------
|
| Sparse Fieldsets are a feature of the JSON API spec that allows clients to request only a subset of the
| attributes for a specific resource type. This can be useful for improving performance by reducing the amount
| of data that needs to be transferred over the network.
|
| @see https://jsonapi.org/format/#fetching-sparse-fieldsets
|
*/
'sparse_fieldsets' => [
// The name of key in the request to where we should look for the fields to return.
'request_key' => 'fields',
],
],

'seeders' => [
/*
|--------------------------------------------------------------------------
| Special seeders for apiato:seed-deploy & apiato:seed-test commands
|--------------------------------------------------------------------------
|
*/
'deployment' => null,
'testing' => null,
],

'tests' => [
/*
|--------------------------------------------------------------------------
| In order to be able to create testing user in your tests using test helpers, tests needs to know
| the name of the user model.This is working by default but if you are using another
| user model you should update this config.
| This user model MUST have a factory defined.
|--------------------------------------------------------------------------
|
*/
'user-class' => null,

/*
|--------------------------------------------------------------------------
| In order to be able to create admin testing user in your tests using test helpers, tests needs to know
| the name of the admin state in user factory. This is working by default but if you are using another
| user model or you have changed the default admin state name you should update this config.
|--------------------------------------------------------------------------
|
*/
'user-admin-state' => 'admin',
],
];
12 changes: 12 additions & 0 deletions src/Commands/SeedDeploymentDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public function __construct()

public function handle(): void
{
if (!config('apiato.seeders.deployment')) {
$this->error('No Deployment Seeder Found, Please Check Your Config File.');

return;
}

if (!class_exists(config('apiato.seeders.deployment'))) {
$this->error('Deployment Seeder Class Not Found.');

return;
}

$this->call('db:seed', [
'--class' => config('apiato.seeders.deployment'),
]);
Expand Down
12 changes: 12 additions & 0 deletions src/Commands/SeedTestingDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public function __construct()

public function handle(): void
{
if (!config('apiato.seeders.testing')) {
$this->error('No Testing Seeder Found, Please Check Your Config File.');

return;
}

if (!class_exists(config('apiato.seeders.testing'))) {
$this->error('Testing Seeder Class Not Found.');

return;
}

$this->call('db:seed', [
'--class' => config('apiato.seeders.testing'),
]);
Expand Down
9 changes: 9 additions & 0 deletions src/Providers/ApiatoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function register(): void
parent::register();

$this->runLoaderRegister();

$this->mergeConfigFrom(
__DIR__ . '/../../config/apiato.php',
'apiato',
);
}

public function boot(): void
Expand All @@ -47,5 +52,9 @@ public function boot(): void

// Registering custom validation rules
$this->extendValidationRules();

$this->publishes([
__DIR__ . '/../../config/apiato.php' => app_path('Ship/Configs/apiato.php'),
], 'apiato-config');
}
}
5 changes: 5 additions & 0 deletions src/Traits/TestTraits/PhpUnit/TestAuthHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function getTestingUser(array|null $userDetails = null, array|null $acces
{
$this->createUserAsAdmin = $createUserAsAdmin;
$this->userClass = $this->userclass ?? config('apiato.tests.user-class');

if (!$this->userClass) {
throw new \RuntimeException('User class is not defined in the test class');
}

$this->userAdminState = config('apiato.tests.user-admin-state');

if (is_null($userDetails)) {
Expand Down

0 comments on commit a27848f

Please sign in to comment.