Permalink
Browse files

Migration to ES6 and API providers in separate modules

  • Loading branch information...
1 parent 61c2310 commit f66c7d5b071302526e31cc7749ddd177ff9e31a2 @daffl daffl committed Jan 3, 2016
View
@@ -0,0 +1,4 @@
+{
+ "plugins": [ "add-module-exports" ],
+ "presets": [ "es2015" ]
+}
View
@@ -29,3 +29,7 @@ node_modules
tmp*
.idea/
+
+
+# The compiled/babelified modules
+lib/
View
@@ -1,14 +1,30 @@
{
- "globals": {
- "it": true,
- "describe": true,
- "before": true,
- "beforeEach": true,
- "after": true,
- "afterEach": true,
- "exports": true
- },
- "unused": true,
- "undef": true,
- "node": true
-}
+ "node": true,
+ "esnext": true,
+ "bitwise": true,
+ "camelcase": true,
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "indent": 2,
+ "latedef": "nofunc",
+ "newcap": false,
+ "noarg": true,
+ "quotmark": "single",
+ "regexp": true,
+ "undef": true,
+ "unused": true,
+ "strict": false,
+ "trailing": true,
+ "smarttabs": true,
+ "white": false,
+ "node": true,
+ "globals": {
+ "it": true,
+ "describe": true,
+ "before": true,
+ "beforeEach": true,
+ "after": true,
+ "afterEach": true
+ }
+}
View
@@ -0,0 +1,8 @@
+./.editorconfig
+./.jshintrc
+./.travis.yml
+./.babelrc
+.idea/
+src/
+test/
+!lib/
View
@@ -12,6 +12,14 @@ If you have any other questions, feel free to submit post them on [Stackoverflow
Before running the tests from the `test/` folder `npm test` will run JSHint. You can check your code changes individually by running `npm run jshint`.
+## ES6 compilation
+
+Feathers uses [Babel](https://babeljs.io/) to leverage the latest developments of the JavaScript language. All code and samples are currently written in ES2015. To transpile the code in this repository run
+
+> npm run compile
+
+__Note:__ `npm test` will run the compilation automatically before the tests.
+
## Tests
[Mocha](http://mochajs.org/) tests are located in the `test/` folder and can be run using the `npm run mocha` or `npm test` (with JSHint) command.
View
@@ -1,41 +0,0 @@
-'use strict';
-
-var _ = require('lodash');
-var express = require('express');
-var Proto = require('uberproto');
-var Application = require('./application');
-var providers = require('./providers');
-var errors = require('feathers-errors');
-
-/**
- * Create a Feathers application that extends Express.
- *
- * @return {Function}
- * @api public
- */
-
-function createApplication() {
- var app = express();
- Proto.mixin(Application, app);
- app.init();
- return app;
-}
-
-/**
- * Expose `createApplication()`.
- */
-module.exports = createApplication;
-
-/**
- * Framework version.
- */
-exports.version = require('../package.json').version;
-
-// Add the providers (REST and SocketIO)
-_.defaults(module.exports, providers);
-
-// Add the error handling
-_.defaults(module.exports, errors);
-
-// Expose all express methods (like express.engine())
-_.defaults(module.exports, express);
View
@@ -1,57 +0,0 @@
-'use strict';
-
-var _ = require('lodash');
-var rubberduck = require('rubberduck');
-var EventEmitter = require('events').EventEmitter;
-var hookObject = require('feathers-commons').hooks.hookObject;
-var eventMappings = {
- create: 'created',
- update: 'updated',
- remove: 'removed',
- patch: 'patched'
-};
-
-module.exports = function (service) {
- var isEmitter = typeof service.on === 'function' &&
- typeof service.emit === 'function';
- var emitter = service._rubberDuck = rubberduck.emitter(service);
-
- if (typeof service.mixin === 'function') {
- if(!isEmitter) {
- service.mixin(EventEmitter.prototype);
- }
- }
-
- service._serviceEvents = _.isArray(service.events) ? service.events.slice() : [];
-
- // Pass the Rubberduck error event through
- // TODO deal with error events properly
- emitter.on('error', function (errors) {
- service.emit('serviceError', errors[0]);
- });
-
- _.each(eventMappings, function (event, method) {
- var alreadyEmits = service._serviceEvents.indexOf(event) !== -1;
-
- if (typeof service[method] === 'function' && !alreadyEmits) {
- // The Rubberduck event name (e.g. afterCreate, afterUpdate or afterDestroy)
- var eventName = 'after' + method.charAt(0).toUpperCase() + method.substring(1);
- service._serviceEvents.push(event);
- // Punch the given method
- emitter.punch(method, -1);
- // Pass the event and error event through
- emitter.on(eventName, function (results, args) {
- if (!results[0]) { // callback without error
- var hook = hookObject(method, 'after', args);
- var data = Array.isArray(results[1]) ? results[1] : [ results[1] ];
-
- data.forEach(function(current) {
- service.emit(event, current, hook);
- });
- } else {
- service.emit('serviceError', results[0]);
- }
- });
- }
- });
-};
@@ -1,19 +0,0 @@
-var _ = require('lodash');
-var getArguments = require('feathers-commons').getArguments;
-
-module.exports = function (service) {
- if (typeof service.mixin === 'function') {
- var mixin = {};
-
- _.each(this.methods, function(method) {
- if(typeof service[method] === 'function') {
- mixin[method] = function() {
- var args = getArguments(method, arguments);
- return this._super.apply(this, args);
- };
- }
- });
-
- service.mixin(mixin);
- }
-};
View
@@ -1,31 +0,0 @@
-'use strict';
-
-var _ = require('lodash');
-
-var wrapper = function () {
- var result = this._super.apply(this, arguments);
- var callback = arguments[arguments.length - 1];
-
- if(typeof result !== 'undefined' && _.isFunction(result.then) && _.isFunction(callback)) {
- result.then(function(data) {
- callback(null, data);
- }, function(error) {
- callback(error);
- });
- }
- return result;
-};
-
-module.exports = function (service) {
- if (typeof service.mixin === 'function') {
- var mixin = {};
-
- _.each(this.methods, function(method) {
- if(typeof service[method] === 'function') {
- mixin[method] = wrapper;
- }
- });
-
- service.mixin(mixin);
- }
-};
@@ -1,7 +0,0 @@
-'use strict';
-
-module.exports = {
- rest: require('./rest'),
- socketio: require('./socket/socketio'),
- primus: require('./socket/primus')
-};
@@ -1,72 +0,0 @@
-'use strict';
-
-var wrappers = require('./wrappers');
-var debug = require('debug')('feathers:rest');
-
-module.exports = function (config) {
- config = config || {};
-
- var handler = config.handler || function (req, res) {
- res.format({
- 'application/json': function () {
- res.json(res.data);
- }
- });
- };
-
- if (typeof config === 'function') {
- handler = config;
- }
-
- return function () {
- var app = this;
-
- app.enable('feathers rest');
-
- debug('Setting up default middleware for REST handler');
-
- app.use(function (req, res, next) {
- req.feathers = {};
- next();
- });
-
- app.rest = wrappers;
-
- // Register the REST provider
- app.providers.push(function (path, service, options) {
- if (app.disabled('feathers rest')) {
- return;
- }
-
- var middleware = (options || {}).middleware || {};
- var before = middleware.before || [];
- var after = middleware.after || [];
-
- var uri = path.indexOf('/') === 0 ? path : '/' + path;
- var baseRoute = app.route(uri);
- var idRoute = app.route(uri + '/:id');
-
- debug('Adding REST provider for service `' + path + '` at base route `' + uri + '`');
-
- // GET / -> service.find(cb, params)
- baseRoute.get.apply(baseRoute, before.concat(app.rest.find(service), after, handler));
- // POST / -> service.create(data, params, cb)
- baseRoute.post.apply(baseRoute, before.concat(app.rest.create(service), after, handler));
- // PATCH / -> service.patch(null, data, params)
- baseRoute.patch.apply(baseRoute, before.concat(app.rest.patch(service), after, handler));
- // PUT / -> service.update(null, data, params)
- baseRoute.put.apply(baseRoute, before.concat(app.rest.update(service), after, handler));
- // DELETE / -> service.remove(null, params)
- baseRoute.delete.apply(baseRoute, before.concat(app.rest.remove(service), after, handler));
-
- // GET /:id -> service.get(id, params, cb)
- idRoute.get.apply(idRoute, before.concat(app.rest.get(service), after, handler));
- // PUT /:id -> service.update(id, data, params, cb)
- idRoute.put.apply(idRoute, before.concat(app.rest.update(service), after, handler));
- // PATCH /:id -> service.patch(id, data, params, callback)
- idRoute.patch.apply(idRoute, before.concat(app.rest.patch(service), after, handler));
- // DELETE /:id -> service.remove(id, params, cb)
- idRoute.delete.apply(idRoute, before.concat(app.rest.remove(service), after, handler));
- });
- };
-};
@@ -1,81 +0,0 @@
-'use strict';
-
-var _ = require('lodash');
-var debug = require('debug')('feathers:rest');
-var errors = require('feathers-errors');
-var status = {
- created: 201,
- noContent: 204,
- methodNotAllowed: 405
-};
-
-// A function that returns the middleware for a given method and service
-// `getArgs` is a function that should return additional leading service arguments
-function getHandler (method, getArgs, service) {
- return function (req, res, next) {
- // Check if the method exists on the service at all. Send 405 (Method not allowed) if not
- if (typeof service[method] !== 'function') {
- debug('Method `' + method + '` not allowed on `' + req.url + '`');
- res.status(status.methodNotAllowed);
- return next(new errors.types.MethodNotAllowed('Method `' + method + '` is not supported by this endpoint.'));
- }
-
- // Run the getArgs callback, if available, for additional parameters
- var args = getArgs(req, res, next);
- // Grab the service parameters. Use req.feathers and set the query to req.query
- var params = _.extend({ query: req.query || {} }, _.omit(req.params || {}, 'id'), req.feathers);
- // The service success callback which sets res.data or calls next() with the error
- var callback = function (error, data) {
- if (error) {
- debug('Error in REST handler: `' + (error.message || error) + '`');
- return next(error);
- }
-
- res.data = data;
-
- if(!data) {
- debug('No content returned for `' + req.url + '`');
- res.status(status.noContent);
- } else if(method === 'create') {
- res.status(status.created);
- }
-
- return next();
- };
-
- debug('REST handler calling `' + method + '` from `' + req.url + '`');
- service[method].apply(service, args.concat([ params, callback ]));
- };
-}
-
-// Returns no leading parameters
-function reqNone () {
- return [];
-}
-
-// Returns the leading parameters for a `get` or `remove` request (the id)
-function reqId (req) {
- return [ req.params.id || null ];
-}
-
-// Returns the leading parameters for an `update` or `patch` request (id, data)
-function reqUpdate (req) {
- return [ req.params.id || null, req.body ];
-}
-
-// Returns the leading parameters for a `create` request (data)
-function reqCreate (req) {
- return [ req.body ];
-}
-
-// Returns wrapped middleware for a service method.
-// Doing some fancy ES 5 .bind argument currying for .getHandler()
-// Basically what you are getting for each is a function(service) {}
-module.exports = {
- find: getHandler.bind(null, 'find', reqNone),
- get: getHandler.bind(null, 'get', reqId),
- create: getHandler.bind(null, 'create', reqCreate),
- update: getHandler.bind(null, 'update', reqUpdate),
- patch: getHandler.bind(null, 'patch', reqUpdate),
- remove: getHandler.bind(null, 'remove', reqId)
-};
Oops, something went wrong.

0 comments on commit f66c7d5

Please sign in to comment.