shalvah пре 6 година
родитељ
комит
ced78cf066

+ 1 - 0
TODO.md

@@ -3,3 +3,4 @@
 - Add tests on output (deterministic)
 - Bring `bindings` outside of `response_calls`
 - Should `routes.*.apply.response_calls.headers` be replaced by `routes.*.apply.headers`?
+- Implement debug flag

+ 6 - 0
config/apidoc.php

@@ -13,6 +13,12 @@ return [
      */
     'router' => 'laravel',
 
+    /*
+     * The base URL to be used in examples and the Postman collection.
+     * By default, this will be the value of config('app.url').
+     */
+    'base_url' => config('app.url'),
+
     /*
      * Generate a Postman collection in addition to HTML docs.
      */

+ 3 - 0
docs/config.md

@@ -8,6 +8,9 @@ This is the file path where the generated documentation will be written to. Note
 ## `router`
 The router to use when processing your routes (can be Laravel or Dingo. Defaults to **Laravel**)
 
+## `base_url`
+The base URL to be used in examples and the Postman collection. By default, this will be the value of config('app.url').
+
 ## `postman`
 This package can automatically generate a Postman collection for your routes, along with the documentation. This section is where you can configure (or disable) that.
 

+ 1 - 1
resources/views/partials/example-requests/bash.blade.php

@@ -1,5 +1,5 @@
 ```bash
-curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
+curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
 @foreach($route['headers'] as $header => $value)
     -H "{{$header}}: {{$value}}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \
 @endif

+ 1 - 1
resources/views/partials/example-requests/javascript.blade.php

@@ -1,5 +1,5 @@
 ```javascript
-const url = new URL("{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['boundUri'], '/') }}");
+const url = new URL("{{ rtrim($baseUrl, '/') }}/{{ ltrim($route['boundUri'], '/') }}");
 @if(count($route['queryParameters']))
 
     let params = {

+ 1 - 1
resources/views/partials/example-requests/php.blade.php

@@ -1,7 +1,7 @@
 ```php
 
 $client = new \GuzzleHttp\Client();
-$response = $client->{{ strtolower($route['methods'][0]) }}("{{ $route['boundUri'] }}", [
+$response = $client->{{ strtolower($route['methods'][0]) }}("{{ rtrim($baseUrl, '/') . '/' . $route['boundUri'] }}", [
 @if(!empty($route['headers']))
     'headers' => [
     @foreach($route['headers'] as $header => $value)

+ 4 - 3
src/Commands/GenerateDocumentation.php

@@ -56,9 +56,9 @@ class GenerateDocumentation extends Command
         $this->docConfig = new DocumentationConfig(config('apidoc'));
         
         try {
-            URL::forceRootUrl(config('app.url'));
+            URL::forceRootUrl($this->docConfig->get('base_url'));
         } catch (\Exception $e) {
-            echo "Warning: Couldn't force base url as Lumen currently doesn't have the forceRootUrl method.\n";
+            echo "Warning: Couldn't force base url as your version of Lumen doesn't have the forceRootUrl method.\n";
             echo "You should probably double check URLs in your generated documentation.\n";
         }
         $usingDingoRouter = strtolower($this->docConfig->get('router')) == 'dingo';
@@ -106,6 +106,7 @@ class GenerateDocumentation extends Command
                 $route['output'] = (string) view('apidoc::partials.route')
                     ->with('route', $route)
                     ->with('settings', $settings)
+                    ->with('baseUrl', $this->docConfig->get('base_url'))
                     ->render();
 
                 return $route;
@@ -277,7 +278,7 @@ class GenerateDocumentation extends Command
      */
     private function generatePostmanCollection(Collection $routes)
     {
-        $writer = new CollectionWriter($routes);
+        $writer = new CollectionWriter($routes, $this->docConfig->get('base_url'));
 
         return $writer->getCollection();
     }

+ 13 - 2
src/Postman/CollectionWriter.php

@@ -13,19 +13,30 @@ class CollectionWriter
      */
     private $routeGroups;
 
+    /**
+     * @var string
+     */
+    private $baseUrl;
+
     /**
      * CollectionWriter constructor.
      *
      * @param Collection $routeGroups
      */
-    public function __construct(Collection $routeGroups)
+    public function __construct(Collection $routeGroups, $baseUrl)
     {
         $this->routeGroups = $routeGroups;
+        $this->baseUrl = $baseUrl;
     }
 
     public function getCollection()
     {
-        URL::forceRootUrl(config('app.url'));
+        try {
+            URL::forceRootUrl($this->baseUrl);
+        } catch (\Exception $e) {
+            echo "Warning: Couldn't force base url as your version of Lumen doesn't have the forceRootUrl method.\n";
+            echo "You should probably double check URLs in your generated Postman collection.\n";
+        }
 
         $collection = [
             'variables' => [],

+ 3 - 0
src/Tools/ResponseStrategies/ResponseCallStrategy.php

@@ -36,6 +36,9 @@ class ResponseCallStrategy
         try {
             $response = [$this->makeApiCall($request)];
         } catch (\Exception $e) {
+            echo "Response call failed for [". implode(',', $route->methods). "] {$route->uri}";
+            // TODO
+            // echo "Run this again with the --debug flag for details
             $response = null;
         } finally {
             $this->finish();

+ 2 - 2
tests/GenerateDocumentationTest.php

@@ -237,7 +237,7 @@ class GenerateDocumentationTest extends TestCase
         $domain = 'http://somedomain.test';
         RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription');
 
-        config(['app.url' => $domain]);
+        config(['apidoc.base_url' => $domain]);
         config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
         $this->artisan('apidoc:generate');
 
@@ -249,7 +249,7 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function generated_postman_collection_can_have_custom_url()
     {
-        Config::set('app.url', 'http://yourapp.app');
+        Config::set('apidoc.base_url', 'http://yourapp.app');
         RouteFacade::get('/api/test', TestController::class.'@withEndpointDescription');
         RouteFacade::post('/api/responseTag', TestController::class.'@withResponseTag');