@selkirk-systems/fetch 0.1.3 → 0.1.5

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/dist/index.js CHANGED
@@ -9,74 +9,72 @@ require('isomorphic-fetch');
9
9
  var _networkTimer = null;
10
10
 
11
11
  function _checkFetchStatus(response) {
12
- if (response.status >= 200 && response.status < 300) {
13
- return response.json();
14
- } else {
15
- var error = new Error(response.statusText);
16
- return response.json().then(function (result) {
17
- error.response = result;
18
- throw error;
19
- });
20
- }
21
- }
22
-
23
- function _parseJSON(response) {
24
- return response.text().then(function (text) {
25
- return text ? JSON.parse(text) : {};
26
- });
12
+ if (response.status >= 200 && response.status < 300) {
13
+ return response.text().then(function (text) {
14
+ return text ? JSON.parse(text) : {};
15
+ });
16
+ } else {
17
+ var error = new Error(response.statusText);
18
+ return response.text().then(function (text) {
19
+ if (text) {
20
+ error.response = JSON.parse(text);
21
+ }
22
+ throw error;
23
+ });
24
+ }
27
25
  }
28
26
 
29
27
  function _invokeFetch(url, props) {
30
- if (!url) {
31
- throw new Error("fetch:Missing Parameter - URL");
32
- }
33
-
34
- var options = {
35
- method: "GET",
36
- credentials: 'same-origin',
37
- headers: {
38
- 'Accept': 'application/json',
39
- 'Content-Type': 'application/json'
40
- }
41
- };
42
-
43
- options = Assign(options, props);
44
-
45
- if (_networkTimer) {
46
- return new Promise(function (resolve, reject) {
47
- setTimeout(resolve, _networkTimer);
48
- });
49
- }
50
-
51
- if (options.headers['Content-Type'] === "application/json" && options.body) options.body = JSON.stringify(options.body);
52
-
53
- return fetch(url, options).then(_checkFetchStatus);
28
+ if (!url) {
29
+ throw new Error("fetch:Missing Parameter - URL");
30
+ }
31
+
32
+ var options = {
33
+ method: "GET",
34
+ credentials: 'same-origin',
35
+ headers: {
36
+ 'Accept': 'application/json',
37
+ 'Content-Type': 'application/json'
38
+ }
39
+ };
40
+
41
+ options = Assign(options, props);
42
+
43
+ if (_networkTimer) {
44
+ return new Promise(function (resolve, reject) {
45
+ setTimeout(resolve, _networkTimer);
46
+ });
47
+ }
48
+
49
+ if (options.headers['Content-Type'] === "application/json" && options.body) options.body = JSON.stringify(options.body);
50
+
51
+ return fetch(url, options).then(_checkFetchStatus);
54
52
  }
55
53
  module.exports = function (dispatcher, params) {
56
- if (!dispatcher) return _invokeFetch;
57
-
58
- var options = Assign({
59
- lastErrorEvent: "LAST_ERROR_EVENT",
60
- errorID: null
61
- }, params);
62
-
63
- return function (url, props, responseEvent) {
64
- return _invokeFetch(url, props).then(function (response) {
65
-
66
- if (!responseEvent) return null;
67
-
68
- dispatcher.handleViewAction({
69
- actionType: responseEvent,
70
- item: response
71
- });
72
-
73
- return response;
74
- }).catch(function (err) {
75
- console.error(new Error("Fetch - " + options.errorID || ""), err);
76
- dispatcher.handleViewAction({
77
- actionType: options.lastErrorEvent,
78
- item: { error: err, id: options.errorID }
79
- });
80
- });
81
- };
54
+ if (!dispatcher) return _invokeFetch;
55
+
56
+ var options = Assign({
57
+ lastErrorEvent: "LAST_ERROR_EVENT",
58
+ errorID: null
59
+ }, params);
60
+
61
+ return function (url, props, responseEvent) {
62
+ return _invokeFetch(url, props).then(function (response) {
63
+
64
+ if (!responseEvent) return null;
65
+
66
+ dispatcher.handleViewAction({
67
+ actionType: responseEvent,
68
+ item: response
69
+ });
70
+
71
+ return response;
72
+ }).catch(function (err) {
73
+ console.error(new Error("Fetch - " + options.errorID || ""), err);
74
+ dispatcher.handleViewAction({
75
+ actionType: options.lastErrorEvent,
76
+ item: { error: err, id: options.errorID }
77
+ });
78
+ });
79
+ };
82
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@selkirk-systems/fetch",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "A wrapper on fetch-polyfill that will work for server and client environments",
5
5
  "selkirk": {
6
6
  "bambooDeploy": "npm",
package/src/index.js CHANGED
@@ -8,22 +8,20 @@ const _networkTimer = null;
8
8
 
9
9
  function _checkFetchStatus(response) {
10
10
  if (response.status >= 200 && response.status < 300) {
11
- return response.json()
11
+ return response.text().then(function(text) {
12
+ return text ? JSON.parse(text) : {}
13
+ });
12
14
  } else {
13
15
  var error = new Error(response.statusText);
14
- return response.json().then((result)=>{
15
- error.response = result;
16
+ return response.text().then(function(text) {
17
+ if (text) {
18
+ error.response = JSON.parse(text);
19
+ }
16
20
  throw error;
17
21
  });
18
22
  }
19
23
  }
20
24
 
21
- function _parseJSON(response) {
22
- return response.text().then(function(text) {
23
- return text ? JSON.parse(text) : {}
24
- });
25
- }
26
-
27
25
  function _invokeFetch(url,props) {
28
26
  if(!url) {
29
27
  throw new Error("fetch:Missing Parameter - URL");