Repository: opencomponents/oc
Pull request #1201 (2021-08-30 22:39:44)

add route for dependencies
On the same fashion as /plugins, this adds a route for dependencies.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4bf6e2cd..cce8684f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,10 +1,6 @@
 ## Change Log
 
-### v0.48.22
-- [#1200](https://github.com/opencomponents/oc/pull/1200) upgrade oc-client to have es6 template by default
-
-### v0.48.21
-- [#1199](https://github.com/opencomponents/oc/pull/1199) Publish new version
+### v0.48.20
 - [#1198](https://github.com/opencomponents/oc/pull/1198) Upgrade oc client browser
 - [#1194](https://github.com/opencomponents/oc/pull/1194) Available plugins
 - [#1196](https://github.com/opencomponents/oc/pull/1196) clean eslint rule warnings/errors
@@ -12,8 +8,6 @@
 - [#1192](https://github.com/opencomponents/oc/pull/1192) Update husky config
 - [#1190](https://github.com/opencomponents/oc/pull/1190) Fix handling of missing dependencies
 - [#1189](https://github.com/opencomponents/oc/pull/1189) Fix tests for windows
-
-### v0.48.19
 - [#1185](https://github.com/opencomponents/oc/pull/1185) update oc-client-browser package
 
 ### v0.48.18
diff --git a/package.json b/package.json
index 8352312d..3d11e3a8 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "oc",
-  "version": "0.48.22",
+  "version": "0.48.20",
   "description": "A framework for developing and distributing html components",
   "main": "./src/index.js",
   "bin": {
@@ -80,7 +80,7 @@
     "multer": "^1.3.0",
     "nice-cache": "0.0.5",
     "oc-client": "3.2.11",
-    "oc-client-browser": "1.4.2",
+    "oc-client-browser": "1.4.1",
     "oc-empty-response-handler": "1.0.0",
     "oc-get-unix-utc-timestamp": "1.0.2",
     "oc-s3-storage-adapter": "1.1.6",
diff --git a/src/components/oc-client/package.json b/src/components/oc-client/package.json
index e4e6a139..3179f2af 100644
--- a/src/components/oc-client/package.json
+++ b/src/components/oc-client/package.json
@@ -1,7 +1,7 @@
 {
   "name": "oc-client",
   "description": "The OpenComponents client-side javascript client",
-  "version": "0.48.22",
+  "version": "0.48.20",
   "repository": "https://github.com/opencomponents/oc/tree/master/components/oc-client",
   "author": "Matteo Figus <matteofigus@gmail.com>",
   "oc": {
diff --git a/src/registry/router.js b/src/registry/router.js
index ad8df2b9..b29fc78c 100644
--- a/src/registry/router.js
+++ b/src/registry/router.js
@@ -10,6 +10,7 @@ const IndexRoute = require('./routes');
 const PublishRoute = require('./routes/publish');
 const StaticRedirectorRoute = require('./routes/static-redirector');
 const PluginsRoute = require('./routes/plugins');
+const DependenciesRoute = require('./routes/dependencies');
 const settings = require('../resources/settings');
 
 module.exports.create = function(app, conf, repository) {
@@ -21,7 +22,8 @@ module.exports.create = function(app, conf, repository) {
     index: new IndexRoute(repository),
     publish: new PublishRoute(repository),
     staticRedirector: new StaticRedirectorRoute(repository),
-    plugins: new PluginsRoute(conf)
+    plugins: new PluginsRoute(conf),
+    dependencies: new DependenciesRoute(conf)
   };
 
   const prefix = conf.prefix;
@@ -35,6 +37,7 @@ module.exports.create = function(app, conf, repository) {
   app.get(`${prefix}oc-client/oc-client.min.map`, routes.staticRedirector);
 
   app.get(`${prefix}~registry/plugins`, routes.plugins);
+  app.get(`${prefix}~registry/dependencies`, routes.dependencies);
 
   if (conf.local) {
     app.get(
diff --git a/src/registry/routes/dependencies.js b/src/registry/routes/dependencies.js
new file mode 100644
index 00000000..baa69f85
--- /dev/null
+++ b/src/registry/routes/dependencies.js
@@ -0,0 +1,24 @@
+'use strict';
+
+const getAvailableDependencies = require('./helpers/get-available-dependencies');
+
+module.exports = function(conf) {
+  return function(req, res) {
+    if (conf.discovery) {
+      const dependencies = getAvailableDependencies(conf.dependencies).map(
+        ({ core, name, version }) => {
+          const dep = { name, core };
+          if (!core && version) {
+            // In the future this could be multiple versions
+            dep.versions = [version];
+          }
+          return dep;
+        }
+      );
+
+      res.status(200).json(dependencies);
+    } else {
+      res.status(401);
+    }
+  };
+};


Test patch:
diff --git a/test/unit/registry-routes-dependencies.js b/test/unit/registry-routes-dependencies.js
new file mode 100644
index 00000000..d6b7aea3
--- /dev/null
+++ b/test/unit/registry-routes-dependencies.js
@@ -0,0 +1,72 @@
+'use strict';
+
+const expect = require('chai').expect;
+const sinon = require('sinon');
+const injectr = require('injectr');
+
+describe('registry : routes : plugins', () => {
+  const dependencyMap = {
+    fs: {
+      core: true,
+      name: 'fs',
+      link: 'http://link1.com'
+    },
+    got: {
+      name: 'got',
+      version: '1.2.3',
+      core: false,
+      link: 'http://link2.com'
+    }
+  };
+  let resJsonStub, statusStub, dependenciesRoute;
+
+  const DependenciesRoute = injectr(
+    '../../src/registry/routes/dependencies.js',
+    {
+      './helpers/get-available-dependencies': list =>
+        list.map(dep => dependencyMap[dep])
+    }
+  );
+
+  const initialise = function() {
+    resJsonStub = sinon.stub();
+    statusStub = sinon.stub().returns({ json: resJsonStub });
+  };
+
+  describe('when is not discoverable', () => {
+    before(() => {
+      initialise();
+      const conf = {
+        dependencies: ['fs', 'got'],
+        discovery: false
+      };
+      dependenciesRoute = new DependenciesRoute(conf);
+
+      dependenciesRoute({ headers: {} }, { conf, status: statusStub });
+    });
+
+    it('should return 401 status code', () => {
+      expect(statusStub.args[0][0]).to.be.equal(401);
+    });
+  });
+
+  describe('when is discoverable', () => {
+    before(() => {
+      initialise();
+      const conf = {
+        dependencies: ['fs', 'got'],
+        discovery: true
+      };
+      dependenciesRoute = new DependenciesRoute(conf);
+
+      dependenciesRoute({ headers: {} }, { conf, status: statusStub });
+    });
+
+    it('should return the list of dependencies', () => {
+      expect(resJsonStub.args[0][0]).to.eql([
+        { name: 'fs', core: true },
+        { name: 'got', core: false, versions: ['1.2.3'] }
+      ]);
+    });
+  });
+});