Several cool features
- replace vars from collection by default - replace vars from environment file with -e - bearer auth header
This commit is contained in:
parent
c630af8795
commit
c4961b1238
42
README.md
42
README.md
@ -15,6 +15,8 @@ These formats are supported for now: `http`, `curl`, `wget`.
|
||||
## Supported features
|
||||
|
||||
* [collection schema **v2.1**](https://schema.postman.com/json/collection/v2.1.0/collection.json);
|
||||
* `Bearer` auth;
|
||||
* replace vars in requests by stored in collection and environment file;
|
||||
* export one or several collections (or even whole directories) into one or all of formats supported at the same time;
|
||||
* all headers (including disabled for `http`-format);
|
||||
* `json` body (forces header `Content-Type` to `application/json`);
|
||||
@ -22,12 +24,10 @@ These formats are supported for now: `http`, `curl`, `wget`.
|
||||
|
||||
## Planned features
|
||||
|
||||
- support as many as possible/necessary of authentication kinds (_currently no ones_);
|
||||
- support as many as possible/necessary of authentication kinds (_currently only `Bearer` supported_);
|
||||
- support as many as possible/necessary of body formats (_currently only `json` and `formdata`_);
|
||||
- documentation generation support (markdown) with responce examples (if present);
|
||||
- maybe some another convert formats (like httpie or something...);
|
||||
- replace `{{vars}}` from folder;
|
||||
- replace `{{vars}}` from environment;
|
||||
- better logging;
|
||||
- tests, phpcs, psalm, etc.;
|
||||
- web version.
|
||||
@ -52,29 +52,33 @@ export PATH="$PATH:~/.config/composer/vendor/bin"
|
||||
$ pm-convert --help
|
||||
Postman collection converter
|
||||
Usage:
|
||||
./pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
php pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
composer pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
./vendor/bin/pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
./pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
php pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
composer pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
./vendor/bin/pm-convert -f|-d PATH -o OUTPUT_PATH [ARGUMENTS] [FORMATS]
|
||||
|
||||
Possible ARGUMENTS:
|
||||
-f, --file - a PATH to single collection located in PATH to convert from
|
||||
-d, --dir - a directory with collections located in COLLECTION_FILEPATH to convert from
|
||||
-o, --output - a directory OUTPUT_PATH to put results in
|
||||
-p, --preserve - do not delete OUTPUT_PATH (if exists)
|
||||
-h, --help - show this help message and exit
|
||||
-v, --version - show version info and exit
|
||||
-f, --file - a PATH to single collection located in PATH to convert from
|
||||
-d, --dir - a directory with collections located in COLLECTION_FILEPATH to convert from
|
||||
-o, --output - a directory OUTPUT_PATH to put results in
|
||||
-e, --env - use environment file with variable values to replace in request
|
||||
-p, --preserve - do not delete OUTPUT_PATH (if exists)
|
||||
-h, --help - show this help message and exit
|
||||
-v, --version - show version info and exit
|
||||
|
||||
If both -c and -d are specified then only unique set of files will be converted.
|
||||
If no ARGUMENTS passed then --help implied.
|
||||
If both -f and -d are specified then only unique set of files will be converted.
|
||||
-f or -d are required to be specified at least once, but each may be specified multiple times.
|
||||
PATH must be a valid path to readable json-file or directory.
|
||||
OUTPUT_PATH must be a valid path to writeable directory.
|
||||
If -o is specified several times then only last one will be applied.
|
||||
If -o is specified several times then only last one will be used.
|
||||
If -e is specified several times then only last one will be used.
|
||||
If -e is not specified then only collection vars will be replaced (if any).
|
||||
|
||||
Possible FORMATS:
|
||||
--http - generate raw *.http files (default)
|
||||
--curl - generate shell scripts with curl command
|
||||
--wget - generate shell scripts with wget command
|
||||
--http - generate raw *.http files (default)
|
||||
--curl - generate shell scripts with curl command
|
||||
--wget - generate shell scripts with wget command
|
||||
If no FORMATS specified then --http implied.
|
||||
Any of FORMATS can be specified at the same time.
|
||||
|
||||
@ -83,8 +87,10 @@ Example:
|
||||
-f ~/dir1/first.postman_collection.json \
|
||||
--directory ~/team \
|
||||
--file ~/dir2/second.postman_collection.json \
|
||||
--env ~/localhost.postman_environment.json \
|
||||
-d ~/personal \
|
||||
-o ~/postman_export
|
||||
|
||||
```
|
||||
### Notice
|
||||
|
||||
|
65
src/Environment.php
Normal file
65
src/Environment.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter;
|
||||
|
||||
class Environment implements \ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $vars = [];
|
||||
|
||||
/**
|
||||
* @param object $env
|
||||
*/
|
||||
public function __construct(protected object $env)
|
||||
{
|
||||
foreach ($env->values as $var) {
|
||||
$this->vars["{{{$var->key}}}"] = $var->value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if there are some vars or not
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasVars(): bool
|
||||
{
|
||||
return !empty($this->vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return array_key_exists($offset, $this->vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
return $this->vars[$offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->vars[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
unset($this->vars[$offset]);
|
||||
}
|
||||
}
|
@ -5,14 +5,16 @@ declare(strict_types=1);
|
||||
namespace PmConverter\Exporters\Abstract;
|
||||
|
||||
use Exception;
|
||||
use PmConverter\Environment;
|
||||
use PmConverter\Exporters\{
|
||||
ConverterContract,
|
||||
RequestContract};
|
||||
use PmConverter\FileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class AbstractConverter
|
||||
abstract class AbstractConverter implements ConverterContract
|
||||
{
|
||||
/**
|
||||
* @var object|null
|
||||
@ -25,6 +27,30 @@ abstract class AbstractConverter
|
||||
protected string $outputPath;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $vars = [];
|
||||
|
||||
/**
|
||||
* @var Environment
|
||||
*/
|
||||
protected Environment $env;
|
||||
|
||||
/**
|
||||
* Sets an environment with vars
|
||||
*
|
||||
* @param Environment $env
|
||||
* @return $this
|
||||
*/
|
||||
public function withEnv(Environment $env): static
|
||||
{
|
||||
$this->env = $env;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts collection requests
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function convert(object $collection, string $outputPath): void
|
||||
@ -32,12 +58,30 @@ abstract class AbstractConverter
|
||||
$outputPath = sprintf('%s%s%s', $outputPath, DIRECTORY_SEPARATOR, static::OUTPUT_DIR);
|
||||
$this->outputPath = FileSystem::makeDir($outputPath);
|
||||
$this->collection = $collection;
|
||||
$this->setVariables();
|
||||
foreach ($collection->item as $item) {
|
||||
$this->convertItem($item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares collection variables
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setVariables(): static
|
||||
{
|
||||
if ($this->collection->variable) {
|
||||
foreach ($this->collection->variable as $var) {
|
||||
$this->vars["{{{$var->key}}}"] = $var->value;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns output path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOutputPath(): string
|
||||
@ -46,6 +90,8 @@ abstract class AbstractConverter
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether item contains another items or not
|
||||
*
|
||||
* @param object $item
|
||||
* @return bool
|
||||
*/
|
||||
@ -55,6 +101,8 @@ abstract class AbstractConverter
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an item to request object and writes it into file
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function convertItem(mixed $item): void
|
||||
@ -77,18 +125,23 @@ abstract class AbstractConverter
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialiazes request object to be written in file
|
||||
*
|
||||
* @param object $item
|
||||
* @return RequestContract
|
||||
*/
|
||||
protected function initRequest(object $item): RequestContract
|
||||
{
|
||||
$request_class = static::REQUEST_CLASS;
|
||||
$result = new $request_class();
|
||||
|
||||
/** @var RequestContract $result */
|
||||
$result = new $request_class($this->vars);
|
||||
$result->setName($item->name);
|
||||
$result->setDescription($item->request?->description ?? null);
|
||||
$result->setVerb($item->request->method);
|
||||
$result->setUrl($item->request->url->raw);
|
||||
$result->setHeaders($item->request->header);
|
||||
$result->setAuth($item->request?->auth ?? $this->collection?->auth ?? null);
|
||||
if ($item->request->method !== 'GET' && !empty($item->request->body)) {
|
||||
$result->setBody($item->request->body);
|
||||
}
|
||||
@ -96,6 +149,8 @@ abstract class AbstractConverter
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes converted request object to file
|
||||
*
|
||||
* @param RequestContract $request
|
||||
* @param string|null $subpath
|
||||
* @return bool
|
||||
@ -106,6 +161,33 @@ abstract class AbstractConverter
|
||||
$filedir = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $subpath);
|
||||
$filedir = FileSystem::makeDir($filedir);
|
||||
$filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $request->getName(), static::FILE_EXT);
|
||||
return file_put_contents($filepath, (string)$request) > 0;
|
||||
$content = $this->interpolate((string)$request);
|
||||
return file_put_contents($filepath, $content) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces variables in request with values from collection or environment
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
protected function interpolate(string $content): string
|
||||
{
|
||||
if (empty($this->vars) && $this->env->hasVars()) {
|
||||
return $content;
|
||||
}
|
||||
$matches = [];
|
||||
if (preg_match_all('/\{\{[a-zA-Z][a-zA-Z0-9]+}}/', $content, $matches, PREG_PATTERN_ORDER) > 0) {
|
||||
foreach ($matches[0] as $key => $var) {
|
||||
if (str_contains($content, $var)) {
|
||||
$content = str_replace($var, $this->vars[$var] ?? $this->env[$var] ?? $var, $content);
|
||||
unset($matches[0][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (!empty($matches[0])) {
|
||||
// fwrite(STDERR, sprintf(' No values found: %s%s', implode(', ', $matches[0]), PHP_EOL));
|
||||
// }
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
@ -127,6 +127,30 @@ abstract class AbstractRequest implements RequestContract
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets authorization headers
|
||||
*
|
||||
* @param object|null $auth
|
||||
* @return $this
|
||||
*/
|
||||
public function setAuth(?object $auth): static
|
||||
{
|
||||
if (!empty($auth)) {
|
||||
switch ($auth->type) {
|
||||
case 'bearer':
|
||||
$this->headers['Authorization'] = [
|
||||
'value' => 'Bearer ' . $auth->{$auth->type}[0]->value,
|
||||
'disabled' => false,
|
||||
];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets body mode from collection item to request object
|
||||
*
|
||||
|
@ -11,6 +11,7 @@ interface RequestContract
|
||||
public function setDescription(?string $description): static;
|
||||
public function setVerb(string $verb): static;
|
||||
public function setUrl(string $url): static;
|
||||
public function setAuth(object $auth): static;
|
||||
public function setHeaders(?array $headers): static;
|
||||
public function setBodymode(string $bodymode): static;
|
||||
public function setBody(object $body): static;
|
||||
|
@ -57,12 +57,22 @@ class Processor
|
||||
/**
|
||||
* @var int Initial timestamp
|
||||
*/
|
||||
protected int $init_time;
|
||||
protected int $initTime;
|
||||
|
||||
/**
|
||||
* @var int Initial RAM usage
|
||||
*/
|
||||
protected int $init_ram;
|
||||
protected int $initRam;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $envFile;
|
||||
|
||||
/**
|
||||
* @var Environment
|
||||
*/
|
||||
protected Environment $env;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -71,8 +81,8 @@ class Processor
|
||||
*/
|
||||
public function __construct(protected array $argv)
|
||||
{
|
||||
$this->init_time = hrtime(true);
|
||||
$this->init_ram = memory_get_usage(true);
|
||||
$this->initTime = hrtime(true);
|
||||
$this->initRam = memory_get_usage(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,14 +125,19 @@ class Processor
|
||||
if (empty($this->argv[$idx + 1])) {
|
||||
throw new InvalidArgumentException('a directory path is expected for -d (--dir)');
|
||||
}
|
||||
$normpath = $this->argv[$idx + 1];
|
||||
$rawpath = $this->argv[$idx + 1];
|
||||
$files = array_filter(
|
||||
FileSystem::dirContents($normpath),
|
||||
FileSystem::dirContents($rawpath),
|
||||
static fn($filename) => FileSystem::isCollectionFile($filename)
|
||||
);
|
||||
$this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files));
|
||||
break;
|
||||
|
||||
case '-e':
|
||||
case '--env':
|
||||
$this->envFile = FileSystem::normalizePath($this->argv[$idx + 1]);
|
||||
break;
|
||||
|
||||
case '-p':
|
||||
case '--preserve':
|
||||
$this->preserveOutput = true;
|
||||
@ -158,6 +173,8 @@ class Processor
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes output directory
|
||||
*
|
||||
* @return void
|
||||
* @throws CannotCreateDirectoryException
|
||||
* @throws DirectoryIsNotWriteableException
|
||||
@ -185,6 +202,8 @@ class Processor
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes collection objects
|
||||
*
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function initCollections(): void
|
||||
@ -199,6 +218,22 @@ class Processor
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes environment object
|
||||
*
|
||||
* @return void
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function initEnv(): void
|
||||
{
|
||||
$content = file_get_contents(FileSystem::normalizePath($this->envFile));
|
||||
$content = json_decode($content, flags: JSON_THROW_ON_ERROR);
|
||||
if (!property_exists($content, 'environment') || empty($content?->environment)) {
|
||||
throw new JsonException("not a valid environment: $this->envFile");
|
||||
}
|
||||
$this->env = new Environment($content->environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins a conversion
|
||||
*
|
||||
@ -210,6 +245,7 @@ class Processor
|
||||
$this->initOutputDirectory();
|
||||
$this->initConverters();
|
||||
$this->initCollections();
|
||||
$this->initEnv();
|
||||
$count = count($this->collections);
|
||||
$current = 0;
|
||||
$success = 0;
|
||||
@ -220,6 +256,9 @@ class Processor
|
||||
foreach ($this->converters as $type => $exporter) {
|
||||
printf('> %s%s', strtolower($type), PHP_EOL);
|
||||
$outputPath = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $collectionName);
|
||||
if (!empty($this->env)) {
|
||||
$exporter->withEnv($this->env);
|
||||
}
|
||||
$exporter->convert($collection, $outputPath);
|
||||
printf(' OK: %s%s', $exporter->getOutputPath(), PHP_EOL);
|
||||
}
|
||||
@ -238,8 +277,8 @@ class Processor
|
||||
*/
|
||||
protected function printStats(int $success, int $count): void
|
||||
{
|
||||
$time = (hrtime(true) - $this->init_time) / 1_000_000;
|
||||
$ram = (memory_get_peak_usage(true) - $this->init_ram) / 1024 / 1024;
|
||||
$time = (hrtime(true) - $this->initTime) / 1_000_000;
|
||||
$ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024;
|
||||
printf('Converted %d of %d in %.3f ms using %.3f MiB RAM', $success, $count, $time, $ram);
|
||||
}
|
||||
|
||||
@ -278,16 +317,19 @@ class Processor
|
||||
"\t-f, --file - a PATH to single collection located in PATH to convert from",
|
||||
"\t-d, --dir - a directory with collections located in COLLECTION_FILEPATH to convert from",
|
||||
"\t-o, --output - a directory OUTPUT_PATH to put results in",
|
||||
"\t-e, --env - use environment file with variable values to replace in request",
|
||||
"\t-p, --preserve - do not delete OUTPUT_PATH (if exists)",
|
||||
"\t-h, --help - show this help message and exit",
|
||||
"\t-v, --version - show version info and exit",
|
||||
'',
|
||||
'If no ARGUMENTS passed then --help implied.',
|
||||
'If both -c and -d are specified then only unique set of files will be converted.',
|
||||
'If both -f and -d are specified then only unique set of files will be converted.',
|
||||
'-f or -d are required to be specified at least once, but each may be specified multiple times.',
|
||||
'PATH must be a valid path to readable json-file or directory.',
|
||||
'OUTPUT_PATH must be a valid path to writeable directory.',
|
||||
'If -o is specified several times then only last one will be used.',
|
||||
'If -e is specified several times then only last one will be used.',
|
||||
'If -e is not specified then only collection vars will be replaced (if any).',
|
||||
'',
|
||||
'Possible FORMATS:',
|
||||
"\t--http - generate raw *.http files (default)",
|
||||
@ -301,6 +343,7 @@ class Processor
|
||||
" -f ~/dir1/first.postman_collection.json \ ",
|
||||
" --directory ~/team \ ",
|
||||
" --file ~/dir2/second.postman_collection.json \ ",
|
||||
" --env ~/localhost.postman_environment.json \ ",
|
||||
" -d ~/personal \ ",
|
||||
" -o ~/postman_export ",
|
||||
"",
|
||||
|
Loading…
Reference in New Issue
Block a user