Skip to content

Commit

Permalink
Merge pull request #95 from ligangty/2.0-refactor
Browse files Browse the repository at this point in the history
Mock server: refactor store get method to single one
  • Loading branch information
ligangty authored Nov 22, 2023
2 parents 3047d43 + 1ccfc50 commit a4b83a3
Showing 1 changed file with 23 additions and 48 deletions.
71 changes: 23 additions & 48 deletions src/main/webui/src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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"});
}
});

Expand Down

0 comments on commit a4b83a3

Please sign in to comment.