Skip to content

Commit

Permalink
Merge pull request #14 from AndreasElia/feature/populated-form-data
Browse files Browse the repository at this point in the history
Populate formdata based on request classes
  • Loading branch information
tomirons authored Feb 17, 2021
2 parents d76e539 + bef8fa2 commit 246305c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ You can modify the `api-postman.php` config values:
- `structured` - If you want folders to be generated based on route names.
- `base_url` - The base URL for all of your endpoints.
- `auth_middleware` - The middleware which wraps your authenticated API routes.
- `enable_formdata` - Determines whether or not form data should be handled.
- `formdata` - The key/values to requests for form data dummy information.

## Usage

Expand Down
19 changes: 19 additions & 0 deletions config/api-postman.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@
],
],

/*
* Enable Form Data.
*
* Determines whether or not form data should be handled.
*/

'enable_formdata' => false,

/*
* Form Data.
*
* The key/values to requests for form data dummy information.
*/

'formdata' => [
// 'email' => '[email protected]',
// 'password' => 'changeme',
],

];
66 changes: 56 additions & 10 deletions src/ExportPostman.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace AndreasElia\PostmanGenerator;

use Closure;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Storage;
use ReflectionClass;
use ReflectionFunction;

class ExportPostman extends Command
{
Expand Down Expand Up @@ -40,8 +44,6 @@ public function handle(): void

$this->initStructure($filename);

$structured = $this->config['structured'];

if ($bearer) {
$this->structure['variable'][] = [
'key' => 'token',
Expand All @@ -57,6 +59,33 @@ public function handle(): void
continue;
}

$requestRules = [];

if ($this->config['enable_formdata']) {
$routeAction = $route->getAction();

if ($routeAction['uses'] instanceof Closure) {
$reflectionMethod = new ReflectionFunction($routeAction['uses']);
} else {
$routeData = explode('@', $routeAction['uses']);
$reflection = new ReflectionClass($routeData[0]);
$reflectionMethod = $reflection->getMethod($routeData[1]);
}

$firstParameter = $reflectionMethod->getParameters()[0] ?? false;

if ($firstParameter) {
$requestClass = $firstParameter->getType()->getName();
$requestClass = new $requestClass();

if ($requestClass instanceof FormRequest) {
$requestRules = $requestClass->rules();

$requestRules = array_keys($requestRules);
}
}
}

$routeHeaders = $this->config['headers'];

if ($bearer && in_array($this->config['auth_middleware'], $middleware)) {
Expand All @@ -66,13 +95,9 @@ public function handle(): void
];
}

$request = $this->makeItem($route, $method, $routeHeaders);
$request = $this->makeItem($route, $method, $routeHeaders, $requestRules);

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

if ($structured) {
if ($this->config['structured']) {
$routeNames = $route->action['as'] ?? null;

if (! $routeNames) {
Expand All @@ -92,6 +117,8 @@ public function handle(): void
$destination = end($routeNames);

$this->ensurePath($this->structure, $routeNames, $request, $destination);
} else {
$this->structure['item'][] = $request;
}
}
}
Expand Down Expand Up @@ -138,9 +165,9 @@ protected function ensurePath(array &$routes, array $segments, array $request, s
}
}

public function makeItem($route, $method, $routeHeaders)
public function makeItem($route, $method, $routeHeaders, $requestRules)
{
return [
$data = [
'name' => $route->uri(),
'request' => [
'method' => strtoupper($method),
Expand All @@ -151,6 +178,25 @@ public function makeItem($route, $method, $routeHeaders)
],
],
];

if ($requestRules) {
$ruleData = [];

foreach ($requestRules as $rule) {
$ruleData[] = [
'key' => $rule,
'value' => $this->config['formdata'][$rule] ?? null,
'type' => 'text',
];
}

$data['request']['body'] = [
'mode' => 'urlencoded',
'urlencoded' => $ruleData,
];
}

return $data;
}

protected function initStructure(string $filename): void
Expand Down

0 comments on commit 246305c

Please sign in to comment.