Skip to content

Commit

Permalink
Added browser surface-area, API generators (node and browser versions…
Browse files Browse the repository at this point in the history
…), refactored util
  • Loading branch information
d-oliveros committed Jul 27, 2015
1 parent c4b3238 commit c1f2f06
Show file tree
Hide file tree
Showing 28 changed files with 375 additions and 310 deletions.
8 changes: 8 additions & 0 deletions index-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/**
* Browser's surface area API
*/
module.exports = {
api: require('./lib/api-browser'),
Proxy: require('./lib/proxy')
};
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
var util = require('./lib/util');

module.exports = require('./src/isomorphine');
/**
* @providesModule isomorphine
*/
module.exports = {
api: require('./lib/api'),
Proxy: require('./lib/proxy'),
router: require('./lib/router'),
config: util.config,
registerEntity: util.registerEntity,
resetEntities: util.resetEntities
};
28 changes: 28 additions & 0 deletions lib/api-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var Proxy = require('./proxy');
var invariant = require('./util').invariant;
var debug = require('debug')('isomorphine:api');

/**
* Browser version of the API factory. If `isomorphine-loader` is not installed,
*
* @requires isomorphine-loader
*
* @param {String} rootDir Path to folder to require from.
* @return {Object} Required modules.
*
* @providesModule browserApiFactory
*/
module.exports = function browserApiFactory(rootDir, map) {
var modules = {};

invariant(typeof map === 'object', 'Entity map is required. '+
'(Hint: Are you sure you are using "isomorphine-loader"?)');

for (var modName in map) {
modules[modName] = Proxy(modName, map[modName]);
}

debug('Loaded isomorphic entities in the browser: ', modules);

return modules;
};
15 changes: 7 additions & 8 deletions src/util/load-entities.js → lib/api.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
var fs = require('fs');
var path = require('path');
var invariant = require('./invariant');
var registerEntity = require('./register-entity');
var util = require('./util');

var debug = require('debug')('isomorphine:util');
var debug = require('debug')('isomorphine:api');

/**
* Requires every module in each folder of "rootDir".
* Creates an isomorphine API using the passed parameter as the API's root
*
* @param {String} rootDir Path to folder to require from.
* @return {Object} Required modules.
*
* @providesModule loadEntities
* @providesModule api
*/
module.exports = function loadEntities(rootDir) {
invariant(rootDir, 'Root directory is required.');
module.exports = function apiFactory(rootDir) {
util.invariant(rootDir, 'Root directory is required.');

debug('Loading all entities in ' + rootDir);

Expand All @@ -23,7 +22,7 @@ module.exports = function loadEntities(rootDir) {

files.forEach(function(file) {
if (file.indexOf('.js') < 0) {
registerEntity(file, modules[file]);
util.registerEntity(file, modules[file]);
modules[file] = require(path.join(rootDir, file));
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/proxy.js → lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var invariant = util.invariant;
* @param {Object} entity The entity map to be used.
*
* @return {Object} Instance of `Proxy`
*
* @providesModule Proxy
*/
module.exports = function Proxy(name, entity) {
if (!(this instanceof Proxy)) return new Proxy(name, entity);
Expand Down
4 changes: 2 additions & 2 deletions src/router/controllers.js → lib/router/controllers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var entities = require('../stores/entities');
var invariant = require('../util/invariant');
var util = require('../util');
var debug = require('debug')('isomorphine:router');

exports.entityLoader = entityLoader;
Expand Down Expand Up @@ -104,6 +104,6 @@ function callEntityMethod(req, res, next) {
* Serves the value in req.entityResponse as a JSON object.
*/
function serve(req, res) {
invariant(Array.isArray(res.entityResponse), 'Response values are required.');
util.invariant(Array.isArray(res.entityResponse), 'Response values are required.');
res.json({ values: res.entityResponse });
}
3 changes: 3 additions & 0 deletions src/router/index.js → lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var getPayload = ctrls.getPayload;
var callEntityMethod = ctrls.callEntityMethod;
var serve = ctrls.serve;

/**
* @providesModule router
*/
module.exports = function apiFactory(routerFactory, bodyParser) {
var router = routerFactory();
router.use(bodyParser);
Expand Down
File renamed without changes.
190 changes: 190 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
var debug = require('debug')('isomorphine:util');
var config = require('../config');
var entities = require('./stores/entities');

exports.buildEndpoint = buildEndpoint;
exports.config = configInterface;
exports.emptyFunction = emptyFunction;
exports.firstFunction = firstFunction;
exports.invariant = invariant;
exports.registerEntity = registerEntity;
exports.resetEntities = resetEntities;
exports.transformCallback = transformCallback;

/**
* Builds an entity's API endpoint.
*
* @param {String} name The entity's name.
* @param {String} method The entity's method to be called.
* @return {String} The endpoint to request.
*
* @providesModule buildEndpoint
*/
function buildEndpoint(name, method) {
var protocol = config.protocol;
var host = config.host;
var port = config.port;

var base = protocol + '://' + host + ':' + port;
var path = '/isomorphine/' + name + '/' + method;
var endpoint = base + path;

debug('Built endpoint: ' + endpoint);

return endpoint;
}

/**
* Gets/Sets the configuration object.
*
* @param {Object} config The config to load.
* @return {Object} The current configuration.
*
* @providesModule configInterface
*/
function configInterface(newConfig) {
if (newConfig) {
debug('Setting new config: ', newConfig);
for (var key in newConfig) {
config[key] = newConfig[key];
}
}

debug('Getting config: ', config);

return config;
}

/**
* An empty function.
*
* @providesModule emptyFunction
*/
function emptyFunction() {}

/**
* Returns the first function in an array.
*
* @param {Array} args The array to take the function from.
* @return {Function} The resulting function. Default: An empty function.
*
* @providesModule firstFunction
*/
function firstFunction(args) {
var func = emptyFunction;

args.forEach(function(arg) {
if (typeof arg === 'function') {
func = arg;
}
});

return func;
}

/**
* Copyright (c) 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule invariant
*/

/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/

function invariant(condition, format, a, b, c, d, e, f) {
if (process.browser) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
}

if (!condition) {
var error;
if (format === undefined) {
error = new Error(
'Minified exception occurred; use the non-minified dev environment ' +
'for the full error message and additional helpful warnings.'
);
} else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(
'Invariant Violation: ' +
format.replace(/%s/g, function() { return args[argIndex++]; })
);
}

error.framesToPop = 1; // we don't care about invariant's own frame
throw error;
}
}

/**
* Registers a single entity.
*
* @param {String} name The name of the entity to register.
* @param {Object} entity The entity to register.
*
* @providesModule registerEntity
*/
function registerEntity(name, entity) {
debug('Registering entity "' + name + '": ', entity);
entities[name] = entity;
}

/**
* Clears the entities.
*
* @return {Number} Number of entities removed.
*
* @providesModule resetEntities
*/
function resetEntities() {
debug('Resetting entities');

var count = Object.keys(entities).length;

for (var id in entities) {
delete entities[id];
}

return count;
}

/**
* Transforms the client's callback function to a callback notice string.
*
* @param {Array} args Array of arguments to transform.
* @return {Array} The transformed arguments array.
*
* @providesModule transformCallback
*/
function transformCallback(args) {
var callback;

debug('Transforming callback in ', args);

return args.map(function(arg) {
if (typeof arg !== 'function') return arg;

// It shouldn't be an argument after the callback function
invariant(!callback, 'Only one callback function is allowed.');

callback = arg;

return '__clientCallback__';
});
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "isomorphine",
"version": "0.2.4",
"description": "Isomorphic object wrapper for node.js and the web",
"version": "0.2.5",
"description": "Isomorphic pain medication for node.js and the web",
"main": "index.js",
"scripts": {
"test": "node node_modules/.bin/mocha"
Expand All @@ -23,6 +23,9 @@
"url": "https://github.com/d-oliveros/isomorphine/issues"
},
"homepage": "https://github.com/d-oliveros/isomorphine#readme",
"browser": {
"./index.js": "./index-browser.js"
},
"dependencies": {
"debug": "^2.2.0",
"superagent": "^1.2.0"
Expand Down
11 changes: 0 additions & 11 deletions src/isomorphine.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/util/build-endpoint.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/util/empty-function.js

This file was deleted.

Loading

0 comments on commit c1f2f06

Please sign in to comment.