processResponse option · JSONScript/jsonscript-express@9783d4e · GitHub
Skip to content

Commit 9783d4e

Browse files
committed
processResponse option
1 parent 4444274 commit 9783d4e

5 files changed

Lines changed: 132 additions & 42 deletions

File tree

README.md

Lines changed: 17 additions & 3 deletions

index.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@ function jsonscriptExpress(app, options) {
1515
jsonscript: { strict: true },
1616
Promise: (typeof Promise !== 'undefined') && Promise
1717
});
18+
19+
var processResponce = processResponceFunc(options);
1820
var js = JSONScript(options.jsonscript);
19-
METHODS.forEach(function (method) {
20-
execRouter[method] = function(args) {
21-
if (args.method && args.method != method) {
22-
console.warn('method specified in args (' + args.method +
23-
') is different from $method in instruction (' + method + '), used ' + method);
24-
}
25-
args.method = method;
26-
return execRouter(args);
27-
};
28-
});
21+
addExecutorMethods();
2922
js.addExecutor(options.routerExecutor, execRouter);
3023
evaluator.js = js;
3124

@@ -41,7 +34,7 @@ function jsonscriptExpress(app, options) {
4134
.then(function (value) {
4235
res.send(value);
4336
}, function (err) {
44-
res.status(err.errors ? 400 : 500)
37+
res.status(err.errors ? 400 : err.statusCode || 500)
4538
.send({
4639
error: err.message,
4740
errors: err.errors
@@ -67,10 +60,53 @@ function jsonscriptExpress(app, options) {
6760
return new options.Promise(function (resolve, reject) {
6861
request.end(function (err, resp) {
6962
if (err) return reject(err);
70-
resp = _.pick(resp, 'statusCode', 'headers', 'body');
71-
resp.request = args;
72-
resolve(resp);
63+
resolve(processResponce(resp, args));
7364
});
7465
});
7566
}
67+
68+
69+
function addExecutorMethods() {
70+
METHODS.forEach(function (method) {
71+
execRouter[method] = function(args) {
72+
if (args.method && args.method != method) {
73+
console.warn('method specified in args (' + args.method +
74+
') is different from $method in instruction (' + method + '), used ' + method);
75+
}
76+
args.method = method;
77+
return execRouter(args);
78+
};
79+
});
80+
}
7681
}
82+
83+
84+
function processResponceFunc(options) {
85+
return options.processResponse == 'body'
86+
? bodyProcessResponce
87+
: typeof options.processResponse == 'function'
88+
? options.processResponse
89+
: defaultProcessResponse;
90+
}
91+
92+
93+
function bodyProcessResponce(resp) {
94+
if (resp.statusCode < 300) return resp.body;
95+
throw new HttpError(resp);
96+
}
97+
98+
99+
function defaultProcessResponse(resp, args) {
100+
resp = _.pick(resp, 'statusCode', 'headers', 'body');
101+
resp.request = args;
102+
return resp;
103+
}
104+
105+
106+
function HttpError(resp) {
107+
this.message = resp.body ? JSON.stringify(resp.body) : 'Error';
108+
this.statusCode = resp.statusCode;
109+
}
110+
111+
HttpError.prototype = Object.create(Error.prototype);
112+
HttpError.prototype.constructor = HttpError;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonscript-express",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Express middleware for batch processing using JSONScript",
55
"main": "index.js",
66
"scripts": {

spec/app.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
'use strict';
22

33
var express = require('express');
4-
var app = express();
54
var bodyParser = require('body-parser');
65
var jsonscript = require('..');
6+
var _ = require('lodash');
77

88

9-
app.use(bodyParser.json());
9+
module.exports = function createApp(jsonscriptOptions) {
10+
var app = express();
1011

11-
app.get('/api/:name/:id', function (req, res) {
12-
var id = req.params.id;
13-
send(res, req.params.name, id);
14-
});
12+
app.use(bodyParser.json());
1513

16-
app.post('/api/:name', function (req, res) {
17-
var id = Date.now();
18-
send(res, req.params.name, id, req.body);
19-
});
20-
21-
app.post('/js', jsonscript(app, { basePath: '/api' }));
22-
23-
function send(res, name, id, data) {
24-
res.send({
25-
name: name,
26-
id: id,
27-
info: 'resource ' + name + ' id ' + id,
28-
data: data
14+
app.get('/api/:name/:id', function (req, res) {
15+
var id = req.params.id;
16+
send(res, req.params.name, id);
2917
});
30-
}
3118

19+
app.post('/api/:name', function (req, res) {
20+
var id = Date.now();
21+
send(res, req.params.name, id, req.body);
22+
});
3223

33-
if (!module.parent) app.listen(3000);
34-
35-
module.exports = app;
24+
jsonscriptOptions = jsonscriptOptions
25+
? _.extend({ basePath: '/api' }, jsonscriptOptions)
26+
: { basePath: '/api' };
27+
app.post('/js', jsonscript(app, jsonscriptOptions));
28+
29+
function send(res, name, id, data) {
30+
res.send({
31+
name: name,
32+
id: id,
33+
info: 'resource ' + name + ' id ' + id,
34+
data: data
35+
});
36+
}
37+
38+
return app;
39+
};

spec/handler.spec.js

Lines changed: 37 additions & 1 deletion

0 commit comments

Comments
 (0)