@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 +63 -65
- package/package.json +1 -1
- package/src/index.js +7 -9
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
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.
|
|
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.
|
|
15
|
-
|
|
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");
|