Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-resource

Package Overview
Dependencies
Maintainers
2
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-resource - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

src/promise.js

2

bower.json

@@ -5,3 +5,3 @@ {

"description": "A web request service for Vue.js",
"version": "0.1.2",
"version": "0.1.3",
"homepage": "https://github.com/vuejs/vue-resource",

@@ -8,0 +8,0 @@ "license": "MIT",

@@ -54,3 +54,3 @@ /******/ (function(modules) { // webpackBootstrap

Vue.http = __webpack_require__(3)(Vue);
Vue.resource = __webpack_require__(4)(Vue);
Vue.resource = __webpack_require__(5)(Vue);
}

@@ -310,62 +310,2 @@

/**
* Promise polyfill (https://gist.github.com/briancavalier/814313)
*/
_.Promise = window.Promise;
if (!_.Promise) {
_.Promise = function (executor) {
executor(this.resolve.bind(this), this.reject.bind(this));
this._thens = [];
};
_.Promise.prototype = {
then: function (onResolve, onReject, onProgress) {
this._thens.push({resolve: onResolve, reject: onReject, progress: onProgress});
},
'catch': function (onReject) {
this._thens.push({reject: onReject});
},
resolve: function (value) {
this._complete('resolve', value);
},
reject: function (reason) {
this._complete('reject', reason);
},
progress: function (status) {
var i = 0, aThen;
while (aThen = this._thens[i++]) {
aThen.progress && aThen.progress(status);
}
},
_complete: function (which, arg) {
this.then = which === 'resolve' ?
function (resolve, reject) { resolve && resolve(arg); } :
function (resolve, reject) { reject && reject(arg); };
this.resolve = this.reject = this.progress =
function () { throw new Error('Promise already completed.'); };
var aThen, i = 0;
while (aThen = this._thens[i++]) {
aThen[which] && aThen[which](arg);
}
delete this._thens;
}
};
}
return _;

@@ -382,2 +322,3 @@ };

var _ = __webpack_require__(2)(Vue);
var Promise = __webpack_require__(4);
var jsonType = { 'Content-Type': 'application/json;charset=utf-8' };

@@ -409,8 +350,4 @@

if (_.isObject(options.data) && /FormData/i.test(options.data.toString())) {
delete options.headers['Content-Type'];
}
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options);
promise = new _.Promise((options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).bind(this, (this.$url || Vue.url), options));
_.extend(promise, {

@@ -460,3 +397,3 @@

function xhr(url, options, resolve, reject) {
function xhr(url, options) {

@@ -476,5 +413,9 @@ var request = new XMLHttpRequest();

options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.data = Vue.url.params(options.data);
options.data = url.params(options.data);
}
if (_.isObject(options.data) && /FormData/i.test(options.data.toString())) {
delete options.headers['Content-Type'];
}
if (_.isPlainObject(options.data)) {

@@ -484,24 +425,37 @@ options.data = JSON.stringify(options.data);

request.open(options.method, url(options), true);
var promise = new Promise(function (resolve, reject) {
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
request.open(options.method, url(options), true);
request.onreadystatechange = function () {
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
if (this.readyState === 4) {
request.onreadystatechange = function () {
if (this.status >= 200 && this.status < 300) {
resolve(this);
} else {
reject(this);
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 300) {
resolve(this);
} else {
reject(this);
}
}
};
request.send(options.data);
});
_.extend(promise, {
abort: function () {
request.abort();
}
};
request.send(options.data);
});
return promise;
}
function jsonp(url, options, resolve, reject) {
function jsonp(url, options) {

@@ -517,29 +471,34 @@ var callback = '_jsonp' + Math.random().toString(36).substr(2), script, result;

script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
var promise = new Promise(function (resolve, reject) {
window[callback] = function (data) {
result = data;
};
script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
var handler = function (event) {
window[callback] = function (data) {
result = data;
};
delete window[callback];
document.body.removeChild(script);
var handler = function (event) {
if (event.type === 'load' && !result) {
event.type = 'error';
}
delete window[callback];
document.body.removeChild(script);
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
if (event.type === 'load' && !result) {
event.type = 'error';
}
(status === 200 ? resolve : reject)({ responseText: text, status: status });
};
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
script.onload = handler;
script.onerror = handler;
(status === 200 ? resolve : reject)({ responseText: text, status: status });
};
document.body.appendChild(script);
script.onload = handler;
script.onerror = handler;
document.body.appendChild(script);
});
return promise;
}

@@ -611,2 +570,64 @@

/**
* Promise polyfill (https://gist.github.com/briancavalier/814313)
*/
function Promise (executor) {
executor(this.resolve.bind(this), this.reject.bind(this));
this._thens = [];
}
Promise.prototype = {
then: function (onResolve, onReject, onProgress) {
this._thens.push({resolve: onResolve, reject: onReject, progress: onProgress});
},
'catch': function (onReject) {
this._thens.push({reject: onReject});
},
resolve: function (value) {
this._complete('resolve', value);
},
reject: function (reason) {
this._complete('reject', reason);
},
progress: function (status) {
var i = 0, aThen;
while (aThen = this._thens[i++]) {
aThen.progress && aThen.progress(status);
}
},
_complete: function (which, arg) {
this.then = which === 'resolve' ?
function (resolve, reject) { resolve && resolve(arg); } :
function (resolve, reject) { reject && reject(arg); };
this.resolve = this.reject = this.progress =
function () { throw new Error('Promise already completed.'); };
var aThen, i = 0;
while (aThen = this._thens[i++]) {
aThen[which] && aThen[which](arg);
}
delete this._thens;
}
};
module.exports = window.Promise ? window.Promise : Promise;
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
module.exports = function (Vue) {

@@ -613,0 +634,0 @@

@@ -1,1 +0,1 @@

!function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){function r(e){e.url=n(4)(e),e.http=n(2)(e),e.resource=n(3)(e)}window.Vue&&Vue.use(r),e.exports=r},function(e,t,n){e.exports=function(e){function t(e,r,o){for(var s in r)o&&(n.isPlainObject(r[s])||n.isArray(r[s]))?(n.isPlainObject(r[s])&&!n.isPlainObject(e[s])&&(e[s]={}),n.isArray(r[s])&&!n.isArray(e[s])&&(e[s]=[]),t(e[s],r[s],o)):void 0!==r[s]&&(e[s]=r[s])}var n=e.util.extend({},e.util);return n.options=function(e,t,r){var o=t.$options||{};return n.extend({},o[e],r)},n.each=function(e,t){var r,o;if("number"==typeof e.length)for(r=0;r<e.length;r++)t.call(e[r],e[r],r);else if(n.isObject(e))for(o in e)e.hasOwnProperty(o)&&t.call(e[o],e[o],o);return e},n.extend=function(e){var n,r=[],o=r.slice.call(arguments,1);return"boolean"==typeof e&&(n=e,e=o.shift()),o.forEach(function(r){t(e,r,n)}),e},n.isFunction=function(e){return e&&"function"==typeof e},n.Promise=window.Promise,n.Promise||(n.Promise=function(e){e(this.resolve.bind(this),this.reject.bind(this)),this._thens=[]},n.Promise.prototype={then:function(e,t,n){this._thens.push({resolve:e,reject:t,progress:n})},"catch":function(e){this._thens.push({reject:e})},resolve:function(e){this._complete("resolve",e)},reject:function(e){this._complete("reject",e)},progress:function(e){for(var t,n=0;t=this._thens[n++];)t.progress&&t.progress(e)},_complete:function(e,t){this.then="resolve"===e?function(e,n){e&&e(t)}:function(e,n){n&&n(t)},this.resolve=this.reject=this.progress=function(){throw new Error("Promise already completed.")};for(var n,r=0;n=this._thens[r++];)n[e]&&n[e](t);delete this._thens}}),n}},function(e,t,n){e.exports=function(e){function t(n,a){var c,u,p=this;return a=a||{},i.isPlainObject(n)&&(a=n,n=""),c=i.extend({},t.headers.common,t.headers[a.method.toLowerCase()]),a=i.extend(!0,{url:n,headers:c},t.options,i.options("http",this,a)),i.isObject(a.data)&&/FormData/i.test(a.data.toString())&&delete a.headers["Content-Type"],u=new i.Promise(("jsonp"==a.method.toLowerCase()?o:r).bind(this,this.$url||e.url,a)),i.extend(u,{success:function(e){return this.then(function(t){e.apply(p,s(t))},function(){}),this},error:function(e){return this["catch"](function(t){e.apply(p,s(t))}),this},always:function(e){var t=function(t){e.apply(p,s(t))};return this.then(t,t),this}}),a.success&&u.success(a.success),a.error&&u.error(a.error),u}function r(t,n,r,o){var s=new XMLHttpRequest;i.isFunction(n.beforeSend)&&n.beforeSend(s,n),n.emulateHTTP&&/^(PUT|PATCH|DELETE)$/i.test(n.method)&&(n.headers["X-HTTP-Method-Override"]=n.method,n.method="POST"),n.emulateJSON&&i.isPlainObject(n.data)&&(n.headers["Content-Type"]="application/x-www-form-urlencoded",n.data=e.url.params(n.data)),i.isPlainObject(n.data)&&(n.data=JSON.stringify(n.data)),s.open(n.method,t(n),!0),i.each(n.headers,function(e,t){s.setRequestHeader(t,e)}),s.onreadystatechange=function(){4===this.readyState&&(this.status>=200&&this.status<300?r(this):o(this))},s.send(n.data)}function o(e,t,n,r){var o,s,a="_jsonp"+Math.random().toString(36).substr(2);i.extend(t.params,t.data),t.params[t.jsonp]=a,i.isFunction(t.beforeSend)&&t.beforeSend({},t),o=document.createElement("script"),o.src=e(t.url,t.params),o.type="text/javascript",o.async=!0,window[a]=function(e){s=e};var c=function(e){delete window[a],document.body.removeChild(o),"load"!==e.type||s||(e.type="error");var t=s?s:e.type,i="error"===e.type?404:200;(200===i?n:r)({responseText:t,status:i})};o.onload=c,o.onerror=c,document.body.appendChild(o)}function s(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e.status,e]}var i=n(1)(e),a={"Content-Type":"application/json;charset=utf-8"};return t.options={method:"GET",params:{},data:"",jsonp:"callback",beforeSend:null,emulateHTTP:!1,emulateJSON:!1},t.headers={put:a,post:a,patch:a,"delete":a,common:{Accept:"application/json, text/plain, */*","X-Requested-With":"XMLHttpRequest"}},["get","put","post","patch","delete","jsonp"].forEach(function(e){t[e]=function(t,n,r,o){return i.isFunction(n)&&(o=r,r=n,n=void 0),this(t,i.extend({method:e,data:n,success:r},o))}}),Object.defineProperty(e.prototype,"$http",{get:function(){return i.extend(t.bind(this),t)}}),t}},function(e,t,n){e.exports=function(e){function t(n,s,i){var a=this,c={};return i=o.extend({},t.actions,i),o.each(i,function(t,i){t=o.extend(!0,{url:n,params:s||{}},t),c[i]=function(){return(a.$http||e.http)(r(t,arguments))}}),c}function r(e,t){var n,r,s,i=o.extend({},e),a={};switch(t.length){case 4:s=t[3],r=t[2];case 3:case 2:if(!o.isFunction(t[1])){a=t[0],n=t[1],r=t[2];break}if(o.isFunction(t[0])){r=t[0],s=t[1];break}r=t[1],s=t[2];case 1:o.isFunction(t[0])?r=t[0]:/^(POST|PUT|PATCH)$/i.test(i.method)?n=t[0]:a=t[0];break;case 0:break;default:throw"Expected up to 4 arguments [params, data, success, error], got "+t.length+" arguments"}return i.url=e.url,i.data=n,i.params=o.extend({},e.params,a),r&&(i.success=r),s&&(i.error=s),i}var o=n(1)(e);return t.actions={get:{method:"GET"},save:{method:"POST"},query:{method:"GET"},remove:{method:"DELETE"},"delete":{method:"DELETE"}},Object.defineProperty(e.prototype,"$resource",{get:function(){return t.bind(this)}}),t}},function(e,t,n){e.exports=function(e){function t(e,n){var r,s={},a={},c=e;return i.isPlainObject(c)||(c={url:e,params:n}),c=i.extend({},t.options,i.options("url",this,c)),e=c.url.replace(/:([a-z]\w*)/gi,function(e,t){return c.params[t]?(s[t]=!0,o(c.params[t])):""}),!e.match(/^(https?:)?\//)&&c.root&&(e=c.root+"/"+e),e=e.replace(/([^:])[\/]{2,}/g,"$1/"),e=e.replace(/(\w+)\/+$/,"$1"),i.each(c.params,function(e,t){s[t]||(a[t]=e)}),r=t.params(a),r&&(e+=(-1==e.indexOf("?")?"?":"&")+r),e}function r(e,t,n){var o,s=i.isArray(t),a=i.isPlainObject(t);i.each(t,function(t,c){o=i.isObject(t)||i.isArray(t),n&&(c=n+"["+(a||o?c:"")+"]"),!n&&s?e.add(t.name,t.value):o?r(e,t,c):e.add(c,t)})}function o(e){return s(e,!0).replace(/%26/gi,"&").replace(/%3D/gi,"=").replace(/%2B/gi,"+")}function s(e,t){return encodeURIComponent(e).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,t?"%20":"+")}var i=n(1)(e);return t.options={url:"",root:"",params:{}},t.params=function(e){var t=[];return t.add=function(e,t){i.isFunction(t)&&(t=t()),null===t&&(t=""),this.push(o(e)+"="+o(t))},r(t,e),t.join("&")},t.parse=function(e){var t=new RegExp("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?"),n=e.match(t);return{url:e,scheme:n[1]||"",host:n[2]||"",path:n[3]||"",query:n[4]||"",fragment:n[5]||""}},Object.defineProperty(e.prototype,"$url",{get:function(){return i.extend(t.bind(this),t)}}),t}}]);
!function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){function r(e){e.url=n(5)(e),e.http=n(2)(e),e.resource=n(4)(e)}window.Vue&&Vue.use(r),e.exports=r},function(e,t,n){e.exports=function(e){function t(e,r,o){for(var s in r)o&&(n.isPlainObject(r[s])||n.isArray(r[s]))?(n.isPlainObject(r[s])&&!n.isPlainObject(e[s])&&(e[s]={}),n.isArray(r[s])&&!n.isArray(e[s])&&(e[s]=[]),t(e[s],r[s],o)):void 0!==r[s]&&(e[s]=r[s])}var n=e.util.extend({},e.util);return n.options=function(e,t,r){var o=t.$options||{};return n.extend({},o[e],r)},n.each=function(e,t){var r,o;if("number"==typeof e.length)for(r=0;r<e.length;r++)t.call(e[r],e[r],r);else if(n.isObject(e))for(o in e)e.hasOwnProperty(o)&&t.call(e[o],e[o],o);return e},n.extend=function(e){var n,r=[],o=r.slice.call(arguments,1);return"boolean"==typeof e&&(n=e,e=o.shift()),o.forEach(function(r){t(e,r,n)}),e},n.isFunction=function(e){return e&&"function"==typeof e},n}},function(e,t,n){e.exports=function(e){function t(n,i){var c,u,p=this;return i=i||{},a.isPlainObject(n)&&(i=n,n=""),c=a.extend({},t.headers.common,t.headers[i.method.toLowerCase()]),i=a.extend(!0,{url:n,headers:c},t.options,a.options("http",this,i)),u=("jsonp"==i.method.toLowerCase()?o:r).call(this,this.$url||e.url,i),a.extend(u,{success:function(e){return this.then(function(t){e.apply(p,s(t))},function(){}),this},error:function(e){return this["catch"](function(t){e.apply(p,s(t))}),this},always:function(e){var t=function(t){e.apply(p,s(t))};return this.then(t,t),this}}),i.success&&u.success(i.success),i.error&&u.error(i.error),u}function r(e,t){var n=new XMLHttpRequest;a.isFunction(t.beforeSend)&&t.beforeSend(n,t),t.emulateHTTP&&/^(PUT|PATCH|DELETE)$/i.test(t.method)&&(t.headers["X-HTTP-Method-Override"]=t.method,t.method="POST"),t.emulateJSON&&a.isPlainObject(t.data)&&(t.headers["Content-Type"]="application/x-www-form-urlencoded",t.data=e.params(t.data)),a.isObject(t.data)&&/FormData/i.test(t.data.toString())&&delete t.headers["Content-Type"],a.isPlainObject(t.data)&&(t.data=JSON.stringify(t.data));var r=new i(function(r,o){n.open(t.method,e(t),!0),a.each(t.headers,function(e,t){n.setRequestHeader(t,e)}),n.onreadystatechange=function(){4===this.readyState&&(this.status>=200&&this.status<300?r(this):o(this))},n.send(t.data)});return a.extend(r,{abort:function(){n.abort()}}),r}function o(e,t){var n,r,o="_jsonp"+Math.random().toString(36).substr(2);a.extend(t.params,t.data),t.params[t.jsonp]=o,a.isFunction(t.beforeSend)&&t.beforeSend({},t);var s=new i(function(s,a){n=document.createElement("script"),n.src=e(t.url,t.params),n.type="text/javascript",n.async=!0,window[o]=function(e){r=e};var i=function(e){delete window[o],document.body.removeChild(n),"load"!==e.type||r||(e.type="error");var t=r?r:e.type,i="error"===e.type?404:200;(200===i?s:a)({responseText:t,status:i})};n.onload=i,n.onerror=i,document.body.appendChild(n)});return s}function s(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e.status,e]}var a=n(1)(e),i=n(3),c={"Content-Type":"application/json;charset=utf-8"};return t.options={method:"GET",params:{},data:"",jsonp:"callback",beforeSend:null,emulateHTTP:!1,emulateJSON:!1},t.headers={put:c,post:c,patch:c,"delete":c,common:{Accept:"application/json, text/plain, */*","X-Requested-With":"XMLHttpRequest"}},["get","put","post","patch","delete","jsonp"].forEach(function(e){t[e]=function(t,n,r,o){return a.isFunction(n)&&(o=r,r=n,n=void 0),this(t,a.extend({method:e,data:n,success:r},o))}}),Object.defineProperty(e.prototype,"$http",{get:function(){return a.extend(t.bind(this),t)}}),t}},function(e,t,n){function r(e){e(this.resolve.bind(this),this.reject.bind(this)),this._thens=[]}r.prototype={then:function(e,t,n){this._thens.push({resolve:e,reject:t,progress:n})},"catch":function(e){this._thens.push({reject:e})},resolve:function(e){this._complete("resolve",e)},reject:function(e){this._complete("reject",e)},progress:function(e){for(var t,n=0;t=this._thens[n++];)t.progress&&t.progress(e)},_complete:function(e,t){this.then="resolve"===e?function(e,n){e&&e(t)}:function(e,n){n&&n(t)},this.resolve=this.reject=this.progress=function(){throw new Error("Promise already completed.")};for(var n,r=0;n=this._thens[r++];)n[e]&&n[e](t);delete this._thens}},e.exports=window.Promise?window.Promise:r},function(e,t,n){e.exports=function(e){function t(n,s,a){var i=this,c={};return a=o.extend({},t.actions,a),o.each(a,function(t,a){t=o.extend(!0,{url:n,params:s||{}},t),c[a]=function(){return(i.$http||e.http)(r(t,arguments))}}),c}function r(e,t){var n,r,s,a=o.extend({},e),i={};switch(t.length){case 4:s=t[3],r=t[2];case 3:case 2:if(!o.isFunction(t[1])){i=t[0],n=t[1],r=t[2];break}if(o.isFunction(t[0])){r=t[0],s=t[1];break}r=t[1],s=t[2];case 1:o.isFunction(t[0])?r=t[0]:/^(POST|PUT|PATCH)$/i.test(a.method)?n=t[0]:i=t[0];break;case 0:break;default:throw"Expected up to 4 arguments [params, data, success, error], got "+t.length+" arguments"}return a.url=e.url,a.data=n,a.params=o.extend({},e.params,i),r&&(a.success=r),s&&(a.error=s),a}var o=n(1)(e);return t.actions={get:{method:"GET"},save:{method:"POST"},query:{method:"GET"},remove:{method:"DELETE"},"delete":{method:"DELETE"}},Object.defineProperty(e.prototype,"$resource",{get:function(){return t.bind(this)}}),t}},function(e,t,n){e.exports=function(e){function t(e,n){var r,s={},i={},c=e;return a.isPlainObject(c)||(c={url:e,params:n}),c=a.extend({},t.options,a.options("url",this,c)),e=c.url.replace(/:([a-z]\w*)/gi,function(e,t){return c.params[t]?(s[t]=!0,o(c.params[t])):""}),!e.match(/^(https?:)?\//)&&c.root&&(e=c.root+"/"+e),e=e.replace(/([^:])[\/]{2,}/g,"$1/"),e=e.replace(/(\w+)\/+$/,"$1"),a.each(c.params,function(e,t){s[t]||(i[t]=e)}),r=t.params(i),r&&(e+=(-1==e.indexOf("?")?"?":"&")+r),e}function r(e,t,n){var o,s=a.isArray(t),i=a.isPlainObject(t);a.each(t,function(t,c){o=a.isObject(t)||a.isArray(t),n&&(c=n+"["+(i||o?c:"")+"]"),!n&&s?e.add(t.name,t.value):o?r(e,t,c):e.add(c,t)})}function o(e){return s(e,!0).replace(/%26/gi,"&").replace(/%3D/gi,"=").replace(/%2B/gi,"+")}function s(e,t){return encodeURIComponent(e).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,t?"%20":"+")}var a=n(1)(e);return t.options={url:"",root:"",params:{}},t.params=function(e){var t=[];return t.add=function(e,t){a.isFunction(t)&&(t=t()),null===t&&(t=""),this.push(o(e)+"="+o(t))},r(t,e),t.join("&")},t.parse=function(e){var t=new RegExp("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?"),n=e.match(t);return{url:e,scheme:n[1]||"",host:n[2]||"",path:n[3]||"",query:n[4]||"",fragment:n[5]||""}},Object.defineProperty(e.prototype,"$url",{get:function(){return a.extend(t.bind(this),t)}}),t}}]);
{
"name": "vue-resource",
"version": "0.1.2",
"version": "0.1.3",
"description": "A web request service for Vue.js",

@@ -5,0 +5,0 @@ "main": "src/index.js",

module.exports = function (Vue) {
var _ = require('./util')(Vue);
var Promise = require('./promise');
var jsonType = { 'Content-Type': 'application/json;charset=utf-8' };

@@ -30,8 +31,4 @@

if (_.isObject(options.data) && /FormData/i.test(options.data.toString())) {
delete options.headers['Content-Type'];
}
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options);
promise = new _.Promise((options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).bind(this, (this.$url || Vue.url), options));
_.extend(promise, {

@@ -81,3 +78,3 @@

function xhr(url, options, resolve, reject) {
function xhr(url, options) {

@@ -97,5 +94,9 @@ var request = new XMLHttpRequest();

options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.data = Vue.url.params(options.data);
options.data = url.params(options.data);
}
if (_.isObject(options.data) && /FormData/i.test(options.data.toString())) {
delete options.headers['Content-Type'];
}
if (_.isPlainObject(options.data)) {

@@ -105,24 +106,37 @@ options.data = JSON.stringify(options.data);

request.open(options.method, url(options), true);
var promise = new Promise(function (resolve, reject) {
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
request.open(options.method, url(options), true);
request.onreadystatechange = function () {
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
if (this.readyState === 4) {
request.onreadystatechange = function () {
if (this.status >= 200 && this.status < 300) {
resolve(this);
} else {
reject(this);
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 300) {
resolve(this);
} else {
reject(this);
}
}
};
request.send(options.data);
});
_.extend(promise, {
abort: function () {
request.abort();
}
};
request.send(options.data);
});
return promise;
}
function jsonp(url, options, resolve, reject) {
function jsonp(url, options) {

@@ -138,29 +152,34 @@ var callback = '_jsonp' + Math.random().toString(36).substr(2), script, result;

script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
var promise = new Promise(function (resolve, reject) {
window[callback] = function (data) {
result = data;
};
script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
var handler = function (event) {
window[callback] = function (data) {
result = data;
};
delete window[callback];
document.body.removeChild(script);
var handler = function (event) {
if (event.type === 'load' && !result) {
event.type = 'error';
}
delete window[callback];
document.body.removeChild(script);
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
if (event.type === 'load' && !result) {
event.type = 'error';
}
(status === 200 ? resolve : reject)({ responseText: text, status: status });
};
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
script.onload = handler;
script.onerror = handler;
(status === 200 ? resolve : reject)({ responseText: text, status: status });
};
document.body.appendChild(script);
script.onload = handler;
script.onerror = handler;
document.body.appendChild(script);
});
return promise;
}

@@ -167,0 +186,0 @@

@@ -74,63 +74,3 @@ /**

/**
* Promise polyfill (https://gist.github.com/briancavalier/814313)
*/
_.Promise = window.Promise;
if (!_.Promise) {
_.Promise = function (executor) {
executor(this.resolve.bind(this), this.reject.bind(this));
this._thens = [];
};
_.Promise.prototype = {
then: function (onResolve, onReject, onProgress) {
this._thens.push({resolve: onResolve, reject: onReject, progress: onProgress});
},
'catch': function (onReject) {
this._thens.push({reject: onReject});
},
resolve: function (value) {
this._complete('resolve', value);
},
reject: function (reason) {
this._complete('reject', reason);
},
progress: function (status) {
var i = 0, aThen;
while (aThen = this._thens[i++]) {
aThen.progress && aThen.progress(status);
}
},
_complete: function (which, arg) {
this.then = which === 'resolve' ?
function (resolve, reject) { resolve && resolve(arg); } :
function (resolve, reject) { reject && reject(arg); };
this.resolve = this.reject = this.progress =
function () { throw new Error('Promise already completed.'); };
var aThen, i = 0;
while (aThen = this._thens[i++]) {
aThen[which] && aThen[which](arg);
}
delete this._thens;
}
};
}
return _;
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc