Skip to content

Commit

Permalink
completed the cli support for conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmin2 committed Sep 23, 2024
1 parent 5ffd626 commit 69febbb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 7 deletions.
8 changes: 4 additions & 4 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ _See code: [src/commands/config/versions.ts](https://github.com/asyncapi/cli/blo

## `asyncapi convert [SPEC-FILE]`

Convert asyncapi documents older to newer versions or OpenAPI documents to AsyncAPI
Convert asyncapi documents older to newer versions or OpenAPI | postman-collection documents to AsyncAPI

```
USAGE
Expand All @@ -318,12 +318,12 @@ ARGUMENTS
SPEC-FILE spec path, url, or context-name
FLAGS
-f, --format=<option> (required) [default: asyncapi] Specify the format to convert from (openapi or asyncapi)
<options: openapi|asyncapi>
-f, --format=<option> (required) [default: asyncapi] Specify the format to convert from (openapi or asyncapi or postman-collection)
<options: openapi|asyncapi|postman-collection>
-h, --help Show CLI help.
-o, --output=<value> path to the file where the result is saved
-p, --perspective=<option> [default: server] Perspective to use when converting OpenAPI to AsyncAPI (client or
server). Note: This option is only applicable for OpenAPI to AsyncAPI conversions.
server). Note: This option is only applicable for OpenAPI | postman-collection to AsyncAPI conversions.
<options: client|server>
-t, --target-version=<value> [default: 3.0.0] asyncapi version to convert to
Expand Down
13 changes: 11 additions & 2 deletions src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Command from '../core/base';
import { ValidationError } from '../core/errors/validation-error';
import { load } from '../core/models/SpecificationFile';
import { SpecificationFileNotFound } from '../core/errors/specification-file';
import { convert, convertOpenAPI } from '@asyncapi/converter';
import { convert, convertOpenAPI, convertPostman } from '@asyncapi/converter';

Check failure on line 8 in src/commands/convert.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Module '"@asyncapi/converter"' has no exported member 'convertPostman'.
import type { AsyncAPIConvertVersion, OpenAPIConvertVersion } from '@asyncapi/converter';
import { cyan, green } from 'picocolors';

Expand All @@ -16,7 +16,7 @@ import { convertFlags } from '../core/flags/convert.flags';
const latestVersion = Object.keys(specs.schemas).pop() as string;

export default class Convert extends Command {
static description = 'Convert asyncapi documents older to newer versions or OpenAPI documents to AsyncAPI';
static description = 'Convert asyncapi documents older to newer versions or OpenAPI/postman-collection documents to AsyncAPI';

static flags = convertFlags(latestVersion);

Expand Down Expand Up @@ -54,6 +54,15 @@ export default class Convert extends Command {
} else if (this.specFile.getFileURL()) {
this.log(`🎉 The URL ${cyan(this.specFile.getFileURL())} has been successfully converted to version ${green(flags['target-version'])}!!`);
}
} else {
convertedFile = convertPostman(this.specFile.text(), '3.0.0', {
perspective: flags['perspective'] as 'client' | 'server'
});
if (this.specFile.getFilePath()) {
this.log(`🎉 The ${cyan(this.specFile.getFilePath())} file has been successfully converted to asyncapi of version ${green(flags['target-version'])}!!`);
} else if (this.specFile.getFileURL()) {
this.log(`🎉 The URL ${cyan(this.specFile.getFileURL())} has been successfully converted to asyncapi of version ${green(flags['target-version'])}!!`);
}
}

if (typeof convertedFile === 'object') {
Expand Down
2 changes: 1 addition & 1 deletion src/core/flags/convert.flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const convertFlags = (latestVersion: string) => {
format: Flags.string({
char: 'f',
description: 'Specify the format to convert from (openapi or asyncapi)',
options: ['openapi', 'asyncapi'],
options: ['openapi', 'asyncapi', 'postman-collection'],
required: true,
default: 'asyncapi',
}),
Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/postman-collection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
info:
name: Sample Postman Collection
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
item:
- name: Sample Request
request:
method: GET
header: []
url:
raw: 'https://jsonplaceholder.typicode.com/posts/1'
protocol: https
host:
- jsonplaceholder
- typicode
- com
path:
- posts
- '1'
response: []
- name: Sample POST Request
request:
method: POST
header:
- key: Content-Type
value: application/json
body:
mode: raw
raw: '{ "title": "foo", "body": "bar", "userId": 1 }'
url:
raw: 'https://jsonplaceholder.typicode.com/posts'
protocol: https
host:
- jsonplaceholder
- typicode
- com
path:
- posts
response: []
63 changes: 63 additions & 0 deletions test/integration/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const testHelper = new TestHelper();
const filePath = './test/fixtures/specification.yml';
const JSONFilePath = './test/fixtures/specification.json';
const openAPIFilePath = './test/fixtures/openapi.yml';
const postmanFilePath = './test/fixtures/postman-collection.yml';

describe('convert', () => {
describe('with file paths', () => {
Expand Down Expand Up @@ -241,4 +242,66 @@ describe('convert', () => {
done();
});
});

describe('with Postman input', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
.command(['convert', postmanFilePath, '-f', 'postman-collection'])
.it('works when Postman file path is passed', (ctx, done) => {
expect(ctx.stdout).to.contain(`🎉 The ${postmanFilePath} file has been successfully converted to asyncapi of version 3.0.0!!`);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['convert', postmanFilePath, '-f', 'postman-collection', '-p=client'])
.it('works when Postman file path is passed with client perspective', (ctx, done) => {
expect(ctx.stdout).to.contain(`🎉 The ${postmanFilePath} file has been successfully converted to asyncapi of version 3.0.0!!`);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['convert', postmanFilePath, '-f', 'postman-collection', '-p=server'])
.it('works when Postman file path is passed with server perspective', (ctx, done) => {
expect(ctx.stdout).to.contain(`🎉 The ${postmanFilePath} file has been successfully converted to asyncapi of version 3.0.0!!`);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['convert', postmanFilePath, '-f', 'postman-collection', '-p=invalid'])
.it('should throw error if invalid perspective is passed', (ctx, done) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.contain('Error: Expected --perspective=invalid to be one of: client, server');
done();
});

test
.stderr()
.stdout()
.command(['convert', postmanFilePath, '-f', 'postman-collection', '-o=./test/fixtures/postman_converted_output.yml'])
.it('works when Postman file is converted and output is saved', (ctx, done) => {
expect(ctx.stdout).to.contain(`🎉 The ${postmanFilePath} file has been successfully converted to asyncapi of version 3.0.0!!`);
expect(fs.existsSync('./test/fixtures/postman_converted_output.yml')).to.equal(true);
expect(ctx.stderr).to.equal('');
fs.unlinkSync('./test/fixtures/postman_converted_output.yml');
done();
});
});
});

0 comments on commit 69febbb

Please sign in to comment.