I'm programming two pieces of a software.
- SDK (that will live under node_modules)
- 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;
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
So your client code can use it
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
In simplest form: just require everything external in constructor.
Actor.js
Then, grant access to new instances of Actors, via the Sdk instance:
Sdk.js
Usage
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
sdk.js
m1.js
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.