@selkirk-systems/fetch 0.1.2 → 0.1.4

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