Skip to content

Commit 9e0acaf

Browse files
committed
chore(connect): use response object instead of string.
1 parent cbfc8d7 commit 9e0acaf

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/connect/junction.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ function isErrorHandingMiddleware(middleware) {
44
let arity = middleware.length;
55
return arity === 3;
66
}
7-
function applyMiddleware(error, data, middleware, next) {
7+
function applyMiddleware(error, response, middleware, next) {
88
let errorOnMiddleware = null;
99
try {
1010
if (error && isErrorHandingMiddleware(middleware)) {
11-
middleware(error, data, next);
11+
middleware(error, response, next);
1212
} else {
13-
middleware(data, next);
13+
middleware(response, next);
1414
}
1515
return;
1616
} catch (error) {
1717
errorOnMiddleware = error;
1818
}
1919
// skip the middleware or Error on the middleware
20-
next(errorOnMiddleware, data);
20+
next(errorOnMiddleware, response);
2121
}
2222

2323
export default class Junction {
@@ -29,14 +29,15 @@ export default class Junction {
2929
this.stack.push(middleware);
3030
}
3131

32-
process(initialData, callback) {
33-
let next = (error, data) => {
32+
process(initialValue, callback) {
33+
let response = {value: initialValue};
34+
let next = (error) => {
3435
let middleware = this.stack.shift();
3536
if (!middleware) {
36-
return callback(error, data);
37+
return callback(error, response);
3738
}
38-
applyMiddleware(error, data, middleware, next);
39+
applyMiddleware(error, response, middleware, next);
3940
};
40-
next(null, initialData);
41+
next();
4142
}
4243
}

test/connect/junction-test.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,45 @@ import Junction from "../../src/connect/junction";
55
describe("junction", function () {
66
context("when register middlewares", function () {
77
it("should connect middleware, the order is register", function (done) {
8-
var junction = new Junction();
8+
let junction = new Junction();
99
junction.use(function errorHandling(error, text, next) {
1010
next(error);
1111
});
12-
junction.use(function toUpper(text, next) {
13-
next(null, text.toLocaleUpperCase());
12+
junction.use(function toUpper(res, next) {
13+
res.value = res.value.toLocaleUpperCase();
14+
next();
1415
});
15-
junction.use(function addDesu(text, next) {
16-
next(null, text + " suffix");
16+
junction.use(function addDesu(res, next) {
17+
res.value += " suffix";
18+
next();
1719
});
1820
junction.process("text", (error, result) => {
1921
if (error) {
2022
return done(error);
2123
}
22-
assert.equal(result, "TEXT suffix");
24+
assert.equal(result.value, "TEXT suffix");
2325
done();
2426
});
2527
});
2628
});
2729
context("when occur error in middleware", function () {
2830
it("should call errorHandling middleware", function (done) {
29-
var junction = new Junction();
30-
junction.use(function toUpper(text, next) {
31-
throw new Error("ROL");
31+
let junction = new Junction();
32+
junction.use(function toUpper(res) {
33+
throw new Error("error on " + res);
3234
});
33-
// TODO: 順番に依存してる
34-
junction.use(function errorHandling(error, text, next) {
35+
junction.use(function errorHandling(error, res, next) {
3536
assert(error instanceof Error);
36-
done();
37+
assert.equal(res.value, "text");
38+
next();
3739
});
38-
junction.process("text", (error, result) => {
40+
junction.process("text", (error, res) => {
3941
if (error) {
4042
return done(error);
4143
}
44+
assert.equal(res.value, "text");
4245
done();
4346
});
4447
});
45-
})
48+
});
4649
});

0 commit comments

Comments
 (0)