Created
September 16, 2015 16:00
-
-
Save uiur/95f1626b4f32bbc5a62a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = require('./package.json') | |
var extend = require('xtend') | |
function lambdaUri (args) { | |
var region = 'us-east-1' | |
var accountId = args.accountId | |
var functionName = args.functionName | |
return 'arn:aws:apigateway:' + region + ':lambda:path/2015-03-31/functions/arn:aws:lambda:' + region + ':' + accountId + ':function:' + functionName + '/invocations' | |
} | |
function integration (config, functionName) { | |
return { | |
type: 'aws', | |
uri: lambdaUri({ accountId: config.deploy.accountId, functionName: functionName }), | |
credentials: 'arn:aws:iam::' + config.deploy.accountId + ':role/' + config.deploy.roleName, | |
httpMethod: 'POST', | |
responses: { | |
'.*error.*': { | |
statusCode: '400' | |
}, | |
default: { | |
statusCode: '200' | |
} | |
} | |
} | |
} | |
function transformOperation (paths, transform) { | |
paths = extend(paths) | |
Object.keys(paths).map(function (path) { | |
Object.keys(paths[path]).map(function (method) { | |
var operation = paths[path][method] | |
paths[path][method] = transform(operation) | |
}) | |
}) | |
return paths | |
} | |
function operationIdToFunctionName (config, operationId) { | |
return config.name + '-' + operationId.replace(/[^a-zA-Z0-9-_]/g, '-') | |
} | |
function convertPaths (paths) { | |
return transformOperation(paths, function (operation) { | |
operation['x-amazon-apigateway-auth'] = { | |
type: 'none' | |
} | |
var functionName = operationIdToFunctionName(config, operation.operationId) | |
operation['x-amazon-apigateway-integration'] = integration(config, functionName) | |
return operation | |
}) | |
} | |
var swagger = { | |
swagger: '2.0', | |
info: { | |
title: config.name, | |
description: config.description || config.name, | |
version: config.version | |
}, | |
schemes: ['http', 'https'], | |
paths: convertPaths(config.deploy.paths) | |
} | |
console.log(JSON.stringify(swagger, null, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment