better-jsonp
Advanced tools
Comparing version 0.2.16 to 1.0.0
/*! | ||
* better-jsonp v0.2.15 | ||
* better-jsonp v0.2.16 | ||
* Copyrights (c) 2018-2018 Bowen (lbwa) | ||
@@ -46,68 +46,66 @@ * Released under the MIT License. | ||
var JSONP = function () { | ||
function JSONP(options) { | ||
classCallCheck(this, JSONP); | ||
var defaultOptions = { | ||
timeout: 6000, | ||
prefix: 'callback', | ||
callbackParams: 'jsonpCallback', | ||
urlParams: {} | ||
}; | ||
this.checkOptions(options); | ||
this.initState(options); | ||
this.encodeURL(this._options.url); | ||
this.insertToElement(this._url); | ||
// Once invoked window[this._id], it will clean script which is used to | ||
// JSONP from HTML | ||
var Jsonp = function () { | ||
function Jsonp() { | ||
classCallCheck(this, Jsonp); | ||
} | ||
createClass(JSONP, [{ | ||
createClass(Jsonp, [{ | ||
key: 'checkOptions', | ||
value: function checkOptions(options) { | ||
if (!options.url) throw new Error('Please check your request url.'); | ||
if (!options.callback) throw new Error('Please check your callback parameter.'); | ||
// if (!options.callback) throw new Error('Please check your callback parameter.') | ||
this._options = options; | ||
this.options = options; | ||
} | ||
}, { | ||
key: 'initState', | ||
value: function initState(options) { | ||
var _this = this; | ||
defineEnumerable(this, '_timer', null); | ||
defineEnumerable(this, '_url', null); | ||
defineEnumerable(this, '_id', null); | ||
defineEnumerable(this, '_insertScript', null); | ||
defineEnumerable(this, '_target', null); | ||
// period of request without timeout error | ||
var timeout = options.timeout || 6000; | ||
if (options.callbackName) { | ||
this._id = options.callbackName; | ||
defineEnumerable(this, '_callbackName', this._id); | ||
key: 'generateJsonpCallback', | ||
value: function generateJsonpCallback(options) { | ||
if (options.jsonpCallback) { | ||
this._jsonpCallback = options.jsonpCallback; | ||
} else { | ||
// prefix for callback name in global env | ||
var prefix = options.prefix || 'callback'; | ||
var prefix = defaultOptions.prefix; | ||
// unique global callback name in global env | ||
this._id = prefix + Date.now(); | ||
this._jsonpCallback = prefix + Date.now(); | ||
} | ||
} | ||
}, { | ||
key: 'defineGlobalCallback', | ||
value: function defineGlobalCallback() { | ||
var _this = this; | ||
/** | ||
* 1. Once invoked window[this._id], it will clean timer for limiting | ||
* 1. Once invoked window[this._jsonpCallback], it will clean timer for limiting | ||
* request period and script element which is used to JSONP. | ||
* 2. use arrow function to define `this` object value. | ||
* 2. use arrow function to define `this` object value (Jsonp instance). | ||
*/ | ||
window[this._id] = function (data) { | ||
_this.cleanScript(); | ||
_this._options.callback(data); | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
window[_this._jsonpCallback] = function (data) { | ||
_this.cleanScript(); | ||
resolve(data); | ||
}; | ||
}); | ||
} | ||
}, { | ||
key: 'generateTimer', | ||
value: function generateTimer(options) { | ||
var _this2 = this; | ||
// timer is used to limit request period. | ||
// limit request period | ||
var timeout = options.timeout || defaultOptions.timeout; | ||
// use arrow function to define `this` object value. | ||
if (timeout) { | ||
this._timer = setTimeout(function () { | ||
window[_this._id] = noop; | ||
_this._timer = null; | ||
_this.cleanScript(); | ||
window[_this2._jsonpCallback] = noop; | ||
_this2._timer = null; | ||
_this2.cleanScript(); | ||
throw new Error('JSONP request unsuccessfully (eg.timeout or wrong url).'); | ||
@@ -118,2 +116,20 @@ }, timeout); | ||
}, { | ||
key: 'initState', | ||
value: function initState(options) { | ||
defineEnumerable(this, '_timer', null); | ||
defineEnumerable(this, '_request', null); | ||
defineEnumerable(this, '_jsonpCallback', null); | ||
defineEnumerable(this, '_insertScript', null); | ||
defineEnumerable(this, '_target', null); | ||
// set this._jsonpCallback | ||
this.generateJsonpCallback(options); | ||
// invoke defineGlobalCallback after setting this._jsonpCallback | ||
defineEnumerable(this, '_globalCallback', this.defineGlobalCallback()); | ||
// set timer for limit request time | ||
this.generateTimer(options); | ||
} | ||
}, { | ||
key: 'encodeURL', | ||
@@ -123,8 +139,8 @@ value: function encodeURL(url) { | ||
// eg. ?callback=... | ||
var callbackParams = this._options.callbackParams || 'jsonpCallback'; | ||
var id = euc(this._id); | ||
var callbackParams = this.options.callbackParams || defaultOptions.callbackParams; | ||
var id = euc(this._jsonpCallback); | ||
url += '' + (url.indexOf('?') < 0 ? '?' : '&') + callbackParams + '=' + id; | ||
// add other parameter to url excluding callback parameter | ||
var params = this._options.urlParams || {}; | ||
var params = this.options.urlParams || defaultOptions.urlParams; | ||
var keys = Object.keys(params); | ||
@@ -136,3 +152,3 @@ keys.forEach(function (key) { | ||
this._url = url; | ||
this._request = url; | ||
} | ||
@@ -158,3 +174,3 @@ }, { | ||
window[this._id] = noop; | ||
window[this._jsonpCallback] = noop; | ||
@@ -164,5 +180,21 @@ if (this._timer) clearTimeout(this._timer); | ||
}]); | ||
return JSONP; | ||
return Jsonp; | ||
}(); | ||
module.exports = JSONP; | ||
// facade in facade pattern | ||
// same as axios, zepto | ||
function createInstance(options) { | ||
var jsonp = new Jsonp(); | ||
jsonp.checkOptions(options); | ||
jsonp.initState(options); | ||
jsonp.encodeURL(jsonp.options.url); | ||
jsonp.insertToElement(jsonp._request); | ||
return jsonp._globalCallback; // from initState(options) | ||
} | ||
module.exports = createInstance; |
/*! | ||
* better-jsonp v0.2.15 | ||
* better-jsonp v0.2.16 | ||
* Copyrights (c) 2018-2018 Bowen (lbwa) | ||
@@ -9,3 +9,3 @@ * Released under the MIT License. | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.JSONP = factory()); | ||
(global.Jsonp = factory()); | ||
}(this, (function () { 'use strict'; | ||
@@ -51,68 +51,66 @@ | ||
var JSONP = function () { | ||
function JSONP(options) { | ||
classCallCheck(this, JSONP); | ||
var defaultOptions = { | ||
timeout: 6000, | ||
prefix: 'callback', | ||
callbackParams: 'jsonpCallback', | ||
urlParams: {} | ||
}; | ||
this.checkOptions(options); | ||
this.initState(options); | ||
this.encodeURL(this._options.url); | ||
this.insertToElement(this._url); | ||
// Once invoked window[this._id], it will clean script which is used to | ||
// JSONP from HTML | ||
var Jsonp = function () { | ||
function Jsonp() { | ||
classCallCheck(this, Jsonp); | ||
} | ||
createClass(JSONP, [{ | ||
createClass(Jsonp, [{ | ||
key: 'checkOptions', | ||
value: function checkOptions(options) { | ||
if (!options.url) throw new Error('Please check your request url.'); | ||
if (!options.callback) throw new Error('Please check your callback parameter.'); | ||
// if (!options.callback) throw new Error('Please check your callback parameter.') | ||
this._options = options; | ||
this.options = options; | ||
} | ||
}, { | ||
key: 'initState', | ||
value: function initState(options) { | ||
var _this = this; | ||
defineEnumerable(this, '_timer', null); | ||
defineEnumerable(this, '_url', null); | ||
defineEnumerable(this, '_id', null); | ||
defineEnumerable(this, '_insertScript', null); | ||
defineEnumerable(this, '_target', null); | ||
// period of request without timeout error | ||
var timeout = options.timeout || 6000; | ||
if (options.callbackName) { | ||
this._id = options.callbackName; | ||
defineEnumerable(this, '_callbackName', this._id); | ||
key: 'generateJsonpCallback', | ||
value: function generateJsonpCallback(options) { | ||
if (options.jsonpCallback) { | ||
this._jsonpCallback = options.jsonpCallback; | ||
} else { | ||
// prefix for callback name in global env | ||
var prefix = options.prefix || 'callback'; | ||
var prefix = defaultOptions.prefix; | ||
// unique global callback name in global env | ||
this._id = prefix + Date.now(); | ||
this._jsonpCallback = prefix + Date.now(); | ||
} | ||
} | ||
}, { | ||
key: 'defineGlobalCallback', | ||
value: function defineGlobalCallback() { | ||
var _this = this; | ||
/** | ||
* 1. Once invoked window[this._id], it will clean timer for limiting | ||
* 1. Once invoked window[this._jsonpCallback], it will clean timer for limiting | ||
* request period and script element which is used to JSONP. | ||
* 2. use arrow function to define `this` object value. | ||
* 2. use arrow function to define `this` object value (Jsonp instance). | ||
*/ | ||
window[this._id] = function (data) { | ||
_this.cleanScript(); | ||
_this._options.callback(data); | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
window[_this._jsonpCallback] = function (data) { | ||
_this.cleanScript(); | ||
resolve(data); | ||
}; | ||
}); | ||
} | ||
}, { | ||
key: 'generateTimer', | ||
value: function generateTimer(options) { | ||
var _this2 = this; | ||
// timer is used to limit request period. | ||
// limit request period | ||
var timeout = options.timeout || defaultOptions.timeout; | ||
// use arrow function to define `this` object value. | ||
if (timeout) { | ||
this._timer = setTimeout(function () { | ||
window[_this._id] = noop; | ||
_this._timer = null; | ||
_this.cleanScript(); | ||
window[_this2._jsonpCallback] = noop; | ||
_this2._timer = null; | ||
_this2.cleanScript(); | ||
throw new Error('JSONP request unsuccessfully (eg.timeout or wrong url).'); | ||
@@ -123,2 +121,20 @@ }, timeout); | ||
}, { | ||
key: 'initState', | ||
value: function initState(options) { | ||
defineEnumerable(this, '_timer', null); | ||
defineEnumerable(this, '_request', null); | ||
defineEnumerable(this, '_jsonpCallback', null); | ||
defineEnumerable(this, '_insertScript', null); | ||
defineEnumerable(this, '_target', null); | ||
// set this._jsonpCallback | ||
this.generateJsonpCallback(options); | ||
// invoke defineGlobalCallback after setting this._jsonpCallback | ||
defineEnumerable(this, '_globalCallback', this.defineGlobalCallback()); | ||
// set timer for limit request time | ||
this.generateTimer(options); | ||
} | ||
}, { | ||
key: 'encodeURL', | ||
@@ -128,8 +144,8 @@ value: function encodeURL(url) { | ||
// eg. ?callback=... | ||
var callbackParams = this._options.callbackParams || 'jsonpCallback'; | ||
var id = euc(this._id); | ||
var callbackParams = this.options.callbackParams || defaultOptions.callbackParams; | ||
var id = euc(this._jsonpCallback); | ||
url += '' + (url.indexOf('?') < 0 ? '?' : '&') + callbackParams + '=' + id; | ||
// add other parameter to url excluding callback parameter | ||
var params = this._options.urlParams || {}; | ||
var params = this.options.urlParams || defaultOptions.urlParams; | ||
var keys = Object.keys(params); | ||
@@ -141,3 +157,3 @@ keys.forEach(function (key) { | ||
this._url = url; | ||
this._request = url; | ||
} | ||
@@ -163,3 +179,3 @@ }, { | ||
window[this._id] = noop; | ||
window[this._jsonpCallback] = noop; | ||
@@ -169,7 +185,23 @@ if (this._timer) clearTimeout(this._timer); | ||
}]); | ||
return JSONP; | ||
return Jsonp; | ||
}(); | ||
return JSONP; | ||
// facade in facade pattern | ||
// same as axios, zepto | ||
function createInstance(options) { | ||
var jsonp = new Jsonp(); | ||
jsonp.checkOptions(options); | ||
jsonp.initState(options); | ||
jsonp.encodeURL(jsonp.options.url); | ||
jsonp.insertToElement(jsonp._request); | ||
return jsonp._globalCallback; // from initState(options) | ||
} | ||
return createInstance; | ||
}))); |
/*! | ||
* better-jsonp v0.2.15 | ||
* better-jsonp v0.2.16 | ||
* Copyrights (c) 2018-2018 Bowen (lbwa) | ||
* Released under the MIT License. | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.JSONP=e()}(this,function(){"use strict";function t(){}function e(t,e,i){Reflect.defineProperty(t,e,{enumerable:!1,writable:!0,value:i})}function i(t){return encodeURIComponent(t)}var n=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},r=function(){function t(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,i,n){return i&&t(e.prototype,i),n&&t(e,n),e}}();return function(){function o(t){n(this,o),this.checkOptions(t),this.initState(t),this.encodeURL(this._options.url),this.insertToElement(this._url)}return r(o,[{key:"checkOptions",value:function(t){if(!t.url)throw new Error("Please check your request url.");if(!t.callback)throw new Error("Please check your callback parameter.");this._options=t}},{key:"initState",value:function(i){var n=this;e(this,"_timer",null),e(this,"_url",null),e(this,"_id",null),e(this,"_insertScript",null),e(this,"_target",null);var r=i.timeout||6e3;if(i.callbackName)this._id=i.callbackName,e(this,"_callbackName",this._id);else{var o=i.prefix||"callback";this._id=o+Date.now()}window[this._id]=function(t){n.cleanScript(),n._options.callback(t)},r&&(this._timer=setTimeout(function(){throw window[n._id]=t,n._timer=null,n.cleanScript(),new Error("JSONP request unsuccessfully (eg.timeout or wrong url).")},r))}},{key:"encodeURL",value:function(t){var e=this._options.callbackParams||"jsonpCallback",n=i(this._id);t+=(t.indexOf("?")<0?"?":"&")+e+"="+n;var r=this._options.urlParams||{};Object.keys(r).forEach(function(e){var n=void 0!==r[e]?r[e]:"";t+="&"+e+"="+i(n)}),this._url=t}},{key:"insertToElement",value:function(t){this._target=document.getElementsByTagName("script")[0]||document.body.lastElementChild,this._insertScript=document.createElement("script"),this._insertScript.src=t,this._target.parentNode.insertBefore(this._insertScript,this._target)}},{key:"cleanScript",value:function(){this._insertScript.parentNode&&(this._target.parentNode.removeChild(this._insertScript),this._insertScript=null),window[this._id]=t,this._timer&&clearTimeout(this._timer)}}]),o}()}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Jsonp=t()}(this,function(){"use strict";function e(){}function t(e,t,n){Reflect.defineProperty(e,t,{enumerable:!1,writable:!0,value:n})}function n(e){return encodeURIComponent(e)}var i=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},r=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),a={timeout:6e3,prefix:"callback",callbackParams:"jsonpCallback",urlParams:{}},l=function(){function l(){i(this,l)}return r(l,[{key:"checkOptions",value:function(e){if(!e.url)throw new Error("Please check your request url.");this.options=e}},{key:"generateJsonpCallback",value:function(e){if(e.jsonpCallback)this._jsonpCallback=e.jsonpCallback;else{var t=a.prefix;this._jsonpCallback=t+Date.now()}}},{key:"defineGlobalCallback",value:function(){var e=this;return new Promise(function(t,n){window[e._jsonpCallback]=function(n){e.cleanScript(),t(n)}})}},{key:"generateTimer",value:function(t){var n=this,i=t.timeout||a.timeout;i&&(this._timer=setTimeout(function(){throw window[n._jsonpCallback]=e,n._timer=null,n.cleanScript(),new Error("JSONP request unsuccessfully (eg.timeout or wrong url).")},i))}},{key:"initState",value:function(e){t(this,"_timer",null),t(this,"_request",null),t(this,"_jsonpCallback",null),t(this,"_insertScript",null),t(this,"_target",null),this.generateJsonpCallback(e),t(this,"_globalCallback",this.defineGlobalCallback()),this.generateTimer(e)}},{key:"encodeURL",value:function(e){var t=this.options.callbackParams||a.callbackParams,i=n(this._jsonpCallback);e+=(e.indexOf("?")<0?"?":"&")+t+"="+i;var r=this.options.urlParams||a.urlParams;Object.keys(r).forEach(function(t){var i=void 0!==r[t]?r[t]:"";e+="&"+t+"="+n(i)}),this._request=e}},{key:"insertToElement",value:function(e){this._target=document.getElementsByTagName("script")[0]||document.body.lastElementChild,this._insertScript=document.createElement("script"),this._insertScript.src=e,this._target.parentNode.insertBefore(this._insertScript,this._target)}},{key:"cleanScript",value:function(){this._insertScript.parentNode&&(this._target.parentNode.removeChild(this._insertScript),this._insertScript=null),window[this._jsonpCallback]=e,this._timer&&clearTimeout(this._timer)}}]),l}();return function(e){var t=new l;return t.checkOptions(e),t.initState(e),t.encodeURL(t.options.url),t.insertToElement(t._request),t._globalCallback}}); |
@@ -13,2 +13,2 @@ export function noop () {} | ||
return encodeURIComponent(value) | ||
} | ||
} |
{ | ||
"name": "better-jsonp", | ||
"version": "0.2.16", | ||
"version": "1.0.0", | ||
"description": "A simple JSONP implementation", | ||
"main": "dist/better-jsonp.esm.js", | ||
"main": "dist/better-jsonp.common.js", | ||
"unpkg": "dist/better-jsonp.js", | ||
@@ -14,8 +14,11 @@ "jsdelivr": "dist/better-jsonp.js", | ||
"dev": "node samples/server.js", | ||
"build": "yarn run build:prod && yarn run build:dev && yarn run build:common", | ||
"build": "yarn run cleaner && yarn run build:prod && yarn run build:dev && yarn run build:common", | ||
"build:prod": "./node_modules/.bin/rollup -c scripts/rollup.config.js --environment TARGET:production", | ||
"build:dev": "./node_modules/.bin/rollup -c scripts/rollup.config.js --environment TARGET:development", | ||
"build:common": "./node_modules/.bin/rollup -c scripts/rollup.config.js --environment TARGET:cjs", | ||
"test": "echo 'Under construction'", | ||
"lint": "./node_modules/.bin/eslint --fix --ext .js lib/ samples/ scripts/", | ||
"release": "sh publish.sh", | ||
"cleaner": "rm -v dist/*.js" | ||
"cleaner": "rm -vrf dist/*", | ||
"commit": "git add . && git-cz" | ||
}, | ||
@@ -42,9 +45,22 @@ "repository": { | ||
"babel-preset-env": "^1.7.0", | ||
"chalk": "^2.4.1", | ||
"commitizen": "^2.10.1", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-friendly-formatter": "^4.0.1", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.8.0", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"rollup": "^0.60.7", | ||
"rollup-plugin-alias": "^1.4.0", | ||
"rollup-plugin-babel": "^3.0.4", | ||
"rollup-plugin-replace": "^2.0.0", | ||
"rollup-plugin-terser": "^1.0.1", | ||
"signale": "^1.2.0" | ||
"rollup-plugin-terser": "^1.0.1" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
} | ||
} |
@@ -1,14 +0,27 @@ | ||
# JSONP | ||
# Better JSONP [![npm](https://img.shields.io/npm/v/better-jsonp.svg)](https://www.npmjs.com/package/better-jsonp) [![Build Status](https://travis-ci.org/lbwa/jsonp.svg?branch=master)](https://travis-ci.org/lbwa/jsonp) | ||
[![npm](https://img.shields.io/npm/v/better-jsonp.svg)](https://www.npmjs.com/package/better-jsonp) | ||
A minimal JSONP implementation which is used to be a kind of cross domain solution. | ||
## Features | ||
- Implement JSONP request from the browser | ||
- Support the [Promise] API | ||
[Promise]:https://promisesaplus.com/ | ||
## Install | ||
```bash | ||
# using npm | ||
npm i better-jsonp | ||
# yarn add better-jsonp | ||
``` | ||
```bash | ||
# using yarn | ||
yarn add better-jsonp | ||
``` | ||
```html | ||
<!-- using CDN --> | ||
<script src="https://cdn.jsdelivr.net/npm/better-jsonp"></script> | ||
``` | ||
@@ -18,7 +31,6 @@ ## Usage | ||
```js | ||
import JSONP from 'jsonp' | ||
const jsonp = new JSONP({ | ||
jsonp({ | ||
url: 'http://localhost', | ||
prefix: 'customName', | ||
// global function named `${jsonpCallback}` will invoked when JSONP response | ||
jsonpCallback: 'jsonp', | ||
timeout: 5000, | ||
@@ -31,6 +43,6 @@ // eg. ?customCallbackParams=... | ||
key1: 1 | ||
// ... | ||
}, | ||
callback: data => console.log(data) | ||
} | ||
}) | ||
.then(res => console.log(res)) | ||
.catch(err => console.error(err)) | ||
``` | ||
@@ -40,8 +52,6 @@ | ||
| ----------------- | ---- | -------- | ----------- | | ||
| `url` | `String` | true | request url | | ||
| `url` | `String` | true | JSONP request address | | ||
| `timeout` | `Number` | false, default : `6000` | how long after timeout error is emitted. `0` to disable | | ||
| `prefix` | `String` | false, default: `callback` | prefix of global callback function name which is used to handle JSONP response | | ||
| `callbackName` | `String` | false, default : `prefix` + `Date.now()` | global callback function name which is used to handle JSONP response | | ||
| `jsonpCallback` | `String` | false, default : `'callback'+'Date.now()'` | global callback function name which is used to handle JSONP response. | | ||
| `callbackParams` | `String` | false, default: `jsonpCallback` | name of query parameter to specify the callback name | | ||
| `urlParams` | `Object` | false, default: `{}` | other parameters in query string parameters | | ||
| `callback` | `Function` | true | This callback which is in global callback function will be invoked when JSONP response | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22649
8
465
0
55
17
1