Compare commits

...

2 Commits

4 changed files with 84 additions and 54 deletions

View File

@ -24,10 +24,9 @@ These formats are supported for now: `http`, `curl`, `wget`.
## Planned features ## Planned features
- conversion between postman schema v2.1 <-> v2.0 (#11);
- support as many as possible/necessary of authentication kinds (_currently only `Bearer` supported_); - 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`_); - support as many as possible/necessary of body formats (_currently only `json` and `formdata`_);
- documentation generation support (markdown) with responce examples (if present) (#6); - documentation generation support (markdown) with response examples (if present) (#6);
- maybe some another convert formats (like httpie or something...); - maybe some another convert formats (like httpie or something...);
- better logging; - better logging;
- 90%+ test coverage, phpcs, psalm, etc.; - 90%+ test coverage, phpcs, psalm, etc.;
@ -65,7 +64,6 @@ Possible ARGUMENTS:
-o, --output - a directory OUTPUT_PATH to put results in -o, --output - a directory OUTPUT_PATH to put results in
-e, --env - use environment file with variables to replace in requests -e, --env - use environment file with variables to replace in requests
--var "NAME=VALUE" - force replace specified env variable called NAME with custom VALUE --var "NAME=VALUE" - force replace specified env variable called NAME with custom VALUE
(see interpolation notes below)
-p, --preserve - do not delete OUTPUT_PATH (if exists) -p, --preserve - do not delete OUTPUT_PATH (if exists)
-h, --help - show this help message and exit -h, --help - show this help message and exit
-v, --version - show version info and exit -v, --version - show version info and exit
@ -78,32 +76,26 @@ OUTPUT_PATH must be a valid path to writeable directory.
If -o or -e was specified several times then only last one will be used. If -o or -e was specified several times then only last one will be used.
Possible FORMATS: Possible FORMATS:
--http - generate raw *.http files (default) --http - generate raw *.http files (default)
--curl - generate shell scripts with curl command --curl - generate shell scripts with curl command
--wget - generate shell scripts with wget command --wget - generate shell scripts with wget command
--v2.0 - convert from Postman Collection Schema v2.1 into v2.0
--v2.1 - convert from Postman Collection Schema v2.0 into v2.1
-a, --all - convert to all of formats listed above
If no FORMATS specified then --http implied. If no FORMATS specified then --http implied.
Any of FORMATS can be specified at the same time. Any of FORMATS can be specified at the same time or replaced by --all.
Notes about variable interpolation:
1. You can use -e to tell where to find variables to replace in requests.
2. You can use one or several --var to replace specific env variables to your own value.
3. Correct syntax is `--var "NAME=VALUE". NAME may be in curly braces like {{NAME}}.
4. Since -e is optional, a bunch of --var will emulate an environment. Also it does not
matter if there is --var in environment file you provided or not.
5. Even if you (not) provided -e and/or --var, any of variable may still be overridden
from collection (if any), so last ones has top priority.
Example: Example:
./pm-convert \ ./pm-convert \
-f ~/dir1/first.postman_collection.json \ -f ~/dir1/first.postman_collection.json \
--directory ~/team \ --directory ~/team \
--file ~/dir2/second.postman_collection.json \ --file ~/dir2/second.postman_collection.json \
--env ~/localhost.postman_environment.json \ --env ~/localhost.postman_environment.json \
-d ~/personal \ -d ~/personal \
--var "myvar=some value" \ --var "myvar=some value" \
-o ~/postman_export -o ~/postman_export \
--all
``` ```
### Notices ### Notices
@ -116,12 +108,39 @@ Example:
If not, you can rename them in Postman or convert collections with similar names into different directories. If not, you can rename them in Postman or convert collections with similar names into different directories.
Otherwise any generated file may be accidently overwritten by another one. Otherwise any generated file may be accidently overwritten by another one.
## Notes about variable interpolation
1. You can use -e to tell where to find variables to replace in requests.
2. You can use one or several --var to replace specific env variables to your own value.
3. Correct syntax is `--var "NAME=VALUE"`. `NAME` may be in curly braces like `{{NAME}}`.
4. Since -e is optional, a bunch of `--var` will emulate an environment. Also it does not matter if there is `--var` in environment file you provided or not.
5. Even if you (not) provided -e and/or `--var`, any of variable may still be overridden from collection (if any), so last ones has top priority.
### Notes about conversion between Postman Schemas
You can use `--v2.1` to convert v2.1 into v2.1 (and this is not a typo).
Same applies to `--v2.0`.
There is a case when a collection has been exported via Postman API.
In such case collection itself places in single root object called `collection` like this:
```
{
"collection": {
// your actual collection here
}
}
```
So, pm-convert will just raise actual data up on top level and write into disk.
## How to implement a new format ## How to implement a new format
1. Create new namespace in `./src/Converters` and name it according to format of your choice 1. Create new namespace in `./src/Converters` and name it according to format of your choice.
2. Create two classes for converter and request object which extends `Converters\Abstract\Abstract{Converter, Request}` respectively 2. Create two classes for converter and request object which extends `Converters\Abstract\Abstract{Converter, Request}` respectively.
3. Change constants values in your new request class according to format you want to implement 3. Change constants values in your new request class according to format you want to implement.
4. Write your own logic in converter's `__toString()` method, write new methods and override abstract ones 4. Add your converter class name in `Converters\ConvertFormat`.
5. Write your own logic in converter, write new methods and override abstract ones.
## License ## License

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace PmConverter\Converters\Postman20; namespace PmConverter\Converters\Postman20;
use PmConverter\Collection; use PmConverter\Collection;
use PmConverter\CollectionVersion;
use PmConverter\Converters\{ use PmConverter\Converters\{
Abstract\AbstractConverter, Abstract\AbstractConverter,
ConverterContract}; ConverterContract};
@ -33,12 +34,16 @@ class Postman20Converter extends AbstractConverter implements ConverterContract
public function convert(Collection $collection, string $outputPath): void public function convert(Collection $collection, string $outputPath): void
{ {
$this->collection = $collection; $this->collection = $collection;
$this->collection->info->schema = str_replace('/v2.1.', '/v2.0.', $this->collection->info->schema); // if data was exported from API, here is already valid json to
$this->prepareOutputDir($outputPath); // just flush it in file, otherwise we need to convert it deeper
$this->convertAuth($this->collection->raw()); if ($this->collection->version() === CollectionVersion::Version21) {
foreach ($this->collection->item as $item) { $this->collection->info->schema = str_replace('/v2.1.', '/v2.0.', $this->collection->info->schema);
$this->convertItem($item); $this->convertAuth($this->collection->raw());
foreach ($this->collection->item as $item) {
$this->convertItem($item);
}
} }
$this->prepareOutputDir($outputPath);
$this->writeCollection(); $this->writeCollection();
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace PmConverter\Converters\Postman21; namespace PmConverter\Converters\Postman21;
use PmConverter\Collection; use PmConverter\Collection;
use PmConverter\CollectionVersion;
use PmConverter\Converters\{ use PmConverter\Converters\{
Abstract\AbstractConverter, Abstract\AbstractConverter,
ConverterContract}; ConverterContract};
@ -33,12 +34,16 @@ class Postman21Converter extends AbstractConverter implements ConverterContract
public function convert(Collection $collection, string $outputPath): void public function convert(Collection $collection, string $outputPath): void
{ {
$this->collection = $collection; $this->collection = $collection;
$this->collection->info->schema = str_replace('/v2.0.', '/v2.1.', $this->collection->info->schema); // if data was exported from API, here is already valid json to
$this->prepareOutputDir($outputPath); // just flush it in file, otherwise we need to convert it deeper
$this->convertAuth($this->collection->raw()); if ($this->collection->version() === CollectionVersion::Version20) {
foreach ($this->collection->item as $item) { $this->collection->info->schema = str_replace('/v2.0.', '/v2.1.', $this->collection->info->schema);
$this->convertItem($item); $this->convertAuth($this->collection->raw());
foreach ($this->collection->item as $item) {
$this->convertItem($item);
}
} }
$this->prepareOutputDir($outputPath);
$this->writeCollection(); $this->writeCollection();
} }

View File

@ -165,6 +165,13 @@ class Processor
$this->formats[ConvertFormat::Postman21->name] = ConvertFormat::Postman21; $this->formats[ConvertFormat::Postman21->name] = ConvertFormat::Postman21;
break; break;
case '-a':
case '--all':
foreach (ConvertFormat::cases() as $format) {
$this->formats[$format->name] = $format;
}
break;
case '--var': case '--var':
[$var, $value] = explode('=', trim($this->argv[$idx + 1])); [$var, $value] = explode('=', trim($this->argv[$idx + 1]));
$this->vars[$var] = $value; $this->vars[$var] = $value;
@ -348,7 +355,6 @@ class Processor
"\t-o, --output - a directory OUTPUT_PATH to put results in", "\t-o, --output - a directory OUTPUT_PATH to put results in",
"\t-e, --env - use environment file with variables to replace in requests", "\t-e, --env - use environment file with variables to replace in requests",
"\t--var \"NAME=VALUE\" - force replace specified env variable called NAME with custom VALUE", "\t--var \"NAME=VALUE\" - force replace specified env variable called NAME with custom VALUE",
"\t (see interpolation notes below)",
"\t-p, --preserve - do not delete OUTPUT_PATH (if exists)", "\t-p, --preserve - do not delete OUTPUT_PATH (if exists)",
"\t-h, --help - show this help message and exit", "\t-h, --help - show this help message and exit",
"\t-v, --version - show version info and exit", "\t-v, --version - show version info and exit",
@ -361,21 +367,15 @@ class Processor
'If -o or -e was specified several times then only last one will be used.', 'If -o or -e was specified several times then only last one will be used.',
'', '',
'Possible FORMATS:', 'Possible FORMATS:',
"\t--http - generate raw *.http files (default)", "\t--http - generate raw *.http files (default)",
"\t--curl - generate shell scripts with curl command", "\t--curl - generate shell scripts with curl command",
"\t--wget - generate shell scripts with wget command", "\t--wget - generate shell scripts with wget command",
"\t--v2.0 - convert from Postman Collection Schema v2.1 into v2.0",
"\t--v2.1 - convert from Postman Collection Schema v2.0 into v2.1",
"\t-a, --all - convert to all of formats listed above",
'', '',
'If no FORMATS specified then --http implied.', 'If no FORMATS specified then --http implied.',
'Any of FORMATS can be specified at the same time.', 'Any of FORMATS can be specified at the same time or replaced by --all.',
'',
'Notes about variable interpolation:',
"\t1. You can use -e to tell where to find variables to replace in requests.",
"\t2. You can use one or several --var to replace specific env variables to your own value.",
"\t3. Correct syntax is `--var \"NAME=VALUE\". NAME may be in curly braces like {{NAME}}.",
"\t4. Since -e is optional, a bunch of --var will emulate an environment. Also it does not",
"\t matter if there is --var in environment file you provided or not.",
"\t5. Even if you (not) provided -e and/or --var, any of variable may still be overridden",
"\t from collection (if any), so last ones has top priority.",
'', '',
'Example:', 'Example:',
" ./pm-convert \ ", " ./pm-convert \ ",
@ -385,7 +385,8 @@ class Processor
" --env ~/localhost.postman_environment.json \ ", " --env ~/localhost.postman_environment.json \ ",
" -d ~/personal \ ", " -d ~/personal \ ",
" --var \"myvar=some value\" \ ", " --var \"myvar=some value\" \ ",
" -o ~/postman_export ", " -o ~/postman_export \ ",
" --all",
"", "",
], $this->copyright()); ], $this->copyright());
} }