Skip to content

Commit 89b0d37

Browse files
author
a-jie
committed
fix any bug
fix any bug
1 parent dd671fb commit 89b0d37

File tree

5 files changed

+100
-52
lines changed

5 files changed

+100
-52
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
return new Promise(function (resolve, reject) {
4141
// create cache data
4242
var id = generateId();
43-
var timeout = getValFromOpt(options, 'callback', 8000) << 0;
43+
var timeout = getValFromOpt(options, 'timeout', 8000);
4444
var callback = getValFromOpt(options, 'callback') || getValFromOpt(options, 'cb') || 'callback';
4545
var callbackName = getValFromOpt(options, 'callbackName', id);
46-
var tid = setTimeout(function () { destroy(', it is timeout'); }, timeout);
46+
var tid = setTimeout(function () { destroy('. It is timeout!'); }, timeout);
4747

4848
// generate url
4949
var url = typeof reqOrUrl === 'object' ? reqOrUrl.url : reqOrUrl + '';
@@ -54,12 +54,12 @@
5454
var head = document.getElementsByTagName('head')[0] || document.head;
5555
var script = document.createElement('script');
5656
script.src = url;
57-
script.onerror = destroy;
57+
script.onerror = function () { destroy('. It is url loaded Error!') };
5858
head.appendChild(script);
5959

6060
window[callbackName] = function (res) {
6161
resolve(new Response(res, url));
62-
destroy();
62+
destroy('ok');
6363
}
6464

6565
// destroy func
@@ -70,8 +70,9 @@
7070
clearTimeout(tid);
7171
} catch (e) { }
7272

73+
if (msg == 'ok') return;
7374
msg = msg || '';
74-
reject(new Error('JSONP request to ' + url + msg));
75+
reject(new Error('Fetch jsonp request to ' + url + ' failed!' + msg));
7576
}
7677
})
7778
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{
22
"name": "fetch-jsonp-polyfill",
3-
"version": "1.1.3",
3+
"version": "1.1.5",
44
"description": "a jsonp fetch polyfill",
55
"main": "dist/index.js",
66
"directories": {
77
"test": "test"
88
},
99
"scripts": {
10-
"build": "uglifyjs ./lib/index.js -o ./dist/index.js -c -m",
11-
"test": "jest"
10+
"build": "uglifyjs ./lib/index.js -o ./dist/index.js -c -m"
1211
},
1312
"keywords": [
1413
"fetch",
@@ -17,6 +16,8 @@
1716
"author": "a-jie",
1817
"license": "MIT",
1918
"devDependencies": {
19+
"chai": "^4.1.2",
20+
"mocha": "^5.0.5",
2021
"uglify-js": "^3.3.17"
2122
}
2223
}

test/test.html

Lines changed: 89 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,89 @@
1-
<script src="../lib/index.js"></script>
2-
3-
<script>
4-
var URL = 'http://jsfiddle.net/echo/jsonp/';
5-
6-
fetch(URL, {
7-
method: "JSONP"
8-
})
9-
.then(response => response.json())
10-
.then(res => {
11-
console.log(res)
12-
if (res.ok) {
13-
console.log(res.json());
14-
}
15-
16-
// expect(res.json()).toBe(3);
17-
})
18-
.catch(err => {
19-
console.log(err);
20-
});
21-
</script>
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Fetch JSONP PolyFill Tests</title>
6+
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
7+
</head>
8+
9+
<body>
10+
<div id="mocha"></div>
11+
<script src="../node_modules/mocha/mocha.js"></script>
12+
<script src="../node_modules/chai/chai.js"></script>
13+
<script src="../lib/index.js"></script>
14+
<script>mocha.setup('bdd')</script>
15+
16+
<script>
17+
var assert = chai.assert;
18+
var expect = chai.expect;
19+
20+
describe('Test Fetch JSONP', function () {
21+
22+
it('basic fetch jsonp should return json data', function (done) {
23+
fetch('http://jsfiddle.net/echo/jsonp/?hello=world', { method: "JSONP" })
24+
.then(response => response.json())
25+
.then(data => {
26+
expect(data.hello).to.equal('world');
27+
done();
28+
})
29+
.catch(err => {
30+
done(err);
31+
console.log(err);
32+
});
33+
});
34+
35+
36+
it('error url should fetch failed!', function (done) {
37+
fetch('http://jsfiddle.net/echo/jsonp1111111/?hello=world', { method: "JSONP" })
38+
.then(response => response.json())
39+
.then(data => {
40+
assert.equal(data.hello, 'world');
41+
done();
42+
})
43+
.catch(err => {
44+
assert.equal(true, true);
45+
done();
46+
});
47+
});
48+
49+
it('use custom callbackName', function (done) {
50+
fetch('http://jsfiddle.net/echo/jsonp/?hello=world', {
51+
method: "JSONP",
52+
callbackName: "jsonp_callback",
53+
})
54+
.then(response => response.json())
55+
.then(data => {
56+
assert.equal(data.hello, 'world');
57+
done();
58+
})
59+
.catch(err => {
60+
console.log(err);
61+
assert.equal(true, true);
62+
});
63+
});
64+
65+
it('use custom callback', function (done) {
66+
fetch('http://jsfiddle.net/echo/jsonp/?hello=world', {
67+
method: "JSONP",
68+
timeout: 8000,
69+
callback: "callback1",
70+
})
71+
.then(response => response.json())
72+
.then(data => {
73+
assert.equal(data.hello, 'world');
74+
done();
75+
})
76+
.catch(err => {
77+
console.log(err);
78+
assert.equal(true, true);
79+
});
80+
});
81+
});
82+
</script>
83+
84+
<script>
85+
mocha.run();
86+
</script>
87+
</body>
88+
89+
</html>

test/test.spec.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)