Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"eslint-plugin-react": "^7.5.1"
},
"dependencies": {
"aws-sdk": "^2.385.0"
"aws-sdk": "^2.385.0",
"semver": "^7.3.2"
}
}
26 changes: 24 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const AWS = require('aws-sdk')
const semver = require('semver')

const getApiName = async function getApiName(serviceName, stage, region, creds) {
const cfn = new AWS.CloudFormation({
Expand All @@ -21,11 +22,31 @@ const getApiName = async function getApiName(serviceName, stage, region, creds)
return apiName
}

const getSwagger = async function getSwagger(apiName, stage, region, creds) {
const getSwagger = async function getSwagger(apiName, stage, region, creds, serverless) {
const ag = new AWS.APIGateway({
credentials: creds,
region,
})
// Get version of currently published documentation.
const apiStage = await ag.getStage({
restApiId: apiName,
stageName: stage,
}).promise()
// Pump up version.
let documentationVersion = '1.0.0'
if (apiStage.documentationVersion) {
documentationVersion = semver.clean(apiStage.documentationVersion)
}
serverless.cli.consoleLog(`ExportSwagger: Current API Version: ${documentationVersion}.`)
const bumpedVersion = semver.inc(documentationVersion, 'patch')
serverless.cli.consoleLog(`ExportSwagger: Next API Version: ${bumpedVersion}.`)
// Publish new version.
await ag.createDocumentationVersion({
documentationVersion: bumpedVersion,
restApiId: apiName,
stageName: stage,
}).promise()
// Get swagger export of newly published documentation version.
const swagger = await ag.getExport({
exportType: 'swagger',
restApiId: apiName,
Expand Down Expand Up @@ -67,7 +88,7 @@ const exportApi = async function exportApi(serverless) {
const serviceName = serverless.service.getServiceName()
const { bucket, key } = getBucketAndKey(serverless)
const apiName = await getApiName(serviceName, stage, region, awsCredentials)
const swagger = await getSwagger(apiName, stage, region, awsCredentials)
const swagger = await getSwagger(apiName, stage, region, awsCredentials, serverless)
await uploadSwaggerToS3(swagger, bucket, key, region, awsCredentials)
serverless.cli.consoleLog('ExportSwagger: File uploaded to s3.')
}
Expand All @@ -88,3 +109,4 @@ class ExportSwagger {
}

module.exports = ExportSwagger