Skip to content

Commit 64250eb

Browse files
authored
refactor: separate responsibilities in req/res flow (#153)
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
1 parent 78fedec commit 64250eb

File tree

22 files changed

+515
-326
lines changed

22 files changed

+515
-326
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lux-framework",
3-
"version": "0.0.1-beta.13",
3+
"version": "1.0.0-rc",
44
"description": "A MVC style Node.js framework for building lightning fast JSON APIs",
55
"repository": "https://github.com/postlight/lux",
66
"keywords": [

src/packages/application/initialize.js

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ export default async function initialize(app: Application, {
3838
enabled: log
3939
});
4040

41-
const router = new Router();
42-
43-
const server = new Server({
44-
router,
45-
logger
46-
});
47-
4841
const store = await new Database({
4942
path,
5043
models,
@@ -55,56 +48,6 @@ export default async function initialize(app: Application, {
5548

5649
Object.freeze(store);
5750

58-
Object.defineProperties(app, {
59-
path: {
60-
value: path,
61-
writable: false,
62-
enumerable: true,
63-
configurable: false
64-
},
65-
66-
port: {
67-
value: port,
68-
writable: false,
69-
enumerable: true,
70-
configurable: false
71-
},
72-
73-
store: {
74-
value: store,
75-
writable: false,
76-
enumerable: false,
77-
configurable: false
78-
},
79-
80-
logger: {
81-
value: logger,
82-
writable: false,
83-
enumerable: true,
84-
configurable: false
85-
},
86-
87-
router: {
88-
value: router,
89-
writable: false,
90-
enumerable: false,
91-
configurable: false
92-
},
93-
94-
server: {
95-
value: server,
96-
writable: false,
97-
enumerable: false,
98-
configurable: false
99-
}
100-
});
101-
102-
Object.assign(app, {
103-
models,
104-
controllers,
105-
serializers
106-
});
107-
10851
models.forEach((model, name) => {
10952
const resource = pluralize(name);
11053

@@ -162,8 +105,15 @@ export default async function initialize(app: Application, {
162105
}
163106
});
164107

165-
router.controllers = controllers;
166-
routes.call(null, router.route, router.resource);
108+
const router = new Router({
109+
routes,
110+
controllers
111+
});
112+
113+
const server = new Server({
114+
router,
115+
logger
116+
});
167117

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

128+
Object.defineProperties(app, {
129+
path: {
130+
value: path,
131+
writable: false,
132+
enumerable: true,
133+
configurable: false
134+
},
135+
136+
port: {
137+
value: port,
138+
writable: false,
139+
enumerable: true,
140+
configurable: false
141+
},
142+
143+
models: {
144+
value: models,
145+
writable: false,
146+
enumerable: true,
147+
configurable: false
148+
},
149+
150+
controllers: {
151+
value: controllers,
152+
writable: false,
153+
enumerable: true,
154+
configurable: false
155+
},
156+
157+
serializers: {
158+
value: serializers,
159+
writable: false,
160+
enumerable: true,
161+
configurable: false
162+
},
163+
164+
store: {
165+
value: store,
166+
writable: false,
167+
enumerable: false,
168+
configurable: false
169+
},
170+
171+
logger: {
172+
value: logger,
173+
writable: false,
174+
enumerable: true,
175+
configurable: false
176+
},
177+
178+
router: {
179+
value: router,
180+
writable: false,
181+
enumerable: false,
182+
configurable: false
183+
},
184+
185+
server: {
186+
value: server,
187+
writable: false,
188+
enumerable: false,
189+
configurable: false
190+
}
191+
});
192+
178193
Object.freeze(app);
179194
Object.freeze(logger);
180195
Object.freeze(router);

src/packages/cli/commands/destroy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default async function destroy(type, name) {
4747
.split('\n')
4848
.reduce((lines, line) => {
4949
const pattern = new RegExp(
50-
`\s*resource\\(('|"|\`)${pluralize(name)}('|"|\`)\\);?`
50+
`\s*this.resource\\(('|"|\`)${pluralize(name)}('|"|\`)\\);?`
5151
);
5252

5353
return pattern.test(line) ? lines : [...lines, line];

src/packages/cli/commands/generate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export default async function generate(type, name, pwd = PWD, attrs = []) {
136136
}
137137

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

142142
return str;

src/packages/serializer/content-stream/index.js renamed to src/packages/content-stream/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ class ContentStream extends Transform {
1414
return this;
1515
}
1616

17-
_transform(chunk: ?Object, encoding: string, done: () => void): void {
17+
_transform(
18+
chunk: string | Buffer | Object,
19+
encoding: string,
20+
done: () => void
21+
): void {
1822
if (chunk && typeof chunk === 'object') {
19-
this.push(JSON.stringify(chunk));
23+
chunk = JSON.stringify(chunk);
2024
}
2125

26+
this.push(chunk);
27+
2228
done(null);
2329
}
2430
}

src/packages/controller/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ class Controller {
412412
}
413413
} = req;
414414

415+
res.statusCode = 201;
416+
415417
return this.model.create(attributes);
416418
}
417419

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

450452
if (record) {
451453
await record.destroy();
452454
}
453455

454-
return record;
456+
return 204;
455457
}
456458

457459
/**
458460
* An action handler used for responding to HEAD or OPTIONS requests.
459461
*
460462
* @private
461463
*/
462-
preflight(req: IncomingMessage, res: ServerResponse): boolean {
463-
return true;
464+
preflight(req: IncomingMessage, res: ServerResponse): number {
465+
return 204;
464466
}
465467
}
466468

src/packages/route/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @flow
2+
export const ID_PATTERN = /(?![\=])(\d+)/g;
3+
export const RESOURCE_PATTERN = /^((?!\/)[a-z\-]+)/ig;

src/packages/route/index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// @flow
2+
import { ID_PATTERN, RESOURCE_PATTERN } from './constants';
3+
24
import createAction from './utils/create-action';
35
import getStaticPath from './utils/get-static-path';
46
import getDynamicSegments from './utils/get-dynamic-segments';
57

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

1011
/**
1112
* @private
@@ -32,13 +33,8 @@ class Route {
3233
action,
3334
controllers,
3435
method
35-
}: {
36-
path: string,
37-
action: string,
38-
controllers: Map<string, Controller>,
39-
method: string
40-
}) {
41-
const [resource] = path.match(resourcePattern) || [path];
36+
}: options) {
37+
const [resource] = path.match(RESOURCE_PATTERN) || [path];
4238
const controller: ?Controller = controllers.get(resource);
4339
const dynamicSegments = getDynamicSegments(path);
4440
let handlers;
@@ -119,6 +115,24 @@ class Route {
119115

120116
return this;
121117
}
118+
119+
parseParams(pathname: string): Object {
120+
const parts = pathname.match(ID_PATTERN) || [];
121+
122+
return parts.reduce((params, val, index) => {
123+
const key = this.dynamicSegments[index];
124+
125+
if (key) {
126+
params = {
127+
...params,
128+
[key]: parseInt(val, 10)
129+
};
130+
}
131+
132+
return params;
133+
}, {});
134+
}
122135
}
123136

137+
export { ID_PATTERN, RESOURCE_PATTERN } from './constants';
124138
export default Route;

src/packages/route/interfaces.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @flow
2+
import type Controller from '../controller';
3+
4+
export type options = {
5+
path: string;
6+
action: string;
7+
method: string;
8+
controllers: Map<string, Controller>;
9+
};

src/packages/router/define/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
import route from './utils/define-route';
3+
import resource from './utils/define-resource';
4+
5+
export { default as route } from './utils/define-route';
6+
export { default as resource } from './utils/define-resource';
7+
8+
export default {
9+
route,
10+
resource
11+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @flow
2+
import type Router from '../index';
3+
import type { options as routeOptions } from '../../route/interfaces';
4+
5+
export type options = routeOptions & { router: Router };

0 commit comments

Comments
 (0)