Skip to content

Commit

Permalink
fix: @feathersjs/express: allow middleware arrays (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam authored and daffl committed Jun 30, 2019
1 parent f25c88b commit b605ab8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/express/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function feathersExpress (feathersApp) {
let middleware = Array.from(arguments)
.slice(1)
.reduce(function (middleware, arg) {
if (typeof arg === 'function') {
if (typeof arg === 'function' || Array.isArray(arg)) {
middleware[service ? 'after' : 'before'].push(arg);
} else if (!service) {
service = arg;
Expand All @@ -42,7 +42,7 @@ function feathersExpress (feathersApp) {
});

const hasMethod = methods => methods.some(name =>
(service && !Array.isArray(service) && typeof service[name] === 'function')
(service && typeof service[name] === 'function')
);

// Check for service (any object with at least one service method)
Expand Down
36 changes: 36 additions & 0 deletions packages/express/test/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,42 @@ describe('@feathersjs/express/rest provider', () => {
.then(() => server.close());
});

it('allows middleware arrays before and after a service', () => {
const app = expressify(feathers());

app.configure(rest())
.use(expressify.json())
.use('/todo', [function (req, res, next) {
req.body.before = ['before first'];
next();
}, function (req, res, next) {
req.body.before.push('before second');
next();
}], {
create (data) {
return Promise.resolve(data);
}
}, [function (req, res, next) {
res.data.after = ['after first'];
next();
}], function (req, res, next) {
res.data.after.push('after second');
next();
});

const server = app.listen(4776);

return axios.post('http://localhost:4776/todo', { text: 'Do dishes' })
.then(res => {
assert.deepStrictEqual(res.data, {
text: 'Do dishes',
before: ['before first', 'before second'],
after: ['after first', 'after second']
});
})
.then(() => server.close());
});

it('allows an array of middleware without a service', () => {
const app = expressify(feathers());
const middlewareArray = [
Expand Down

0 comments on commit b605ab8

Please sign in to comment.