claudia-api-builder
Advanced tools
Comparing version
{ | ||
"name": "claudia-api-builder", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "Simplify AWS ApiGateway handling", | ||
@@ -31,3 +31,3 @@ "license": "MIT", | ||
"scripts": { | ||
"pretest": "jshint src spec && jscs src spec", | ||
"pretest": "eslint .", | ||
"test": "node spec/support/jasmine-runner.js", | ||
@@ -39,7 +39,8 @@ "debug": "node debug spec/support/jasmine-runner.js" | ||
"bluebird": "^3.3.0", | ||
"eslint": "^3.12.2", | ||
"eslint-config-crockford": "^0.2.0", | ||
"eslint-config-defaults": "^9.0.0", | ||
"jasmine": "^2.5.2", | ||
"jasmine-spec-reporter": "^2.7.0", | ||
"jscs": "^2.9.0", | ||
"jshint": "^2.9.1" | ||
"jasmine-spec-reporter": "^2.7.0" | ||
} | ||
} |
@@ -1,9 +0,17 @@ | ||
/*global module, require, Promise, console */ | ||
var convertApiGWProxyRequest = require('./convert-api-gw-proxy-request'), | ||
const convertApiGWProxyRequest = require('./convert-api-gw-proxy-request'), | ||
lowercaseKeys = require('./lowercase-keys'); | ||
module.exports = function ApiBuilder(options) { | ||
'use strict'; | ||
var self = this, | ||
let customCorsHandler, | ||
customCorsHeaders, | ||
customCorsMaxAge, | ||
unsupportedEventCallback, | ||
authorizers, | ||
interceptCallback, | ||
requestFormat, | ||
binaryMediaTypes; | ||
const self = this, | ||
getRequestFormat = function (newFormat) { | ||
var supportedFormats = ['AWS_PROXY', 'CLAUDIA_API_BUILDER']; | ||
const supportedFormats = ['AWS_PROXY', 'CLAUDIA_API_BUILDER']; | ||
if (!newFormat) { | ||
@@ -19,12 +27,17 @@ return 'CLAUDIA_API_BUILDER'; | ||
}, | ||
requestFormat = getRequestFormat(options && options.requestFormat), | ||
defaultBinaryMediaTypes = [ | ||
'image/webp', | ||
'image/*', | ||
'image/jpg', | ||
'image/jpeg', | ||
'image/gif', | ||
'image/png', | ||
'application/octet-stream', | ||
'application/pdf', | ||
'application/zip' | ||
], | ||
logger = (options && options.logger) || console.log, | ||
methodConfigurations = {}, | ||
routes = {}, | ||
customCorsHandler, | ||
postDeploySteps = {}, | ||
customCorsHeaders, | ||
customCorsMaxAge, | ||
unsupportedEventCallback, | ||
authorizers, | ||
v2DeprecationWarning = function (what) { | ||
@@ -34,3 +47,2 @@ logger(what + ' are deprecated, and be removed in claudia api builder v3. Check https://claudiajs.com/tutorials/migrating_to_2.html'); | ||
supportedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH'], | ||
interceptCallback, | ||
prompter = (options && options.prompter) || require('./ask'), | ||
@@ -50,3 +62,3 @@ isApiResponse = function (obj) { | ||
getContentType = function (configuration, result) { | ||
var staticHeader = (configuration && configuration.headers && lowercaseKeys(configuration.headers)['content-type']), | ||
const staticHeader = (configuration && configuration.headers && lowercaseKeys(configuration.headers)['content-type']), | ||
dynamicHeader = (result && isApiResponse(result) && result.headers && lowercaseKeys(result.headers)['content-type']), | ||
@@ -58,3 +70,3 @@ staticConfig = configuration && configuration.contentType; | ||
getStatusCode = function (configuration, result, resultType) { | ||
var defaultCode = { | ||
const defaultCode = { | ||
'success': 200, | ||
@@ -68,3 +80,3 @@ 'error': 500 | ||
getRedirectLocation = function (configuration, result) { | ||
var dynamicHeader = result && isApiResponse(result) && result.headers && lowercaseKeys(result.headers).location, | ||
const dynamicHeader = result && isApiResponse(result) && result.headers && lowercaseKeys(result.headers).location, | ||
dynamicBody = isApiResponse(result) ? result.response : result, | ||
@@ -78,3 +90,3 @@ staticHeader = configuration && configuration.headers && lowercaseKeys(configuration.headers).location; | ||
getSuccessBody = function (contentType, handlerResult) { | ||
var contents = isApiResponse(handlerResult) ? handlerResult.response : handlerResult; | ||
const contents = isApiResponse(handlerResult) ? handlerResult.response : handlerResult; | ||
if (getCanonicalContentType(contentType) === 'application/json') { | ||
@@ -89,2 +101,4 @@ if (contents === '' || contents === undefined) { | ||
return ''; | ||
} else if (Buffer.isBuffer(contents)) { | ||
return contents.toString('base64'); | ||
} else if (typeof contents === 'object') { | ||
@@ -101,3 +115,3 @@ return JSON.stringify(contents); | ||
logError = function (err) { | ||
var logInfo = err; | ||
let logInfo = err; | ||
if (isApiResponse(err)) { | ||
@@ -111,3 +125,3 @@ logInfo = JSON.stringify(err); | ||
getErrorBody = function (contentType, handlerResult) { | ||
var contents = isApiResponse(handlerResult) ? handlerResult.response : handlerResult; | ||
let contents = isApiResponse(handlerResult) ? handlerResult.response : handlerResult; | ||
if (isError(contents)) { | ||
@@ -126,3 +140,3 @@ contents = contents.message; | ||
packResult = function (handlerResult, routingInfo, corsHeaders, resultType) { | ||
var path = routingInfo.path.replace(/^\//, ''), | ||
const path = routingInfo.path.replace(/^\//, ''), | ||
method = routingInfo.method, | ||
@@ -138,2 +152,5 @@ configuration = methodConfigurations[path] && methodConfigurations[path][method] && methodConfigurations[path][method][resultType], | ||
}; | ||
if (configuration && configuration.contentHandling === 'CONVERT_TO_BINARY' && resultType === 'success') { | ||
result.isBase64Encoded = true; | ||
} | ||
mergeObjects(corsHeaders, result.headers); | ||
@@ -178,7 +195,6 @@ if (customHeaders) { | ||
routeEvent = function (routingInfo, event, context) { | ||
var handler; | ||
if (!routingInfo) { | ||
throw 'routingInfo not set'; | ||
} | ||
handler = routes[routingInfo.path] && ( | ||
const handler = routes[routingInfo.path] && ( | ||
routes[routingInfo.path][routingInfo.method] || | ||
@@ -243,4 +259,4 @@ routes[routingInfo.path].ANY | ||
self[method.toLowerCase()] = function (route, handler, options) { | ||
var pathPart = route.replace(/^\//, ''), | ||
canonicalRoute = route; | ||
const pathPart = route.replace(/^\//, ''); | ||
let canonicalRoute = route; | ||
if (!/^\//.test(canonicalRoute)) { | ||
@@ -250,3 +266,3 @@ canonicalRoute = '/' + route; | ||
if (!methodConfigurations[pathPart]) { | ||
methodConfigurations[pathPart] = {} ; | ||
methodConfigurations[pathPart] = {}; | ||
} | ||
@@ -261,5 +277,4 @@ methodConfigurations[pathPart][method] = (options || {}); | ||
['ANY'].concat(supportedMethods).forEach(setUpHandler); | ||
self.apiConfig = function () { | ||
var result = {version: 3, routes: methodConfigurations}; | ||
const result = {version: 3, routes: methodConfigurations}; | ||
if (customCorsHandler !== undefined) { | ||
@@ -277,2 +292,5 @@ result.corsHandlers = !!customCorsHandler; | ||
} | ||
if (binaryMediaTypes) { | ||
result.binaryMediaTypes = binaryMediaTypes; | ||
} | ||
return result; | ||
@@ -320,7 +338,7 @@ }; | ||
self.proxyRouter = function (event, context, callback) { | ||
var request = getRequest(event, context), | ||
routingInfo, | ||
const request = getRequest(event, context), | ||
handleError = function (e) { | ||
context.done(e); | ||
}; | ||
let routingInfo; | ||
context.callbackWaitsForEmptyEventLoop = false; | ||
@@ -369,4 +387,4 @@ return executeInterceptor(request, context).then(function (modifiedRequest) { | ||
self.addPostDeployStep(stageVarName, function (options, lambdaDetails, utils) { | ||
var configureDeployment = function (varValue) { | ||
var result = { | ||
const configureDeployment = function (varValue) { | ||
const result = { | ||
restApiId: lambdaDetails.apiId, | ||
@@ -399,3 +417,3 @@ stageName: lambdaDetails.alias, | ||
self.postDeploy = function (options, lambdaDetails, utils) { | ||
var steps = Object.keys(postDeploySteps), | ||
const steps = Object.keys(postDeploySteps), | ||
stepResults = {}, | ||
@@ -431,2 +449,9 @@ executeStepMapper = function (stepName) { | ||
}; | ||
self.setBinaryMediaTypes = function (types) { | ||
binaryMediaTypes = types; | ||
}; | ||
binaryMediaTypes = defaultBinaryMediaTypes; | ||
requestFormat = getRequestFormat(options && options.requestFormat); | ||
['ANY'].concat(supportedMethods).forEach(setUpHandler); | ||
}; |
/*global require, module, process */ | ||
var readline = require('readline'); | ||
const readline = require('readline'); | ||
@@ -7,3 +7,3 @@ module.exports = function ask(question, PromiseImpl) { | ||
return new PromiseImpl(function (resolve, reject) { | ||
var rl = readline.createInterface({ | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
@@ -10,0 +10,0 @@ output: process.stdout |
/*global module, require */ | ||
var qs = require('querystring'), | ||
const qs = require('querystring'), | ||
lowercaseKeys = require('./lowercase-keys'), | ||
getCanonicalContentType = function (normalizedHeaders) { | ||
'use strict'; | ||
var contentType = normalizedHeaders['content-type'] || ''; | ||
let contentType = normalizedHeaders['content-type'] || ''; | ||
if (contentType.indexOf(';') >= 0) { | ||
@@ -20,3 +20,3 @@ contentType = contentType.split(';')[0]; | ||
'use strict'; | ||
var identity = requestContext.identity || {}; | ||
const identity = requestContext.identity || {}; | ||
return { | ||
@@ -35,7 +35,21 @@ method: (requestContext.httpMethod || 'GET').toUpperCase(), | ||
cognitoAuthenticationProvider: identity.cognitoAuthenticationProvider, | ||
cognitoAuthenticationType: identity.cognitoAuthenticationType, | ||
cognitoIdentityId: identity.cognitoIdentityId, | ||
cognitoAuthenticationType: identity.cognitoAuthenticationType, | ||
cognitoIdentityId: identity.cognitoIdentityId, | ||
cognitoIdentityPoolId: identity.cognitoIdentityPoolId | ||
}; | ||
}, | ||
getConvertedBody = function (body, contentType, isBase64Encoded) { | ||
'use strict'; | ||
const textContentTypes = ['application/json', 'text/plain', 'application/xml', 'text/xml', 'application/x-www-form-urlencoded']; | ||
if (!isBase64Encoded) { | ||
return body; | ||
} else { | ||
const buffer = new Buffer(body, 'base64'); | ||
if (textContentTypes.indexOf(contentType) >= 0) { | ||
return buffer.toString('utf8'); | ||
} else { | ||
return buffer; | ||
} | ||
} | ||
}; | ||
@@ -45,3 +59,3 @@ | ||
'use strict'; | ||
var result = { | ||
const result = { | ||
v: 3, | ||
@@ -53,3 +67,4 @@ rawBody: request.body || '', | ||
}, | ||
canonicalContentType = getCanonicalContentType(result.normalizedHeaders); | ||
canonicalContentType = getCanonicalContentType(result.normalizedHeaders), | ||
convertedBody = getConvertedBody(result.rawBody, canonicalContentType, request.isBase64Encoded); | ||
@@ -63,10 +78,10 @@ copyProperties(request, result, { | ||
if (canonicalContentType === 'application/x-www-form-urlencoded') { | ||
result.post = qs.parse(result.rawBody); | ||
result.post = qs.parse(convertedBody); | ||
} | ||
if (canonicalContentType === 'application/json' && | ||
(typeof result.rawBody !== 'object' || !result.rawBody) // null will also result in type 'object' | ||
(typeof convertedBody !== 'object' || !convertedBody) // null will also result in type 'object' | ||
) { | ||
result.body = JSON.parse(result.rawBody || '{}'); | ||
result.body = JSON.parse(convertedBody || '{}'); | ||
} else { | ||
result.body = result.rawBody; | ||
result.body = convertedBody; | ||
} | ||
@@ -73,0 +88,0 @@ result.context = request.requestContext ? convertContext(request.requestContext) : {}; |
/*global module */ | ||
module.exports = function lowercaseKeys(object) { | ||
'use strict'; | ||
var result = {}; | ||
const result = {}; | ||
if (object && typeof object === 'object' && !Array.isArray(object)) { | ||
@@ -6,0 +6,0 @@ Object.keys(object).forEach(function (key) { |
25614
5.11%556
7.34%6
20%