user2727195 user2727195 - 7 months ago 40
Node.js Question

Configuration data for the SDK Library

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.


Let's say I've app.js

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

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

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


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

module.exports = SDK;


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


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


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.


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

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

var m1 = new SDK.M1();


var SDK = function(){};

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

module.exports = SDK;

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


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.