From cc175aca05998863cd797ad55c662d0ac7bca0a6 Mon Sep 17 00:00:00 2001 From: Ziqiang Li Date: Mon, 7 Aug 2017 20:24:25 +0800 Subject: [PATCH] [feat] promise based jsonp --- .gitignore | 8 +++++++ README.md | 24 ++++++++++++++++++- index.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..94c2417 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Created by .ignore support plugin (hsz.mobi) +.idea/ +.classpath +.project +.settings/ +node_modules +.DS_Store +*.log diff --git a/README.md b/README.md index 466d8f5..ba1b2c8 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ -# simple-jsonp-promise \ No newline at end of file +# simple-jsonp-promise + +## Installation + +``` bash +yarn add simple-jsonp-promise +``` + +## Example + +``` bash +import jsonp from 'simple-jsonp-promise' +let options = { + params:{ + a : 1, + b : 2 + }, + jsonp : 'callback', + prefix : '__jp', + timeout : 15000 + } +let response = await jsonp('https://localhost/api.jsonp' , options); +``` \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..e62e1bd --- /dev/null +++ b/index.js @@ -0,0 +1,65 @@ +/** + * Created by lizq on 2017/8/7 + */ + +// Callback index. +let count = 0; +/** + * JSONP handler + * + * Options: + * - prefix {String} callback prefix (defaults to `__jp`) + * - jsonp {String} qs jsonpeter (defaults to `callback`) + * - timeout {Number} how long after the request until a timeout error + * is emitted (defaults to `15000`) + */ +let jsonp = function( url, options ) { + options = options || {}; + let prefix = options.prefix || '__jp'; + let jsonp = options.jsonp || 'callback'; + let params = options.params || {}; + let timeout = options.timeout ? options.timeout : 15000; + let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head; + let script; + let timer; + let cleanup; + let promise; + let noop = function() {}; + // Generate a unique id for the request. + let id = prefix + (count++); + cleanup = function() { + // Remove the script tag. + if ( script && script.parentNode ) { + script.parentNode.removeChild( script ); + } + window[ id ] = noop; + if ( timer ) { clearTimeout( timer ); } + }; + promise = new Promise( ( resolve, reject ) => { + if ( timeout ) { + timer = setTimeout( function() { + cleanup(); + reject( new Error( 'Timeout' ) ); + }, timeout ); + } + window[ id ] = function( data ) { + cleanup(); + resolve( data ); + }; + params[ jsonp ] = id; + for ( let key in params ) { + url += (~url.indexOf( '?' ) ? '&' : '?') + key + '=' + encodeURIComponent( params[ key ] ); + } + url = url.replace( '?&', '?' ); + // Create script. + script = document.createElement( 'script' ); + script.src = url; + script.onerror = function() { + cleanup(); + reject( new Error( 'Network Error' ) ); + }; + target.parentNode.insertBefore( script, target ); + } ); + return promise; +}; +export default jsonp; diff --git a/package.json b/package.json new file mode 100644 index 0000000..9c50349 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "simple-jsonp-promise", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/liziqiang/simple-jsonp-promise.git" + }, + "keywords": [ + "jsonp", + "promise" + ], + "author": "Ziqiang Li ", + "license": "MIT", + "bugs": { + "url": "https://github.com/liziqiang/simple-jsonp-promise/issues" + }, + "homepage": "https://github.com/liziqiang/simple-jsonp-promise#readme" +}