From b605ab8ff2158a5faad793c00c949eeda78cf321 Mon Sep 17 00:00:00 2001 From: Dmitrii Maganov Date: Mon, 1 Jul 2019 01:20:02 +0300 Subject: [PATCH] fix: @feathersjs/express: allow middleware arrays (#1421) --- packages/express/lib/index.js | 4 ++-- packages/express/test/rest.test.js | 36 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/express/lib/index.js b/packages/express/lib/index.js index b6ae0c55c9..fa33eac33f 100644 --- a/packages/express/lib/index.js +++ b/packages/express/lib/index.js @@ -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; @@ -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) diff --git a/packages/express/test/rest.test.js b/packages/express/test/rest.test.js index f1eb65589d..5e1cd9fcb6 100644 --- a/packages/express/test/rest.test.js +++ b/packages/express/test/rest.test.js @@ -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 = [