[refactor] bump version 1.0.2
parent
10ae6427ec
commit
68077ec0eb
32
index.js
32
index.js
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
// Callback index.
|
// Callback index.
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSONP handler
|
* JSONP handler
|
||||||
*
|
*
|
||||||
|
@ -13,7 +14,7 @@ let count = 0;
|
||||||
* - timeout {Number} how long after the request until a timeout error
|
* - timeout {Number} how long after the request until a timeout error
|
||||||
* is emitted (defaults to `15000`)
|
* is emitted (defaults to `15000`)
|
||||||
*/
|
*/
|
||||||
let jsonp = function( url, options ) {
|
function jsonp( url, options ) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
let prefix = options.prefix || '__jp';
|
let prefix = options.prefix || '__jp';
|
||||||
let callback = options.callback || 'callback';
|
let callback = options.callback || 'callback';
|
||||||
|
@ -22,22 +23,34 @@ let jsonp = function( url, options ) {
|
||||||
let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head;
|
let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head;
|
||||||
let script;
|
let script;
|
||||||
let timer;
|
let timer;
|
||||||
let cleanup;
|
|
||||||
let promise;
|
let promise;
|
||||||
let noop = function() {};
|
|
||||||
// Generate a unique id for the request.
|
// Generate a unique id for the request.
|
||||||
let id = prefix + (count++);
|
let id = prefix + (count++);
|
||||||
cleanup = function() {
|
|
||||||
|
function noop() {}
|
||||||
|
|
||||||
|
function cleanup() {
|
||||||
// Remove the script tag.
|
// Remove the script tag.
|
||||||
if ( script && script.parentNode ) {
|
if ( script && script.parentNode ) {
|
||||||
script.parentNode.removeChild( script );
|
script.parentNode.removeChild( script );
|
||||||
}
|
}
|
||||||
window[ id ] = noop;
|
window[ id ] = noop;
|
||||||
if ( timer ) { clearTimeout( timer ); }
|
if ( timer ) { clearTimeout( timer ); }
|
||||||
};
|
}
|
||||||
|
|
||||||
|
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 ) => {
|
promise = new Promise( ( resolve, reject ) => {
|
||||||
if ( timeout ) {
|
if ( timeout ) {
|
||||||
timer = setTimeout( function() {
|
timer = setTimeout( () => {
|
||||||
cleanup();
|
cleanup();
|
||||||
reject( new Error( 'Timeout' ) );
|
reject( new Error( 'Timeout' ) );
|
||||||
}, timeout );
|
}, timeout );
|
||||||
|
@ -47,9 +60,7 @@ let jsonp = function( url, options ) {
|
||||||
resolve( data );
|
resolve( data );
|
||||||
};
|
};
|
||||||
params[ callback ] = id;
|
params[ callback ] = id;
|
||||||
for ( let key in params ) {
|
url += serialize( params );
|
||||||
url += (~url.indexOf( '?' ) ? '&' : '?') + key + '=' + encodeURIComponent( params[ key ] );
|
|
||||||
}
|
|
||||||
url = url.replace( '?&', '?' );
|
url = url.replace( '?&', '?' );
|
||||||
// Create script.
|
// Create script.
|
||||||
script = document.createElement( 'script' );
|
script = document.createElement( 'script' );
|
||||||
|
@ -61,5 +72,6 @@ let jsonp = function( url, options ) {
|
||||||
target.parentNode.insertBefore( script, target );
|
target.parentNode.insertBefore( script, target );
|
||||||
} );
|
} );
|
||||||
return promise;
|
return promise;
|
||||||
};
|
}
|
||||||
|
|
||||||
export default jsonp;
|
export default jsonp;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "simple-jsonp-promise",
|
"name": "simple-jsonp-promise",
|
||||||
"version": "1.0.1",
|
"version": "1.0.2",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
Loading…
Reference in New Issue