Skip to content

Commit

Permalink
close #52 by adding main handler async support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Jul 11, 2018
1 parent 95994c7 commit 5086386
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class API {

// Set the event, context and callback
this._event = event
this._context = this.context = context
this._context = this.context = typeof context === 'object' ? context : {}
this._cb = cb

// Initalize request and response objects
Expand All @@ -136,7 +136,7 @@ class API {
try {

// Parse the request
request.parseRequest()
await request.parseRequest()

// Loop through the middleware and await response
for (const mw of this._middleware) {
Expand Down Expand Up @@ -175,9 +175,12 @@ class API {
}

} catch(e) {
this.catchErrors(e,response)
await this.catchErrors(e,response)
}

// Return the final response
return response._response

} // end run function


Expand Down Expand Up @@ -236,7 +239,7 @@ class API {
await this._finally(response._request,response)

// Execute the primary callback
this._cb(err,res)
typeof this._cb === 'function' && this._cb(err,res)

} // end _callback

Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class REQUEST {
} // end constructor

// Parse the request
parseRequest() {
async parseRequest() {

// Set the method
this.method = this.app._event.httpMethod.toUpperCase()
Expand Down
7 changes: 5 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class RESPONSE {

// Default Etag support
this._etag = false

// Default response object
this._response = {}
}

// Sets the statusCode
Expand Down Expand Up @@ -386,15 +389,15 @@ class RESPONSE {
}

// Create the response
const response = {
this._response = {
headers: this._headers,
statusCode: this._statusCode,
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body),
isBase64Encoded: this._isBase64
}

// Trigger the callback function
this.app._callback(null, response, this)
this.app._callback(null, this._response, this)

} // end send

Expand Down
86 changes: 86 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const expect = require('chai').expect // Assertion library

// Init API instance
const api = require('../index')({ version: 'v1.0' })
const api_error = require('../index')({ version: 'v1.0' })
const api_error_path = require('../index')({ version: 'v1.0' })

// NOTE: Set test to true
api._test = true;
api_error._test = true;
api_error_path._test = true;

let event = {
httpMethod: 'get',
path: '/test',
body: {},
headers: {
'Content-Type': 'application/json'
}
}

/******************************************************************************/
/*** DEFINE TEST ROUTE ***/
/******************************************************************************/
api.get('/test', function(req,res) {
res.status(200).json({
method: 'get',
status: 'ok'
})
})

api.get('/testError', function(req,res) {
res.status(404).error('some error')
})

api_error.get('/testErrorThrown', function(req,res) {
throw new Error('some thrown error')
})


/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/

describe('Main handler Async/Await:', function() {

it('With context object', async function() {
let _event = Object.assign({},event,{})
let result = await api.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('Without context object', async function() {
let _event = Object.assign({},event,{})
let result = await api.run(_event)
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('With callback', async function() {
let _event = Object.assign({},event,{})
let result = await api.run(_event,{},(err,res) => {})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('Triggered Error', async function() {
let _event = Object.assign({},event,{ path: '/testError' })
let result = await api.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '{"error":"some error"}', isBase64Encoded: false })
}) // end it

it('Thrown Error', async function() {
let _event = Object.assign({},event,{ path: '/testErrorThrown' })
let result = await api_error.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 500, body: '{"error":"some thrown error"}', isBase64Encoded: false })
}) // end it

it('Routes Error', async function() {
let _event = Object.assign({},event,{ path: '/testRoute' })
let result = await api_error_path.run(_event,{})
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '{"error":"Route not found"}', isBase64Encoded: false })
}) // end it


}) // end tests

0 comments on commit 5086386

Please sign in to comment.