From 28ba0eb3d35b914155540e6b07d33ae7266c1a77 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 5 Feb 2021 11:54:05 +0100 Subject: [PATCH] test middleware --- packages/dashboard/pages/api/hello.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/dashboard/pages/api/hello.js b/packages/dashboard/pages/api/hello.js index 65e907ea..469c8801 100644 --- a/packages/dashboard/pages/api/hello.js +++ b/packages/dashboard/pages/api/hello.js @@ -17,10 +17,26 @@ const middleware = jwt({ algorithms: ["RS256"], }); -export default middleware((req, res) => { +// Helper method to wait for a middleware to execute before continuing +// And to throw an error when an error happens in a middleware +function runMiddleware(req, res, fn) { + return new Promise((resolve, reject) => { + fn(req, res, (result) => { + if (result instanceof Error) { + return reject(result); + } + + return resolve(result); + }); + }); +} + +export default (req, res) => { + await runMiddleware(req, res, middleware) + console.log(Object.keys(req)); console.log(req); console.log(JSON.stringify(req)); res.statusCode = 200; res.json({ name: "John Doe" }); -}); +};