Skip to content

Commit b605ab8

Browse files
vonagamdaffl
authored andcommitted
fix: @feathersjs/express: allow middleware arrays (#1421)
1 parent f25c88b commit b605ab8

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/express/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function feathersExpress (feathersApp) {
2828
let middleware = Array.from(arguments)
2929
.slice(1)
3030
.reduce(function (middleware, arg) {
31-
if (typeof arg === 'function') {
31+
if (typeof arg === 'function' || Array.isArray(arg)) {
3232
middleware[service ? 'after' : 'before'].push(arg);
3333
} else if (!service) {
3434
service = arg;
@@ -42,7 +42,7 @@ function feathersExpress (feathersApp) {
4242
});
4343

4444
const hasMethod = methods => methods.some(name =>
45-
(service && !Array.isArray(service) && typeof service[name] === 'function')
45+
(service && typeof service[name] === 'function')
4646
);
4747

4848
// Check for service (any object with at least one service method)

packages/express/test/rest.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,42 @@ describe('@feathersjs/express/rest provider', () => {
368368
.then(() => server.close());
369369
});
370370

371+
it('allows middleware arrays before and after a service', () => {
372+
const app = expressify(feathers());
373+
374+
app.configure(rest())
375+
.use(expressify.json())
376+
.use('/todo', [function (req, res, next) {
377+
req.body.before = ['before first'];
378+
next();
379+
}, function (req, res, next) {
380+
req.body.before.push('before second');
381+
next();
382+
}], {
383+
create (data) {
384+
return Promise.resolve(data);
385+
}
386+
}, [function (req, res, next) {
387+
res.data.after = ['after first'];
388+
next();
389+
}], function (req, res, next) {
390+
res.data.after.push('after second');
391+
next();
392+
});
393+
394+
const server = app.listen(4776);
395+
396+
return axios.post('http://localhost:4776/todo', { text: 'Do dishes' })
397+
.then(res => {
398+
assert.deepStrictEqual(res.data, {
399+
text: 'Do dishes',
400+
before: ['before first', 'before second'],
401+
after: ['after first', 'after second']
402+
});
403+
})
404+
.then(() => server.close());
405+
});
406+
371407
it('allows an array of middleware without a service', () => {
372408
const app = expressify(feathers());
373409
const middlewareArray = [

0 commit comments

Comments
 (0)