Skip to content

Make headers configurable #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ composer require andreaselia/laravel-api-to-postman
Publish the config file:

```bash
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider" --tag="config"
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider" --tag="postman-config"
```

## Configuration
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@
"AndreasElia\\PostmanGenerator\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
"php": "^7.4|^8.0"
"php": "^7.4|^8.0",
"ext-json": "*",
"illuminate/config": "^8.0",
"illuminate/console": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/support": "^8.0",
"illuminate/routing": "^8.0"
},
"extra": {
"laravel": {
"providers": [
"AndreasElia\\PostmanGenerator\\PostmanGeneratorServiceProvider"
]
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
17 changes: 17 additions & 0 deletions config/api-postman.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@

'auth_middleware' => 'auth:api',

/*
* Headers.
*
* The headers applied to all routes within the collection.
*/

'headers' => [
[
'key' => 'Accept',
'value' => 'application/json',
],
[
'key' => 'Content-Type',
'value' => 'application/json',
],
],

];
61 changes: 34 additions & 27 deletions src/ExportPostman.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AndreasElia\PostmanGenerator;

use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Storage;

Expand All @@ -18,37 +19,31 @@ class ExportPostman extends Command
protected $router;

/** @var array */
protected $routes;
protected $structure;

public function __construct(Router $router)
/** @var array */
protected $config;

public function __construct(Router $router, Repository $config)
{
parent::__construct();

$this->router = $router;
$this->config = $config['api-postman'];
}

public function handle(): void
{
$bearer = $this->option('bearer') ?? false;

$this->routes = [
'variable' => [
[
'key' => 'base_url',
'value' => config('api-postman.base_url'),
],
],
'info' => [
'name' => $filename = date('Y_m_d_His').'_postman',
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
],
'item' => [],
];
$filename = date('Y_m_d_His').'_postman';

$this->initStructure($filename);

$structured = config('api-postman.structured');
$structured = $this->config['structured'];

if ($bearer) {
$this->routes['variable'][] = [
$this->structure['variable'][] = [
'key' => 'token',
'value' => $bearer,
];
Expand All @@ -62,14 +57,9 @@ public function handle(): void
continue;
}

$routeHeaders = [
[
'key' => 'Content-Type',
'value' => 'application/json',
],
];
$routeHeaders = $this->config['headers'];

if ($bearer && in_array(config('api-postman.auth_middleware'), $middleware)) {
if ($bearer && in_array($this->config['auth_middleware'], $middleware)) {
$routeHeaders[] = [
'key' => 'Authorization',
'value' => 'Bearer {{token}}',
Expand All @@ -79,7 +69,7 @@ public function handle(): void
$request = $this->makeItem($route, $method, $routeHeaders);

if (! $structured) {
$this->routes['item'][] = $request;
$this->structure['item'][] = $request;
}

if ($structured) {
Expand All @@ -91,12 +81,12 @@ public function handle(): void

$destination = end($routeNames);

$this->ensurePath($this->routes, $routeNames, $request, $destination);
$this->ensurePath($this->structure, $routeNames, $request, $destination);
}
}
}

Storage::put($exportName = "$filename.json", json_encode($this->routes));
Storage::put($exportName = "$filename.json", json_encode($this->structure));

$this->info("Postman Collection Exported: $exportName");
}
Expand Down Expand Up @@ -152,4 +142,21 @@ public function makeItem($route, $method, $routeHeaders)
],
];
}

protected function initStructure(string $filename): void
{
$this->structure = [
'variable' => [
[
'key' => 'base_url',
'value' => $this->config['base_url'],
],
],
'info' => [
'name' => $filename,
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
],
'item' => [],
];
}
}
21 changes: 19 additions & 2 deletions src/PostmanGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@

class PostmanGeneratorServiceProvider extends ServiceProvider
{
public function register()
/**
* Bootstrap any package services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/api-postman.php' => config_path('api-postman.php'),
], 'config');
], 'postman-config');
}

$this->commands(ExportPostman::class);
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../config/api-postman.php', 'api-postman'
);
}
}