Skip to content

Commit 00ae803

Browse files
committed
refactor(app): large app refactor, mostly es6-ification, some minor code style tweaks
1 parent ecf9014 commit 00ae803

37 files changed

+241
-296
lines changed

Diff for: app/templates/client/app/admin(auth)/admin.controller.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
(function() {
44

55
class AdminController {
6-
76
constructor(User) {
87
// Use the User $resource to fetch all users
98
this.users = User.query();

Diff for: app/templates/client/components/auth(auth)/auth.service.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
2020
* @param {Function} callback - optional, function(error, user)
2121
* @return {Promise}
2222
*/
23-
login: function(user, callback) {
23+
login(user, callback) {
2424
return $http.post('/auth/local', {
2525
email: user.email,
2626
password: user.password
2727
})
28-
.then(function(res) {
29-
$cookies.put('token', res.data.token);
30-
currentUser = User.get();
31-
return currentUser.$promise;
32-
})
33-
.then(function(user) {
34-
safeCb(callback)(null, user);
35-
return user;
36-
})
37-
.catch(function(err) {
38-
Auth.logout();
39-
safeCb(callback)(err.data);
40-
return $q.reject(err.data);
41-
});
28+
.then(res => {
29+
$cookies.put('token', res.data.token);
30+
currentUser = User.get();
31+
return currentUser.$promise;
32+
})
33+
.then(user => {
34+
safeCb(callback)(null, user);
35+
return user;
36+
})
37+
.catch(err => {
38+
Auth.logout();
39+
safeCb(callback)(err.data);
40+
return $q.reject(err.data);
41+
});
4242
},
4343

4444
/**
4545
* Delete access token and user info
4646
*/
47-
logout: function() {
47+
logout() {
4848
$cookies.remove('token');
4949
currentUser = {};
5050
},
@@ -56,7 +56,7 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
5656
* @param {Function} callback - optional, function(error, user)
5757
* @return {Promise}
5858
*/
59-
createUser: function(user, callback) {
59+
createUser(user, callback) {
6060
return User.save(user,
6161
function(data) {
6262
$cookies.put('token', data.token);
@@ -77,7 +77,7 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
7777
* @param {Function} callback - optional, function(error, user)
7878
* @return {Promise}
7979
*/
80-
changePassword: function(oldPassword, newPassword, callback) {
80+
changePassword(oldPassword, newPassword, callback) {
8181
return User.changePassword({ id: currentUser._id }, {
8282
oldPassword: oldPassword,
8383
newPassword: newPassword
@@ -95,18 +95,18 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
9595
* @param {Function|*} callback - optional, funciton(user)
9696
* @return {Object|Promise}
9797
*/
98-
getCurrentUser: function(callback) {
98+
getCurrentUser(callback) {
9999
if (arguments.length === 0) {
100100
return currentUser;
101101
}
102102

103103
var value = (currentUser.hasOwnProperty('$promise')) ?
104104
currentUser.$promise : currentUser;
105105
return $q.when(value)
106-
.then(function(user) {
106+
.then(user => {
107107
safeCb(callback)(user);
108108
return user;
109-
}, function() {
109+
}, () => {
110110
safeCb(callback)({});
111111
return {};
112112
});
@@ -119,13 +119,13 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
119119
* @param {Function|*} callback - optional, function(is)
120120
* @return {Bool|Promise}
121121
*/
122-
isLoggedIn: function(callback) {
122+
isLoggedIn(callback) {
123123
if (arguments.length === 0) {
124124
return currentUser.hasOwnProperty('role');
125125
}
126126

127127
return Auth.getCurrentUser(null)
128-
.then(function(user) {
128+
.then(user => {
129129
var is = user.hasOwnProperty('role');
130130
safeCb(callback)(is);
131131
return is;
@@ -140,7 +140,7 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
140140
* @param {Function|*} callback - optional, function(has)
141141
* @return {Bool|Promise}
142142
*/
143-
hasRole: function(role, callback) {
143+
hasRole(role, callback) {
144144
var hasRole = function(r, h) {
145145
return userRoles.indexOf(r) >= userRoles.indexOf(h);
146146
};
@@ -150,7 +150,7 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
150150
}
151151

152152
return Auth.getCurrentUser(null)
153-
.then(function(user) {
153+
.then(user => {
154154
var has = (user.hasOwnProperty('role')) ?
155155
hasRole(user.role, role) : false;
156156
safeCb(callback)(has);
@@ -165,7 +165,7 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
165165
* @param {Function|*} callback - optional, function(is)
166166
* @return {Bool|Promise}
167167
*/
168-
isAdmin: function() {
168+
isAdmin() {
169169
return Auth.hasRole
170170
.apply(Auth, [].concat.apply(['admin'], arguments));
171171
},
@@ -175,7 +175,7 @@ function AuthService($location, $http, $cookies, $q, appConfig, Util, User) {
175175
*
176176
* @return {String} - a token string used for authenticating
177177
*/
178-
getToken: function() {
178+
getToken() {
179179
return $cookies.get('token');
180180
}
181181
};

Diff for: app/templates/client/components/auth(auth)/interceptor.service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function authInterceptor($rootScope, $q, $cookies<% if (filters.ngroute) { %>, $
66
<% if (filters.uirouter) { %>var state;
77
<% } %>return {
88
// Add authorization token to headers
9-
request: function(config) {
9+
request(config) {
1010
config.headers = config.headers || {};
1111
if ($cookies.get('token') && Util.isSameOrigin(config.url)) {
1212
config.headers.Authorization = 'Bearer ' + $cookies.get('token');
@@ -15,7 +15,7 @@ function authInterceptor($rootScope, $q, $cookies<% if (filters.ngroute) { %>, $
1515
},
1616

1717
// Intercept 401s and redirect you to login
18-
responseError: function(response) {
18+
responseError(response) {
1919
if (response.status === 401) {
2020
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>(state || (state = $injector.get('$state'))).go('login');<% } %>
2121
// remove any stale tokens

Diff for: app/templates/client/components/auth(auth)/user.service.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
function UserResource($resource) {
66
return $resource('/api/users/:id/:controller', {
77
id: '@_id'
8-
},
9-
{
8+
}, {
109
changePassword: {
1110
method: 'PUT',
1211
params: {

Diff for: app/templates/client/components/footer/footer.directive.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ angular.module('<%= scriptAppName %>')
55
return {
66
templateUrl: 'components/footer/footer.html',
77
restrict: 'E',
8-
link: function (scope, element) {
8+
link: function(scope, element) {
99
element.addClass('footer');
1010
}
1111
};

Diff for: app/templates/client/components/modal(uibootstrap)/modal.service.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ angular.module('<%= scriptAppName %>')
88
* @param {String} modalClass - (optional) class(es) to be applied to the modal
99
* @return {Object} - the instance $modal.open() returns
1010
*/
11-
function openModal(scope, modalClass) {
11+
function openModal(scope = {}, modalClass = 'modal-default') {
1212
var modalScope = $rootScope.$new();
13-
scope = scope || {};
14-
modalClass = modalClass || 'modal-default';
1513

1614
angular.extend(modalScope, scope);
1715

@@ -33,9 +31,7 @@ angular.module('<%= scriptAppName %>')
3331
* @param {Function} del - callback, ran when delete is confirmed
3432
* @return {Function} - the function to open the modal (ex. myModalFn)
3533
*/
36-
delete: function(del) {
37-
del = del || angular.noop;
38-
34+
delete(del = angular.noop) {
3935
/**
4036
* Open a delete confirmation modal
4137
* @param {String} name - name or info to show on modal

Diff for: app/templates/client/components/mongoose-error(auth)/mongoose-error.directive.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
* Removes server error when user updates input
55
*/
66
angular.module('<%= scriptAppName %>')
7-
.directive('mongooseError', function () {
7+
.directive('mongooseError', function() {
88
return {
99
restrict: 'A',
1010
require: 'ngModel',
1111
link: function(scope, element, attrs, ngModel) {
12-
element.on('keydown', function() {
13-
return ngModel.$setValidity('mongoose', true);
14-
});
12+
element.on('keydown', () => ngModel.$setValidity('mongoose', true));
1513
}
1614
};
1715
});

Diff for: app/templates/client/components/socket(socketio)/socket.service.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33

44
angular.module('<%= scriptAppName %>')
55
.factory('socket', function(socketFactory) {
6-
76
// socket.io now auto-configures its connection when we ommit a connection url
87
var ioSocket = io('', {
98
// Send auth token on connection, you will need to DI the Auth service above
109
// 'query': 'token=' + Auth.getToken()
1110
path: '/socket.io-client'
1211
});
1312

14-
var socket = socketFactory({
15-
ioSocket: ioSocket
16-
});
13+
var socket = socketFactory({ ioSocket });
1714

1815
return {
19-
socket: socket,
16+
socket,
2017

2118
/**
2219
* Register listeners to sync an array with updates on a model
@@ -28,7 +25,7 @@ angular.module('<%= scriptAppName %>')
2825
* @param {Array} array
2926
* @param {Function} cb
3027
*/
31-
syncUpdates: function (modelName, array, cb) {
28+
syncUpdates(modelName, array, cb) {
3229
cb = cb || angular.noop;
3330

3431
/**
@@ -66,7 +63,7 @@ angular.module('<%= scriptAppName %>')
6663
*
6764
* @param modelName
6865
*/
69-
unsyncUpdates: function (modelName) {
66+
unsyncUpdates(modelName) {
7067
socket.removeAllListeners(modelName + ':save');
7168
socket.removeAllListeners(modelName + ':remove');
7269
}

Diff for: app/templates/client/components/util/util.service.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
* The Util service is for thin, globally reusable, utility functions
77
*/
88
function UtilService($window) {
9-
109
var Util = {
11-
1210
/**
1311
* Return a callback or noop function
1412
*
1513
* @param {Function|*} cb - a 'potential' function
1614
* @return {Function}
1715
*/
18-
safeCb: function(cb) {
16+
safeCb(cb) {
1917
return (angular.isFunction(cb)) ? cb : angular.noop;
2018
},
2119

@@ -25,7 +23,7 @@ function UtilService($window) {
2523
* @param {String} url - the url to parse
2624
* @return {Object} - the parsed url, anchor element
2725
*/
28-
urlParse: function(url) {
26+
urlParse(url) {
2927
var a = document.createElement('a');
3028
a.href = url;
3129
return a;
@@ -38,7 +36,7 @@ function UtilService($window) {
3836
* @param {String|String[]} [origins] - additional origins to test against
3937
* @return {Boolean} - true if url is same origin
4038
*/
41-
isSameOrigin: function(url, origins) {
39+
isSameOrigin(url, origins) {
4240
url = Util.urlParse(url);
4341
origins = (origins && [].concat(origins)) || [];
4442
origins = origins.map(Util.urlParse);
@@ -50,7 +48,6 @@ function UtilService($window) {
5048
});
5149
return (origins.length >= 1);
5250
}
53-
5451
};
5552

5653
return Util;

Diff for: app/templates/server/api/user(auth)/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
import express from 'express';
4-
import controller from './user.controller';
5-
import auth from '../../auth/auth.service';
3+
import {Router} from 'express';
4+
import * as controller from './user.controller';
5+
import * as auth from '../../auth/auth.service';
66

7-
var router = express.Router();
7+
var router = new Router();
88

99
router.get('/', auth.hasRole('admin'), controller.index);
1010
router.delete('/:id', auth.hasRole('admin'), controller.destroy);
@@ -13,4 +13,4 @@ router.put('/:id/password', auth.isAuthenticated(), controller.changePassword);
1313
router.get('/:id', auth.isAuthenticated(), controller.show);
1414
router.post('/', controller.create);
1515

16-
module.exports = router;
16+
export default router;

Diff for: app/templates/server/api/user(auth)/index.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ var userCtrlStub = {
1212
};
1313

1414
var authServiceStub = {
15-
isAuthenticated: function() {
15+
isAuthenticated() {
1616
return 'authService.isAuthenticated';
1717
},
18-
hasRole: function(role) {
18+
hasRole(role) {
1919
return 'authService.hasRole.' + role;
2020
}
2121
};
@@ -30,7 +30,7 @@ var routerStub = {
3030
// require the index with our stubbed out modules
3131
var userIndex = proxyquire('./index', {
3232
'express': {
33-
Router: function() {
33+
Router() {
3434
return routerStub;
3535
}
3636
},

0 commit comments

Comments
 (0)