Skip to content

Commit efe2894

Browse files
authored
Merge pull request #10 from AndreasElia/feature/header-configuration
Make headers configurable
2 parents f6f0c0f + f4a2199 commit efe2894

File tree

5 files changed

+81
-33
lines changed

5 files changed

+81
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ composer require andreaselia/laravel-api-to-postman
2222
Publish the config file:
2323

2424
```bash
25-
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider" --tag="config"
25+
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider" --tag="postman-config"
2626
```
2727

2828
## Configuration

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@
2121
"AndreasElia\\PostmanGenerator\\": "src/"
2222
}
2323
},
24-
"minimum-stability": "dev",
2524
"require": {
26-
"php": "^7.4|^8.0"
25+
"php": "^7.4|^8.0",
26+
"ext-json": "*",
27+
"illuminate/config": "^8.0",
28+
"illuminate/console": "^8.0",
29+
"illuminate/contracts": "^8.0",
30+
"illuminate/support": "^8.0",
31+
"illuminate/routing": "^8.0"
2732
},
2833
"extra": {
2934
"laravel": {
3035
"providers": [
3136
"AndreasElia\\PostmanGenerator\\PostmanGeneratorServiceProvider"
3237
]
3338
}
34-
}
39+
},
40+
"minimum-stability": "dev",
41+
"prefer-stable": true
3542
}

config/api-postman.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@
2828

2929
'auth_middleware' => 'auth:api',
3030

31+
/*
32+
* Headers.
33+
*
34+
* The headers applied to all routes within the collection.
35+
*/
36+
37+
'headers' => [
38+
[
39+
'key' => 'Accept',
40+
'value' => 'application/json',
41+
],
42+
[
43+
'key' => 'Content-Type',
44+
'value' => 'application/json',
45+
],
46+
],
47+
3148
];

src/ExportPostman.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AndreasElia\PostmanGenerator;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Config\Repository;
67
use Illuminate\Routing\Router;
78
use Illuminate\Support\Facades\Storage;
89

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

2021
/** @var array */
21-
protected $routes;
22+
protected $structure;
2223

23-
public function __construct(Router $router)
24+
/** @var array */
25+
protected $config;
26+
27+
public function __construct(Router $router, Repository $config)
2428
{
2529
parent::__construct();
2630

2731
$this->router = $router;
32+
$this->config = $config['api-postman'];
2833
}
2934

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

34-
$this->routes = [
35-
'variable' => [
36-
[
37-
'key' => 'base_url',
38-
'value' => config('api-postman.base_url'),
39-
],
40-
],
41-
'info' => [
42-
'name' => $filename = date('Y_m_d_His').'_postman',
43-
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
44-
],
45-
'item' => [],
46-
];
39+
$filename = date('Y_m_d_His').'_postman';
40+
41+
$this->initStructure($filename);
4742

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

5045
if ($bearer) {
51-
$this->routes['variable'][] = [
46+
$this->structure['variable'][] = [
5247
'key' => 'token',
5348
'value' => $bearer,
5449
];
@@ -62,14 +57,9 @@ public function handle(): void
6257
continue;
6358
}
6459

65-
$routeHeaders = [
66-
[
67-
'key' => 'Content-Type',
68-
'value' => 'application/json',
69-
],
70-
];
60+
$routeHeaders = $this->config['headers'];
7161

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

8171
if (! $structured) {
82-
$this->routes['item'][] = $request;
72+
$this->structure['item'][] = $request;
8373
}
8474

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

9282
$destination = end($routeNames);
9383

94-
$this->ensurePath($this->routes, $routeNames, $request, $destination);
84+
$this->ensurePath($this->structure, $routeNames, $request, $destination);
9585
}
9686
}
9787
}
9888

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

10191
$this->info("Postman Collection Exported: $exportName");
10292
}
@@ -152,4 +142,21 @@ public function makeItem($route, $method, $routeHeaders)
152142
],
153143
];
154144
}
145+
146+
protected function initStructure(string $filename): void
147+
{
148+
$this->structure = [
149+
'variable' => [
150+
[
151+
'key' => 'base_url',
152+
'value' => $this->config['base_url'],
153+
],
154+
],
155+
'info' => [
156+
'name' => $filename,
157+
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
158+
],
159+
'item' => [],
160+
];
161+
}
155162
}

src/PostmanGeneratorServiceProvider.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,31 @@
66

77
class PostmanGeneratorServiceProvider extends ServiceProvider
88
{
9-
public function register()
9+
/**
10+
* Bootstrap any package services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
1015
{
1116
if ($this->app->runningInConsole()) {
1217
$this->publishes([
1318
__DIR__.'/../config/api-postman.php' => config_path('api-postman.php'),
14-
], 'config');
19+
], 'postman-config');
1520
}
1621

1722
$this->commands(ExportPostman::class);
1823
}
24+
25+
/**
26+
* Register any application services.
27+
*
28+
* @return void
29+
*/
30+
public function register()
31+
{
32+
$this->mergeConfigFrom(
33+
__DIR__.'/../config/api-postman.php', 'api-postman'
34+
);
35+
}
1936
}

0 commit comments

Comments
 (0)