Skip to content

Commit

Permalink
refactor: separate responsibilities in req/res flow (#153)
Browse files Browse the repository at this point in the history
refactor: router doesn't need it's own serializer

fix: router doesn't need idPattern constant

refactor: add missing ID_PATTERN constant import to router
  • Loading branch information
zacharygolba authored Jun 18, 2016
1 parent 78fedec commit 64250eb
Show file tree
Hide file tree
Showing 22 changed files with 515 additions and 326 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lux-framework",
"version": "0.0.1-beta.13",
"version": "1.0.0-rc",
"description": "A MVC style Node.js framework for building lightning fast JSON APIs",
"repository": "https://github.com/postlight/lux",
"keywords": [
Expand Down
133 changes: 74 additions & 59 deletions src/packages/application/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ export default async function initialize(app: Application, {
enabled: log
});

const router = new Router();

const server = new Server({
router,
logger
});

const store = await new Database({
path,
models,
Expand All @@ -55,56 +48,6 @@ export default async function initialize(app: Application, {

Object.freeze(store);

Object.defineProperties(app, {
path: {
value: path,
writable: false,
enumerable: true,
configurable: false
},

port: {
value: port,
writable: false,
enumerable: true,
configurable: false
},

store: {
value: store,
writable: false,
enumerable: false,
configurable: false
},

logger: {
value: logger,
writable: false,
enumerable: true,
configurable: false
},

router: {
value: router,
writable: false,
enumerable: false,
configurable: false
},

server: {
value: server,
writable: false,
enumerable: false,
configurable: false
}
});

Object.assign(app, {
models,
controllers,
serializers
});

models.forEach((model, name) => {
const resource = pluralize(name);

Expand Down Expand Up @@ -162,8 +105,15 @@ export default async function initialize(app: Application, {
}
});

router.controllers = controllers;
routes.call(null, router.route, router.resource);
const router = new Router({
routes,
controllers
});

const server = new Server({
router,
logger
});

server.instance.listen(port).once('listening', () => {
if (typeof process.send === 'function') {
Expand All @@ -175,6 +125,71 @@ export default async function initialize(app: Application, {
}
});

Object.defineProperties(app, {
path: {
value: path,
writable: false,
enumerable: true,
configurable: false
},

port: {
value: port,
writable: false,
enumerable: true,
configurable: false
},

models: {
value: models,
writable: false,
enumerable: true,
configurable: false
},

controllers: {
value: controllers,
writable: false,
enumerable: true,
configurable: false
},

serializers: {
value: serializers,
writable: false,
enumerable: true,
configurable: false
},

store: {
value: store,
writable: false,
enumerable: false,
configurable: false
},

logger: {
value: logger,
writable: false,
enumerable: true,
configurable: false
},

router: {
value: router,
writable: false,
enumerable: false,
configurable: false
},

server: {
value: server,
writable: false,
enumerable: false,
configurable: false
}
});

Object.freeze(app);
Object.freeze(logger);
Object.freeze(router);
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/commands/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default async function destroy(type, name) {
.split('\n')
.reduce((lines, line) => {
const pattern = new RegExp(
`\s*resource\\(('|"|\`)${pluralize(name)}('|"|\`)\\);?`
`\s*this.resource\\(('|"|\`)${pluralize(name)}('|"|\`)\\);?`
);

return pattern.test(line) ? lines : [...lines, line];
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default async function generate(type, name, pwd = PWD, attrs = []) {
}

if (index + 1 === closeIndex) {
str += `${indent(2)}resource('${pluralize(name)}');\n`;
str += `${indent(2)}this.resource('${pluralize(name)}');\n`;
}

return str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ class ContentStream extends Transform {
return this;
}

_transform(chunk: ?Object, encoding: string, done: () => void): void {
_transform(
chunk: string | Buffer | Object,
encoding: string,
done: () => void
): void {
if (chunk && typeof chunk === 'object') {
this.push(JSON.stringify(chunk));
chunk = JSON.stringify(chunk);
}

this.push(chunk);

done(null);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/packages/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ class Controller {
}
} = req;

res.statusCode = 201;

return this.model.create(attributes);
}

Expand Down Expand Up @@ -444,23 +446,23 @@ class Controller {
/**
* Destroy a single `Model` instance that the Controller instance represents.
*/
async destroy(req: IncomingMessage, res: ServerResponse): Promise<?Model> {
async destroy(req: IncomingMessage, res: ServerResponse): Promise<number> {
const record = await this.model.find(req.params.id);

if (record) {
await record.destroy();
}

return record;
return 204;
}

/**
* An action handler used for responding to HEAD or OPTIONS requests.
*
* @private
*/
preflight(req: IncomingMessage, res: ServerResponse): boolean {
return true;
preflight(req: IncomingMessage, res: ServerResponse): number {
return 204;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/packages/route/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
export const ID_PATTERN = /(?![\=])(\d+)/g;
export const RESOURCE_PATTERN = /^((?!\/)[a-z\-]+)/ig;
34 changes: 24 additions & 10 deletions src/packages/route/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @flow
import { ID_PATTERN, RESOURCE_PATTERN } from './constants';

import createAction from './utils/create-action';
import getStaticPath from './utils/get-static-path';
import getDynamicSegments from './utils/get-dynamic-segments';

import type { Controller } from '../controller';

const resourcePattern = /^((?!\/)[a-z\-]+)/ig;
import type Controller from '../controller';
import type { options } from './interfaces';

/**
* @private
Expand All @@ -32,13 +33,8 @@ class Route {
action,
controllers,
method
}: {
path: string,
action: string,
controllers: Map<string, Controller>,
method: string
}) {
const [resource] = path.match(resourcePattern) || [path];
}: options) {
const [resource] = path.match(RESOURCE_PATTERN) || [path];
const controller: ?Controller = controllers.get(resource);
const dynamicSegments = getDynamicSegments(path);
let handlers;
Expand Down Expand Up @@ -119,6 +115,24 @@ class Route {

return this;
}

parseParams(pathname: string): Object {
const parts = pathname.match(ID_PATTERN) || [];

return parts.reduce((params, val, index) => {
const key = this.dynamicSegments[index];

if (key) {
params = {
...params,
[key]: parseInt(val, 10)
};
}

return params;
}, {});
}
}

export { ID_PATTERN, RESOURCE_PATTERN } from './constants';
export default Route;
9 changes: 9 additions & 0 deletions src/packages/route/interfaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow
import type Controller from '../controller';

export type options = {
path: string;
action: string;
method: string;
controllers: Map<string, Controller>;
};
11 changes: 11 additions & 0 deletions src/packages/router/define/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @flow
import route from './utils/define-route';
import resource from './utils/define-resource';

export { default as route } from './utils/define-route';
export { default as resource } from './utils/define-resource';

export default {
route,
resource
};
5 changes: 5 additions & 0 deletions src/packages/router/define/interfaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow
import type Router from '../index';
import type { options as routeOptions } from '../../route/interfaces';

export type options = routeOptions & { router: Router };
Loading

0 comments on commit 64250eb

Please sign in to comment.