Skip to content

Commit

Permalink
fix usage of url in config
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Oct 2, 2022
1 parent d4b8e54 commit 7ffe0d4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
6 changes: 4 additions & 2 deletions config/scramble.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

return [
/*
* Your API server base URL: will be used as base URL in docs.
* Your API path. Full API base URL will be created using `url` helper: `url(config('scramble.api_path'))`.
* By default, all routes starting with this path will be added to the docs. If you need to change
* this behavior, you can add your custom routes resolver using `Scramble::routes()`.
*/
'api_base_url' => url('/api'),
'api_path' => 'api',

'info' => [
/*
Expand Down
4 changes: 2 additions & 2 deletions docs/usage/endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ title: Endpoint documentation
weight: 1
---

By default, all routes with `api` middleware are added to the documentation.
By default, all routes starting with `api` are added to the documentation.

To customize which routes are added to docs, you may provide your own route resolver function using `Scramble::route` in `boot` method of a service provider. For example, in your `AppServiceProvider`:
To customize which routes are added to docs, you can either modify `scramble.api_path` config value, or you may provide your own route resolver function using `Scramble::route` in `boot` method of a service provider. For example, in your `AppServiceProvider`:

```php
use Dedoc\Scramble\Scramble;
Expand Down
8 changes: 5 additions & 3 deletions docs/usage/info.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Documentation config
weight: 5
---

Scramble allows to customize API base URL and OpenAPI document's `info` block by publishing a config file.
Scramble allows to customize API path and OpenAPI document's `info` block by publishing a config file.

`info` block includes API version and API description. API description is rendered on the home page (`/docs/api`).

Expand All @@ -20,9 +20,11 @@ use Dedoc\Scramble\Http\Middleware\RestrictedDocsAccess;

return [
/*
* Your API server base URL: will be used as base URL in docs.
* Your API path. Full API base URL will be created using `url` helper: `url(config('scramble.api_path'))`.
* By default, all routes starting with this path will be added to the docs. If you need to change
* this behavior, you can add your custom routes resolver using `Scramble::routes()`.
*/
'api_base_url' => url('/api'),
'api_path' => 'api',

'info' => [
/*
Expand Down
10 changes: 7 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function __invoke()
->filter() // Closure based routes are filtered out for now, right here
->each(fn (Operation $operation) => $openApi->addPath(
Path::make(
str_replace(config('scramble.api_base_url', url('/api')).'/', '', url($operation->path))
(string) Str::of($operation->path)
->replaceFirst(config('scramble.api_path', 'api'), '')
->trim('/')
)->addOperation($operation)
))
->toArray();
Expand All @@ -58,7 +60,9 @@ private function makeOpenApi()
->setDescription(config('scramble.info.description', ''))
);

$openApi->addServer(Server::make(config('scramble.api_base_url', url('/api'))));
$openApi->addServer(Server::make(
url(config('scramble.api_path', 'api'))
));

return $openApi;
}
Expand Down Expand Up @@ -89,7 +93,7 @@ private function getRoutes(): Collection
return ! ($name = $route->getAction('as')) || ! Str::startsWith($name, 'scramble');
})
->filter(function (Route $route) {
$routeResolver = Scramble::$routeResolver ?? fn (Route $route) => in_array('api', $route->gatherMiddleware());
$routeResolver = Scramble::$routeResolver ?? fn (Route $route) => Str::startsWith($route->uri, config('scramble.api_path', 'api'));

return $routeResolver($route);
})
Expand Down

0 comments on commit 7ffe0d4

Please sign in to comment.