lambda-api
Advanced tools
Comparing version 0.8.1 to 0.9.0
20
index.js
@@ -6,11 +6,12 @@ 'use strict' | ||
* @author Jeremy Daly <jeremy@jeremydaly.com> | ||
* @version 0.8.1 | ||
* @version 0.9.0 | ||
* @license MIT | ||
*/ | ||
const REQUEST = require('./lib/request.js') // Resquest object | ||
const RESPONSE = require('./lib/response.js') // Response object | ||
const UTILS = require('./lib/utils.js') // Require utils library | ||
const LOGGER = require('./lib/logger.js') // Require logger library | ||
const REQUEST = require('./lib/request') // Resquest object | ||
const RESPONSE = require('./lib/response') // Response object | ||
const UTILS = require('./lib/utils') // Require utils library | ||
const LOGGER = require('./lib/logger') // Require logger library | ||
const prettyPrint = require('./lib/prettyPrint') // Pretty print for debugging | ||
const { ConfigurationError } = require('./lib/errors') // Require custom errors | ||
@@ -28,2 +29,3 @@ // Create the API class | ||
this._mimeTypes = props && props.mimeTypes && typeof props.mimeTypes === 'object' ? props.mimeTypes : {} | ||
this._serializer = props && props.serializer && typeof props.serializer === 'function' ? props.serializer : JSON.stringify | ||
@@ -95,3 +97,3 @@ // Set sampling info | ||
if (typeof handler !== 'function') { | ||
throw new Error(`No route handler specified for ${method} method on ${path} route.`) | ||
throw new ConfigurationError(`No route handler specified for ${method} method on ${path} route.`) | ||
} | ||
@@ -153,3 +155,3 @@ | ||
// Set the event, context and callback | ||
this._event = event | ||
this._event = event || {} | ||
this._context = this.context = typeof context === 'object' ? context : {} | ||
@@ -219,2 +221,4 @@ this._cb = cb ? cb : undefined | ||
// console.log('\n\n------------------------\n',e,'\n------------------------\n\n'); | ||
// Error messages should never be base64 encoded | ||
@@ -314,3 +318,3 @@ response._isBase64 = false | ||
} else { | ||
throw new Error('Middleware must have 3 or 4 parameters') | ||
throw new ConfigurationError('Middleware must have 3 or 4 parameters') | ||
} | ||
@@ -317,0 +321,0 @@ } |
@@ -14,2 +14,3 @@ 'use strict' | ||
const UTILS = require('./utils') // Require utils library | ||
const { ConfigurationError } = require('./errors') // Require custom errors | ||
@@ -25,3 +26,3 @@ // Config logger | ||
if (!/^[A-Za-z_]\w*$/.test(lvl) || isNaN(cfg.levels[lvl])) { | ||
throw new Error('Invalid level configuration') | ||
throw new ConfigurationError('Invalid level configuration') | ||
} | ||
@@ -230,3 +231,3 @@ } | ||
// Error on invalid config | ||
if (cfg === false) throw new Error('Invalid sampler configuration') | ||
if (cfg === false) throw new ConfigurationError('Invalid sampler configuration') | ||
@@ -250,3 +251,3 @@ // Create rule default | ||
if (!rule.route || typeof rule.route !== 'string') | ||
throw new Error('Invalid route specified in rule') | ||
throw new ConfigurationError('Invalid route specified in rule') | ||
@@ -253,0 +254,0 @@ // Parse methods into array (if not already) |
@@ -10,4 +10,5 @@ 'use strict' | ||
const QS = require('querystring') // Require the querystring library | ||
const UTILS = require('./utils.js') // Require utils library | ||
const LOGGER = require('./logger.js') // Require logger library | ||
const UTILS = require('./utils') // Require utils library | ||
const LOGGER = require('./logger') // Require logger library | ||
const { RouteError, MethodError } = require('./errors') // Require custom errors | ||
@@ -58,3 +59,3 @@ class REQUEST { | ||
// Set the method | ||
this.method = this.app._event.httpMethod.toUpperCase() | ||
this.method = this.app._event.httpMethod ? this.app._event.httpMethod.toUpperCase() : 'GET' | ||
@@ -68,3 +69,3 @@ // Set the path | ||
// Set the raw headers | ||
this.rawHeaders = this.app._event.headers | ||
this.rawHeaders = this.app._event.headers || {} | ||
@@ -98,2 +99,11 @@ // Set the headers to lowercase | ||
// Set the pathParameters | ||
this.pathParameters = this.app._event.pathParameters || {} | ||
// Set the stageVariables | ||
this.stageVariables = this.app._event.stageVariables || {} | ||
// Set the isBase64Encoded | ||
this.isBase64Encoded = this.app._event.isBase64Encoded || {} | ||
// Add context | ||
@@ -133,3 +143,3 @@ this.context = this.app.context && typeof this.app.context === 'object' ? this.app.context : {} | ||
// Extract path from event (strip querystring just in case) | ||
let path = UTILS.parsePath(this.app._event.path) | ||
let path = UTILS.parsePath(this.path) | ||
@@ -158,3 +168,3 @@ // Init the route | ||
this.app._errorStatus = 404 | ||
throw new Error('Route not found') | ||
throw new RouteError('Route not found','/'+path.join('/')) | ||
} | ||
@@ -187,3 +197,3 @@ } // end for loop | ||
this.app._errorStatus = 405 | ||
throw new Error('Method not allowed') | ||
throw new MethodError('Method not allowed',this.method,'/'+path.join('/')) | ||
} | ||
@@ -190,0 +200,0 @@ |
@@ -13,2 +13,3 @@ 'use strict' | ||
const path = require('path') // Require Node.js path | ||
const { ResponseError, FileError } = require('./errors') // Require custom errors | ||
@@ -32,2 +33,5 @@ // Require AWS S3 service | ||
// Create a reference to the JSON serializer | ||
this._serializer = app._serializer | ||
// Set the default state to processing | ||
@@ -91,3 +95,3 @@ this._state = 'processing' | ||
json(body) { | ||
this.header('Content-Type','application/json').send(JSON.stringify(body)) | ||
this.header('Content-Type','application/json').send(this._serializer(body)) | ||
} | ||
@@ -102,3 +106,3 @@ | ||
this.header('Content-Type','application/json') | ||
.send((cb ? cb.replace(' ','_') : 'callback') + '(' + JSON.stringify(body) + ')') | ||
.send((cb ? cb.replace(' ','_') : 'callback') + '(' + this._serializer(body) + ')') | ||
} | ||
@@ -128,3 +132,3 @@ | ||
} else { | ||
throw new Error(arguments[0] + ' is an invalid redirect status code') | ||
throw new ResponseError(arguments[0] + ' is an invalid redirect status code',arguments[0]) | ||
} | ||
@@ -265,3 +269,3 @@ } | ||
let opts = typeof options === 'object' ? options : {} | ||
let fn = typeof callback === 'function' ? callback : e => { if(e) this.error(e) } | ||
let fn = typeof callback === 'function' ? callback : () => {} | ||
@@ -305,3 +309,3 @@ // Add optional parameter support | ||
} else { | ||
throw new Error('Invalid file') | ||
throw new FileError('Invalid file',{path:file}) | ||
} | ||
@@ -344,3 +348,9 @@ | ||
await fn(e) | ||
this.error(e) // Throw error if not done in callback | ||
// If missing file | ||
if (e.code === 'ENOENT') { | ||
this.error(new FileError('No such file',e)) | ||
} else { | ||
this.error(e) // Throw error if not done in callback | ||
} | ||
} | ||
@@ -444,3 +454,3 @@ | ||
statusCode: this._statusCode, | ||
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body), | ||
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body,this._serializer), | ||
isBase64Encoded: this._isBase64 | ||
@@ -447,0 +457,0 @@ } |
@@ -11,2 +11,3 @@ 'use strict' | ||
const crypto = require('crypto') // Require Node.js crypto library | ||
const { FileError } = require('./errors') // Require custom errors | ||
@@ -35,4 +36,6 @@ const entityMap = { | ||
const encodeBody = body => | ||
typeof body === 'object' ? JSON.stringify(body) : (body && typeof body !== 'string' ? body.toString() : (body ? body : '')) | ||
const encodeBody = (body,serializer) => { | ||
const encode = typeof serializer === 'function' ? serializer : JSON.stringify | ||
return typeof body === 'object' ? encode(body) : (body && typeof body !== 'string' ? body.toString() : (body ? body : '')) | ||
} | ||
@@ -42,3 +45,3 @@ exports.encodeBody = encodeBody | ||
exports.parsePath = path => { | ||
return path.trim().split('?')[0].replace(/^\/(.*?)(\/)*$/,'$1').split('/') | ||
return path ? path.trim().split('?')[0].replace(/^\/(.*?)(\/)*$/,'$1').split('/') : [] | ||
} | ||
@@ -125,3 +128,3 @@ | ||
exports.parseS3 = path => { | ||
if (!this.isS3(path)) throw new Error('Invalid S3 path') | ||
if (!this.isS3(path)) throw new FileError('Invalid S3 path',{path}) | ||
let s3object = path.replace(/^s3:\/\//i,'').split('/') | ||
@@ -128,0 +131,0 @@ return { Bucket: s3object.shift(), Key: s3object.join('/') } |
{ | ||
"name": "lambda-api", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Lightweight web framework for your serverless applications", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
@@ -35,13 +36,15 @@ "test": "mocha --check-leaks --recursive", | ||
"devDependencies": { | ||
"aws-sdk": "^2.228.1", | ||
"bluebird": "^3.5.1", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.1", | ||
"@types/aws-lambda": "^8.10.15", | ||
"@types/node": "^10.12.9", | ||
"aws-sdk": "^2.357.0", | ||
"bluebird": "^3.5.3", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^4.0.1", | ||
"mocha": "^4.1.0", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"nyc": "^11.8.0", | ||
"nyc": "^11.9.0", | ||
"sinon": "^4.5.0" | ||
@@ -48,0 +51,0 @@ }, |
Sorry, the diff of this file is too big to display
119934
12
1305
1313
14