/
create.js
139 lines (118 loc) · 4.17 KB
/
create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 BaseCommand = require('../../support/command/base-command')
const UpdateCommand = require('../../support/command/update-command')
const isApiNameAvailable = response => response.status === 404
class CreateAPICommand extends UpdateCommand {
async checkApiName(path) {
return this.executeHttp({
execute: () => getApi(path),
onResolve: isApiNameAvailable,
options: { resolveStatus: [403, 404] }
})
}
async tryCreateApi({ path, version, oas, 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)
return true
}
return isNameAvailable
}
async tryCreateApiVersion({ path, ...args }) {
return this.tryCreateApi({
path: [...path, args.version],
...args
})
}
async createApi([owner, name, version], oas, flags, pathHasVersion) {
const isPrivate = flags.visibility === 'private'
const createApiObj = {
pathParams: [owner, name],
queryParams: { version, isPrivate, oas },
body: readFileSync(flags.file)
}
if (pathHasVersion) {
this.logCommandSuccess = this.setSuccessMessage('createdApiVersion')
}
return await this.executeHttp({
execute: () => postApi(createApiObj),
onResolve: this.logCommandSuccess({ owner, name, version }),
options: { resolveStatus: [403] }
})
}
async run() {
const { args, flags } = this.parse(CreateAPICommand)
const definition = parseDefinition(flags.file)
const oas = getOasVersion(definition)
const apiVersion = getVersion(definition)
const apiPath = getApiIdentifierArg(args)
const [owner, name, version = apiVersion] = splitPathParams(apiPath)
const type = 'apis'
const argsObj = {
path: [owner, name],
version,
flags,
oas
}
const createdApi = (
await this.tryCreateApi(argsObj) ||
await this.tryCreateApiVersion(argsObj)
)
if (!createdApi) {
return this.throwCommandError({
owner, name, version
})()
}
if (flags.published === 'publish') await this.updatePublish(type, owner, name, version, true)
if (flags.setdefault) await this.updateDefault(type, owner, name, version)
}
}
CreateAPICommand.description = `creates a new API / API version from a YAML/JSON file
The API version from the file will be used unless the version is specified in the command argument.
An error will occur if the API version already exists.
`
CreateAPICommand.examples = [
'swaggerhub api:create organization/api/1.0.0 --file api.yaml --visibility public',
'swaggerhub api:create organization/api --file api.yaml',
'swaggerhub api:create organization/api/1.0.0 --published=publish --file api.json',
'swaggerhub api:create organization/api/1.0.0 --setdefault --file api.json',
'swaggerhub api:create organization/api/1.0.0 --published=publish --setdefault --file api.json'
]
CreateAPICommand.args = [{
name: 'OWNER/API_NAME/[VERSION]',
required: true,
description: 'API to create in SwaggerHub'
}]
CreateAPICommand.flags = {
file: flags.string({
char: 'f',
description: 'file location of API to create',
required: true
}),
visibility: flags.string({
description: 'visibility of API in SwaggerHub',
options: ['public', 'private'],
default: 'private'
}),
published: flags.string({
description: 'sets the lifecycle setting of the API version',
options: ['publish', 'unpublish'],
default: 'unpublish',
required: false,
dependsOn: ['file']
}),
setdefault: flags.boolean({
description: 'sets API version to be the default',
default: false,
required: false,
dependsOn: ['file']
}),
...BaseCommand.flags
}
module.exports = CreateAPICommand