Skip to content

Commit 346c54a

Browse files
committed
feat(core/Jsonp.js): handle 404/500
handle code 404/500, and then reject request promise
1 parent 3cd216d commit 346c54a

File tree

8 files changed

+125
-110
lines changed

8 files changed

+125
-110
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ yarn add better-jsonp
2222
```
2323
```html
2424
<!-- using CDN -->
25-
<script src="https://unpkg.com/better-jsonp"></script>
26-
<script src="https://cdn.jsdelivr.net/npm/better-jsonp@latest/dist/better-jsonp.min.js"></script>
25+
<script src="https://unpkg.com/better-jsonp@latest"></script>
26+
<script src="https://cdn.jsdelivr.net/npm/better-jsonp@latest"></script>
2727
```
2828

2929
## Promise polyfill

dist/better-jsonp.common.js

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* better-jsonp v1.0.2
2+
* better-jsonp v1.1.0
33
* Copyrights (c) 2018 Bowen (lbwa)
44
* Released under the MIT License.
55
*/
@@ -51,20 +51,28 @@ var defaultOptions = {
5151
};
5252

5353
var Jsonp = function () {
54-
function Jsonp() {
54+
function Jsonp(options) {
5555
classCallCheck(this, Jsonp);
56+
57+
this.checkOptions(options);
58+
59+
this.initState(options);
60+
61+
this.encodeURL(options.url);
62+
63+
this.insertToElement(this._request);
5664
}
5765

5866
createClass(Jsonp, [{
5967
key: 'checkOptions',
6068
value: function checkOptions(options) {
61-
if (!options.url) throw new Error('Please check your request url.');
69+
if (!options || !options.url) throw new Error('Please check your request url.');
6270

6371
this.options = options;
6472
}
6573
}, {
66-
key: 'generateJsonpCallback',
67-
value: function generateJsonpCallback(options) {
74+
key: 'genJsonpCallback',
75+
value: function genJsonpCallback(options) {
6876
if (options.jsonpCallback) {
6977
this._jsonpCallback = options.jsonpCallback;
7078
} else {
@@ -86,21 +94,27 @@ var Jsonp = function () {
8694
* 2. use arrow function to define `this` object value (Jsonp instance).
8795
*/
8896
return new Promise(function (resolve, reject) {
97+
// handle 404/500 in response
98+
_this._insertScript.onerror = function () {
99+
_this.cleanScript();
100+
reject(new Error('Countdown has been clear! JSONP request unsuccessfully due to 404/500'));
101+
};
102+
89103
window[_this._jsonpCallback] = function (data) {
90104
_this.cleanScript();
91105
resolve(data);
92106
};
93107
});
94108
}
95109
}, {
96-
key: 'generateTimer',
97-
value: function generateTimer(options) {
110+
key: 'genTimer',
111+
value: function genTimer(options) {
98112
var _this2 = this;
99113

100114
// limit request period
101115
var timeout = options.timeout || defaultOptions.timeout;
102116

103-
// use arrow function to define `this` object value.
117+
// use arrow function to define `this` object value (Jsonp instance).
104118
if (timeout) {
105119
this._timer = setTimeout(function () {
106120
window[_this2._jsonpCallback] = noop;
@@ -110,6 +124,12 @@ var Jsonp = function () {
110124
}, timeout);
111125
}
112126
}
127+
}, {
128+
key: 'genScript',
129+
value: function genScript() {
130+
this._target = document.getElementsByTagName('script')[0] || document.body.lastElementChild;
131+
this._insertScript = document.createElement('script');
132+
}
113133
}, {
114134
key: 'initState',
115135
value: function initState(options) {
@@ -119,14 +139,16 @@ var Jsonp = function () {
119139
defineEnumerable(this, '_insertScript', null);
120140
defineEnumerable(this, '_target', null);
121141

142+
this.genScript();
143+
122144
// set this._jsonpCallback
123-
this.generateJsonpCallback(options);
145+
this.genJsonpCallback(options);
124146

125147
// invoke defineGlobalCallback after setting this._jsonpCallback
126148
defineEnumerable(this, '_globalCallback', this.defineGlobalCallback());
127149

128150
// set timer for limit request time
129-
this.generateTimer(options);
151+
this.genTimer(options);
130152
}
131153
}, {
132154
key: 'encodeURL',
@@ -147,23 +169,13 @@ var Jsonp = function () {
147169

148170
this._request = url;
149171
}
172+
173+
// activate JSONP
174+
150175
}, {
151176
key: 'insertToElement',
152177
value: function insertToElement(url) {
153-
var _this3 = this;
154-
155-
this._target = document.getElementsByTagName('script')[0] || document.body.lastElementChild;
156-
157-
this._insertScript = document.createElement('script');
158178
this._insertScript.src = url;
159-
160-
// listening 404/500
161-
this._insertScript.onerror = function () {
162-
_this3.cleanScript();
163-
throw new Error('Countdown has been clear! JSONP request unsuccessfully due to 404/500');
164-
};
165-
166-
// activate JSONP
167179
this._target.parentNode.insertBefore(this._insertScript, this._target);
168180
}
169181
}, {
@@ -175,7 +187,6 @@ var Jsonp = function () {
175187
}
176188

177189
window[this._jsonpCallback] = noop;
178-
179190
if (this._timer) clearTimeout(this._timer);
180191
}
181192
}]);
@@ -185,17 +196,10 @@ var Jsonp = function () {
185196
// facade in facade pattern
186197
// same as axios, zepto
187198
function createInstance(options) {
188-
var jsonp = new Jsonp();
189-
190-
jsonp.checkOptions(options);
191-
192-
jsonp.initState(options);
193-
194-
jsonp.encodeURL(jsonp.options.url);
195-
196-
jsonp.insertToElement(jsonp._request);
199+
var jsonp = new Jsonp(options);
197200

198-
return jsonp._globalCallback; // from initState(options)
201+
// from initState(options)
202+
return jsonp._globalCallback;
199203
}
200204

201205
module.exports = createInstance;

dist/better-jsonp.js

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* better-jsonp v1.0.2
2+
* better-jsonp v1.1.0
33
* Copyrights (c) 2018 Bowen (lbwa)
44
* Released under the MIT License.
55
*/
@@ -55,20 +55,28 @@
5555
};
5656

5757
var Jsonp = function () {
58-
function Jsonp() {
58+
function Jsonp(options) {
5959
classCallCheck(this, Jsonp);
60+
61+
this.checkOptions(options);
62+
63+
this.initState(options);
64+
65+
this.encodeURL(options.url);
66+
67+
this.insertToElement(this._request);
6068
}
6169

6270
createClass(Jsonp, [{
6371
key: 'checkOptions',
6472
value: function checkOptions(options) {
65-
if (!options.url) throw new Error('Please check your request url.');
73+
if (!options || !options.url) throw new Error('Please check your request url.');
6674

6775
this.options = options;
6876
}
6977
}, {
70-
key: 'generateJsonpCallback',
71-
value: function generateJsonpCallback(options) {
78+
key: 'genJsonpCallback',
79+
value: function genJsonpCallback(options) {
7280
if (options.jsonpCallback) {
7381
this._jsonpCallback = options.jsonpCallback;
7482
} else {
@@ -90,21 +98,27 @@
9098
* 2. use arrow function to define `this` object value (Jsonp instance).
9199
*/
92100
return new Promise(function (resolve, reject) {
101+
// handle 404/500 in response
102+
_this._insertScript.onerror = function () {
103+
_this.cleanScript();
104+
reject(new Error('Countdown has been clear! JSONP request unsuccessfully due to 404/500'));
105+
};
106+
93107
window[_this._jsonpCallback] = function (data) {
94108
_this.cleanScript();
95109
resolve(data);
96110
};
97111
});
98112
}
99113
}, {
100-
key: 'generateTimer',
101-
value: function generateTimer(options) {
114+
key: 'genTimer',
115+
value: function genTimer(options) {
102116
var _this2 = this;
103117

104118
// limit request period
105119
var timeout = options.timeout || defaultOptions.timeout;
106120

107-
// use arrow function to define `this` object value.
121+
// use arrow function to define `this` object value (Jsonp instance).
108122
if (timeout) {
109123
this._timer = setTimeout(function () {
110124
window[_this2._jsonpCallback] = noop;
@@ -114,6 +128,12 @@
114128
}, timeout);
115129
}
116130
}
131+
}, {
132+
key: 'genScript',
133+
value: function genScript() {
134+
this._target = document.getElementsByTagName('script')[0] || document.body.lastElementChild;
135+
this._insertScript = document.createElement('script');
136+
}
117137
}, {
118138
key: 'initState',
119139
value: function initState(options) {
@@ -123,14 +143,16 @@
123143
defineEnumerable(this, '_insertScript', null);
124144
defineEnumerable(this, '_target', null);
125145

146+
this.genScript();
147+
126148
// set this._jsonpCallback
127-
this.generateJsonpCallback(options);
149+
this.genJsonpCallback(options);
128150

129151
// invoke defineGlobalCallback after setting this._jsonpCallback
130152
defineEnumerable(this, '_globalCallback', this.defineGlobalCallback());
131153

132154
// set timer for limit request time
133-
this.generateTimer(options);
155+
this.genTimer(options);
134156
}
135157
}, {
136158
key: 'encodeURL',
@@ -151,23 +173,13 @@
151173

152174
this._request = url;
153175
}
176+
177+
// activate JSONP
178+
154179
}, {
155180
key: 'insertToElement',
156181
value: function insertToElement(url) {
157-
var _this3 = this;
158-
159-
this._target = document.getElementsByTagName('script')[0] || document.body.lastElementChild;
160-
161-
this._insertScript = document.createElement('script');
162182
this._insertScript.src = url;
163-
164-
// listening 404/500
165-
this._insertScript.onerror = function () {
166-
_this3.cleanScript();
167-
throw new Error('Countdown has been clear! JSONP request unsuccessfully due to 404/500');
168-
};
169-
170-
// activate JSONP
171183
this._target.parentNode.insertBefore(this._insertScript, this._target);
172184
}
173185
}, {
@@ -179,7 +191,6 @@
179191
}
180192

181193
window[this._jsonpCallback] = noop;
182-
183194
if (this._timer) clearTimeout(this._timer);
184195
}
185196
}]);
@@ -189,17 +200,10 @@
189200
// facade in facade pattern
190201
// same as axios, zepto
191202
function createInstance(options) {
192-
var jsonp = new Jsonp();
193-
194-
jsonp.checkOptions(options);
195-
196-
jsonp.initState(options);
197-
198-
jsonp.encodeURL(jsonp.options.url);
199-
200-
jsonp.insertToElement(jsonp._request);
203+
var jsonp = new Jsonp(options);
201204

202-
return jsonp._globalCallback; // from initState(options)
205+
// from initState(options)
206+
return jsonp._globalCallback;
203207
}
204208

205209
return createInstance;

dist/better-jsonp.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)