1
1
import { options } from '../utils/types'
2
- import { noop , euc } from '../utils/index'
2
+ import { noop } from '../utils/index'
3
3
4
4
const PREFIX = 'callback'
5
5
@@ -10,9 +10,9 @@ export default class Jsonp {
10
10
private _jsonpCallback : string // response handler
11
11
private _reference : Element // reference element
12
12
private _script : HTMLScriptElement | null // trigger element
13
- private _timer : number // timer ID
13
+ private _timer : number
14
14
15
- constructor ( {
15
+ constructor ( {
16
16
url,
17
17
timeout = 6000 ,
18
18
jsonpCallback = `${ PREFIX } ${ Date . now ( ) } ` ,
@@ -23,32 +23,32 @@ export default class Jsonp {
23
23
url,
24
24
jsonpCallback
25
25
} )
26
- this . initState ( {
27
- timeout,
28
- jsonpCallback
29
- } )
30
26
this . encodeURL ( {
31
27
url,
32
28
callbackParams,
33
29
urlParams
34
30
} )
31
+ this . initState ( {
32
+ timeout,
33
+ jsonpCallback
34
+ } )
35
35
this . insert ( this . _url )
36
36
}
37
37
38
- checkOptions ( {
39
- url,
40
- jsonpCallback
41
- } : options ) {
38
+ checkOptions ( { url, jsonpCallback } : options ) {
42
39
if ( ! url ) throw new Error ( 'Please check your request url.' )
43
40
44
41
// Every jsonp request will reset global request function named value of
45
42
// jsonpCallback, so this value MUST NOT be `jsonp`.
46
43
47
44
// This checking only works in CDN installing, not as a dependency using
48
- if ( jsonpCallback === 'jsonp' ) throw new Error ( 'Don\'t name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value' )
45
+ if ( jsonpCallback === 'jsonp' )
46
+ throw new Error (
47
+ "Don't name jsonpCallback to `jsonp` for unexpected reset. Please use any non-jsonp value"
48
+ )
49
49
}
50
50
51
- initState ( {
51
+ initState ( {
52
52
timeout,
53
53
jsonpCallback
54
54
} : {
@@ -67,79 +67,86 @@ export default class Jsonp {
67
67
this . createTimer ( timeout )
68
68
}
69
69
70
- createScript ( ) {
71
- this . _reference = document . getElementsByTagName ( 'script' ) [ 0 ]
72
- || document . body . lastElementChild
70
+ createScript ( ) {
71
+ this . _reference =
72
+ document . getElementsByTagName ( 'script' ) [ 0 ] ||
73
+ document . body . lastElementChild
73
74
this . _script = document . createElement ( 'script' )
74
75
}
75
76
76
77
/**
77
78
* 1. Request timer will be cleaned when response handler invoked.
78
79
* 2. use arrow function to keep `this` keywords value (Jsonp instance).
79
80
*/
80
- createHandler ( ) {
81
- return new Promise ( ( resolve , reject ) => {
81
+ createHandler ( ) {
82
+ return new Promise < object > ( ( resolve , reject ) => {
82
83
// handle 404/500 in response
83
84
this . _script . onerror = ( ) => {
84
85
this . cleanScript ( )
85
- reject ( new Error ( `Countdown has been clear! JSONP request unsuccessfully due to 404/500` ) )
86
+ // clear timer
87
+ if ( this . _timer ) {
88
+ window . clearTimeout ( this . _timer )
89
+ this . _timer = null
90
+ }
91
+ reject (
92
+ new Error (
93
+ `[SCRIPT ONERROR]: JSONP request unsuccessfully due to 404/500`
94
+ )
95
+ )
86
96
}
87
-
88
- ( < any > window ) [ this . _jsonpCallback ] = ( data : object ) => {
97
+ ; ( window as any ) [ this . _jsonpCallback ] = ( data : object ) => {
89
98
this . cleanScript ( )
90
99
resolve ( data )
91
100
}
92
101
} )
93
102
}
94
103
95
104
// create a request timer for limiting request period
96
- createTimer ( timeout : options [ 'timeout' ] ) {
105
+ createTimer ( timeout : options [ 'timeout' ] ) {
97
106
// It can be disable when param timeout equal falsy value (0, null etc.)
98
107
if ( timeout ) {
99
- this . _timer = window . setTimeout ( ( ) => {
100
- ( < any > window ) [ this . _jsonpCallback ] = noop
101
- this . _timer = null
108
+ const id = window . setTimeout ( ( ) => {
109
+ ; ( window as any ) [ this . _jsonpCallback ] = noop
102
110
this . cleanScript ( )
103
- throw new Error ( 'JSONP request unsuccessfully (eg.timeout or wrong url).' )
111
+ this . _timer = null
112
+ throw new Error (
113
+ '[TIMEOUT]: JSONP request unsuccessfully (eg.timeout or wrong url).'
114
+ )
104
115
} , timeout )
116
+ this . _timer = id
105
117
}
106
118
}
107
119
108
- encodeURL ( {
109
- url,
110
- callbackParams,
111
- urlParams
112
- } : options ) {
120
+ encodeURL ( { url, callbackParams, urlParams } : options ) {
113
121
// name of query parameter to specify the callback name
114
122
// eg. ?callback=...
115
- const id = euc ( this . _jsonpCallback )
123
+ const id = encodeURIComponent ( this . _jsonpCallback )
116
124
url += `${ url . indexOf ( '?' ) < 0 ? '?' : '&' } ${ callbackParams } =${ id } `
117
125
118
126
// add other parameters to url ending excluding callback name parameter
119
127
const keys = Object . keys ( urlParams )
120
128
keys . forEach ( key => {
121
129
const value = urlParams [ key ] !== undefined ? urlParams [ key ] : ''
122
- url += `&${ key } =${ euc ( value ) } `
130
+ url += `&${ key } =${ encodeURIComponent ( value ) } `
123
131
} )
124
132
125
133
// converted request url
126
134
this . _url = url
127
135
}
128
136
129
137
// activate JSONP
130
- insert ( url : string ) {
138
+ insert ( url : string ) {
131
139
this . _script . src = url
132
140
this . _reference . parentNode . insertBefore ( this . _script , this . _reference )
133
141
}
134
142
135
- cleanScript ( ) {
136
- if ( this . _script . parentNode ) {
143
+ cleanScript ( ) {
144
+ if ( this . _script && this . _script . parentNode ) {
137
145
this . _script . parentNode . removeChild ( this . _script )
138
146
this . _script = null
139
147
}
140
148
141
149
// reset response handler
142
- ( < any > window ) [ this . _jsonpCallback ] = noop
143
- if ( this . _timer ) window . clearTimeout ( this . _timer )
150
+ ; ( window as any ) [ this . _jsonpCallback ] = noop
144
151
}
145
152
}
0 commit comments