From d2dcb2ef303da3e3bc840aba1fd27010c5e7f968 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Fri, 3 Feb 2023 14:26:09 +0200 Subject: [PATCH 1/8] . --- __tests__/middleware.unit.js | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/__tests__/middleware.unit.js b/__tests__/middleware.unit.js index 868b0b3..f2a4b05 100644 --- a/__tests__/middleware.unit.js +++ b/__tests__/middleware.unit.js @@ -144,6 +144,17 @@ api9.use(middleware3); /*** DEFINE TEST ROUTES ***/ /******************************************************************************/ +api.get('/', function(req, res) { + res.status(200).json({ + testMiddleware: req.testMiddleware, + }) +}); +api.post('/', function(req, res) { + res.status(200).json({ + testMiddleware2: req.testMiddleware2, + }) +}); + api.get("/test", function (req, res) { res.status(200).json({ method: "get", @@ -327,6 +338,36 @@ api9.get("/data/test", (req, res) => { describe("Middleware Tests:", function () { // this.slow(300); + it('should return testMiddleware: 123 when calling the root route with GET', async function () { + let _event = Object.assign({}, event, {path: "/"}); + let result = await new Promise((r) => + api.run(_event, {}, (e, res) => { + r(res); + }) + ); + expect(result).toEqual({ + multiValueHeaders: { "content-type": ["application/json"] }, + statusCode: 200, + body: '{"testMiddleware":"123"}', + isBase64Encoded: false, + }); + }) + + it('should return testMiddleware2: 456 when calling the root route with POST', async function () { + let _event = Object.assign({}, event, {path: "/", httpMethod: "POST"}); + let result = await new Promise((r) => + api.run(_event, {}, (e, res) => { + r(res); + }) + ); + expect(result).toEqual({ + multiValueHeaders: { "content-type": ["application/json"] }, + statusCode: 200, + body: '{"testMiddleware2":"456"}', + isBase64Encoded: false, + }); + }) + it("Set Values in res object", async function () { let _event = Object.assign({}, event, {}); let result = await new Promise((r) => From a6dc6b1260d3d8d52f7c4a1c82a7610ec93714fd Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Fri, 3 Feb 2023 14:29:34 +0200 Subject: [PATCH 2/8] . --- __tests__/middleware.unit.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/__tests__/middleware.unit.js b/__tests__/middleware.unit.js index f2a4b05..796e341 100644 --- a/__tests__/middleware.unit.js +++ b/__tests__/middleware.unit.js @@ -144,16 +144,22 @@ api9.use(middleware3); /*** DEFINE TEST ROUTES ***/ /******************************************************************************/ -api.get('/', function(req, res) { +api.get("/", function(req, res) { res.status(200).json({ testMiddleware: req.testMiddleware, }) }); -api.post('/', function(req, res) { +api.post("/", function(req, res) { res.status(200).json({ testMiddleware2: req.testMiddleware2, }) }); +api.any("/", function(req, res) { + res.status(200).json({ + testMiddleware: req.testMiddleware, + testMiddleware2: req.testMiddleware2, + }) +}); api.get("/test", function (req, res) { res.status(200).json({ @@ -368,6 +374,21 @@ describe("Middleware Tests:", function () { }); }) + it('should return testMiddleware: 123 when calling the root route with PATCH', async function () { + let _event = Object.assign({}, event, {path: "/", httpMethod: "PATCH"}); + let result = await new Promise((r) => + api.run(_event, {}, (e, res) => { + r(res); + }) + ); + expect(result).toEqual({ + multiValueHeaders: { "content-type": ["application/json"] }, + statusCode: 200, + body: '{"testMiddleware":"123","testMiddleware2":"456"}', + isBase64Encoded: false, + }); + }) + it("Set Values in res object", async function () { let _event = Object.assign({}, event, {}); let result = await new Promise((r) => From 4da8a7693be1a08fad598211d80e1614a98c02f9 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Fri, 3 Feb 2023 16:45:55 +0200 Subject: [PATCH 3/8] wip --- __tests__/basePath.unit.js | 18 ++++++++++++++++++ index.js | 2 ++ 2 files changed, 20 insertions(+) diff --git a/__tests__/basePath.unit.js b/__tests__/basePath.unit.js index 5e8fc5e..7df7e4e 100644 --- a/__tests__/basePath.unit.js +++ b/__tests__/basePath.unit.js @@ -17,9 +17,21 @@ let event = { } } +api.use(function (req, res, next) { + console.log('HELLO FROM MIDDLEWARE') + req.testMiddleware = "123"; + next(); +}); + /******************************************************************************/ /*** DEFINE TEST ROUTES ***/ /******************************************************************************/ +api.get("/", function(req, res) { + res.status(200).json({ + testMiddleware: req.testMiddleware, + }) +}); + api.get('/test', function(req,res) { res.status(200).json({ method: 'get', status: 'ok' }) }) @@ -40,6 +52,12 @@ api.get('/test/test2/test3', function(req,res) { describe('Base Path Tests:', function() { + it('should return testMiddleware: 123 when calling the root path', async function() { + let _event = Object.assign({},event,{ path: '/v1' }) + let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) + expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false }) + }) + it('Simple path with base: /v1/test', async function() { let _event = Object.assign({},event,{ path: '/v1/test' }) let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) diff --git a/index.js b/index.js index 86e3439..73811ed 100644 --- a/index.js +++ b/index.js @@ -149,6 +149,7 @@ class API { // Make a local copy of routes let routes = this._routes; + // console.log('routes', JSON.stringify(routes)); // Create a local stack for inheritance let _stack = { '*': [], m: [] }; @@ -477,6 +478,7 @@ class API { this.METHOD('__MW__', route, ...middleware); }); } + } // end use // Finally handler From 1a805bfb17beef76f6b69b2a8fabe9402265a4b0 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sat, 3 Aug 2024 20:12:09 +0300 Subject: [PATCH 4/8] fix format --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 73811ed..21e399b 100644 --- a/index.js +++ b/index.js @@ -478,7 +478,6 @@ class API { this.METHOD('__MW__', route, ...middleware); }); } - } // end use // Finally handler From 18f0d7c3fc3ab4a62c5f53604f624c9217c10f83 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sat, 17 Aug 2024 23:15:43 +0300 Subject: [PATCH 5/8] . --- index.js | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 21e399b..3eabaed 100644 --- a/index.js +++ b/index.js @@ -179,20 +179,17 @@ class API { methods.forEach((_method) => { // Method must be a string if (typeof _method === 'string') { - // Check for wild card at this level - if (routes['ROUTES']['*']) { + const wildcardForPathLevel = routes[i] == '' ? routes['ROUTES']['*'] : routes['ROUTES'][route[i]]['*']; + if (wildcardForPathLevel != null) { if ( - routes['ROUTES']['*']['MIDDLEWARE'] && + wildcardForPathLevel['MIDDLEWARE'] && (route[i] !== '*' || _method !== '__MW__') ) { - _stack['*'][method] = routes['ROUTES']['*']['MIDDLEWARE'].stack; + _stack['*'][method] = wildcardForPathLevel['MIDDLEWARE'].stack; } - if ( - routes['ROUTES']['*']['METHODS'] && - routes['ROUTES']['*']['METHODS'][method] - ) { + if (wildcardForPathLevel?.['METHODS']?.[method] != null) { _stack['m'][method] = - routes['ROUTES']['*']['METHODS'][method].stack; + wildcardForPathLevel['METHODS'][method].stack; } } // end if wild card @@ -216,13 +213,12 @@ class API { : _stack['*'][method] ? _stack['*'][method].concat(stack) : stack, - // inherited: _stack[method] ? _stack[method] : [], route: '/' + parsedPath.join('/'), path: '/' + this._prefix.concat(parsedPath).join('/'), }; - // If mounting middleware - if (method === '__MW__') { + const isMountingMiddleware = _method === '__MW__'; + if (isMountingMiddleware) { // Merge stacks if middleware exists if (routes['ROUTES'][route[i]]['MIDDLEWARE']) { meta.stack = @@ -234,17 +230,6 @@ class API { } // Add/update middleware routes['ROUTES'][route[i]]['MIDDLEWARE'] = meta; - - // Apply middleware to all child middlware routes - // if (route[i] === "*") { - // // console.log("APPLY NESTED MIDDLEWARE"); - // // console.log(JSON.stringify(routes["ROUTES"], null, 2)); - // Object.keys(routes["ROUTES"]).forEach((nestedRoute) => { - // if (nestedRoute != "*") { - // console.log(nestedRoute); - // } - // }); - // } } else { // Create the methods section if it doesn't exist if (!routes['ROUTES'][route[i]]['METHODS']) @@ -266,8 +251,6 @@ class API { routes['ROUTES'][route[i]]['METHODS'][_method] = meta; } // end else - // console.log('STACK:',meta); - // If there's a wild card that's not at the end } else if (route[i] === '*') { throw new ConfigurationError( From 4c888dfb6200feda2892c2898abada4c142d6721 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sat, 17 Aug 2024 23:17:21 +0300 Subject: [PATCH 6/8] . --- .gitignore | 8 +------- __tests__/basePath.unit.js | 2 -- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 346bd84..b1725a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,6 @@ -# Dependency directories node_modules - -# Optional npm cache directory .npm - -# Local REDIS test data dump.rdb - -# Coverage reports .nyc_output coverage +.idea diff --git a/__tests__/basePath.unit.js b/__tests__/basePath.unit.js index 7df7e4e..9ac5bde 100644 --- a/__tests__/basePath.unit.js +++ b/__tests__/basePath.unit.js @@ -1,7 +1,5 @@ 'use strict'; - - // Init API instance const api = require('../index')({ version: 'v1.0', base: '/v1' }) From 6b677389843f9de9c505abcf4af8693deb58350c Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sat, 17 Aug 2024 23:18:12 +0300 Subject: [PATCH 7/8] . --- __tests__/basePath.unit.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/__tests__/basePath.unit.js b/__tests__/basePath.unit.js index 9ac5bde..e093b4a 100644 --- a/__tests__/basePath.unit.js +++ b/__tests__/basePath.unit.js @@ -49,11 +49,10 @@ api.get('/test/test2/test3', function(req,res) { /******************************************************************************/ describe('Base Path Tests:', function() { - it('should return testMiddleware: 123 when calling the root path', async function() { let _event = Object.assign({},event,{ path: '/v1' }) let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) - expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false }) + expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"testMiddleware":"123"}', isBase64Encoded: false }) }) it('Simple path with base: /v1/test', async function() { From dd85ee88d45ac22078a6628c34ef0636987637f5 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Sat, 17 Aug 2024 23:18:51 +0300 Subject: [PATCH 8/8] . --- index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/index.js b/index.js index 3eabaed..54cf518 100644 --- a/index.js +++ b/index.js @@ -144,14 +144,8 @@ class API { route.push(''); } - // Keep track of path variables let pathVars = {}; - - // Make a local copy of routes let routes = this._routes; - // console.log('routes', JSON.stringify(routes)); - - // Create a local stack for inheritance let _stack = { '*': [], m: [] }; // Loop through the path levels