Skip to content

Commit

Permalink
response formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
shipengqi committed Jun 11, 2018
1 parent 1a39a61 commit 7353c16
Show file tree
Hide file tree
Showing 5 changed files with 811 additions and 821 deletions.
19 changes: 1 addition & 18 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
{
"extends": "sactive",
"rules": {
"space-unary-ops":
["error", {
"words": true,
"nonwords": true,
"overrides": {
"!": false
}
}],
"no-useless-call": 0,
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "ignore"
}],
"new-cap": ["error", { "newIsCap": false }]
}
"extends": "sactive"
}
8 changes: 8 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ const _ = require('lodash');
const {Sactive} = require('./di');
const {Route} = require('./route');
const Log = require('./logger');
const {
ResponseProcessorFactory,
JsonProcessor,
HtmlProcessor
} = require('./response_formatter/response');
const binder = new Sactive();
binder.bindClass('logger', Log);
binder.bindClass('route', Route);
binder.bindClass('jsonProcessor', JsonProcessor);
binder.bindClass('htmlProcessor', HtmlProcessor);
binder.bindClass('responseProcessor', ResponseProcessorFactory);

/**
* Create a new `Application`.
Expand Down
21 changes: 10 additions & 11 deletions lib/response_formatter/response.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
const _ = require('lodash');
const {UnifiedError} = require('./error');

class responseProcessorFactory {
constructor(processors) {
this.processors = processors;
class ResponseProcessorFactory {
constructor($$jsonProcessor, $$htmlProcessor) {
this.processors = [$$jsonProcessor, $$htmlProcessor];
}

determine(ctx) {
ctx.accepts('text/html');
return _.find(this.processors, processor => {
return processor.determine(ctx);
});
}
}

class JsonProcessor {
processSuccess(response, ctx) {
processSuccess(ctx, response) {
ctx.body = {
code: 200,
msg: 'success.',
data: response
};
}

processFail(response, ctx) {
processFail(ctx, response) {
let result = null;
if (!response) {
response = new UnifiedError();
Expand All @@ -42,16 +41,16 @@ class JsonProcessor {
if (_.isEmpty(contentType)) {
return false;
}
return ctx.is(contentType).includes('json');
return ctx.accepts('json') && ctx.is(contentType).includes('json');
}
}

class HtmlProcessor {
processSuccess(response, ctx) {
processSuccess(ctx, response) {
ctx.body = response;
}

processFail(response, ctx) {
processFail(ctx, response) {
let result = null;
if (!response) {
response = new UnifiedError();
Expand All @@ -67,11 +66,11 @@ class HtmlProcessor {
}

determine(ctx) {
return true;
return ctx.accepts('html');
}
}
module.exports = {
responseProcessorFactory,
ResponseProcessorFactory,
JsonProcessor,
HtmlProcessor
};
Loading

0 comments on commit 7353c16

Please sign in to comment.