cloudant / nodejs-cloudant

Cloudant Node.js client library
Apache License 2.0
255 stars 90 forks source link

Development (#1) #381

Closed spezifant closed 5 years ago

spezifant commented 5 years ago

Allow plugins to be loaded from an absolute or relative path

Checklist

Description

Allow to load plugins based on a relative or absolute path.

Approach

Right now plugins can only be loaded if they reside in the plugins/ directory of the Cloudant NPM module. With this change it's possible to load a plugin from an arbitrary file system location.

Example:

let cloudantPluginPath = path.join(process.cwd(), "myPlugin.js");
dbConfig.plugins = [{
  [cloudantPluginPath]: {
    myParam: 42
  }
}];

Schema & API Changes

No change

Security and Privacy

No Change

Testing

Monitoring and Logging

No change

smithsz commented 5 years ago

Hey, nice work 👍 I think we just need to use this helper, is this right?

diff --git a/lib/client.js b/lib/client.js
index 6d133a6..a2812e2 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -120,7 +120,7 @@ class CloudantClient {
           }

           try {
-            Plugin = require('../plugins/' + plugin);
+            Plugin = require(self._buildPluginPath(plugin));
           } catch (e) {
             throw new Error(`Failed to load plugin - ${e.message}`);
           }

Can you update the PR and also add a test that uses a custom plugin? Thanks.

spezifant commented 5 years ago

@smithsz thanks for the feedback. The function all was actually missing, yes. Also added a test to load a "real" plugin from some different directory than plugins/.