crisp-api 6.4.0 → 6.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,12 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ ## v6.4.1
5
+
6
+ ### Bug Fixes
7
+
8
+ * Return more informative error reasons for non `2xx / 3xx` response codes on `HEAD` requests.
9
+
4
10
  ## v6.4.0
5
11
 
6
12
  ### New Features
package/lib/crisp.js CHANGED
@@ -29,8 +29,8 @@ Crisp.DEFAULT_USERAGENT_PREFIX = "node-crisp-api/";
29
29
 
30
30
 
31
31
  // REST API defaults
32
- Crisp.DEFAULT_REST_HOST = "https://api.crisp.chat";
33
- Crisp.DEFAULT_REST_BASE_PATH = "/v1/";
32
+ Crisp.DEFAULT_REST_HOST = "https://api.crisp.chat";
33
+ Crisp.DEFAULT_REST_BASE_PATH = "/v1/";
34
34
 
35
35
 
36
36
  // RTM API defaults
@@ -666,21 +666,23 @@ Crisp.prototype = {
666
666
  * @param {function} reject
667
667
  */
668
668
  _request : function(resource, method, query, body, resolve, reject) {
669
+ var self = this;
670
+
669
671
  var requestParameters = {
670
672
  responseType : "json",
671
673
  timeout : Crisp.DEFAULT_REQUEST_TIMEOUT,
672
674
 
673
675
  headers : {
674
- "User-Agent" : this._useragent,
675
- "X-Crisp-Tier" : this._tier
676
+ "User-Agent" : self._useragent,
677
+ "X-Crisp-Tier" : self._tier
676
678
  },
677
679
 
678
680
  throwHttpErrors : false
679
681
  };
680
682
 
681
683
  // Add authorization?
682
- if (this.auth.token) {
683
- requestParameters.headers.Authorization = ("Basic " + this.auth.token);
684
+ if (self.auth.token) {
685
+ requestParameters.headers.Authorization = ("Basic " + self.auth.token);
684
686
  }
685
687
 
686
688
  // Add body?
@@ -720,7 +722,9 @@ Crisp.prototype = {
720
722
 
721
723
  // Response error?
722
724
  if (response.statusCode >= 400) {
723
- var reason_message = ((response.body || {}).reason || "http_error");
725
+ var reason_message = self._readErrorResponseReason(
726
+ method, response.statusCode, response
727
+ );
724
728
  var data_message = ((response.body || {}).data || {}).message;
725
729
 
726
730
  return reject({
@@ -780,6 +784,35 @@ Crisp.prototype = {
780
784
  tier : tier,
781
785
  events : this._boundEvents
782
786
  });
787
+ },
788
+
789
+ /**
790
+ * Reads reason for error response
791
+ * @memberof Crisp
792
+ * @private
793
+ * @method _readErrorResponseReason
794
+ * @param {string} method
795
+ * @param {number} statusCode
796
+ * @param {object} response
797
+ */
798
+ _readErrorResponseReason : function(method, statusCode, response) {
799
+ // HEAD method? As HEAD requests do not expect any response body, then we \
800
+ // cannot map a reason from the response.
801
+ if (method === "head") {
802
+ // 5xx errors?
803
+ if (statusCode >= 500) {
804
+ return "server_error";
805
+ }
806
+
807
+ // 4xx errors?
808
+ if (statusCode >= 400) {
809
+ return "route_error";
810
+ }
811
+ }
812
+
813
+ // Other methods must hold a response body, therefore we can fallback on \
814
+ // an HTTP error if we fail to acquire any reason at all.
815
+ return ((response.body || {}).reason || "http_error");
783
816
  }
784
817
  };
785
818
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "crisp-api",
3
3
  "description": "Crisp API wrapper for Node - official, maintained by Crisp",
4
- "version": "6.4.0",
4
+ "version": "6.4.1",
5
5
  "homepage": "https://github.com/crisp-im/node-crisp-api",
6
6
  "license": "MIT",
7
7
  "author": {