Comparing version 0.0.3 to 0.0.4
52
api.js
var fs = require('fs'); | ||
var _ = require('lodash'); | ||
var sa = require('superagent'); | ||
@@ -18,4 +17,2 @@ | ||
readStub(req, res, next, callArgs); | ||
else if (!callArgs.apiPath) | ||
next(); | ||
else | ||
@@ -29,3 +26,3 @@ getData(req, res, next, callArgs); | ||
// if path ends with '/', assume it needs an id | ||
// if path ends with '/', assume it gets an id from the express request :id matcher | ||
callArgs.apiPath = callArgs.apiPath.match(/\/$/) ? callArgs.apiPath + req.params.id : callArgs.apiPath; | ||
@@ -36,14 +33,2 @@ callArgs.apiVerb = callArgs.apiVerb || 'get'; | ||
callArgs.paramMethod = (callArgs.apiVerb === 'get') ? 'query' : 'send'; | ||
// TODO - assuming http to API server, should probably be customizable | ||
var apiServer = callArgs.apiHost ? 'http://' + callArgs.apiHost : config.apiServer; | ||
callArgs.apiPath = apiServer + callArgs.apiPath; // we don't want the apiServer in timer results | ||
// set status codes to handle at the component level rather than framework level | ||
var validStatusCodes = _.clone(config.defaultValidStatusCodes || []); | ||
if (typeof callArgs.handleError === 'number') | ||
validStatusCodes.push(callArgs.handleError); | ||
else if (callArgs.handleError instanceof Array) | ||
validStatusCodes = validStatusCodes.concat(callArgs.handleError); | ||
@@ -63,24 +48,14 @@ config.beforeApiCall(callArgs, req, res); | ||
call.end(function(err, response) { | ||
config.afterApiCall(callArgs, response, req, res); | ||
var error = true, errorHandledByComponent = false; | ||
if (!err && response.body && (callArgs.handleError === true || validStatusCodes.indexOf(response.statusCode) > -1)) { | ||
error = null; | ||
errorHandledByComponent = true; | ||
if (!err && response.body) { | ||
res.locals[callArgs.namespace] = response.body; | ||
res.locals[callArgs.namespace].statusCode = response.statusCode; | ||
config.onApiSuccess(response, callArgs, req, res); | ||
} | ||
callArgs.apiError = err; | ||
callArgs.apiResponse = response; | ||
// do error logic if we see certain errors, even if handled by component | ||
if (error || (errorHandledByComponent && config.defaultValidStatusCodes.indexOf(response.statusCode) === -1 && response.body.errors)) { | ||
config.onApiError(err, response, callArgs, req, res); | ||
} | ||
if ((!error || errorHandledByComponent) && callArgs.callback) { | ||
error = null; | ||
callArgs.callback(req, res); | ||
} | ||
setTimeout(function() { next(error); }, req.nodule.apiSleep); // simulates API taking a certain amount of time | ||
if (config.afterApiCall) | ||
config.afterApiCall(callArgs, req, res, next); | ||
else | ||
next(err); | ||
}); | ||
@@ -106,8 +81,7 @@ } | ||
config.onApiSuccess(data, callArgs, req, res); | ||
if (callArgs.callback) callArgs.callback(req, res); | ||
setTimeout(function() { next(); }, req.nodule.apiSleep); // simulates API call | ||
if (config.afterStubCall) | ||
config.afterStubCall(callArgs, req, res, next); | ||
else | ||
next(); | ||
} | ||
}; |
42
index.js
@@ -35,24 +35,17 @@ var _ = require('lodash'); | ||
var defaultConfig = { | ||
// required, to set apiServer on individual nodule, use nodule.apiHost | ||
apiServer: [], | ||
// called at the start of every api call if defined | ||
beforeApiCall: null, | ||
// called at the start of every api call, things like timers and custom headers can be set here | ||
beforeApiCall: function(callArgs, req, res) { }, | ||
// called after every api call if defined | ||
afterApiCall: null, | ||
// called after every api call, regardless of success or failure, do things like logging here | ||
// Note: called before onApiSuccess and onApiError | ||
afterApiCall: function(callArgs, req, res) { }, | ||
// called after every stub call if defined | ||
afterStubCall: null, | ||
// called on every successful API call (can be called multiple times per request if running multiple API calls) | ||
onApiSuccess: function(req, res) { }, | ||
// called on every failed API call (should only be called once per request as framework routes into error flow on first unhandled error) | ||
onApiError: function(req, res) { }, | ||
// these API calls will always be assumed success, all others will be routed through error flow unless handleError is set at the nodule level | ||
defaultValidStatusCodes: [200, 204], | ||
// directory to start in for templateNames with a directory in them | ||
templateRoot: 'nodules', | ||
// alternate place to look for stubs than the nodule dir | ||
sharedStubPath: null, | ||
// default debug function | ||
@@ -125,10 +118,2 @@ yukonCustomDebug: function(identifier) { | ||
// can be [null|true|number|array] - tells the component to handle the error rather than the framework | ||
// if there are multiple APIs, set a value for each API - within an array | ||
// set to true to handle all API errors locally, | ||
// set to a number (IE - 503 to handle all 503 statusCodes) | ||
// set to array (IE [404,502,503] to handle all those statusCodes) | ||
// NOTE: statusCodes 200,204,400,401,403 are always handled by the component | ||
handleError: null, | ||
// if not specified, app looks for [nodule name].stub.json - looks for stubName first in nodule folder, then in app/shared/stubs | ||
@@ -138,4 +123,4 @@ // Note: can be array to use stubs for multiple api calls | ||
// alternate place to look for stubs than the nodule dir, can be changed at request-time if needed | ||
sharedStubPath: null, | ||
// set true to force api to use stub (IE - if API isn't ready yet) | ||
forceStub: false, | ||
@@ -145,6 +130,3 @@ // 'html', 'json' only current values - use this to force any nodule to behave like a json or html call regardless of naming conventions or directory conventions | ||
// set true to force api to use stub (IE - if API isn't ready yet) | ||
forceStub: false, | ||
// set this to an Error() instance to "throw" an error from your page - see channel.js for example | ||
// set this to an Error() instance to "throw" an error from your nodule - see channel.js for example | ||
error: null, | ||
@@ -151,0 +133,0 @@ |
{ | ||
"name": "yukon", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Self-discovering component-based API-driven framework based on express", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,5 +0,4 @@ | ||
// wraps nodule.postProcessor, called after all API calls return | ||
var path = require('path'); | ||
// wraps nodule.postProcessor, called after all API calls return | ||
module.exports = function(app, config) { | ||
@@ -6,0 +5,0 @@ var debug = config.debug('yukon->postApi'); |
// wraps nodule.preProcessor, called after app-level appPreApi middleware | ||
module.exports = function(app, config, api) { | ||
@@ -4,0 +3,0 @@ return function(req, res, next) { |
@@ -5,1 +5,3 @@ yukon | ||
Node component discovery and initialization interface | ||
Usage: Writeup and test cases coming by Jan 5th I promise! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
27880
318