diff --git a/build/index.js b/build/index.js index 823154e..a306dd0 100644 --- a/build/index.js +++ b/build/index.js @@ -46,7 +46,7 @@ function jsonp(url, options) { } function serialize(params) { - var param = '?'; + var param = ''; for (var key in params) { if (params.hasOwnProperty(key)) { param += '&' + key + '=' + encodeURIComponent(params[key]); @@ -55,6 +55,14 @@ function jsonp(url, options) { return param; } + function handleUrl(url, params) { + if (!~url.indexOf('?')) { + url += '?'; + } + url += serialize(params); + url = url.replace('?&', '?'); + } + promise = new Promise(function (resolve, reject) { if (timeout) { timer = setTimeout(function () { @@ -67,8 +75,7 @@ function jsonp(url, options) { resolve(data); }; params[callback] = id; - url += serialize(params); - url = url.replace('?&', '?'); + url = handleUrl(url, params); // Create script. script = document.createElement('script'); script.src = url; diff --git a/package.json b/package.json index b4a56c6..ae3b40c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-jsonp-promise", - "version": "1.0.6", + "version": "1.0.7", "description": "", "main": "build/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index 108e272..d1d00db 100644 --- a/src/index.js +++ b/src/index.js @@ -14,64 +14,71 @@ let count = 0; * - timeout {Number} how long after the request until a timeout error * is emitted (defaults to `15000`) */ -function jsonp( url, options ) { - options = options || {}; - let prefix = options.prefix || '__jp'; +function jsonp(url, options) { + options = options || {}; + let prefix = options.prefix || '__jp'; let callback = options.callback || 'callback'; - let params = options.data || {}; - let timeout = options.timeout ? options.timeout : 15000; - let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head; + let params = options.data || {}; + let timeout = options.timeout ? options.timeout : 15000; + let target = document.getElementsByTagName('script')[0] || document.head; let script; let timer; let promise; // Generate a unique id for the request. - let id = prefix + (count++); + let id = prefix + (count++); function noop() {} function cleanup() { // Remove the script tag. - if ( script && script.parentNode ) { - script.parentNode.removeChild( script ); + if (script && script.parentNode) { + script.parentNode.removeChild(script); + } + window[id] = noop; + if (timer) { + clearTimeout(timer); } - window[ id ] = noop; - if ( timer ) { clearTimeout( timer ); } } - function serialize( params ) { - let param = '?'; - for ( let key in params ) { - if ( params.hasOwnProperty( key ) ) { + function serialize(params) { + let param = ''; + for (let key in params) { + if (params.hasOwnProperty(key)) { param += `&${key}=${encodeURIComponent( params[ key ] )}`; } } return param; } - promise = new Promise( ( resolve, reject ) => { - if ( timeout ) { - timer = setTimeout( () => { + function handleUrl(url, params) { + if (!~url.indexOf('?')) { url += '?'; } + url += serialize(params); + url = url.replace('?&', '?'); + } + + promise = new Promise((resolve, reject) => { + if (timeout) { + timer = setTimeout(() => { cleanup(); - reject( new Error( 'Timeout' ) ); - }, timeout ); + reject(new Error('Timeout')); + }, timeout); } - window[ id ] = function( data ) { + window[id] = function (data) { cleanup(); - resolve( data ); + resolve(data); }; - params[ callback ] = id; - url += serialize( params ); - url = url.replace( '?&', '?' ); + params[callback] = id; + url = handleUrl(url, params); // Create script. - script = document.createElement( 'script' ); - script.src = url; - script.onerror = function() { + script = document.createElement('script'); + script.src = url; + script.onerror = function () { cleanup(); - reject( new Error( 'Network Error' ) ); + reject(new Error('Network Error')); }; - target.parentNode.insertBefore( script, target ); - } ); + target.parentNode.insertBefore(script, target); + }); return promise; } -export default jsonp; +export default jsonp; \ No newline at end of file