[mod] modify
This commit is contained in:
parent
7d225c0b37
commit
dc27282ed7
3 changed files with 88 additions and 4 deletions
84
build/index.js
Normal file
84
build/index.js
Normal file
|
@ -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;
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "simple-jsonp-promise",
|
"name": "simple-jsonp-promise",
|
||||||
"version": "1.0.4",
|
"version": "1.0.5",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "build/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build": "babel index.js --out-dir .",
|
"build": "babel src -d build",
|
||||||
"prepublish": "yarn run build"
|
"precommit":"yarn run build"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
Loading…
Add table
Reference in a new issue