From dc27282ed7ce45f08afb72e0f89f6e129045aae2 Mon Sep 17 00:00:00 2001 From: Ziqiang Li Date: Mon, 7 Aug 2017 22:29:52 +0800 Subject: [PATCH] [mod] modify --- build/index.js | 84 ++++++++++++++++++++++++++++++++++++++++ package.json | 8 ++-- index.js => src/index.js | 0 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 build/index.js rename index.js => src/index.js (100%) diff --git a/build/index.js b/build/index.js new file mode 100644 index 0000000..b2295ae --- /dev/null +++ b/build/index.js @@ -0,0 +1,84 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Created by lizq on 2017/8/7 + */ + +// Callback index. +var count = 0; + +/** + * JSONP handler + * + * Options: + * - prefix {String} callback prefix (defaults to `__jp`) + * - callback {String} (defaults to `callback`) + * - timeout {Number} how long after the request until a timeout error + * is emitted (defaults to `15000`) + */ +function jsonp(url, options) { + options = options || {}; + var prefix = options.prefix || '__jp'; + var callback = options.callback || 'callback'; + var params = options.data || {}; + var timeout = options.timeout ? options.timeout : 15000; + var target = document.getElementsByTagName('script')[0] || document.head; + var script = void 0; + var timer = void 0; + var promise = void 0; + // Generate a unique id for the request. + var id = prefix + count++; + + function noop() {} + + function cleanup() { + // Remove the script tag. + if (script && script.parentNode) { + script.parentNode.removeChild(script); + } + window[id] = noop; + if (timer) { + clearTimeout(timer); + } + } + + function serialize(params) { + var param = ''; + for (var key in params) { + if (params.hasOwnProperty(key)) { + param += '&' + key + '=' + encodeURIComponent(params[key]); + } + } + return param; + } + + promise = new Promise(function (resolve, reject) { + if (timeout) { + timer = setTimeout(function () { + cleanup(); + reject(new Error('Timeout')); + }, timeout); + } + window[id] = function (data) { + cleanup(); + resolve(data); + }; + params[callback] = id; + url += serialize(params); + 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; +} + +exports.default = jsonp; \ No newline at end of file diff --git a/package.json b/package.json index aba8bb1..f150ca0 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "simple-jsonp-promise", - "version": "1.0.4", + "version": "1.0.5", "description": "", - "main": "index.js", + "main": "build/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "babel index.js --out-dir .", - "prepublish": "yarn run build" + "build": "babel src -d build", + "precommit":"yarn run build" }, "repository": { "type": "git", diff --git a/index.js b/src/index.js similarity index 100% rename from index.js rename to src/index.js