Skip to content

Commit

Permalink
refactor: rename serializer methods and return objects (#155)
Browse files Browse the repository at this point in the history
* refactor: rename serializer methods and return objects

* fix: ensure a successful POST request has a 201 status
  • Loading branch information
zacharygolba authored Jun 19, 2016
1 parent 7597031 commit 6748638
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 63 deletions.
2 changes: 0 additions & 2 deletions src/packages/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,6 @@ class Controller {
}
} = req;

res.statusCode = 201;

return this.model.create(attributes);
}

Expand Down
6 changes: 3 additions & 3 deletions src/packages/route/utils/create-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function createAction(
controller: Controller,
action: () => Promise
): Array<Function> {
const { middleware } = controller;
const { middleware, serializer } = controller;

const builtIns = [
sanitizeParams,
Expand Down Expand Up @@ -83,7 +83,7 @@ export default function createAction(
})
};

return controller.serializer.stream({
return serializer.format({
data,
links,
domain,
Expand All @@ -99,7 +99,7 @@ export default function createAction(
fields = controller.attributes;
}

return controller.serializer.stream({
return serializer.format({
data,
links,
domain,
Expand Down
46 changes: 8 additions & 38 deletions src/packages/serializer/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import { pluralize } from 'inflection';

import ContentStream from '../content-stream';
import { Model } from '../database';

import pick from '../../utils/pick';
Expand Down Expand Up @@ -296,36 +295,7 @@ class Serializer {
/**
* @private
*/
stream({
data,
links = {},
fields = [],
domain = '',
include = {}
}: {
data?: ?(Model | Array<Model>);
links?: Object;
fields?: Array<string>;
domain?: string;
include?: Object;
}): ContentStream {
return new ContentStream().once('ready', (stream: ContentStream) => {
const serialized = this.serialize({
data,
links,
fields,
domain,
include
});

stream.end(serialized);
});
}

/**
* @private
*/
serialize({
format({
data,
links,
fields,
Expand Down Expand Up @@ -354,7 +324,7 @@ class Serializer {
...serialized,

data: data.map(item => {
return this.serializeOne({
return this.formatOne({
item,
fields,
domain,
Expand All @@ -367,7 +337,7 @@ class Serializer {
serialized = {
...serialized,

data: this.serializeOne({
data: this.formatOne({
fields,
domain,
include,
Expand Down Expand Up @@ -407,7 +377,7 @@ class Serializer {
/**
* @private
*/
serializeOne({
formatOne({
item,
links,
fields,
Expand Down Expand Up @@ -451,7 +421,7 @@ class Serializer {
attrs = attrs.filter(field => !idRegExp.test(field));

if (related instanceof Model) {
hash[name] = this.serializeRelationship({
hash[name] = this.formatRelationship({
domain,
included,
item: related,
Expand All @@ -464,7 +434,7 @@ class Serializer {
const {
data: relatedData,
links: relatedLinks
} = this.serializeRelationship({
} = this.formatRelationship({
domain,
included,
item: relatedItem,
Expand Down Expand Up @@ -503,7 +473,7 @@ class Serializer {
/**
* @private
*/
serializeRelationship({
formatRelationship({
item,
fields,
domain,
Expand Down Expand Up @@ -532,7 +502,7 @@ class Serializer {

if (shouldInclude) {
included.push(
serializer.serializeOne({
serializer.formatOne({
item,
domain,
fields,
Expand Down
14 changes: 10 additions & 4 deletions src/packages/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import type Router from '../router';
*/
class Server {
router: Router;

logger: Logger;

instance: HTTPServer;

constructor({
Expand Down Expand Up @@ -95,17 +97,21 @@ class Server {
};
}

this.sendResponse(res, await this.router.visit(req, res));
this.sendResponse(req, res, await this.router.visit(req, res));
}, err => {
this.sendResponse(res, err);
this.sendResponse(req, res, err);
});
}

sendResponse(res: ServerResponse, data: void | mixed): void {
sendResponse(
req: IncomingMessage,
res: ServerResponse,
data: void | ?mixed
): void {
if (data && typeof data.pipe === 'function') {
data.pipe(res);
} else {
responder.resolve(res, data);
responder.resolve(req, res, data);
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/packages/server/responder/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
// @flow
import ContentStream from '../../content-stream';
import Response from './response';

import normalize from './utils/normalize';

import type { ServerResponse } from 'http';
import type { IncomingMessage, ServerResponse } from 'http';

export function resolve(res: ServerResponse, data: ?mixed | void): void {
new ContentStream()
.once('ready', (stream: ContentStream) => {
const {
export function resolve(
req: IncomingMessage,
res: ServerResponse,
data: ?mixed | void
): void {
new Response()
.once('ready', (stream: Response) => {
let {
normalized,
statusCode
} = normalize(data);

if (statusCode === 200 && req.method === 'POST') {
statusCode++;
}

res.statusCode = statusCode;
stream.end(normalized);
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Transform } from 'stream';

class ContentStream extends Transform {
constructor(): ContentStream {
class Response extends Transform {
constructor(): Response {
super({
encoding: 'utf8',
writableObjectMode: true
Expand Down Expand Up @@ -29,4 +29,4 @@ class ContentStream extends Transform {
}
}

export default ContentStream;
export default Response;
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// @flow
import { STATUS_CODES } from '../constants.js';

export default function responseFor(
/**
* @private
*/
export default function dataFor(
status: number,
err?: Error
): void | Object {
Expand Down
14 changes: 8 additions & 6 deletions src/packages/server/responder/utils/normalize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { STATUS_CODES } from '../constants';
import responseFor from './response-for';
import dataFor from './data-for';

export default function normalize(data: ?mixed | void): {
normalized: mixed;
Expand All @@ -15,7 +15,7 @@ export default function normalize(data: ?mixed | void): {
statusCode = 204;
} else {
statusCode = 401;
normalized = responseFor(statusCode);
normalized = dataFor(statusCode);
}
break;

Expand All @@ -25,22 +25,24 @@ export default function normalize(data: ?mixed | void): {
} else {
statusCode = 404;
}
normalized = responseFor(statusCode);
normalized = dataFor(statusCode);
break;

case 'object':
if (!data) {
statusCode = 404;
normalized = responseFor(statusCode);
normalized = dataFor(statusCode);
} else if (data instanceof Error) {
statusCode = 500;
normalized = responseFor(statusCode, data);
normalized = dataFor(statusCode, data);
} else {
normalized = data;
}
break;

case 'undefined':
statusCode = 404;
normalized = responseFor(statusCode);
normalized = dataFor(statusCode);
break;

default:
Expand Down

0 comments on commit 6748638

Please sign in to comment.