Skip to content

Commit

Permalink
* Added AsyncAPI support to {api/domain}:{create/update} commands (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalemRoelofsSB authored Sep 5, 2022
1 parent c6afa2d commit 68d6035
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 32 deletions.
14 changes: 7 additions & 7 deletions src/commands/api/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { flags } = require('@oclif/command')
const { readFileSync } = require('fs-extra')
const { getApi, postApi } = require('../../requests/api')
const { getApiIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
const { getOasVersion, getVersion, parseDefinition } = require('../../utils/oas')
const { getVersion, parseDefinition, getSpecification } = require('../../utils/definitions')
const BaseCommand = require('../../support/command/base-command')
const UpdateCommand = require('../../support/command/update-command')

Expand All @@ -18,13 +18,13 @@ class CreateAPICommand extends UpdateCommand {
})
}

async tryCreateApi({ path, version, oas, flags }) {
async tryCreateApi({ path, version, specification, flags }) {
const isNameAvailable = await this.checkApiName(path)
const pathHasVersion = path.length === 3
const fullPath = pathHasVersion ? path : [...path, version]

if (isNameAvailable) {
await this.createApi(fullPath, oas, flags, pathHasVersion)
await this.createApi(fullPath, specification, flags, pathHasVersion)
return true
}

Expand All @@ -38,12 +38,12 @@ class CreateAPICommand extends UpdateCommand {
})
}

async createApi([owner, name, version], oas, flags, pathHasVersion) {
async createApi([owner, name, version], specification, flags, pathHasVersion) {
const isPrivate = flags.visibility === 'private'

const createApiObj = {
pathParams: [owner, name],
queryParams: { version, isPrivate, oas },
queryParams: { version, isPrivate, specification },
body: readFileSync(flags.file)
}

Expand All @@ -61,7 +61,7 @@ class CreateAPICommand extends UpdateCommand {
async run() {
const { args, flags } = this.parse(CreateAPICommand)
const definition = parseDefinition(flags.file)
const oas = getOasVersion(definition)
const specification = getSpecification(definition)
const apiVersion = getVersion(definition)
const apiPath = getApiIdentifierArg(args)
const [owner, name, version = apiVersion] = splitPathParams(apiPath)
Expand All @@ -71,7 +71,7 @@ class CreateAPICommand extends UpdateCommand {
path: [owner, name],
version,
flags,
oas
specification
}

const createdApi = (
Expand Down
2 changes: 1 addition & 1 deletion src/commands/api/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { flags } = require('@oclif/command')
const { readFileSync } = require('fs-extra')
const { getApi, postApi } = require('../../requests/api')
const { getApiIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
const { getVersion, parseDefinition } = require('../../utils/oas')
const { getVersion, parseDefinition } = require('../../utils/definitions')
const BaseCommand = require('../../support/command/base-command')
const UpdateCommand = require('../../support/command/update-command')

Expand Down
2 changes: 1 addition & 1 deletion src/commands/domain/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { flags } = require('@oclif/command')
const { readFileSync } = require('fs-extra')
const { getDomain, postDomain } = require('../../requests/domain')
const { getDomainIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
const { getVersion, parseDefinition } = require('../../utils/oas')
const { getVersion, parseDefinition } = require('../../utils/definitions')
const BaseCommand = require('../../support/command/base-command')
const UpdateCommand = require('../../support/command/update-command')

Expand Down
2 changes: 1 addition & 1 deletion src/commands/domain/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { flags } = require('@oclif/command')
const { readFileSync } = require('fs-extra')
const { getDomain, postDomain } = require('../../requests/domain')
const { getDomainIdentifierArg, splitPathParams } = require('../../support/command/parse-input')
const { getVersion, parseDefinition } = require('../../utils/oas')
const { getVersion, parseDefinition } = require('../../utils/definitions')
const BaseCommand = require('../../support/command/base-command')
const UpdateCommand = require('../../support/command/update-command')

Expand Down
2 changes: 1 addition & 1 deletion src/template-strings/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const errorMsg = {

cannotParseDefinition: 'There was a problem with parsing {{filename}}. \nReason: {{e}}',

cannotParseOasVersion: 'Cannot determine OAS version from file',
cannotParseSpecification: 'Cannot determine specification from file',

cannotParseVersion: 'Cannot determine version from file',

Expand Down
19 changes: 14 additions & 5 deletions src/utils/oas.js → src/utils/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ const yaml = require('js-yaml')
const { existsSync, readFileSync } = require('fs-extra')
const { errorMsg } = require('../template-strings')

const getOasVersion = ({ swagger, openapi }) => {
if (!swagger && !openapi) {
throw new CLIError(errorMsg.cannotParseOasVersion())
const specVersionToSpecification = specVersion => {
if (/2.0$/.test(specVersion))
return 'openapi-2.0'
if (/2.\d+.\d+$/.test(specVersion))
return 'asyncapi-2.x.x'
return 'openapi-3.0.0'
}

const getSpecification = ({ swagger, openapi, asyncapi }) => {
if (!swagger && !openapi && !asyncapi) {
throw new CLIError(errorMsg.cannotParseSpecification())
}
return swagger || openapi
const specVersion = [swagger, openapi, asyncapi].filter(version => Boolean(version))[0]
return specVersionToSpecification(specVersion)
}

const getVersion = definition => {
Expand All @@ -34,7 +43,7 @@ const parseDefinition = filename => {
}

module.exports = {
getOasVersion,
getSpecification,
getVersion,
parseDefinition,
}
73 changes: 58 additions & 15 deletions test/commands/api/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe('invalid api:create file issues', () => {
test
.command(['api:create', validIdentifier, '--file=test/resources/missing_oas_version.yaml'])
.catch(ctx => {
expect(ctx.message).to.contain('Cannot determine OAS version from file')
expect(ctx.message).to.contain('Cannot determine specification from file')
})
.it('runs api:create with file missing OAS Version')
.it('runs api:create with file missing specification')

test
.command(['api:create', 'org/api', '--file=test/resources/missing_version.yaml'])
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('invalid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/orgNotExist/api?version=1.0.0&isPrivate=true&oas=3.0.0')
.post('/orgNotExist/api?version=1.0.0&isPrivate=true&specification=openapi-3.0.0')
.reply(404, '{"code":404,"message":"{\\\"code\\\":404,\\\"message\\\":\\\"Object doesn\'t exist\\\"}"}')
)
.command(['api:create', 'orgNotExist/api/1.0.0', '--file=test/resources/valid_api.json'])
Expand All @@ -114,7 +114,7 @@ describe('invalid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/overLimitApi?version=1.0.0&isPrivate=true&oas=3.0.0')
.post('/org/overLimitApi?version=1.0.0&isPrivate=true&specification=openapi-3.0.0')
.reply(403, '{"code":403,"message":"You have reached the limit of APIs"}')
)
.command(['api:create', 'org/overLimitApi/1.0.0', '--file=test/resources/valid_api.json'])
Expand All @@ -130,7 +130,7 @@ describe('invalid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/orgNotExist/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/orgNotExist/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.reply(404, '{"code":404,"message":"{\\\"code\\\":404,\\\"message\\\":\\\"Object doesn\'t exist\\\"}"}')
)
.command([
Expand All @@ -152,7 +152,7 @@ describe('invalid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand All @@ -173,7 +173,7 @@ describe('invalid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand All @@ -200,7 +200,7 @@ describe('valid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand All @@ -217,7 +217,7 @@ describe('valid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand All @@ -238,7 +238,7 @@ describe('valid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand All @@ -259,7 +259,7 @@ describe('valid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand Down Expand Up @@ -291,7 +291,7 @@ describe('valid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.0&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.0&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('valid api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=2.0.0&isPrivate=false&oas=3.0.0')
.post('/org/api?version=2.0.0&isPrivate=false&specification=openapi-3.0.0')
.matchHeader('Content-Type', 'application/json')
.reply(201)
)
Expand All @@ -333,6 +333,49 @@ describe('valid api:create', () => {
.it('runs api:create with json file', ctx => {
expect(ctx.stdout).to.contains('Created API \'org/api\'')
})

test
.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: shubUrl }))
.nock(`${shubUrl}/apis`, api => api
.get('/org/api')
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=2.0.0&isPrivate=false&specification=asyncapi-2.x.x')
.matchHeader('Content-Type', 'application/json')
.reply(201)
)
.stdout()
.command([
'api:create',
'org/api/2.0.0',
'--file=test/resources/valid_asyncapi.json',
'--visibility=public'
])
.it('runs api:create with asyncapi json file', ctx => {
expect(ctx.stdout).to.contains('Created API \'org/api\'')
})
test
.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: shubUrl }))
.nock(`${shubUrl}/apis`, api => api
.get('/org/api')
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=2.0.0&isPrivate=false&specification=asyncapi-2.x.x')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
.stdout()
.command([
'api:create',
'org/api/2.0.0',
'--file=test/resources/valid_asyncapi.yaml',
'--visibility=public'
])
.it('runs api:create with asyncapi yaml file', ctx => {
expect(ctx.stdout).to.contains('Created API \'org/api\'')
})
})

describe('valid create new version with api:create', () => {
Expand All @@ -347,7 +390,7 @@ describe('valid create new version with api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=1.0.1&isPrivate=true&oas=2.0')
.post('/org/api?version=1.0.1&isPrivate=true&specification=openapi-2.0')
.matchHeader('Content-Type', 'application/yaml')
.reply(201)
)
Expand All @@ -368,7 +411,7 @@ describe('valid create new version with api:create', () => {
.reply(404)
)
.nock(`${shubUrl}/apis`, api => api
.post('/org/api?version=2.0.0&isPrivate=false&oas=3.0.0')
.post('/org/api?version=2.0.0&isPrivate=false&specification=openapi-3.0.0')
.reply(201)
)
.stdout()
Expand Down
Loading

0 comments on commit 68d6035

Please sign in to comment.