axa-group / nlp.js

An NLP library for building bots, with entity extraction, sentiment analysis, automatic language identify, and so more
MIT License
6.28k stars 621 forks source link

Question: pass configuration to plugin #815

Closed ericzon closed 1 year ago

ericzon commented 3 years ago

Description

Hi, I've been searching in the examples and the docs but I didn't found the answer. How can I pass a list of plugins to express-api-server on dockStart(...)?

I tried to pass on each of these 2 places but nothing arrives to class constructor, only the settings (I use cors as a simple example):

 {​
   settings: {​
     'api-server': {​
       'port': process.env.PORT,
       'serveBot': true,
       plugins[cors()] <--
     }​,
   }​,
   use: [{
    'className': 'ExpressApiServer', 
       plugins[cors()] <--
   }]
   ...
} 
​

In the code I see this: https://github.com/axa-group/nlp.js/blob/fc8b61078c04a0627c3e8585912d6fcfb399b1a2/packages/express-api-server/src/express-api-server.js#L48

it shouldn't be better something like this? image

jesus-seijas-sp commented 3 years ago

Well, I don't think that is possible right now to provide plugins from conf.json.

Try looking at this issue: https://github.com/axa-group/nlp.js/issues/414

Invoking @nfreear as he developed the plugin part for the express-api-server, and let's debate: "how we want to provide middlewares to express?"

ericzon commented 3 years ago

Well, I was able to add plugins without touching the library, just using "pathPlugins" field. For me it's not so much declarative but it works perfectly and is not intrusive.

I put here an example:

conf.json

{
    pathPlugins: "./src/plugins",
    settings: {
      nlp: {
        forceNER: true
      },
      ner: {},
      'api-server': {
        'port': 3000,
        'serveBot': true
      }
    },
    use: [
      'Basic',
      'LangEn',
      'Nlp',
      'ExpressApiServer',
      'DirectlineConnector',
      'Bot'
    ]
}

./src/plugins/test-plugin.js

const myMiddleware = (req, res, next) => {
  console.log('###> req info: ', req.path);
  next();
};

class TestPlugin {
   constructor({ container }) {
      const apiServer = container.get('api-server')
      console.trace('TestPlugin! >  container', apiServer);
      apiServer.plugins = [...apiServer.plugins, myMiddleware]
   }
}

module.exports = TestPlugin;

If you prefer I can send a PR to document this.

For me an alternative definition more declarative (although both could work in a complementary way) should be something plugin scoped like this:

conf.json

{
    pathPlugins: "./src/plugins",
    settings: {
      nlp: {
        forceNER: true
      },
      ner: {},
      'api-server': {
        'port': 3000,
        'serveBot': true
      }
    },
    use: [
      ...
      {
          className: 'ExpressApiServer',
-->       plugins: ['...my custom path/myPlugin1.js', '...my custom path/myPlugin2.js', '...my custom path/myPlugin3.js']
      }
      ...
    ]
}
aigloss commented 1 year ago

Closing due to inactivity. Please, re-open if you think the topic is still alive.