Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

yukon

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yukon - npm Package Compare versions

Comparing version
0.0.15
to
0.1.0
+37
middlewares/postProcessor.js
// wraps nodule.postProcessor, called after API calls return
var path = require('path');
// wraps nodule.postProcessor, called after all API calls return
module.exports = function(app, config) {
var debug = config.customDebug('yukon->postApi');
return function(req, res, next) {
debug("called");
var nodule = req.nodule;
// execute nodule-level post API business logic
nodule.postProcessor(req, res);
// nodules can throw their own errors on certain conditions
if (nodule.error) {
next(nodule.error);
return;
}
// convenience method so devs don't have to set renderData for default single API case
if (!res.renderData)
res.renderData = res.locals.data1 || {};
if (req.nodule.contentType !== 'json') {
// if template name is not specified assume (nodule name).(nodule extention)
var templateName = (nodule.templateName) ? nodule.templateName : nodule.name + nodule.templateExt;
res.templatePath = (templateName.indexOf('/') > -1)
? path.join(process.cwd(), templateName)
: path.join(nodule.path, templateName);
}
debug('res.templatePath = ' + res.templatePath);
next();
};
};
// wraps nodule.preProcessor, called after app-level appStart middleware
module.exports = function(app, config) {
return function(req, res, next) {
config.customDebug('yukon-preApi')('called');
req.nodule.preProcessor(req, res);
next();
};
};
+18
-198

@@ -1,202 +0,22 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
(The MIT License)
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>
1. Definitions.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@@ -10,3 +10,3 @@ // custom middleware invoked by doAPI (not express middleware)

module.exports = function(app, config) {
var debug = config.debug('yukon->api');
var debug = config.customDebug('yukon->api');

@@ -62,6 +62,4 @@ return {

if (config.apiCallback)
config.apiCallback(callArgs, req, res, next);
else
next(err);
// IMPORTANT: this function must call next() and therefore must always be last in this block
config.apiCallback(callArgs, req, res, next);
});

@@ -95,7 +93,5 @@ }

if (config.apiCallback)
config.apiCallback(callArgs, req, res, next);
else
next();
// IMPORTANT: this function must call next() and therefore must always be last in this block
config.apiCallback(callArgs, req, res, next);
}
};

@@ -1,5 +0,5 @@

// calls all APIs in parallel (inlcuding those added by the app-level appDoApi middleware)
// calls all APIs in parallel (inlcuding those added by the app-level appPreApi middleware)
module.exports = function(app, config) {
var debug = config.debug('yukon->doApi');
var debug = config.customDebug('yukon->doApi');

@@ -6,0 +6,0 @@ var api = require('./api.js')(app, config);

@@ -6,3 +6,3 @@ // called last in middleware chain, sends response to client

return function(req, res, next) {
config.debug('yukon->finish')('called');
config.customDebug('yukon->finish')('called');

@@ -9,0 +9,0 @@ if (req.nodule.contentType === 'json')

{
"name": "yukon",
"version": "0.0.15",
"version": "0.1.0",
"description": "Self-discovering component-based API-driven framework based on express",

@@ -18,3 +18,3 @@ "main": "yukon.js",

},
"license": "Apache 2.0",
"license": "MIT",
"bugs": {

@@ -26,3 +26,3 @@ "url": "https://github.com/jackspaniel/yukon/issues"

"lodash": "^2.4.1",
"nodulejs": "^1.1.12",
"nodulejs": "^1.1.17",
"superagent": "^0.21.0"

@@ -29,0 +29,0 @@ },

+296
-4

@@ -1,6 +0,298 @@

yukon API framework
=====
# yukon API framework
Node component discovery and initialization interface
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
Usage: Writeup and test cases coming by Jan 5th I promise!
yukon is a component-based framework built on top of node/express - which calls 0-N REST APIs in parallel - and extends the [nodulejs framework](https://github.com/jackspaniel/nodulejs).
"nodules" are self-discovering, self-initializing web components, which propagate throughout the express middleware chain as __req.nodule__. nodulejs was split off from yukon to separate out the core self-discovery and initialization features - which could be used as a potential building block for a wide variety of frameworks.
## Installation
```
$ npm install yukon
```
## Usage
```
require('yukon')(app, config);
```
+ __app__ = express instance.
+ __config__ = any custom properties you want to add or defaults you want to override. See the [demoApp](https://github.com/jackspaniel/yukon/blob/master/demo/demoApp.js) for an example of a workin yukon app. See the Config section below more details on config options.
## What is a yukon nodule?
A __nodule__ is a self-discovering, self-initializing component that would be to a JSP or PHP page in those worlds. A __yukon nodule__ extends the base nodule behavior to include back-end API calls, stub-handling and template-rendering.
Unlike the PHP/JSP worlds however, a nodule's route is declared and not tied by default to the filename or folder structure. So you are free to re-organize nodules without upsetting urls. But more importantly, because nodules are self-discovering, there are no onerous config files to maintain (IE - Spring). This system allows a much more scalable architecture on large sites--as there are no config or other shared files which grow to enormous sizes as the site grows, and nodules can be re-organized with zero impact.
## Motivation
From a __feature-development point of view__, we wanted to give developers the flexibility of [component-based architecture](http://en.wikipedia.org/wiki/Component-based_software_engineering) as much as possible, but still keep system-wide control over the middleware chain. On a small site with a small development team the latter might not be an issue. But on a large site with devs scattered all over the globe, some kind of middleware sandbox was a necessity.
Our feature devs spend 80-90% of their effort in jade templates or on the client side. For them, node components are often just a pass-through to the API(s), with some business logic applied to the request on the way in, and api data on the way out. Ideally they should have to learn the as little as possible of the vagaries/plumbing/whatever-your-favorite-metaphor-for-framework-stuff of node. Creating a new node component should be as easy for them as creating a new JSP - but again, without the framework losing control of the middleware chain.
From a __framework-development point of view__, we knew that as requirements evolved, we would constantly need to add default properties to each component, while hopefully causing as little disruption as possible to existing components. This is easily accomplished by adding a default property to the base config then specifying the property only in the nodules that need the new property.
We also knew we'd need to add slices of business logic globally or semi-globally at any point in the request chain. By keeping control of the middleware chain we are able to do this with ease.
This diagram might make the concept a little more clear:
![](http://i.imgur.com/eXExJi8.gif)
## Config
Yukon config is broken into 3 sections:
1. Nodule-specific properties
2. API-specific properties
3. App-defined middleware functions and global settings
*Note: You may occasionally see "MAGIC ALERT" below, this is for the handful of times where the framework does some convenience method that isn't immediately obvious, but comes up so much we felt the code saving was worth the loss in conceptual clarity.**
### Nodule-specific properties (config.noduleDefaults)
Yukon inherits the 4 core [nodulejs]([yukon API framework](https://github.com/jackspaniel/nodulejs) defaults:
1. __route__: <span style="color:grey">(REQUIRED)</span> one or more express routes - can be a string, RegExp, or array of either
2. __routeVerb__: <span style="color:grey">(OPTIONAL, default=get)</span> get, post, put, del
3. __routeIndex__: <span style="color:grey">(OPTIONAL, default=0)</span> use to match express routes before or after others, can be negative, like z-index
4. __middlewares__: <span style="color:grey">(OPTIONAL)</span> define this in your nodule to override the entire yukon request chain. See [404.js from the demoApp](https://github.com/jackspaniel/yukon/blob/master/demo/404.js) for example.
Yukon also adds the following optional nodule properties:
1. __templateName:__ MAGIC ALERT: if null the framework looks for a template in the same folder and of the same name as the nodule filename + templateExt. So if you have myPage.jade in the same folder as myPage.js, there is no need to specify template name.
2. __templateExt:__ default template extension
3. __contentType:__ 'html' and 'json' are the only current values
4. __preProcessor:__ use this function to manipulate query params or other business logic before api call
5. __postProcessor__: use this function to process data returned from the API before calling template or sending back to client as JSON
6. __error:__ set to a string or an Error() instance to get the framework to call next(error)
7. __apiCalls:__ array of API calls to made in parallel for this nodule, see the section below for details what constitutes an API call.
<br>NOTE: global or semi-global calls like getProfile, getGlobalNav, etc. can be added to this array in the appPreApi middleware.
### API-specific properties (config.apiDefaults)
Yukon defines the following properties for each API call. It is important to understand that these exist in a one-to-many relationship with nodules.
1. __path:__ path to API (not including server).
<br>MAGIC ALERT: if the API path ends with a slash(/), the framework automatically tries to append req.params.id from the express :id wildcard. For us at least this is a very common REST paradigm.
2. __params:__ params to send to API server. If the api verb is 'post', this can be a deep json object (bodyType=json) or a shallow object of name value pairs (bodyType=form).
3. __verb:__ get, post, put, del
4. __bodyType:__ valid values: json, form
5. __host:__ path to the API server, can be set in app-defined middleware or overridden at the nodule level.
6. __customHeaders:__ custom headers to sent with API call
7. __timeout:__ (numeric) - max API return time in ms
8. __useStub:__ set true to force framework to use stub instead of API
9. __stubPath:__ can contain path or just name if in same folder
<br>MAGIC ALERT: if not specified, app looks for [nodule name].stub.json in nodule folder
### App-defined middlware
An app can create and use 4 optional express middleware functions, which splice in between the built-in yukon middleware (see [yukon.js](https://github.com/jackspaniel/yukon/blob/master/yukon.js) for more detail:
1. __appStart:__ called at start of middleware, before nodule.preProcessor
2. __appPreApi:__ called after nodule.preProcessor, before API call(s)
3. __appPostApi:__ called after API call(s), before nodule.postProcessor
4. __appFinish:__ called after nodule.postProcessor, before res.send() or res.
An app can also create 2 global functions, which are executed before and after every API call. It's important to understand that there can be several API calls per express request. So these functions are not in the standard middleware chain, although the api callback does make use of the middleware paradigm.
1. __apiCallBefore:__ a synchronous function executed before of every api call. Do any common API pre-processing here.
2. __apiCallback:__ an asynchronous function executed after every api call, must execute next() if defined. Do error handling and other common post-API processing here.
### Global properties
There are also 3 global config properties inherited from [nodulejs]([yukon API framework](https://github.com/jackspaniel/nodulejs):
1. __dirs__: <span color="grey">(OPTIONAL, default='/nodules')</span> *path(s) to look for your nodules, exclude property can be full or partal match* <br>__example:__ [{ path: '/app', exclude: ['demoApp.js', '.test.js', '/shared/'] }, { path: '/lib/nodules', exclude: ['.test.js'] }]
2. __debugToConsole__: <span style="color:grey">(OPTIONAL, default=false)</span> *set to true to see nodulejs debug output in the console*
3. __customDebug__: <span style="color:grey">(OPTIONAL)</span> *custom debug function* <br>__example:__ function(identifier) { return function(msg){... your debug function here ...} }
### To Run Node Tests
```
$ npm install
$ make test
```
## To Do
1. Stop populating res.locals and res.renderData with API response data. All API response data and errors should be tacked on to the req.nodules.callArgs[namespace] object. (Maybe rename callArgs to something a little less arg-y?) An app can add these convenience shortcuts if desired.
2. Get demoApp working as standalone.
3. Write more detailed unit tests?
4. Hook up Travis CI and code coverage.
## Features for future consideration
+ __Other forms of async data gathering.__ Currently yukon only knows how to make REST API calls. But it woudn't take much work to extend this behavior to any sort of asynchronous data store - such as a Mondo DB or Redis cache.
+ __Sequential API calls.__ Currently yukon makes all API calls in parallel. We've been fortunate in that we haven't needed dependent API calls yet.
+ __API error handling.__ It seems that there can be a huge variation in error behavior, and even in what constitutes an API error (status code-based?), from web-app to web-app. So for now I've punted on advanced API error handling, and let the app deal with it in the API callback. But if something like a standard is more or less agreed-upon, I will be happy to add flexible error handling.
## Examples:
#### HTML response
([homePage.js](https://github.com/jackspaniel/yukon/blob/master/demo/homePage/homePage.js) from the demoApp)
```js
module.exports = function(app) {
return {
route: ['/', '/home', '/special'], // multiple routes
apiCalls: [
{path: '/api/cms/home'},
{path: '/api/data/homeslices'}
],
// pre-API(s) business logic
preProcessor: function(req, res) {
this.debug('preProcessor called');
if (req.path.indexOf('special') > -1) {
this.apiCalls[1].params = {isSpecial: true}; // setting api params at request-time
this.templateName = 'altHomePage.jade'; // using alternate template
}
},
// post-API(s) business logic
postProcessor: function(req, res) {
this.debug('postProcessor called');
var clientMsg = res.locals.data2.specialMsg || res.locals.data2.msg;
res.renderData = {
globalNav: res.locals.globalNav,
cmsData: res.locals.data1,
myData: res.locals.data2,
clientMsg: clientMsg
};
}
};
};
```
#### JSON response
([getData.js](https://github.com/jackspaniel/yukon/blob/master/demo/json/getData.js) from the demoApp)
```js
module.exports = function(app) {
return {
route : '/json/getData/:id',
apiCalls: [
{path: '/api/getdata/'}, // :id is tacked on by the framework automatically
],
preProcessor: function(req, res) {
this.debug('preProcessor called');
this.apiCalls[0].params = {myParam: req.query.myParam};
},
postProcessor: function(req, res) {
this.debug('postProcessor called');
res.renderData = {
systemMsg: res.locals.data1.systemMsg,
data: res.locals.data1
};
}
};
};
```
#### Form submit
([submitForm.js](https://github.com/jackspaniel/yukon/blob/master/demo/json/submitForm.js) from the demoApp)
```js
var _ = require('lodash');
module.exports = function(app) {
return {
route : '/json/submitForm',
routeVerb: 'post', // default = get
apiCalls: [{
path: '/api/submitform',
verb: 'post', // post to API
bodyType: 'form', // default = 'json'
}],
preProcessor: function(req, res) {
this.debug('preProcessor called');
if (!_.isEmpty(req.body)) {
this.apiCalls[0].bodyType = 'json';
this.apiCalls[0].params = req.body; // JSON body
}
else {
this.apiCalls[0].params = req.query; // url-encoded
}
},
postProcessor: function(req, res) {
this.debug('postProcessor called');
res.renderData = {
response: res.locals.data1
};
}
};
};
```
#### App-defined middleware
(from [demoApp.js](https://github.com/jackspaniel/yukon/blob/master/demo/demoApp.js))
```js
function demoStart(req, res, next) {
debug("demoStart called");
res.locals.pretty = true; // jade pretty setting - turn off at the component level if necessary
// example of setting nodule property globally
if (req.nodule.contentType !== 'html' && req.path.indexOf('/json/') === 0)
req.nodule.contentType = 'json';
// example of app-level logic - simple device detection (used throughout demoApp)
if (req.headers['user-agent'].match(/android/i))
req.deviceType = 'Android';
else if (req.headers['user-agent'].match(/iphone/i))
req.deviceType = 'iPhone';
else if (req.headers['user-agent'].match(/ipad/i))
req.deviceType = 'iPad';
else
req.deviceType = 'web';
next();
}
```
#### Global API callback and error handling
(from [demoApp.js](https://github.com/jackspaniel/yukon/blob/master/demo/demoApp.js))
```js
function demoApiCallback(callArgs, req, res, next) {
// custom error handling
if (callArgs.apiError && !callArgs.handleError) {
debug(callArgs.apiError.stack || callArgs.apiError);
next(new Error('API failed for '+callArgs.path +': '+callArgs.apiError));
}
else {
var msg = "RESPONSE FROM "+callArgs.apiResponse.req.path+": statusCode=" + callArgs.apiResponse.statusCode;
debug(msg);
// example of app-level logic on every api response (remember there can be multiple API calls per request)
res.locals[callArgs.namespace].systemMsg = msg;
// used by kitchen sink to test if API custom headers are being set
if (callArgs.apiResponse.req._headers)
res.locals[callArgs.namespace].customHeaders = callArgs.apiResponse.req._headers;
next();
}
}
```
For more examples see the [Kitchen Sink](https://github.com/jackspaniel/yukon/blob/master/demo/kitchenSink/kitchenSink.js) and the rest of the [Demo App](https://github.com/jackspaniel/yukon/blob/master/demo/)
## License
### MIT
[npm-image]: https://img.shields.io/npm/v/nodulejs.svg?style=flat
[npm-url]: https://www.npmjs.com/package/nodulejs
[downloads-image]: https://img.shields.io/npm/dm/nodulejs.svg?style=flat
[downloads-url]: https://npmjs.org/package/ndoulejs
+38
-15

@@ -8,19 +8,19 @@ // add yukon default config to nodulejs default config

var yukonConfig = _.merge(defaultConfig, config);
yukonConfig.debug = yukonConfig.customDebug || yukonConfig.yukonCustomDebug;
var debug = yukonConfig.debug('yukon->index');
var debug = yukonConfig.customDebug('yukon->index');
debug('initializing');
// array of middleware functions to be executed on each request
// splicing app-defined middleware in-between yukon system middlware
yukonConfig.noduleDefaults.middlewares = [
config.appPreApi || passThrough,
require('./middlewares/preApi')(app, yukonConfig), // preprocessing logic before APIs are called
config.appStart,
require('./middlewares/preProcessor')(app, yukonConfig), // preprocessing logic before APIs are called
config.appDoApi || passThrough,
config.appPreApi,
require('./middlewares/doApi')(app, yukonConfig), // handles all API calls in parallel
config.appPostApi || passThrough,
require('./middlewares/postApi')(app, yukonConfig), // common post-processing logic after all APIs return
config.appPostApi,
require('./middlewares/postProcessor')(app, yukonConfig), // common post-processing logic after all APIs return
config.appFinish || passThrough,
config.appFinish,
require('./middlewares/finish')(app, yukonConfig), // finish with json or html

@@ -38,10 +38,33 @@ ];

var defaultConfig = {
// called at the start of every api call
apiCallBefore: null,
/////////////////////////////////////////////////////////////////
/// APP-DEFINED EXPRESS MIDDLEWARE FUNCTIONS INVOKED BY YUKON ///
/////////////////////////////////////////////////////////////////
// called after every api call
apiCallback: null,
// called before nodule.preProcessor
appStart: passThrough,
// called after nodule.preProcessor, before API call(s)
appPreApi: passThrough,
// called after API call(s), before nodule.postProcessor
appPostApi: passThrough,
// called after nodule.postProcessor, before res.send or res.render
appFinish: passThrough,
///////////////////////////////////////////////////
/// FUNCTIONS INVOKED PRE AND POST API BY YUKON ///
///////////////////////////////////////////////////
// (OPTIONAL) synchronous function called at the start of every api call
apiCallBefore: function(callArgs, req, res) { },
// (OPTIONAL) asynchronous function called after every api call
// NOTE: must execute next() if defined
apiCallback: function(callArgs, req, res, next) { next(callArgs.apiError); },
// default debug function
yukonCustomDebug: function(identifier) {
customDebug: function(identifier) {
return function(msg) {

@@ -86,3 +109,3 @@ if (defaultConfig.debugToConsole) console.log(identifier+': '+msg);

// array of apiCalls to call in parallel
// NOTE: global or semi-global calls like getProfile, getGlobalNav, etc. can be added to this array in the appDoApi middleware
// NOTE: global or semi-global calls like getProfile, getGlobalNav, etc. can be added to this array in the appPreApi middleware
apiCalls: [],

@@ -108,3 +131,3 @@ },

// valid values: json, form (use 'form' for a standard post submit with name/value pairs - everything wants json body)
// valid values: json, form
bodyType: 'json',

@@ -111,0 +134,0 @@

// wraps nodule.postProcessor, called after API calls return
var path = require('path');
// wraps nodule.postProcessor, called after all API calls return
module.exports = function(app, config) {
var debug = config.debug('yukon->postApi');
return function(req, res, next) {
debug("called");
var nodule = req.nodule;
// execute nodule-level post API business logic
nodule.postProcessor(req, res);
// nodules can throw their own errors on certain conditions
if (nodule.error) {
next(nodule.error);
return;
}
// convenience method so devs don't have to set renderData for default single API case
if (!res.renderData)
res.renderData = res.locals.data1 || {};
if (req.nodule.contentType !== 'json') {
// if template name is not specified assume (nodule name).(nodule extention)
var templateName = (nodule.templateName) ? nodule.templateName : nodule.name + nodule.templateExt;
res.templatePath = (templateName.indexOf('/') > -1)
? path.join(process.cwd(), templateName)
: path.join(nodule.path, templateName);
}
debug('res.templatePath = ' + res.templatePath);
next();
};
};
// wraps nodule.preProcessor, called after app-level appPreApi middleware
module.exports = function(app, config) {
return function(req, res, next) {
config.debug('yukon-preApi')('called');
req.nodule.preProcessor(req, res);
next();
};
};