diff --git a/.gitignore b/.gitignore index 00cbbdf..4460c35 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,5 @@ typings/ # dotenv environment variables file .env +# intellij files +.idea/ diff --git a/README.md b/README.md index 34de199..3d620ab 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/package-lock.json b/package-lock.json index 1249bc1..055e1a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -99,9 +99,9 @@ "dev": true }, "aws-sdk": { - "version": "2.385.0", - "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.385.0.tgz", - "integrity": "sha512-ensf8QVbFdHQYLwhHgkH0FpE7nVxDl1SYJMM09p7neveW48rxQEwSOi/BmXDuI2/xsm7aHASUEZqK6zzeEx77g==", + "version": "2.410.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.410.0.tgz", + "integrity": "sha512-1ekVT5gr1m2yJXHZbHb8TAJogLA3Xj//b7yffHNXScAjFx2KNE+hmJ29J716372C0HDNgIPTKqWBp+lsYhvrjA==", "requires": { "buffer": "4.9.1", "events": "1.1.1", diff --git a/package.json b/package.json index 8c90744..e2d8cc5 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -30,6 +30,6 @@ "eslint-plugin-react": "^7.5.1" }, "dependencies": { - "aws-sdk": "^2.385.0" + "aws-sdk": "^2.410.0" } } diff --git a/src/index.js b/src/index.js index 4d2ebdf..0ad8def 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -50,14 +101,16 @@ 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') @@ -65,11 +118,25 @@ const exportApi = async function exportApi(serverless) { 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) - 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') { + 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.') } /**