From 1ccfc509d749a3bc2f09d988753397df50d00da4 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Wed, 22 Nov 2023 22:10:13 +0800 Subject: [PATCH] Mock server: refactor store get method to single one --- src/main/webui/src/server/app.js | 71 +++++++++++--------------------- 1 file changed, 23 insertions(+), 48 deletions(-) diff --git a/src/main/webui/src/server/app.js b/src/main/webui/src/server/app.js index e81d3e3..81d57ae 100644 --- a/src/main/webui/src/server/app.js +++ b/src/main/webui/src/server/app.js @@ -36,43 +36,30 @@ app.get('/api/stats/version-info', (req, res) => { }); }); +const decideMockListFile = (packgeType, type) => { + const pkgToFileMapping = {"maven": "Maven", "npm": "NPM"}; + const typeToFileMapping = {"remote": "Remote", "hosted": "Hosted", "group": "Group"}; + return `./mock/list/Fake${pkgToFileMapping[packgeType]}${typeToFileMapping[type]}List.json`; +}; + // For store listing app.get(`${STORE_API_BASE}/:packageType/:type`, (req, res) => { const [pkgType, type] = [req.params.packageType, req.params.type]; - const pkgToFileMapping = {"maven": "Maven", "npm": "NPM"}; - const typeToFileMapping = {"remote": "Remote", "hosted": "Hosted", "group": "Group"}; - let list = []; if(pkgType==="_all"){ // TODO: do all packageType for type handling here } - const mockFile = `./mock/list/Fake${pkgToFileMapping[pkgType]}${typeToFileMapping[type]}List.json`; - list = require(mockFile); + const mockFile = decideMockListFile(pkgType, type); + const list = require(mockFile); res.status(200).json(list); }); -app.get('/api/admin/schedule/store/all/disable-timeout', (req, res) => { - const disableTimeouts = require('./mock/FakeDisableTimeouts.json'); - res.status(200).json(disableTimeouts); -}); - -app.get('/api/admin/schedule/store/:packageType/:type/:name/disable-timeout', (req, res) => { - const group = `${req.params.packageType}:${req.params.type}:${req.params.name}`; - if(group && group.length > 0){ - const disList = require('./mock/FakeDisableTimeouts.json'); - const result = disList.items.find(item=>item.group.includes(group)); - if(result){ - res.status(200).json(result); - }else{ - res.status(404).json({error: "No such store!"}); - } - } -}); - -app.get(`${STORE_API_BASE}/maven/remote/:name`, (req, res) => { - const name = req.params.name; - if(name){ - const remoteList = require('./mock/list/FakeMavenRemoteList.json'); - const result = remoteList.items.find(item=>item.name===name); +// For single store get +app.get(`${STORE_API_BASE}/:packageType/:type/:name`, (req, res) => { + const [pkgType, type, name] = [req.params.packageType, req.params.type, req.params.name]; + if(pkgType && type && name){ + const mockListFile = decideMockListFile(pkgType, type); + const repoList = require(mockListFile); + const result = repoList.items.find(item=>item.name===name); if(result){ res.status(200).json(result); }else{ @@ -83,33 +70,21 @@ app.get(`${STORE_API_BASE}/maven/remote/:name`, (req, res) => { } }); -app.get(`${STORE_API_BASE}/maven/hosted/:name`, (req, res) => { - const name=req.params.name; - if(name){ - const remoteList = require('./mock/list/FakeMavenHostedList.json'); - const result = remoteList.items.find(item=>item.name===name); - if(result){ - res.status(200).json(result); - }else{ - res.status(404).json({error: "No such store!"}); - } - }else{ - res.status(400).json({error: "Missing store name"}); - } +app.get('/api/admin/schedule/store/all/disable-timeout', (req, res) => { + const disableTimeouts = require('./mock/FakeDisableTimeouts.json'); + res.status(200).json(disableTimeouts); }); -app.get(`${STORE_API_BASE}/maven/group/:name`, (req, res) => { - const name=req.params.name; - if(name){ - const remoteList = require('./mock/list/FakeMavenGroupList.json'); - const result = remoteList.items.find(item=>item.name===name); +app.get('/api/admin/schedule/store/:packageType/:type/:name/disable-timeout', (req, res) => { + const group = `${req.params.packageType}:${req.params.type}:${req.params.name}`; + if(group && group.length > 0){ + const disList = require('./mock/FakeDisableTimeouts.json'); + const result = disList.items.find(item=>item.group.includes(group)); if(result){ res.status(200).json(result); }else{ res.status(404).json({error: "No such store!"}); } - }else{ - res.status(400).json({error: "Missing store name"}); } });