Configuration data for the SDK Library

拈花ヽ惹草 提交于 2019-12-13 10:37:32

问题


I'm programming two pieces of a software.

  1. SDK (that will live under node_modules)
  2. Application that uses SDK

I want the SDK to be generic and able to work on some configuration data passed by the application.

Question is, there are several actors (.js modules) within the SDK, how the rest of the actors (.js modules) can access the configuration data that I've passed to the main SDK module (lib/index.js), or how that works in nodejs SDK world when it comes to configuration data.

The configuration data is highly reusable and every actor in my SDK library would need this configuration data, my objective is to pass it once to the main JS module (index.js) and then rest of the modules reach out to this for their operations.

I hope it's clear and I can further refine my question, please comment.

Edit.

Let's say I've app.js

var SDK = require("./sdk");
SDK("key", "secret");

var M1 = require("./sdk/m1");
var m1 = new M1();
m1.doSomething();

var M2 = require("./sdk/m2");
var m2 = new M2();
m2.doSomethikng()

SDK.js

var SDK = function(key, secret) {
    this.key = key;
    this.secret = secret;
}

module.exports = SDK;

sdk/M1.js

var M1 = function() {}
M1.prototype.doSomething = function() {
    //uses key and secret
}
module.exports = M1;

sdk/M2.js

var M2 = function() {}
M2.prototype.doSomething = function() {
    //uses key and secret
}
module.exports = M2;

回答1:


There are many architectural ways to do it, difficult to say which one is the best without knowing your use case and what creational pattern you use.

A simple example could be, using factories which pass the configuration (or a copy) to each other: basically you let your sdk create its dependencies

// mysdk/index.js
const myLib = require('./lib/myLib');
module.exports = function sdkFactory(options){
  const {configuration} = options;
  const libInstance = myLib(configuration);
 //your sdk api
  return {
    method1:function(){
      //whatever using the libInstance
      return libInstance.foo();
    }, 
   //etc
  }
}

So your client code can use it

const sdkFactory = require('sdk');
const sdk = sdkFactory({configuration:{prp:'val'}});
sdk.method1();
//etc

You can use dependency injection pattern, and many others.

I suggest you to get a look at the source code of popular libraries you use everyday so you can have an idea of good/bad practices




回答2:


In simplest form: just require everything external in constructor.

Actor.js

function Actor(sdkConfig,config){
   this.sdkConfig = sdkconfig;
   this.config = config;
}

Actor.prototype.doSomething = function(){};

.....

Actor.prototype.doSomethingElse = function(){};

module.exports = Actor;     

Then, grant access to new instances of Actors, via the Sdk instance:

Sdk.js

function Sdk(config){
  this.config = config;
}

Sdk.prototype.createActor = function(actorConfig){
   return new Actor(this.config,actorConfig);
}

module.exports = Sdk;

Usage

var Sdk = require("Sdk");

var sdk = new Sdk({"sdkSetting":true});

var actor = sdk.createActor({"actorSetting":false});



回答3:


Here's a pattern that I've discovered without Factory methods on the main module (can't have factory method for each of my several modules) while the other actors still have access to config data which is held by main module only.

app.js

var SDK = require("./sdk");

SDK.config = {key: "key", secret: "secret"};

var m1 = new SDK.M1();
m1.doSomething();

sdk.js

var SDK = function(){};

SDK.config = {key: null, secret: null};

module.exports = SDK;

require("./m1"); //executing other modules to make it work

m1.js

var SDK = require("./sdk");

var M1 = function(){};

M1.prototype.doSomething = function(){
    var config = SDK.config //getting access to main module's config data
};

SDK.M1 = M1;

And same with the other actors of the system, there's no passing of config parameters around and forcing each actor to have a similar constructor.



来源:https://stackoverflow.com/questions/37888699/configuration-data-for-the-sdk-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!