Skip to content
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

Add Custom Collection Name Option & Refactor Authentication Logic #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions config/api-postman.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@

'filename' => '{timestamp}_{app}_collection.json',

/*
|--------------------------------------------------------------------------
| Collection Name
|--------------------------------------------------------------------------
|
| The name for the collection in Postman.
|
*/

'collection_name' => '{app}',

/*
|--------------------------------------------------------------------------
| Structured
Expand Down
65 changes: 45 additions & 20 deletions src/Commands/ExportPostmanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AndreasElia\PostmanGenerator\Commands;

use AndreasElia\PostmanGenerator\Authentication\AuthenticationMethod;
use AndreasElia\PostmanGenerator\Exporter;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
Expand All @@ -15,39 +16,63 @@ class ExportPostmanCommand extends Command
{--basic= : The basic auth to use on your endpoints}';

/** @var string */
protected $description = 'Automatically generate a Postman collection for your API routes';
protected $description = "Automatically generate a Postman collection for your API routes";

protected ?AuthenticationMethod $authenticationMethod = null;

public function handle(Exporter $exporter): void
{
$filename = str_replace(
['{timestamp}', '{app}'],
[date('Y_m_d_His'), Str::snake(config('app.name'))],
config('api-postman.filename')
["{timestamp}", "{app}"],
[date("Y_m_d_His"), Str::snake(config("app.name"))],
config("api-postman.filename")
);

$collectionName = str_replace(
"{app}",
config("app.name"),
config("api-postman.collection_name")
);

config()->set('api-postman.authentication', [
'method' => $this->option('bearer') ? 'bearer' : ($this->option('basic') ? 'basic' : null),
'token' => $this->option('bearer') ?? $this->option('basic') ?? null,
$this->resolveAuth();
Copy link
Collaborator

@tomirons tomirons Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of breaking this out. I would like to keep it fluent.

$exporter
    ->to($filename)
    ->collectionName($collectionName)
    ->setAuthentication($this->resolveAuth())
    ->export();

What gets returned from resolveAuth is the instance of the class.


config()->set("api-postman.authentication", [
"method" => $this->authenticationMethod?->prefix() ?? null,
"token" => $this->authenticationMethod?->getToken() ?? null,
]);

$exporter
->to($filename)
->setAuthentication(value(function () {
if (filled($this->option('bearer'))) {
return new \AndreasElia\PostmanGenerator\Authentication\Bearer($this->option('bearer'));
}
->collectionName($collectionName)
->export();

if (filled($this->option('basic'))) {
return new \AndreasElia\PostmanGenerator\Authentication\Basic($this->option('basic'));
}
Storage::disk(config("api-postman.disk"))
->put("postman/" . $filename, $exporter->getOutput());

return null;
}))
->export();
$this->info("Postman Collection Exported: " . storage_path("app/postman/" . $filename));
}

protected function resolveAuth(): void
{
$optionDefault = config("api-postman.authentication.method");
$tokenDefault = config("api-postman.authentication.token");

$option = $this->option("bearer")
? "bearer"
: ($this->option("basic")
? "basic"
: $optionDefault);

Storage::disk(config('api-postman.disk'))
->put('postman/'.$filename, $exporter->getOutput());
if ($option === "bearer") {
$this->authenticationMethod = new \AndreasElia\PostmanGenerator\Authentication\Bearer(
$this->option("bearer") ?: $tokenDefault
);
}

$this->info('Postman Collection Exported: '.storage_path('app/postman/'.$filename));
if ($option === "basic") {
$this->authenticationMethod = new \AndreasElia\PostmanGenerator\Authentication\Basic(
$this->option("basic") ?: $tokenDefault
);
}
}
}
11 changes: 10 additions & 1 deletion src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Exporter

protected string $filename;

protected string $collectionName;

protected array $output;

private array $config;
Expand All @@ -28,6 +30,13 @@ public function to(string $filename): self
return $this;
}

public function collectionName(?string $collectionName = null): self
{
$this->collectionName = $collectionName ?? '';

return $this;
}

public function getOutput()
{
return json_encode($this->output);
Expand All @@ -50,7 +59,7 @@ protected function generateStructure(): array
],
],
'info' => [
'name' => $this->filename,
'name' => $this->collectionName ?: $this->filename,
'schema' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
],
'item' => [],
Expand Down
Loading