Skip to content

Commit

Permalink
Minor cleanup in package manager API endpoints (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Apr 11, 2020
1 parent 3dbe6bb commit 317a736
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/providers/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,24 @@ class PackageServiceProvider extends ServiceProvider {

this.core.singleton('osjs/packages', () => this.packages);

routeAuthenticated('GET', '/api/packages/metadata', (req, res) => {
this.packages.readPackageManifests(req.query.root || [], req.session.user)
.then(json => res.json(json))
.catch(error => res.status(400).json({error}));
});
const usingPackageManager = cb => (req, res) => cb(req, res)
.then(json => res.json(json))
.catch((error) => {
console.error(error);
res.status(400).json({error: 'Action failed'});
});

routeAuthenticated('POST', '/api/packages/install', (req, res) => {
this.packages.installPackage(req.body.url, req.body.options, req.session.user)
.then(body => res.json(body))
.catch((error) => {
console.error(error);
res.status(400).json({error: 'Package installation failed'});
});
});
routeAuthenticated('GET', '/api/packages/metadata', usingPackageManager((req, res) => {
return this.packages.readPackageManifests(req.query.root || [], req.session.user);
}));

routeAuthenticated('POST', '/api/packages/uninstall', (req, res) => {
this.packages.uninstallPackage(req.body.name, req.body.options, req.session.user)
.then(body => res.json(body))
.catch((error) => {
console.error(error);
res.status(400).json({error: 'Package uninstallation failed'});
});
});
routeAuthenticated('POST', '/api/packages/install', usingPackageManager((req, res) => {
return this.packages.installPackage(req.body.url, req.body.options, req.session.user);
}));

routeAuthenticated('POST', '/api/packages/uninstall', usingPackageManager((req, res) => {
return this.packages.uninstallPackage(req.body.name, req.body.options, req.session.user);
}));

return this.packages.init();
}
Expand Down

0 comments on commit 317a736

Please sign in to comment.