Skip to content

Commit

Permalink
Threading through flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhool committed Mar 30, 2017
1 parent bf93a93 commit 5c32d40
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 70 deletions.
7 changes: 7 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ignore]

[include]

[libs]

[options]
6 changes: 4 additions & 2 deletions app/config/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @flow */

import path from 'path';
import merge from 'lodash/merge';

Expand Down Expand Up @@ -45,7 +47,7 @@ const defaultConfig = {
const environmentConfigs = {
development: {
mongo: {
uri: process.env.MONGO_URI || 'mongodb://localhost/development',
uri: process.env.MONGO_URI || 'mongodb://localhost/content-dev',
},
security: {
saltRounds: 4,
Expand All @@ -54,7 +56,7 @@ const environmentConfigs = {
test: {
port: 5678,
mongo: {
uri: process.env.MONGO_URI || 'mongodb://localhost/test',
uri: process.env.MONGO_URI || 'mongodb://localhost/content-test',
},
security: {
saltRounds: 4,
Expand Down
25 changes: 0 additions & 25 deletions app/controllers/auth.controller.js

This file was deleted.

22 changes: 12 additions & 10 deletions app/controllers/base.controller.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
/* @flow */

class BaseController {
filterParams(params, whitelist) {
const filtered = {};
filterParams(params, whitelist : Array<string>) {
const filtered = {}
for (const key in params) {
if (whitelist.indexOf(key) > -1) {
filtered[key] = params[key];
filtered[key] = params[key]
}
}
return filtered;
return filtered
}

formatApiError(err) {
if (!err) {
// eslint-disable-next-line no-console
return console.error('Provide an error');
return console.error('Provide an error')
}

const formatted = {
message: err.message,
};

if (err.errors) {
formatted.errors = {};
const errors = err.errors;
formatted.errors = {}
const errors = err.errors
for (const type in errors) {
if (errors.hasOwnProperty(type)) {
formatted.errors[type] = errors[type].message;
formatted.errors[type] = errors[type].message
}
}
}

return formatted;
return formatted
}
}

export default BaseController;
export default BaseController
8 changes: 5 additions & 3 deletions app/controllers/meta.controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import BaseController from './base.controller';
import Constants from '../config/constants';
/* @flow */

import BaseController from './base.controller'
import Constants from '../config/constants'

class MetaController extends BaseController {
index(req, res) {
res.json({
version: Constants.version,
});
})
}
}

Expand Down
48 changes: 25 additions & 23 deletions app/controllers/posts.controller.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
import BaseController from './base.controller';
import Post from '../models/post';
/* @flow */

import BaseController from './base.controller'
import Post from '../models/post'

class PostController extends BaseController {

whitelist = [
whitelist : Array<string> = [
'text',
];
]

// Middleware to populate post based on url param
_populate = async (req, res, next) => {
const { id } = req.params;
const { id } = req.params

try {
const post = await Post.findById(id);
const post = await Post.findById(id)

if (!post) {
const err = new Error('Post not found.');
err.status = 404;
return next(err);
const err = new Error('Post not found.')
err.status = 404
return next(err)
}

req.post = post;
next();
req.post = post
next()
} catch(err) {
err.status = err.name ==='CastError' ? 404 : 500;
next(err);
err.status = err.name ==='CastError' ? 404 : 500
next(err)
}
}

search = async (req, res, next) => {
try {
const posts =
await Post.find({})
.populate({ path: '_user', select: '-posts -role' });
.populate({ path: '_user', select: '-posts -role' })

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

fetch = (req, res) => {
res.json(req.post);
res.json(req.post)
}

/**
* req.user is populated by middleware in routes.js
*/

create = async (req, res, next) => {
const params = this.filterParams(req.body, this.whitelist);
const params = this.filterParams(req.body, this.whitelist)

const post = new Post({
...params,
_user: req.currentUser._id,
});

try {
res.status(201).json(await post.save());
res.status(201).json(await post.save())
} catch(err) {
next(err);
next(err)
}
}

Expand All @@ -75,15 +77,15 @@ class PostController extends BaseController {
*/
if (req.post._user.toString() === req.currentUser._id.toString()) {
try {
await req.post.remove();
res.sendStatus(204);
await req.post.remove()
res.sendStatus(204)
} catch(err) {
next(err);
next(err)
}
} else {
res.sendStatus(403);
res.sendStatus(403)
}
}
}

export default new PostController();
export default new PostController()
4 changes: 3 additions & 1 deletion app/middleware/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import jwt from 'jsonwebtoken';
import User from '../models/user';
import Constants from '../config/constants';

const { sessionSecret } = Constants.security;

//TODO: Authenticate access to all posts with this user's ID, but we don't
// Need the actual user to search for it here.

export default function authenticate(req, res, next) {
const { authorization } = req.headers;
jwt.verify(authorization, sessionSecret, async (err, decoded) => {
Expand Down
4 changes: 3 additions & 1 deletion app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const PostSchema = new Schema({
// likes : [{ type: Schema.Types.ObjectId, ref: 'Like' }],
// comments : [{ type: Schema.Types.ObjectId, ref: 'Comment' }],
// flags : [{ type: Schema.Types.ObjectId, ref: 'Flag' }]
_user: { type: Schema.Types.ObjectId, ref: 'User' },

//ajh removal:
//_user: { type: Schema.Types.ObjectId, ref: 'User' },
}, {
timestamps: true,
});
Expand Down
3 changes: 1 addition & 2 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Router } from 'express';

import MetaController from './controllers/meta.controller';
import AuthController from './controllers/auth.controller';
import PostsController from './controllers/posts.controller';

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

// Authentication
routes.post('/auth/login', AuthController.login);
//routes.post('/auth/login', AuthController.login);

// Post
routes.get('/posts', PostsController.search);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"repository": {
"type": "git",
"url": "git://github.com/kylealwyn/node-rest-api-boilerplate.git"
"url": "git://github.com/ajhool/node-rest-api-boilerplate.git"
},
"keywords": [
"express",
Expand All @@ -32,9 +32,9 @@
"author": "Kyle Alwyn <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/kylealwyn/node-rest-api-boilerplate/issues"
"url": "https://github.com/ajhool/node-rest-api-boilerplate/issues"
},
"homepage": "https://github.com/kylealwyn/node-rest-api-boilerplate",
"homepage": "https://github.com/ajhool/node-rest-api-boilerplate",
"dependencies": {
"babel-core": "^6.18.0",
"bcrypt": "^1.0.0",
Expand Down

0 comments on commit 5c32d40

Please sign in to comment.