Conversion to 2.0 (--v2.0), improvements and fixes (#10)

First of all, now you can convert a collection manually exported from Postman UI.

Until this commit, any collection json had to be inside of root 'collection' object. Postman API returns collections in a such way and that was my case. So any collection exported using UI was mistakenly not detected as correct one.

The second thing that it is now possible to convert collections from v2.1 to v2.0 using --v2.0 flag. Even if they are exported via Postman API, of course.

Also some important refactorings are here.
This commit is contained in:
2023-09-17 21:42:06 +08:00
parent 5c85f23514
commit 3c1871ce1f
11 changed files with 296 additions and 26 deletions

View File

@@ -107,7 +107,7 @@ class Processor
$normpath = FileSystem::normalizePath($rawpath);
if (!FileSystem::isCollectionFile($normpath)) {
throw new InvalidArgumentException(
sprintf("this is not a valid collection file:%s\t%s %s", PHP_EOL, $arg, $rawpath)
sprintf("not a valid collection:%s\t%s %s", PHP_EOL, $arg, $rawpath)
);
}
$this->collectionPaths[] = $this->argv[$idx + 1];
@@ -156,6 +156,10 @@ class Processor
$this->formats[ConvertFormat::Wget->name] = ConvertFormat::Wget;
break;
case '--v2.0':
$this->formats[ConvertFormat::Postman20->name] = ConvertFormat::Postman20;
break;
case '--var':
[$var, $value] = explode('=', trim($this->argv[$idx + 1]));
$this->vars[$var] = $value;
@@ -219,12 +223,8 @@ class Processor
protected function initCollections(): void
{
foreach ($this->collectionPaths as $collectionPath) {
$content = file_get_contents(FileSystem::normalizePath($collectionPath));
$content = json_decode($content, flags: JSON_THROW_ON_ERROR);
if (!property_exists($content, 'collection') || empty($content?->collection)) {
throw new JsonException("not a valid collection: $collectionPath");
}
$this->collections[$content->collection->info->name] = $content->collection;
$collection = Collection::fromFile($collectionPath);
$this->collections[$collection->name()] = $collection;
}
unset($this->collectionPaths, $content);
}