user2727195
user2727195

Reputation: 7340

passing params to node js module

There are other related answers but my question is different since I'm using prototype based approach.

Authorization.js

var Authorization = function() {};

Authorization.prototype.requestPermissions = function(access_token, authToken, publicationId) {

};

Authorization.prototype.verifyRoles = function(access_token, authToken, roles, permissions) {

};

module.exports = new Authorization();

App.js

authorization = require("../../authorizationService/Authorization");
authorization.requestPermissions("abcd", "1234", "pub1234");
authorization.verifyRoles("abcd", "1234", "param1", "param2");

Notice the problem of repeated use of access_token, authToken in each function and my problem is passing "abcd", "1234" again and again for each call.

Ideally I'd like to pass access_token and authToken for once to the module and then keep using it for each subsequent call.

I'd like to parameterize the module, perhaps pass the access_token and authToken in the constructor while keeping the module prototype based. Please advise in rewriting the module.

Upvotes: 0

Views: 23

Answers (1)

go-oleg
go-oleg

Reputation: 19480

You should return the constructor from the module instead of an instance:

Authorization.js

var Authorization = function(access_token, authToken) {
  this.access_token = access_token;
  this.authToken = authToken
};

Authorization.prototype.requestPermissions = function(publicationId) {

};

Authorization.prototype.verifyRoles = function(roles, permissions) {

};

module.exports = Authorization;

App.js

Authorization = require("../../authorizationService/Authorization");
authorization = new Authorization("abcd", "1234");
authorization.requestPermissions("pub1234");
authorization.verifyRoles("param1", "param2");

Upvotes: 2

Related Questions