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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# intellij files
.idea/
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ plugins:
custom:
swaggerDestinations:
s3BucketName: 'bucket-name'
s3KeyName: 'swagger/api.json'
s3KeyName: 'swagger/api'
# optional, default: private
acl: public-read
#optional settings to upload yaml and openapi files.
uploadOpenAPI: true
uploadYaml: true
```
6 changes: 3 additions & 3 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-export-swagger",
"version": "2.0.0",
"version": "2.0.1",
"description": "export swagger definition to s3 ",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -30,6 +30,6 @@
"eslint-plugin-react": "^7.5.1"
},
"dependencies": {
"aws-sdk": "^2.385.0"
"aws-sdk": "^2.410.0"
}
}
79 changes: 73 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,57 @@ const getSwagger = async function getSwagger(apiName, stage, region, creds) {
return swagger
}

const getSwaggerYaml = async function getSwagger(apiName, stage, region, creds) {
const ag = new AWS.APIGateway({
credentials: creds,
region,
})
const swaggeryaml = await ag.getExport({
exportType: 'swagger',
restApiId: apiName,
stageName: stage,
accepts: 'application/yaml',
parameters: {
extensions: 'integrations',
},
}).promise()
return swaggeryaml
}

const getOpenapi = async function getOpenapi(apiName, stage, region, creds) {
const ag = new AWS.APIGateway({
credentials: creds,
region,
})
const oas30 = await ag.getExport({
exportType: 'oas30',
restApiId: apiName,
stageName: stage,
accepts: 'application/json',
parameters: {
extensions: 'integrations',
},
}).promise()
return oas30
}

const getOpenapiYaml = async function getOpenapi(apiName, stage, region, creds) {
const ag = new AWS.APIGateway({
credentials: creds,
region,
})
const oas30yaml = await ag.getExport({
exportType: 'oas30',
restApiId: apiName,
stageName: stage,
accepts: 'application/yaml',
parameters: {
extensions: 'integrations',
},
}).promise()
return oas30yaml
}

const uploadSwaggerToS3 = async function uploadSwaggerToS3(swagger, bucket, key, region, creds) {
const s3 = new AWS.S3({
credentials: creds,
Expand All @@ -50,26 +101,42 @@ const uploadSwaggerToS3 = async function uploadSwaggerToS3(swagger, bucket, key,
}).promise()
}

const getBucketAndKey = function getBucketAndKey(serverless) {
const getServerlessOptions = function getServerlessOptions(serverless) {
const bucket = serverless.service.custom.swaggerDestinations.s3BucketName
const key = serverless.service.custom.swaggerDestinations.s3KeyName
const openapi = serverless.service.custom.swaggerDestinations.uploadOpenAPI
const yaml = serverless.service.custom.swaggerDestinations.uploadYaml
if (!bucket || !key) {
throw new Error('ExportSwagger: Bucket name and key are required fields')
}
return { bucket, key }
};
return { bucket, key, openapi, yaml }
}

const exportApi = async function exportApi(serverless) {
const provider = serverless.getProvider('aws')
const awsCredentials = provider.getCredentials().credentials
const region = provider.getRegion()
const stage = provider.getStage()
const serviceName = serverless.service.getServiceName()
const { bucket, key } = getBucketAndKey(serverless)
const { bucket, key, openapi, yaml } = getServerlessOptions(serverless)
const apiName = await getApiName(serviceName, stage, region, awsCredentials)
const swagger = await getSwagger(apiName, stage, region, awsCredentials)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@babyhuey i think we should check for the new options here itself

if (openapi) {
  if (yaml) {
    //get yaml openapi and upload it
  } else {
    //get json openapi and upload it
  }
} else {
  if (yaml) {
    //get swagger yaml and upload it
  } else {
    //get swagger json and upload it
  }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I set it up the way I did because I wanted all 4 styles uploaded. If I understand this correctly, it will only upload one file, correct?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes my update will upload only one version, is there a reason why one would need all the versions uploaded?

await uploadSwaggerToS3(swagger, bucket, key, region, awsCredentials)
serverless.cli.consoleLog('ExportSwagger: File uploaded to s3.')
const swaggeryaml = await getSwaggerYaml(apiName, stage, region, awsCredentials)
const oas30 = await getOpenapi(apiName, stage, region, awsCredentials)
const oas30yaml = await getOpenapiYaml(apiName, stage, region, awsCredentials)
const swaggerKey = `${key}-swagger.json`
await uploadSwaggerToS3(swagger, bucket, swaggerKey, region, awsCredentials)
if (typeof openapi !== 'undefined') {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than checking for !== 'undefined' can you change this to === true.. do this everywhere you are checking

const oas30Key = `${key}-oas30.json`
await uploadSwaggerToS3(oas30, bucket, oas30Key, region, awsCredentials) }
if (typeof yaml !== 'undefined') {
const swaggerYamlKey = `${key}-swagger.yaml`
await uploadSwaggerToS3(swaggeryaml, bucket, swaggerYamlKey, region, awsCredentials) }
if (typeof openapi !== 'undefined' && typeof yaml !== 'undefined') {
const oas30YamlKey = `${key}-oas30.yaml`
await uploadSwaggerToS3(oas30yaml, bucket, oas30YamlKey, region, awsCredentials) }

serverless.cli.consoleLog('ExportSwagger: Files uploaded to s3.')
}

/**
Expand Down