@resourcefulhumans/rheactor-aws-lambda
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -14,6 +14,2 @@ 'use strict'; | ||
var _joi = require('joi'); | ||
var _joi2 = _interopRequireDefault(_joi); | ||
var _rheactorModels = require('rheactor-models'); | ||
@@ -52,2 +48,4 @@ | ||
var allowedMethods = /^(GET|POST)$/; | ||
_bluebird2.default.try(function () { | ||
@@ -59,7 +57,9 @@ (0, _api.checkContentType)(event, contentType); | ||
if (!operation.length || !operations[operation]) throw new _rheactorModels.HttpProblem(new _rheactorValueObjects.URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Unknown operation "' + event.path + '"', 404); | ||
var v = _joi2.default.validate(event.httpMethod, _joi2.default.string().lowercase().required().valid(['GET', 'POST'])); | ||
var method = v.value.toLowerCase(); | ||
if (v.error || !operations[operation][method]) { | ||
throw new _rheactorModels.HttpProblem(new _rheactorValueObjects.URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Unsupported action "' + event.httpMethod + ' ' + event.path + '"', 400); | ||
if (!allowedMethods.test(event.httpMethod)) { | ||
throw new _rheactorModels.HttpProblem(new _rheactorValueObjects.URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Method not allowed: "' + event.httpMethod + '"', 405); | ||
} | ||
var method = event.httpMethod.toLowerCase(); | ||
if (typeof operations[operation][method] === 'undefined') { | ||
throw new _rheactorModels.HttpProblem(new _rheactorValueObjects.URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Unsupported operation "' + event.httpMethod + ' ' + event.path + '"', 400); | ||
} | ||
var body = event.body ? JSON.parse(event.body) : {}; | ||
@@ -66,0 +66,0 @@ return (0, _api.getOptionalToken)(event, tokenSecretOrPrivateKey).then(function (token) { |
@@ -31,14 +31,2 @@ 'use strict'; | ||
var _util = require('./util'); | ||
Object.keys(_util).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _util[key]; | ||
} | ||
}); | ||
}); | ||
var _apiindex = require('./operations/apiindex'); | ||
@@ -45,0 +33,0 @@ |
{ | ||
"name": "@resourcefulhumans/rheactor-aws-lambda", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Core components for RESTful AWS lambda endpoints", | ||
@@ -35,3 +35,2 @@ "main": "dist/index.js", | ||
"bluebird": "^3.4.7", | ||
"joi": "^10.2.1", | ||
"jsonwebtoken": "^7.2.1", | ||
@@ -38,0 +37,0 @@ "rheactor-models": "3.x >=3.2.2", |
import Promise from 'bluebird' | ||
import {checkContentType, getOptionalToken} from './api' | ||
import Joi from 'joi' | ||
import {HttpProblem} from 'rheactor-models' | ||
@@ -34,2 +33,4 @@ import {URIValue} from 'rheactor-value-objects' | ||
const allowedMethods = /^(GET|POST)$/ | ||
Promise | ||
@@ -42,7 +43,9 @@ .try(() => { | ||
if (!operation.length || !operations[operation]) throw new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), `Unknown operation "${event.path}"`, 404) | ||
const v = Joi.validate(event.httpMethod, Joi.string().lowercase().required().valid(['GET', 'POST'])) | ||
const method = v.value.toLowerCase() | ||
if (v.error || !operations[operation][method]) { | ||
throw new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), `Unsupported action "${event.httpMethod} ${event.path}"`, 400) | ||
if (!allowedMethods.test(event.httpMethod)) { | ||
throw new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), `Method not allowed: "${event.httpMethod}"`, 405) | ||
} | ||
const method = event.httpMethod.toLowerCase() | ||
if (typeof operations[operation][method] === 'undefined') { | ||
throw new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), `Unsupported operation "${event.httpMethod} ${event.path}"`, 400) | ||
} | ||
const body = event.body ? JSON.parse(event.body) : {} | ||
@@ -49,0 +52,0 @@ return getOptionalToken(event, tokenSecretOrPrivateKey) |
export * from './api' | ||
export * from './handler' | ||
export * from './util' | ||
export * from './operations/apiindex' | ||
export * from './operations/status' |
@@ -10,6 +10,4 @@ import Promise from 'bluebird' | ||
*/ | ||
export const statusOperation = (version, environment, deployTime) => { | ||
return { | ||
post: () => Promise.resolve(new Status('ok', new Date(), `${version}+${environment}.${deployTime}`)) | ||
} | ||
} | ||
export const statusOperation = (version, environment, deployTime) => ({ | ||
post: () => Promise.resolve(new Status('ok', new Date(), `${version}+${environment}.${deployTime}`)) | ||
}) |
@@ -53,3 +53,3 @@ /* global describe, it */ | ||
it('should send bad request if operation does not support method', done => { | ||
it('should send method not allwed if unsupported method is used', done => { | ||
const path = '/status' | ||
@@ -70,2 +70,34 @@ const httpMethod = 'DELETE' | ||
expect(err).to.equal(null) | ||
expect(res.statusCode).to.equal(405) | ||
expect(res.headers).to.deep.equal({ | ||
'Content-Type': contentType, | ||
'Access-Control-Allow-Origin': '*' | ||
}) | ||
const expectedProblem = new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Method not allowed: "DELETE"', 405) | ||
const body = JSON.parse(res.body) | ||
const sentProblem = HttpProblem.fromJSON(body) | ||
expect(sentProblem.name).to.equal(expectedProblem.name) | ||
expect(sentProblem.type.equals(expectedProblem.type)).to.equal(true) | ||
expect(sentProblem.title).to.equal(expectedProblem.title) | ||
expect(sentProblem.$context).to.equal(expectedProblem.$context) | ||
done() | ||
}) | ||
}) | ||
it('should send bad request if operation does not support method', done => { | ||
const path = '/status' | ||
const httpMethod = 'GET' | ||
const body = JSON.stringify({}) | ||
handler( | ||
contentType, | ||
environment, | ||
tokenSecretOrPrivateKey, | ||
operations, | ||
{ | ||
headers, | ||
httpMethod, | ||
path, | ||
body | ||
}, null, (err, res) => { | ||
expect(err).to.equal(null) | ||
expect(res.statusCode).to.equal(400) | ||
@@ -76,3 +108,3 @@ expect(res.headers).to.deep.equal({ | ||
}) | ||
const expectedProblem = new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Unsupported action "DELETE /status"', 400) | ||
const expectedProblem = new HttpProblem(new URIValue('https://github.com/ResourcefulHumans/rheactor-aws-lambda#Error'), 'Unsupported operation "GET /status"', 400) | ||
const body = JSON.parse(res.body) | ||
@@ -79,0 +111,0 @@ const sentProblem = HttpProblem.fromJSON(body) |
5
28603
16
687