@selkirk-systems/fetch 0.1.0 → 0.1.2
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 +15 -7
- package/package.json +2 -5
- package/src/index.js +18 -9
package/dist/index.js
CHANGED
|
@@ -10,13 +10,19 @@ var _networkTimer = null;
|
|
|
10
10
|
|
|
11
11
|
function _checkFetchStatus(response) {
|
|
12
12
|
if (response.status >= 200 && response.status < 300) {
|
|
13
|
-
|
|
13
|
+
if (response.text.length > 0) {
|
|
14
|
+
return response.json();
|
|
15
|
+
}
|
|
14
16
|
} else {
|
|
15
17
|
var error = new Error(response.statusText);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
if (response.text.length) {
|
|
19
|
+
return response.json().then(function (result) {
|
|
20
|
+
error.response = result;
|
|
21
|
+
throw error;
|
|
22
|
+
});
|
|
23
|
+
} else {
|
|
18
24
|
throw error;
|
|
19
|
-
}
|
|
25
|
+
}
|
|
20
26
|
}
|
|
21
27
|
}
|
|
22
28
|
|
|
@@ -25,14 +31,16 @@ function _invokeFetch(url, props) {
|
|
|
25
31
|
throw new Error("fetch:Missing Parameter - URL");
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
var options =
|
|
34
|
+
var options = {
|
|
29
35
|
method: "GET",
|
|
30
36
|
credentials: 'same-origin',
|
|
31
37
|
headers: {
|
|
32
38
|
'Accept': 'application/json',
|
|
33
39
|
'Content-Type': 'application/json'
|
|
34
40
|
}
|
|
35
|
-
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
options = Assign(options, props);
|
|
36
44
|
|
|
37
45
|
if (_networkTimer) {
|
|
38
46
|
return new Promise(function (resolve, reject) {
|
|
@@ -40,7 +48,7 @@ function _invokeFetch(url, props) {
|
|
|
40
48
|
});
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
if (options.body) options.body = JSON.stringify(options.body);
|
|
51
|
+
if (options.headers['Content-Type'] === "application/json" && options.body) options.body = JSON.stringify(options.body);
|
|
44
52
|
|
|
45
53
|
return fetch(url, options).then(_checkFetchStatus);
|
|
46
54
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selkirk-systems/fetch",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A wrapper on fetch-polyfill that will work for server and client environments",
|
|
5
5
|
"selkirk": {
|
|
6
6
|
"bambooDeploy": "npm",
|
|
@@ -10,9 +10,6 @@
|
|
|
10
10
|
"http://bamboo5.selkirksystems.com/browse/RCB-EDFPUB"
|
|
11
11
|
]
|
|
12
12
|
},
|
|
13
|
-
"publishConfig": {
|
|
14
|
-
"registry": "https://registry.npmjs.org/"
|
|
15
|
-
},
|
|
16
13
|
"repository": {
|
|
17
14
|
"type": "git",
|
|
18
15
|
"url": "https://bitbucket.org/selkirk/selkirk-fetch.git"
|
|
@@ -65,7 +62,7 @@
|
|
|
65
62
|
"dependencies": {
|
|
66
63
|
"babel-runtime": "^6.6.1",
|
|
67
64
|
"bluebird": "^3.4.6",
|
|
68
|
-
"es6-promise": "^
|
|
65
|
+
"es6-promise": "^4.2.2",
|
|
69
66
|
"isomorphic-fetch": "^2.2.1",
|
|
70
67
|
"object-assign": "^4.1.0"
|
|
71
68
|
}
|
package/src/index.js
CHANGED
|
@@ -8,13 +8,19 @@ const _networkTimer = null;
|
|
|
8
8
|
|
|
9
9
|
function _checkFetchStatus(response) {
|
|
10
10
|
if (response.status >= 200 && response.status < 300) {
|
|
11
|
-
|
|
11
|
+
if (response.text.length > 0) {
|
|
12
|
+
return response.json()
|
|
13
|
+
}
|
|
12
14
|
} else {
|
|
13
15
|
var error = new Error(response.statusText);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
}
|
|
18
24
|
}
|
|
19
25
|
}
|
|
20
26
|
|
|
@@ -22,15 +28,18 @@ function _invokeFetch(url,props) {
|
|
|
22
28
|
if(!url) {
|
|
23
29
|
throw new Error("fetch:Missing Parameter - URL");
|
|
24
30
|
}
|
|
25
|
-
|
|
26
|
-
var options =
|
|
31
|
+
|
|
32
|
+
var options = {
|
|
27
33
|
method:"GET",
|
|
28
34
|
credentials:'same-origin',
|
|
29
35
|
headers: {
|
|
30
36
|
'Accept': 'application/json',
|
|
31
37
|
'Content-Type': 'application/json'
|
|
32
38
|
}
|
|
33
|
-
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
options = Assign(options,props);
|
|
42
|
+
|
|
34
43
|
|
|
35
44
|
if(_networkTimer){
|
|
36
45
|
return new Promise((resolve,reject)=>{
|
|
@@ -38,7 +47,7 @@ function _invokeFetch(url,props) {
|
|
|
38
47
|
})
|
|
39
48
|
}
|
|
40
49
|
|
|
41
|
-
if(options.body)options.body = JSON.stringify(options.body);
|
|
50
|
+
if(options.headers['Content-Type'] === "application/json" && options.body)options.body = JSON.stringify(options.body);
|
|
42
51
|
|
|
43
52
|
return fetch(url, options)
|
|
44
53
|
.then(_checkFetchStatus)
|