Skip to content

Commit 5c32d40

Browse files
author
ajhool
committed
Threading through flow
1 parent bf93a93 commit 5c32d40

File tree

10 files changed

+63
-70
lines changed

10 files changed

+63
-70
lines changed

.flowconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[options]

app/config/constants.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* @flow */
2+
13
import path from 'path';
24
import merge from 'lodash/merge';
35

@@ -45,7 +47,7 @@ const defaultConfig = {
4547
const environmentConfigs = {
4648
development: {
4749
mongo: {
48-
uri: process.env.MONGO_URI || 'mongodb://localhost/development',
50+
uri: process.env.MONGO_URI || 'mongodb://localhost/content-dev',
4951
},
5052
security: {
5153
saltRounds: 4,
@@ -54,7 +56,7 @@ const environmentConfigs = {
5456
test: {
5557
port: 5678,
5658
mongo: {
57-
uri: process.env.MONGO_URI || 'mongodb://localhost/test',
59+
uri: process.env.MONGO_URI || 'mongodb://localhost/content-test',
5860
},
5961
security: {
6062
saltRounds: 4,

app/controllers/auth.controller.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

app/controllers/base.controller.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1+
/* @flow */
2+
13
class BaseController {
2-
filterParams(params, whitelist) {
3-
const filtered = {};
4+
filterParams(params, whitelist : Array<string>) {
5+
const filtered = {}
46
for (const key in params) {
57
if (whitelist.indexOf(key) > -1) {
6-
filtered[key] = params[key];
8+
filtered[key] = params[key]
79
}
810
}
9-
return filtered;
11+
return filtered
1012
}
1113

1214
formatApiError(err) {
1315
if (!err) {
1416
// eslint-disable-next-line no-console
15-
return console.error('Provide an error');
17+
return console.error('Provide an error')
1618
}
1719

1820
const formatted = {
1921
message: err.message,
2022
};
2123

2224
if (err.errors) {
23-
formatted.errors = {};
24-
const errors = err.errors;
25+
formatted.errors = {}
26+
const errors = err.errors
2527
for (const type in errors) {
2628
if (errors.hasOwnProperty(type)) {
27-
formatted.errors[type] = errors[type].message;
29+
formatted.errors[type] = errors[type].message
2830
}
2931
}
3032
}
3133

32-
return formatted;
34+
return formatted
3335
}
3436
}
3537

36-
export default BaseController;
38+
export default BaseController

app/controllers/meta.controller.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import BaseController from './base.controller';
2-
import Constants from '../config/constants';
1+
/* @flow */
2+
3+
import BaseController from './base.controller'
4+
import Constants from '../config/constants'
35

46
class MetaController extends BaseController {
57
index(req, res) {
68
res.json({
79
version: Constants.version,
8-
});
10+
})
911
}
1012
}
1113

app/controllers/posts.controller.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1-
import BaseController from './base.controller';
2-
import Post from '../models/post';
1+
/* @flow */
2+
3+
import BaseController from './base.controller'
4+
import Post from '../models/post'
35

46
class PostController extends BaseController {
57

6-
whitelist = [
8+
whitelist : Array<string> = [
79
'text',
8-
];
10+
]
911

1012
// Middleware to populate post based on url param
1113
_populate = async (req, res, next) => {
12-
const { id } = req.params;
14+
const { id } = req.params
1315

1416
try {
15-
const post = await Post.findById(id);
17+
const post = await Post.findById(id)
1618

1719
if (!post) {
18-
const err = new Error('Post not found.');
19-
err.status = 404;
20-
return next(err);
20+
const err = new Error('Post not found.')
21+
err.status = 404
22+
return next(err)
2123
}
2224

23-
req.post = post;
24-
next();
25+
req.post = post
26+
next()
2527
} catch(err) {
26-
err.status = err.name ==='CastError' ? 404 : 500;
27-
next(err);
28+
err.status = err.name ==='CastError' ? 404 : 500
29+
next(err)
2830
}
2931
}
3032

3133
search = async (req, res, next) => {
3234
try {
3335
const posts =
3436
await Post.find({})
35-
.populate({ path: '_user', select: '-posts -role' });
37+
.populate({ path: '_user', select: '-posts -role' })
3638

3739
res.json(posts);
3840
} catch(err) {
@@ -45,25 +47,25 @@ class PostController extends BaseController {
4547
*/
4648

4749
fetch = (req, res) => {
48-
res.json(req.post);
50+
res.json(req.post)
4951
}
5052

5153
/**
5254
* req.user is populated by middleware in routes.js
5355
*/
5456

5557
create = async (req, res, next) => {
56-
const params = this.filterParams(req.body, this.whitelist);
58+
const params = this.filterParams(req.body, this.whitelist)
5759

5860
const post = new Post({
5961
...params,
6062
_user: req.currentUser._id,
6163
});
6264

6365
try {
64-
res.status(201).json(await post.save());
66+
res.status(201).json(await post.save())
6567
} catch(err) {
66-
next(err);
68+
next(err)
6769
}
6870
}
6971

@@ -75,15 +77,15 @@ class PostController extends BaseController {
7577
*/
7678
if (req.post._user.toString() === req.currentUser._id.toString()) {
7779
try {
78-
await req.post.remove();
79-
res.sendStatus(204);
80+
await req.post.remove()
81+
res.sendStatus(204)
8082
} catch(err) {
81-
next(err);
83+
next(err)
8284
}
8385
} else {
84-
res.sendStatus(403);
86+
res.sendStatus(403)
8587
}
8688
}
8789
}
8890

89-
export default new PostController();
91+
export default new PostController()

app/middleware/authenticate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import jwt from 'jsonwebtoken';
2-
import User from '../models/user';
32
import Constants from '../config/constants';
43

54
const { sessionSecret } = Constants.security;
65

6+
//TODO: Authenticate access to all posts with this user's ID, but we don't
7+
// Need the actual user to search for it here.
8+
79
export default function authenticate(req, res, next) {
810
const { authorization } = req.headers;
911
jwt.verify(authorization, sessionSecret, async (err, decoded) => {

app/models/post.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const PostSchema = new Schema({
1010
// likes : [{ type: Schema.Types.ObjectId, ref: 'Like' }],
1111
// comments : [{ type: Schema.Types.ObjectId, ref: 'Comment' }],
1212
// flags : [{ type: Schema.Types.ObjectId, ref: 'Flag' }]
13-
_user: { type: Schema.Types.ObjectId, ref: 'User' },
13+
14+
//ajh removal:
15+
//_user: { type: Schema.Types.ObjectId, ref: 'User' },
1416
}, {
1517
timestamps: true,
1618
});

app/routes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Router } from 'express';
22

33
import MetaController from './controllers/meta.controller';
4-
import AuthController from './controllers/auth.controller';
54
import PostsController from './controllers/posts.controller';
65

76
import authenticate from './middleware/authenticate';
@@ -13,7 +12,7 @@ const routes = new Router();
1312
routes.get('/', MetaController.index);
1413

1514
// Authentication
16-
routes.post('/auth/login', AuthController.login);
15+
//routes.post('/auth/login', AuthController.login);
1716

1817
// Post
1918
routes.get('/posts', PostsController.search);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"repository": {
2121
"type": "git",
22-
"url": "git://github.com/kylealwyn/node-rest-api-boilerplate.git"
22+
"url": "git://github.com/ajhool/node-rest-api-boilerplate.git"
2323
},
2424
"keywords": [
2525
"express",
@@ -32,9 +32,9 @@
3232
"author": "Kyle Alwyn <[email protected]>",
3333
"license": "MIT",
3434
"bugs": {
35-
"url": "https://github.com/kylealwyn/node-rest-api-boilerplate/issues"
35+
"url": "https://github.com/ajhool/node-rest-api-boilerplate/issues"
3636
},
37-
"homepage": "https://github.com/kylealwyn/node-rest-api-boilerplate",
37+
"homepage": "https://github.com/ajhool/node-rest-api-boilerplate",
3838
"dependencies": {
3939
"babel-core": "^6.18.0",
4040
"bcrypt": "^1.0.0",

0 commit comments

Comments
 (0)