comprodls-sdk 2.74.1 → 2.75.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -351,6 +351,7 @@ exports.PUB_API_URLS = {
351
351
  exports.PUSHX_API_URLS = {
352
352
  grantByUserOrgId: '/orgs/{orgId}/grants',
353
353
  grantByAccountId: '/accounts/{accountId}/grants',
354
+ grantV2: '/ver/v2/grants',
354
355
  getPushedEvents: '/accounts/{accountId}/pushed-events/channels'
355
356
  };
356
357
 
@@ -46,15 +46,16 @@ var keepaliveAgent = new Agent({
46
46
  * Public Function definitions
47
47
  **********************************/
48
48
  function pushX() {
49
- var _pubnubClientWrapper = pubnubClientWrapper();
50
- return {
51
- "connect": _connect.bind(this, _pubnubClientWrapper),
52
- "cleanup": _cleanup.bind(this, _pubnubClientWrapper),
53
- "grantByUserOrgId": grantByUserOrgId.bind(this),
54
- "grantByAccountId": grantByAccountId.bind(this),
55
- "grantByAccountIdOnExtUserId": grantByAccountIdOnExtUserId.bind(this),
56
- "getPushedEvents": getPushedEvents.bind(this)
57
- };
49
+ var _pubnubClientWrapper = pubnubClientWrapper();
50
+ return {
51
+ "connect": _connect.bind(this, _pubnubClientWrapper),
52
+ "cleanup": _cleanup.bind(this, _pubnubClientWrapper),
53
+ "grantByUserOrgId": grantByUserOrgId.bind(this),
54
+ "grantByAccountId": grantByAccountId.bind(this),
55
+ "grantByAccountIdOnExtUserId": grantByAccountIdOnExtUserId.bind(this),
56
+ "grantV2": grantV2.bind(this),
57
+ "getPushedEvents": getPushedEvents.bind(this)
58
+ };
58
59
  }
59
60
 
60
61
  /*********************************
@@ -62,146 +63,147 @@ function pushX() {
62
63
  **********************************/
63
64
 
64
65
  function _connect(pubnubCW, options) {
65
- var bpubnubV7 = true; // If pubnub v7 or higher in package.json, set it true
66
- if (bpubnubV7 && !options.userid) {
67
- var err = {};
68
- err.message = err.description = 'Mandatory parameter userid not found in request options.';
69
- err = new DLSError(helpers.errors.ERROR_TYPES.PUSHX_ERROR, err);
70
- throw err;
71
- }
66
+ var bpubnubV7 = true; // If pubnub v7 or higher in package.json, set it true
67
+ if (bpubnubV7 && !options.userid) {
68
+ var err = {};
69
+ err.message = err.description = 'Mandatory parameter userid not found in request options.';
70
+ err = new DLSError(helpers.errors.ERROR_TYPES.PUSHX_ERROR, err);
71
+ throw err;
72
+ }
72
73
 
73
- // Adding SSL flag
74
- return pubnubCW.setup({
75
- 'userid': options.userid,
76
- 'accountid': options.accountid,
77
- 'pubnub': {
78
- 'publishKey': options.publishKey,
79
- 'subscribeKey': options.subscribeKey,
80
- 'authKey': options.authKey,
81
- 'ssl': true
82
- }
83
- });
74
+ // Adding SSL flag
75
+ return pubnubCW.setup({
76
+ 'userid': options.userid,
77
+ 'accountid': options.accountid,
78
+ 'token': options.token,
79
+ 'pubnub': {
80
+ 'publishKey': options.publishKey,
81
+ 'subscribeKey': options.subscribeKey,
82
+ 'ssl': true
83
+ }
84
+ });
84
85
  }
85
86
 
86
87
  function _cleanup(pubnubCW) { pubnubCW.cleanup(); }
87
88
 
88
89
  /*options = {
89
- authKey: <authKey>
90
+ orgId: <orgId>, userId: <userId>
90
91
  }*/
91
92
  function grantByUserOrgId(options) {
92
- var self = this;
93
- var dfd = q.defer(); // Initializing promise
94
- // Validations
95
- var err = helpers.validations.isAuthenticated(self.orgId, self.token);
96
- if(err) { dfd.reject(err); }
97
- else {
98
- // Passed all validations, Construct API url
99
- var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantByUserOrgId;
100
- url = helpers.api.constructAPIUrl(url, { orgId: self.orgId });
101
- // Setup request with URL and Params
102
- var requestAPI = request.post(url)
103
- .set('Content-Type', 'application/json')
104
- .set('Accept', 'application/json');
105
-
106
- var body = {};
107
- if(options.authKey) { body.authKey = options.authKey; }
108
-
109
- requestAPI.send(body);
110
-
111
- //Setup token in Authorization header
112
- requestAPI = helpers.api.setupAPIToken(requestAPI, self.token);
113
- if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
114
-
115
- // Call Change Password Api
116
- requestAPI.end(function(err, response) {
117
- if(err) {
118
- err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
119
- dfd.reject(err);
120
- }
121
- else { dfd.resolve(response.body); }
122
- });
123
- }
124
- return dfd.promise;
93
+ var dfd = q.defer(); // Initializing promise
94
+ var allowedChannels = ['jobs', 'systemevents'];
95
+ var baseChannelName = "o-" + options.orgId + "\\$u-" + options.userId + "\\$";
96
+ var patterns = [];
97
+ for(var i in allowedChannels) {
98
+ var channelName = baseChannelName + allowedChannels[i] + "\\..*";
99
+ patterns.push(channelName);
100
+ }
101
+ console.log("0a781d69 TAKING GRANT FOR CHANNEL PATTERNS: ", patterns);
102
+ options.channels = {};
103
+ options.channels.patterns = patterns;
104
+ grantV2.call(this, options)
105
+ .then(function (res) {
106
+ dfd.resolve(res);
107
+ })
108
+ .catch(function(err) {
109
+ dfd.reject(err);
110
+ });
111
+ return dfd.promise;
125
112
  }
126
113
 
127
114
  /*options = {
128
115
  accountId: <accountid>,
129
- refId: <extRefId>,
130
- authKey: <authKey>
116
+ refId: <extRefId>
131
117
  }*/
132
118
  function grantByAccountId(options) {
133
- var self = this, err ={};
134
- var dfd = q.defer(); // Initializing promise
135
-
136
- if(options.accountId && options.refId) {
137
- // Passed all validations, Construct API url
138
- var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantByAccountId;
139
- url = helpers.api.constructAPIUrl(url, { accountId: options.accountId });
140
- // Setup request with URL and Params
141
- var requestAPI = request.post(url)
142
- .set('Content-Type', 'application/json')
143
- .set('Accept', 'application/json');
144
- if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
145
-
146
- var body = { refId: options.refId };
147
- if(options.authKey) { body.authKey = options.authKey; }
148
-
149
- requestAPI.send(body);
150
- requestAPI.end(function(err, response) {
151
- if(err) {
152
- err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
153
- dfd.reject(err);
154
- }
155
- else { dfd.resolve(response.body); }
156
- });
157
- }
158
- else {
159
- err.message = err.description = "Required parameter ['accountId', 'refId'] " +
160
- "not found in request options";
161
- err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
162
- dfd.reject(err);
163
- }
164
- return dfd.promise;
119
+ var err ={};
120
+ var dfd = q.defer(); // Initializing promise
121
+
122
+ if(options.accountId && options.refId) {
123
+ var channel = "a-" + options.accountId + "$refid." + options.refId;
124
+ var exactMatch = [channel];
125
+ options.channels = {};
126
+ options.channels.exactMatch = exactMatch;
127
+ grantV2.call(this, options)
128
+ .then(function (res) {
129
+ dfd.resolve(res);
130
+ })
131
+ .catch(function(err) {
132
+ dfd.reject(err);
133
+ });
134
+ }
135
+ else {
136
+ err.message = err.description = "Required parameter ['accountId', 'refId'] " +
137
+ "not found in request options";
138
+ err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
139
+ dfd.reject(err);
140
+ }
141
+ return dfd.promise;
165
142
  }
166
143
 
167
144
  /*options = {
168
145
  accountId: <accountid>,
169
146
  extUserId: <extUserId>, //mandatory
170
- authKey: <authKey>
171
147
  }*/
172
148
  function grantByAccountIdOnExtUserId(options) {
173
- var self = this, err ={};
174
- var dfd = q.defer(); // Initializing promise
175
-
176
- if(options.accountId && options.extUserId) {
177
- // Passed all validations, Construct API url
178
- var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantByAccountId;
179
- url = helpers.api.constructAPIUrl(url, { accountId: options.accountId });
180
- // Setup request with URL and Params
181
- var requestAPI = request.post(url)
182
- .set('Content-Type', 'application/json')
183
- .set('Accept', 'application/json');
184
- if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
185
-
186
- var body = { extUserId: options.extUserId };
187
- if(options.authKey) { body.authKey = options.authKey; }
188
-
189
- requestAPI.send(body);
190
- requestAPI.end(function(err, response) {
191
- if(err) {
192
- err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
193
- dfd.reject(err);
194
- }
195
- else { dfd.resolve(response.body); }
196
- });
197
- }
198
- else {
199
- err.message = err.description = "Required parameter ['accountId', 'extUserId'] " +
200
- "not found in request options";
201
- err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
202
- dfd.reject(err);
203
- }
149
+ var err ={};
150
+ var dfd = q.defer(); // Initializing promise
151
+
152
+ if(options.accountId && options.extUserId) {
153
+ var channel = "a-" + options.accountId + "$u-" + options.extUserId;
154
+ var exactMatch = [channel];
155
+ options.channels = {};
156
+ options.channels.exactMatch = exactMatch;
157
+ grantV2.call(this, options)
158
+ .then(function (res) {
159
+ dfd.resolve(res);
160
+ })
161
+ .catch(function(err) {
162
+ dfd.reject(err);
163
+ });
164
+ }
165
+ else {
166
+ err.message = err.description = "Required parameter ['accountId', 'extUserId'] " +
167
+ "not found in request options";
168
+ err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
169
+ dfd.reject(err);
170
+ }
171
+ return dfd.promise;
172
+ }
173
+
174
+ /**
175
+ * This function provides grant token on given channels
176
+ * @param { channels: { exactMatch: [], patterns: [] } } options
177
+ * @returns { token, publishKey, subscribeKey }
178
+ */
179
+ function grantV2(options) {
180
+ var self = this, err = {};
181
+ var dfd = q.defer();
182
+ var body = { channels: {} };
183
+ if(!options.channels) {
184
+ err.message = err.description = "channels not given in input body.";
185
+ err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
186
+ dfd.reject(err);
204
187
  return dfd.promise;
188
+ }
189
+ body.channels = options.channels;
190
+ // Passed all validations, Construct API url
191
+ var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantV2;
192
+ // Setup request with URL and Params
193
+ var requestAPI = request.post(url)
194
+ .set('Content-Type', 'application/json')
195
+ .set('Accept', 'application/json');
196
+ if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
197
+
198
+ requestAPI.send(body);
199
+ requestAPI.end(function(err, response) {
200
+ if(err) {
201
+ err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
202
+ dfd.reject(err);
203
+ }
204
+ else { dfd.resolve(response.body); }
205
+ });
206
+ return dfd.promise;
205
207
  }
206
208
 
207
209
  /**
@@ -36,6 +36,7 @@ module.exports = function () {
36
36
  //Returning the adaptor (Plain Javascript object)
37
37
  return {
38
38
  "on": function (channelObj, handler) {
39
+ console.log("0a781d69 PARAMS OF .on FUNCTION: ", JSON.stringify(channelObj, null, 2));
39
40
  var pubNubChannel;
40
41
  var channelContext = [];
41
42
 
@@ -57,6 +58,7 @@ module.exports = function () {
57
58
  status: 'pending'
58
59
  };
59
60
  _eventEmitter.on(pubNubChannel, handler);
61
+ console.log("0a781d69 GOING TO SUBSCRIBE ON PUBNUB CHANNEL: ", pubNubChannel);
60
62
  _subscribeToPubNubChannels(pubNubChannel);
61
63
  }
62
64
  }
@@ -77,6 +79,7 @@ module.exports = function () {
77
79
 
78
80
  var _translatePubnubStatus = function(status, options) {
79
81
  var channels = [], error, successObj;
82
+ console.log('0a781d69 PUBNUB STATUS: ', JSON.stringify(status, null, 4));
80
83
  switch (status.category) {
81
84
  case "PNConnectedCategory":
82
85
  if(status.operation === "PNSubscribeOperation") {
@@ -104,8 +107,10 @@ module.exports = function () {
104
107
  break;
105
108
  case "PNAccessDeniedCategory":
106
109
  if(status.operation === "PNSubscribeOperation") {
110
+ var errorText = status.errorData.response && status.errorData.response.text;
111
+ var errorJSON = errorText ? JSON.parse(errorText) : undefined;
107
112
  var errorData = {
108
- payload: JSON.parse(status.errorData.response.text).payload,
113
+ payload: errorJSON && errorJSON.payload || status.errorData.payload,
109
114
  message: 'Forbidden: Subscription failed.',
110
115
  errorDetails: {
111
116
  operation: status.operation,
@@ -208,10 +213,17 @@ module.exports = function () {
208
213
  var pubnubConfig = userOptions.pubnub;
209
214
  pubnubConfig.uuid = userOptions.userid;
210
215
  var accountId = userOptions.accountid;
216
+ var token = userOptions.token;
211
217
 
212
218
  if (!_pubnubClient && pubnubConfig) {
213
- _pubnubClient = new pubNub(pubnubConfig); //Connect with PubNub SDK
214
- processSetup();
219
+ try {
220
+ _pubnubClient = new pubNub(pubnubConfig); //Connect with PubNub SDK
221
+ _pubnubClient.setToken(token);
222
+ setTimeout(processSetup, 1000);
223
+ processSetup();
224
+ } catch(err) {
225
+ console.error('0a781d69 ERROR WHILE CONFIGURING PUBNUB: ', JSON.stringify(err, null, 2));
226
+ }
215
227
  } else {
216
228
  return new Error('Already Initialized');
217
229
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "comprodls-sdk",
3
3
  "description": "comproDLS SDK for JavaScript",
4
- "version": "2.74.1",
4
+ "version": "2.75.2",
5
5
  "author": {
6
6
  "name": "Compro Technologies Private Limited",
7
7
  "url": "http://www.comprotechnologies.com/"