crisp-api 7.3.0 → 7.4.1

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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,22 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ ## v7.4.1
5
+
6
+ ### Bug Fixes
7
+
8
+ * Changed type of exported services in JSDoc to `any` so that TypeScript projects can use resource methods without `tsc` throwing errors.
9
+
10
+ ## v7.4.0
11
+
12
+ ### New Features
13
+
14
+ * Generated up-to-date TypeScript definitions.
15
+
16
+ ### Bug Fixes
17
+
18
+ * Fixed the automatic generation of TypeScript definitions.
19
+
4
20
  ## v7.3.0
5
21
 
6
22
  ### New Features
package/lib/crisp.js CHANGED
@@ -161,48 +161,105 @@ var services = {
161
161
  * @classdesc This is the Crisp Library. Handles REST and RTM operations
162
162
  */
163
163
  function Crisp() {
164
- this.auth = {};
164
+ /**
165
+ * @public
166
+ * @type {*}
167
+ */
168
+ this.bucket = {};
165
169
 
166
170
  /**
167
- * @private
168
- * @type {string}
171
+ * @public
172
+ * @type {*}
173
+ */
174
+ this.media = {};
175
+
176
+ /**
177
+ * @public
178
+ * @type {*}
179
+ */
180
+ this.plugin = {};
181
+
182
+ /**
183
+ * @public
184
+ * @type {*}
185
+ */
186
+ this.website = {};
187
+
188
+ /**
189
+ * @public
190
+ * @type {object}
169
191
  */
170
- this._tier = "user";
192
+ this.auth = {
193
+ tier : "user",
194
+ identifier : null,
195
+ key : null,
196
+ token : null
197
+ };
171
198
 
172
- /** @private */
199
+ /**
200
+ * @private
201
+ * @type {object}
202
+ */
173
203
  this._rest = {
174
204
  host : Crisp.DEFAULT_REST_HOST,
175
205
  basePath : Crisp.DEFAULT_REST_BASE_PATH
176
206
  };
177
207
 
178
- /** @private */
208
+ /**
209
+ * @private
210
+ * @type {object}
211
+ */
179
212
  this._rtm = {
180
213
  host : null,
181
214
  mode : Crisp.DEFAULT_RTM_MODE
182
215
  };
183
216
 
184
- /** @private */
217
+ /**
218
+ * @private
219
+ * @type {string}
220
+ */
185
221
  this._useragent = (Crisp.DEFAULT_USERAGENT_PREFIX + pkg.version);
186
222
 
187
- /** @private */
223
+ /**
224
+ * @private
225
+ * @type {object}
226
+ */
188
227
  this._emitter = new EventEmitter();
189
228
 
190
- /** @private */
229
+ /**
230
+ * @private
231
+ * @type {object|null}
232
+ */
191
233
  this._socket = null;
192
234
 
193
- /** @private */
235
+ /**
236
+ * @private
237
+ * @type {object|null}
238
+ */
194
239
  this._loopback = null;
195
240
 
196
- /** @private */
241
+ /**
242
+ * @private
243
+ * @type {number|null}
244
+ */
197
245
  this._lastEventRebind = null;
198
246
 
199
- /** @private */
247
+ /**
248
+ * @private
249
+ * @type {object|null}
250
+ */
200
251
  this._brokerScheduler = null;
201
252
 
202
- /** @private */
253
+ /**
254
+ * @private
255
+ * @type {Array}
256
+ */
203
257
  this._brokerBindHooks = [];
204
258
 
205
- /** @private */
259
+ /**
260
+ * @private
261
+ * @type {object}
262
+ */
206
263
  this._boundEvents = {};
207
264
 
208
265
  // Prepare
@@ -214,8 +271,10 @@ Crisp.prototype = {
214
271
  /**
215
272
  * Sets the REST API host
216
273
  * @memberof Crisp
274
+ * @public
217
275
  * @method setRestHost
218
276
  * @param {string} host - Hostname
277
+ * @return {undefined}
219
278
  */
220
279
  setRestHost : function(host) {
221
280
  if (typeof host === "string") {
@@ -228,8 +287,10 @@ Crisp.prototype = {
228
287
  /**
229
288
  * Sets the RTM API host
230
289
  * @memberof Crisp
290
+ * @public
231
291
  * @method setRtmHost
232
292
  * @param {string} host - Hostname
293
+ * @return {undefined}
233
294
  */
234
295
  setRtmHost : function(host) {
235
296
  if (typeof host === "string") {
@@ -242,8 +303,10 @@ Crisp.prototype = {
242
303
  /**
243
304
  * Sets the RTM channel mode (ie. WebSockets or Web Hooks)
244
305
  * @memberof Crisp
306
+ * @public
245
307
  * @method setRtmMode
246
308
  * @param {string} mode - RTM mode ('websockets' or 'webhooks')
309
+ * @return {undefined}
247
310
  */
248
311
  setRtmMode : function(mode) {
249
312
  if (Crisp.AVAILABLE_RTM_MODES.indexOf(mode) !== -1) {
@@ -257,38 +320,46 @@ Crisp.prototype = {
257
320
  },
258
321
 
259
322
  /**
260
- * Sets the tier
323
+ * Sets the authentication tier
261
324
  * @memberof Crisp
325
+ * @public
262
326
  * @method setTier
263
327
  * @param {string} tier
328
+ * @return {undefined}
264
329
  */
265
330
  setTier : function(tier) {
266
- this._tier = (tier || "user");
331
+ this.auth.tier = (tier || "user");
267
332
  },
268
333
 
269
334
  /**
270
335
  * Authenticates
271
336
  * @memberof Crisp
337
+ * @public
272
338
  * @method authenticate
273
339
  * @param {string} identifier
274
340
  * @param {string} key
341
+ * @return {undefined}
275
342
  */
276
343
  authenticate : function(identifier, key) {
277
344
  var auth = this.auth;
278
345
 
346
+ // Store credentials
279
347
  auth.identifier = identifier;
280
348
  auth.key = key;
281
349
 
282
- auth.token = Buffer.from(identifier + ":" + key).toString("base64");
350
+ // Assign pre-computed authentication token
351
+ auth.token = Buffer.from(identifier + ":" + key).toString("base64");
283
352
  },
284
353
 
285
354
  /**
286
355
  * Authenticates (with tier)
287
356
  * @memberof Crisp
357
+ * @public
288
358
  * @method authenticateTier
289
359
  * @param {string} tier
290
360
  * @param {string} identifier
291
361
  * @param {string} key
362
+ * @return {undefined}
292
363
  */
293
364
  authenticateTier : function(tier, identifier, key) {
294
365
  this.setTier(tier);
@@ -298,10 +369,12 @@ Crisp.prototype = {
298
369
  /**
299
370
  * Method wrapper to HEAD a resource
300
371
  * @memberof Crisp
372
+ * @public
301
373
  * @method head
302
374
  * @param {string} resource
303
375
  * @param {object} query
304
376
  * @param {object} body
377
+ * @return {Promise}
305
378
  */
306
379
  head : function(resource, query, body) {
307
380
  var self = this;
@@ -316,10 +389,12 @@ Crisp.prototype = {
316
389
  /**
317
390
  * Method wrapper to GET a resource
318
391
  * @memberof Crisp
392
+ * @public
319
393
  * @method get
320
394
  * @param {string} resource
321
395
  * @param {object} query
322
396
  * @param {object} body
397
+ * @return {Promise}
323
398
  */
324
399
  get : function(resource, query) {
325
400
  var self = this;
@@ -334,10 +409,12 @@ Crisp.prototype = {
334
409
  /**
335
410
  * Method wrapper to POST a resource
336
411
  * @memberof Crisp
412
+ * @public
337
413
  * @method post
338
414
  * @param {string} resource
339
415
  * @param {object} query
340
416
  * @param {object} body
417
+ * @return {Promise}
341
418
  */
342
419
  post : function(resource, query, body) {
343
420
  var self = this;
@@ -352,10 +429,12 @@ Crisp.prototype = {
352
429
  /**
353
430
  * Method wrapper to PATCH a resource
354
431
  * @memberof Crisp
432
+ * @public
355
433
  * @method patch
356
434
  * @param {string} resource
357
435
  * @param {object} query
358
436
  * @param {object} body
437
+ * @return {Promise}
359
438
  */
360
439
  patch : function(resource, query, body) {
361
440
  var self = this;
@@ -370,10 +449,12 @@ Crisp.prototype = {
370
449
  /**
371
450
  * Method wrapper to PUT a resource
372
451
  * @memberof Crisp
452
+ * @public
373
453
  * @method put
374
454
  * @param {string} resource
375
455
  * @param {object} query
376
456
  * @param {object} body
457
+ * @return {Promise}
377
458
  */
378
459
  put : function(resource, query, body) {
379
460
  var self = this;
@@ -388,10 +469,12 @@ Crisp.prototype = {
388
469
  /**
389
470
  * Method wrapper to DELETE a resource
390
471
  * @memberof Crisp
472
+ * @public
391
473
  * @method delete
392
474
  * @param {string} resource
393
475
  * @param {object} query
394
476
  * @param {object} body
477
+ * @return {Promise}
395
478
  */
396
479
  delete : function(resource, query, body) {
397
480
  var self = this;
@@ -406,9 +489,11 @@ Crisp.prototype = {
406
489
  /**
407
490
  * Binds RTM event
408
491
  * @memberof Crisp
492
+ * @public
409
493
  * @method on
410
494
  * @param {string} event
411
495
  * @param {function} callback
496
+ * @return {Promise}
412
497
  */
413
498
  on : function(event, callback) {
414
499
  // Ensure all input arguments are set
@@ -472,8 +557,10 @@ Crisp.prototype = {
472
557
  /**
473
558
  * Receives a raw event and dispatches it to the listener (used for Web Hooks)
474
559
  * @memberof Crisp
560
+ * @public
475
561
  * @method receiveHook
476
562
  * @param {object} body
563
+ * @return {undefined}
477
564
  */
478
565
  receiveHook : function(body) {
479
566
  var self = this;
@@ -516,11 +603,13 @@ Crisp.prototype = {
516
603
  * Verifies an event string and checks that signatures match (used for Web \
517
604
  * Hooks)
518
605
  * @memberof Crisp
606
+ * @public
519
607
  * @method verifyHook
520
608
  * @param {string} secret
521
609
  * @param {object} body
522
610
  * @param {string} timestamp
523
611
  * @param {string} signature
612
+ * @return {boolean}
524
613
  */
525
614
  verifyHook : function(secret, body, timestamp, signature) {
526
615
  if (this._loopback) {
@@ -535,11 +624,13 @@ Crisp.prototype = {
535
624
  * Verifies an event string and checks that signatures match (used for \
536
625
  * Widgets)
537
626
  * @memberof Crisp
627
+ * @public
538
628
  * @method verifyWidget
539
629
  * @param {string} secret
540
630
  * @param {object} body
541
631
  * @param {string} timestamp
542
632
  * @param {string} signature
633
+ * @return {boolean}
543
634
  */
544
635
  verifyWidget : function(secret, body, timestamp, signature) {
545
636
  return this._verifySignature(secret, body, timestamp, signature);
@@ -548,7 +639,9 @@ Crisp.prototype = {
548
639
  /**
549
640
  * Rebinds socket events (used for WebSockets)
550
641
  * @memberof Crisp
642
+ * @public
551
643
  * @method rebind
644
+ * @return {Promise}
552
645
  */
553
646
  rebindSocket : function() {
554
647
  if (!this._socket) {
@@ -570,12 +663,15 @@ Crisp.prototype = {
570
663
  );
571
664
  }
572
665
 
573
- // Rebind to socket events (eg. newly bound websites)
574
- this._lastEventRebind = nowTime;
666
+ return Promise.resolve()
667
+ .then(function() {
668
+ // Rebind to socket events (eg. newly bound websites)
669
+ this._lastEventRebind = nowTime;
575
670
 
576
- this._socket.emit("socket:bind", {});
671
+ this._socket.emit("socket:bind", {});
577
672
 
578
- return Promise.resolve();
673
+ return Promise.resolve();
674
+ });
579
675
  },
580
676
 
581
677
  /**
@@ -584,6 +680,7 @@ Crisp.prototype = {
584
680
  * @private
585
681
  * @method _prepareRestUrl
586
682
  * @param {Array} paths - List of paths ['session', 'login']
683
+ * @return {string}
587
684
  */
588
685
  _prepareRestUrl : function(paths) {
589
686
  if (Array.isArray(paths) === true) {
@@ -604,12 +701,23 @@ Crisp.prototype = {
604
701
  * @memberof Crisp
605
702
  * @private
606
703
  * @method _prepareServices
704
+ * @return {undefined}
607
705
  */
608
706
  _prepareServices : function() {
609
707
  // Bind services
610
708
  for (var name in services) {
611
709
  var serviceInstance = new services[name]();
612
710
 
711
+ // Acquire service map
712
+ var serviceMap = this[(name[0].toLowerCase() + name.substring(1))];
713
+
714
+ // No service map available?
715
+ if (!serviceMap) {
716
+ throw new Error(
717
+ "[Crisp] prepareServices: service '" + name + "' has no map available"
718
+ );
719
+ }
720
+
613
721
  // No resources defined in service?
614
722
  if (!serviceInstance._resources ||
615
723
  serviceInstance._resources.length === 0) {
@@ -621,10 +729,8 @@ Crisp.prototype = {
621
729
 
622
730
  // Prepare all resources (for service)
623
731
  this._prepareResources(
624
- serviceInstance, serviceInstance._resources
732
+ serviceMap, serviceInstance._resources
625
733
  );
626
-
627
- this[(name[0].toLowerCase() + name.substring(1))] = serviceInstance;
628
734
  }
629
735
  },
630
736
 
@@ -633,15 +739,16 @@ Crisp.prototype = {
633
739
  * @memberof Crisp
634
740
  * @private
635
741
  * @method _prepareResources
636
- * @param {object} serviceInstance
742
+ * @param {object} serviceMap
637
743
  * @param {Array} resources
744
+ * @return {undefined}
638
745
  */
639
- _prepareResources : function(serviceInstance, resources) {
746
+ _prepareResources : function(serviceMap, resources) {
640
747
  for (var i = 0; i < resources.length; i++) {
641
748
  var resourceConstructor = require("./resources/" + resources[i]);
642
749
 
643
750
  // Instanciate resource, which will auto-bind itself to service prototype
644
- new resourceConstructor(serviceInstance, this);
751
+ new resourceConstructor(serviceMap, this);
645
752
  }
646
753
  },
647
754
 
@@ -651,6 +758,7 @@ Crisp.prototype = {
651
758
  * @private
652
759
  * @method _prepareBroker
653
760
  * @param {function} fnBindHook
761
+ * @return {Promise}
654
762
  */
655
763
  _prepareBroker : function(fnBindHook) {
656
764
  var self = this;
@@ -717,15 +825,19 @@ Crisp.prototype = {
717
825
  * @memberof Crisp
718
826
  * @private
719
827
  * @method _connectLoopback
828
+ * @return {Promise}
720
829
  */
721
830
  _connectLoopback : function() {
722
- // Assign emitter to loopback
723
- this._loopback = this._emitter;
831
+ return Promise.resolve()
832
+ .then(function() {
833
+ // Assign emitter to loopback
834
+ this._loopback = this._emitter;
724
835
 
725
- // Unstack broker bind hooks immediately
726
- this._unstackBrokerBindHooks(this._loopback);
836
+ // Unstack broker bind hooks immediately
837
+ this._unstackBrokerBindHooks(this._loopback);
727
838
 
728
- return Promise.resolve();
839
+ return Promise.resolve();
840
+ });
729
841
  },
730
842
 
731
843
  /**
@@ -734,6 +846,7 @@ Crisp.prototype = {
734
846
  * @private
735
847
  * @method _connectSocket
736
848
  * @param {string} rtmHostOverride
849
+ * @return {Promise}
737
850
  */
738
851
  _connectSocket : function(rtmHostOverride) {
739
852
  var self = this;
@@ -752,7 +865,7 @@ Crisp.prototype = {
752
865
  // Acquire RTM API URL from remote
753
866
  var restUrlSegments;
754
867
 
755
- switch (self._tier) {
868
+ switch (self.auth.tier) {
756
869
  case "plugin": {
757
870
  restUrlSegments = ["plugin", "connect", "endpoints"];
758
871
 
@@ -823,10 +936,10 @@ Crisp.prototype = {
823
936
  * @memberof Crisp
824
937
  * @private
825
938
  * @method _emitAuthenticateSocket
939
+ * @return {undefined}
826
940
  */
827
941
  _emitAuthenticateSocket : function() {
828
942
  var auth = this.auth,
829
- tier = this._tier,
830
943
  boundEvents = Object.keys(this._boundEvents);
831
944
 
832
945
  if (!this._socket) {
@@ -851,7 +964,7 @@ Crisp.prototype = {
851
964
  this._socket.emit("authentication", {
852
965
  username : auth.identifier,
853
966
  password : auth.key,
854
- tier : tier,
967
+ tier : auth.tier,
855
968
  events : boundEvents
856
969
  });
857
970
  },
@@ -862,6 +975,7 @@ Crisp.prototype = {
862
975
  * @private
863
976
  * @method _unstackBrokerBindHooks
864
977
  * @param {object} modeInstance
978
+ * @return {undefined}
865
979
  */
866
980
  _unstackBrokerBindHooks : function(modeInstance) {
867
981
  // Setup user socket event listeners
@@ -883,6 +997,7 @@ Crisp.prototype = {
883
997
  * @param {object} body
884
998
  * @param {function} resolve
885
999
  * @param {function} reject
1000
+ * @return {undefined}
886
1001
  */
887
1002
  _request : function(resource, method, query, body, resolve, reject) {
888
1003
  var self = this;
@@ -893,7 +1008,7 @@ Crisp.prototype = {
893
1008
 
894
1009
  headers : {
895
1010
  "User-Agent" : self._useragent,
896
- "X-Crisp-Tier" : self._tier
1011
+ "X-Crisp-Tier" : self.auth.tier
897
1012
  },
898
1013
 
899
1014
  throwHttpErrors : false
@@ -976,6 +1091,7 @@ Crisp.prototype = {
976
1091
  * @param {string} method
977
1092
  * @param {number} statusCode
978
1093
  * @param {object} response
1094
+ * @return {string}
979
1095
  */
980
1096
  _readErrorResponseReason : function(method, statusCode, response) {
981
1097
  // HEAD method? As HEAD requests do not expect any response body, then we \
@@ -1006,6 +1122,7 @@ Crisp.prototype = {
1006
1122
  * @param {object} body
1007
1123
  * @param {string} timestamp
1008
1124
  * @param {string} signature
1125
+ * @return {boolean}
1009
1126
  */
1010
1127
  _verifySignature : function(secret, body, timestamp, signature) {
1011
1128
  // Ensure all provided data is valid
@@ -18,8 +18,10 @@ function BucketURL(service, crisp) {
18
18
  /**
19
19
  * Generate Bucket URL
20
20
  * @memberof BucketURL
21
+ * @public
21
22
  * @method generateBucketURL
22
- * @return Promise
23
+ * @param {object} data
24
+ * @return {Promise}
23
25
  */
24
26
  service.generateBucketURL = function(data) {
25
27
  return crisp.post(
@@ -18,8 +18,12 @@ function MediaAnimation(service, crisp) {
18
18
  /**
19
19
  * List Animation Medias
20
20
  * @memberof MediaAnimation
21
+ * @public
21
22
  * @method listAnimationMedias
22
- * @return Promise
23
+ * @param {number} pageNumber
24
+ * @param {string} listID
25
+ * @param {object} searchQuery
26
+ * @return {Promise}
23
27
  */
24
28
  service.listAnimationMedias = function(pageNumber, listID, searchQuery) {
25
29
  return crisp.get(
@@ -18,8 +18,9 @@ function PluginConnect(service, crisp) {
18
18
  /**
19
19
  * Get Connect Account
20
20
  * @memberof PluginConnect
21
+ * @public
21
22
  * @method getConnectAccount
22
- * @return Promise
23
+ * @return {Promise}
23
24
  */
24
25
  service.getConnectAccount = function() {
25
26
  return crisp.get(
@@ -30,8 +31,9 @@ function PluginConnect(service, crisp) {
30
31
  /**
31
32
  * Check Connect Session Validity
32
33
  * @memberof PluginConnect
34
+ * @public
33
35
  * @method checkConnectSessionValidity
34
- * @return Promise
36
+ * @return {Promise}
35
37
  */
36
38
  service.checkConnectSessionValidity = function() {
37
39
  return crisp.head(
@@ -42,8 +44,12 @@ function PluginConnect(service, crisp) {
42
44
  /**
43
45
  * List All Connect Websites
44
46
  * @memberof PluginConnect
47
+ * @public
45
48
  * @method listAllConnectWebsites
46
- * @return Promise
49
+ * @param {number} pageNumber
50
+ * @param {boolean} [filterConfigured]
51
+ * @param {string} [dateSince]
52
+ * @return {Promise}
47
53
  */
48
54
  service.listAllConnectWebsites = function(
49
55
  pageNumber, filterConfigured, dateSince
@@ -73,8 +79,9 @@ function PluginConnect(service, crisp) {
73
79
  /**
74
80
  * Get Connect Endpoints
75
81
  * @memberof PluginConnect
82
+ * @public
76
83
  * @method getConnectEndpoints
77
- * @return Promise
84
+ * @return {Promise}
78
85
  */
79
86
  service.getConnectEndpoints = function() {
80
87
  return crisp.get(