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.8 to 0.1.9

src/lib/jsonp.js

691

dist/vue-resource.js
/**
* vue-resource v0.1.7
* vue-resource v0.1.9
* https://github.com/vuejs/vue-resource

@@ -67,6 +67,6 @@ * Released under the MIT License.

function install (Vue) {
function install(Vue) {
Vue.url = __webpack_require__(1)(Vue);
Vue.http = __webpack_require__(3)(Vue);
Vue.resource = __webpack_require__(5)(Vue);
Vue.resource = __webpack_require__(7)(Vue);
}

@@ -85,14 +85,12 @@

module.exports = function (Vue) {
/**
* Service for URL templating.
*/
var _ = __webpack_require__(2)(Vue);
var _ = __webpack_require__(2);
var el = document.createElement('a');
/**
* Url provides URL templating.
*
* @param {String} url
* @param {Object} params
*/
module.exports = function (Vue) {
function Url (url, params) {
function Url(url, params) {

@@ -184,16 +182,17 @@ var urlParams = {}, queryParams = {}, options = url, query;

var pattern = new RegExp("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?"),
matches = url.match(pattern);
el.href = url;
return {
url: url,
scheme: matches[1] || '',
host: matches[2] || '',
path: matches[3] || '',
query: matches[4] || '',
fragment: matches[5] || ''
href: el.href,
protocol: el.protocol ? el.protocol.replace(/:$/, '') : '',
port: el.port,
host: el.host,
hostname: el.hostname,
pathname: el.pathname.charAt(0) === '/' ? el.pathname : '/' + el.pathname,
search: el.search ? el.search.replace(/^\?/, '') : '',
hash: el.hash ? el.hash.replace(/^#/, '') : ''
};
};
function serialize (params, obj, scope) {
function serialize(params, obj, scope) {

@@ -220,3 +219,3 @@ var array = _.isArray(obj), plain = _.isPlainObject(obj), hash;

function encodeUriSegment (value) {
function encodeUriSegment(value) {

@@ -229,3 +228,3 @@ return encodeUriQuery(value, true).

function encodeUriQuery (value, spaces) {
function encodeUriQuery(value, spaces) {

@@ -260,75 +259,80 @@ return encodeURIComponent(value).

module.exports = function (Vue) {
var _ = exports;
var _ = Vue.util.extend({}, Vue.util);
_.isArray = Array.isArray;
_.options = function (key, obj, options) {
_.isFunction = function (obj) {
return obj && typeof obj === 'function';
};
var opts = obj.$options || {};
_.isObject = function (obj) {
return obj !== null && typeof obj === 'object';
};
return _.extend({},
opts[key],
options
);
};
_.isPlainObject = function (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
};
_.each = function (obj, iterator) {
_.options = function (key, obj, options) {
var i, key;
var opts = obj.$options || {};
if (typeof obj.length == 'number') {
for (i = 0; i < obj.length; i++) {
iterator.call(obj[i], obj[i], i);
return _.extend({},
opts[key],
options
);
};
_.each = function (obj, iterator) {
var i, key;
if (typeof obj.length == 'number') {
for (i = 0; i < obj.length; i++) {
iterator.call(obj[i], obj[i], i);
}
} else if (_.isObject(obj)) {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(obj[key], obj[key], key);
}
} else if (_.isObject(obj)) {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(obj[key], obj[key], key);
}
}
}
}
return obj;
};
return obj;
};
_.extend = function (target) {
_.extend = function (target) {
var array = [], args = array.slice.call(arguments, 1), deep;
var array = [], args = array.slice.call(arguments, 1), deep;
if (typeof target == 'boolean') {
deep = target;
target = args.shift();
}
if (typeof target == 'boolean') {
deep = target;
target = args.shift();
}
args.forEach(function (arg) {
extend(target, arg, deep);
});
args.forEach(function (arg) {
extend(target, arg, deep);
});
return target;
};
return target;
};
function extend (target, source, deep) {
for (var key in source) {
if (deep && (_.isPlainObject(source[key]) || _.isArray(source[key]))) {
if (_.isPlainObject(source[key]) && !_.isPlainObject(target[key])) {
target[key] = {};
}
if (_.isArray(source[key]) && !_.isArray(target[key])) {
target[key] = [];
}
extend(target[key], source[key], deep);
} else if (source[key] !== undefined) {
target[key] = source[key];
function extend(target, source, deep) {
for (var key in source) {
if (deep && (_.isPlainObject(source[key]) || _.isArray(source[key]))) {
if (_.isPlainObject(source[key]) && !_.isPlainObject(target[key])) {
target[key] = {};
}
if (_.isArray(source[key]) && !_.isArray(target[key])) {
target[key] = [];
}
extend(target[key], source[key], deep);
} else if (source[key] !== undefined) {
target[key] = source[key];
}
}
}
_.isFunction = function (obj) {
return obj && typeof obj === 'function';
};
return _;
};
/***/ },

@@ -338,16 +342,20 @@ /* 3 */

/**
* Service for sending network requests.
*/
var _ = __webpack_require__(2);
var xhr = __webpack_require__(4);
var jsonp = __webpack_require__(6);
var jsonType = {'Content-Type': 'application/json;charset=utf-8'};
module.exports = function (Vue) {
var _ = __webpack_require__(2)(Vue);
var Promise = __webpack_require__(4);
var jsonType = { 'Content-Type': 'application/json;charset=utf-8' };
var Url = Vue.url;
var originUrl = Url.parse(location.href);
/**
* Http provides a service for sending XMLHttpRequests.
*/
function Http(url, options) {
function Http (url, options) {
var self = this, promise;
var self = this, headers, promise;
options = options || {};

@@ -360,9 +368,15 @@

headers = _.extend({},
Http.headers.common,
Http.headers[options.method.toLowerCase()]
options = _.extend(true, {url: url},
Http.options, _.options('http', this, options)
);
options = _.extend(true, {url: url, headers: headers},
Http.options, _.options('http', this, options)
if (options.crossOrigin === null) {
options.crossOrigin = crossOrigin(options.url);
}
options.headers = _.extend({},
Http.headers.common,
!options.crossOrigin ? Http.headers.custom : {},
Http.headers[options.method.toLowerCase()],
options.headers
);

@@ -372,7 +386,25 @@

_.extend(options.params, options.data);
options.data = '';
delete options.data;
}
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options);
if (options.emulateHTTP && !option.crossOrigin && /^(put|patch|delete)$/i.test(options.method)) {
options.headers['X-HTTP-Method-Override'] = options.method;
options.method = 'post';
}
if (options.emulateJSON && _.isPlainObject(options.data)) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
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)) {
options.data = JSON.stringify(options.data);
}
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Url, options);
promise.then(transformResponse, transformResponse);

@@ -420,106 +452,2 @@

function xhr(url, options) {
var request = new XMLHttpRequest();
if (_.isFunction(options.beforeSend)) {
options.beforeSend(request, options);
}
if (options.emulateHTTP && /^(put|patch|delete)$/i.test(options.method)) {
options.headers['X-HTTP-Method-Override'] = options.method;
options.method = 'post';
}
if (options.emulateJSON && _.isPlainObject(options.data)) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
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)) {
options.data = JSON.stringify(options.data);
}
var promise = new Promise(function (resolve, reject) {
request.open(options.method, url(options), true);
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
request.onreadystatechange = function () {
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();
}
});
return promise;
}
function jsonp(url, options) {
var callback = '_jsonp' + Math.random().toString(36).substr(2), script, result;
options.params[options.jsonp] = callback;
if (_.isFunction(options.beforeSend)) {
options.beforeSend({}, options);
}
var promise = new Promise(function (resolve, reject) {
script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
window[callback] = function (data) {
result = data;
};
var handler = function (event) {
delete window[callback];
document.body.removeChild(script);
if (event.type === 'load' && !result) {
event.type = 'error';
}
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
(status === 200 ? resolve : reject)({ responseText: text, status: status });
};
script.onload = handler;
script.onerror = handler;
document.body.appendChild(script);
});
return promise;
}
function transformResponse(response) {

@@ -535,2 +463,9 @@

function crossOrigin(url) {
var requestUrl = Url.parse(url);
return (requestUrl.protocol !== originUrl.protocol || requestUrl.host !== originUrl.host);
}
Http.options = {

@@ -542,2 +477,3 @@ method: 'get',

beforeSend: null,
crossOrigin: null,
emulateHTTP: false,

@@ -552,6 +488,4 @@ emulateJSON: false

delete: jsonType,
common: {
'Accept': 'application/json, text/plain, */*',
'X-Requested-With': 'XMLHttpRequest'
}
common: {'Accept': 'application/json, text/plain, */*'},
custom: {'X-Requested-With': 'XMLHttpRequest'}
};

@@ -587,76 +521,341 @@

/* 4 */
/***/ function(module, exports, __webpack_require__) {
/**
* XMLHttp request.
*/
var _ = __webpack_require__(2);
var Promise = __webpack_require__(5);
module.exports = function (url, options) {
var request = new XMLHttpRequest(), promise;
if (_.isFunction(options.beforeSend)) {
options.beforeSend.call(this, request, options);
}
promise = new Promise(function (resolve, reject) {
request.open(options.method, url(options), true);
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
request.onreadystatechange = function () {
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();
}
});
return promise;
};
/***/ },
/* 5 */
/***/ function(module, exports) {
/**
* Promise polyfill (https://gist.github.com/briancavalier/814313)
* Promises/A+ polyfill v1.1.0 (https://github.com/bramstein/promis)
*/
function Promise (executor) {
executor(this.resolve.bind(this), this.reject.bind(this));
this._thens = [];
function Promise(executor) {
this.state = Promise.State.PENDING;
this.value = undefined;
this.deferred = [];
var promise = this;
try {
executor(function (x) {
promise.resolve(x);
}, function (r) {
promise.reject(r);
});
} catch (e) {
promise.reject(e);
}
}
Promise.prototype = {
Promise.State = {
RESOLVED: 0,
REJECTED: 1,
PENDING: 2
};
then: function (onResolve, onReject, onProgress) {
this._thens.push({resolve: onResolve, reject: onReject, progress: onProgress});
},
Promise.reject = function (r) {
return new Promise(function (resolve, reject) {
reject(r);
});
};
'catch': function (onReject) {
this._thens.push({reject: onReject});
},
Promise.resolve = function (x) {
return new Promise(function (resolve, reject) {
resolve(x);
});
};
resolve: function (value) {
this._complete('resolve', value);
},
Promise.all = function all(iterable) {
return new Promise(function (resolve, reject) {
var count = 0,
result = [];
reject: function (reason) {
this._complete('reject', reason);
},
if (iterable.length === 0) {
resolve(result);
}
progress: function (status) {
function resolver(i) {
return function (x) {
result[i] = x;
count += 1;
var i = 0, aThen;
if (count === iterable.length) {
resolve(result);
}
};
}
while (aThen = this._thens[i++]) {
aThen.progress && aThen.progress(status);
for (var i = 0; i < iterable.length; i += 1) {
iterable[i].then(resolver(i), reject);
}
},
});
};
_complete: function (which, arg) {
Promise.race = function race(iterable) {
return new Promise(function (resolve, reject) {
for (var i = 0; i < iterable.length; i += 1) {
iterable[i].then(resolve, reject);
}
});
};
this.then = which === 'resolve' ?
function (resolve, reject) { resolve && resolve(arg); } :
function (resolve, reject) { reject && reject(arg); };
var p = Promise.prototype;
this.resolve = this.reject = this.progress =
function () { throw new Error('Promise already completed.'); };
p.resolve = function resolve(x) {
var promise = this;
var aThen, i = 0;
if (promise.state === Promise.State.PENDING) {
if (x === promise) {
throw new TypeError('Promise settled with itself.');
}
while (aThen = this._thens[i++]) {
aThen[which] && aThen[which](arg);
var called = false;
try {
var then = x && x['then'];
if (x !== null && typeof x === 'object' && typeof then === 'function') {
then.call(x, function (x) {
if (!called) {
promise.resolve(x);
}
called = true;
}, function (r) {
if (!called) {
promise.reject(r);
}
called = true;
});
return;
}
} catch (e) {
if (!called) {
promise.reject(e);
}
return;
}
promise.state = Promise.State.RESOLVED;
promise.value = x;
promise.notify();
}
};
delete this._thens;
p.reject = function reject(reason) {
var promise = this;
if (promise.state === Promise.State.PENDING) {
if (reason === promise) {
throw new TypeError('Promise settled with itself.');
}
promise.state = Promise.State.REJECTED;
promise.value = reason;
promise.notify();
}
};
module.exports = window.Promise ? window.Promise : Promise;
p.notify = function notify() {
var promise = this;
async(function () {
if (promise.state !== Promise.State.PENDING) {
while (promise.deferred.length) {
var deferred = promise.deferred.shift(),
onResolved = deferred[0],
onRejected = deferred[1],
resolve = deferred[2],
reject = deferred[3];
try {
if (promise.state === Promise.State.RESOLVED) {
if (typeof onResolved === 'function') {
resolve(onResolved.call(undefined, promise.value));
} else {
resolve(promise.value);
}
} else if (promise.state === Promise.State.REJECTED) {
if (typeof onRejected === 'function') {
resolve(onRejected.call(undefined, promise.value));
} else {
reject(promise.value);
}
}
} catch (e) {
reject(e);
}
}
}
});
};
p.catch = function (onRejected) {
return this.then(undefined, onRejected);
};
p.then = function then(onResolved, onRejected) {
var promise = this;
return new Promise(function (resolve, reject) {
promise.deferred.push([onResolved, onRejected, resolve, reject]);
promise.notify();
});
};
var queue = [];
var async = function (callback) {
queue.push(callback);
if (queue.length === 1) {
async.async();
}
};
async.run = function () {
while (queue.length) {
queue[0]();
queue.shift();
}
};
if (window.MutationObserver) {
var el = document.createElement('div');
var mo = new MutationObserver(async.run);
mo.observe(el, {
attributes: true
});
async.async = function () {
el.setAttribute("x", 0);
};
} else {
async.async = function () {
setTimeout(async.run);
};
}
module.exports = window.Promise || Promise;
/***/ },
/* 5 */
/* 6 */
/***/ function(module, exports, __webpack_require__) {
module.exports = function (Vue) {
/**
* JSONP request.
*/
var _ = __webpack_require__(2)(Vue);
var _ = __webpack_require__(2);
var Promise = __webpack_require__(5);
/**
* Resource provides interaction support with RESTful services.
*/
module.exports = function (url, options) {
function Resource (url, params, actions) {
var callback = '_jsonp' + Math.random().toString(36).substr(2), script, body;
options.params[options.jsonp] = callback;
if (_.isFunction(options.beforeSend)) {
options.beforeSend.call(this, {}, options);
}
return new Promise(function (resolve, reject) {
script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
window[callback] = function (data) {
body = data;
};
var handler = function (event) {
delete window[callback];
document.body.removeChild(script);
if (event.type === 'load' && !body) {
event.type = 'error';
}
var text = body ? body : event.type, status = event.type === 'error' ? 404 : 200;
(status === 200 ? resolve : reject)({responseText: text, status: status});
};
script.onload = handler;
script.onerror = handler;
document.body.appendChild(script);
});
};
/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
/**
* Service for interacting with RESTful services.
*/
var _ = __webpack_require__(2);
module.exports = function (Vue) {
function Resource(url, params, actions) {
var self = this, resource = {};

@@ -681,3 +880,3 @@

function opts (action, args) {
function opts(action, args) {

@@ -722,3 +921,3 @@ var options = _.extend({}, action), params = {}, data, success, error;

success = args[0];
} else if (/^(POST|PUT|PATCH)$/i.test(options.method)) {
} else if (/^(post|put|patch)$/i.test(options.method)) {
data = args[0];

@@ -757,7 +956,7 @@ } else {

get: {method: 'GET'},
save: {method: 'POST'},
query: {method: 'GET'},
remove: {method: 'DELETE'},
delete: {method: 'DELETE'}
get: {method: 'get'},
save: {method: 'post'},
query: {method: 'get'},
remove: {method: 'delete'},
delete: {method: 'delete'}

@@ -764,0 +963,0 @@ };

/**
* vue-resource v0.1.7
* vue-resource v0.1.9
* https://github.com/vuejs/vue-resource

@@ -7,2 +7,2 @@ * Released under the MIT License.

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):"object"==typeof exports?exports.VueResource=t():e.VueResource=t()}(this,function(){return 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(1)(e),e.http=n(3)(e),e.resource=n(5)(e)}window.Vue&&Vue.use(r),e.exports=r},function(e,t,n){e.exports=function(e){function t(e,n){var r,a={},i={},c=e;return s.isPlainObject(c)||(c={url:e,params:n}),c=s.extend({},t.options,s.options("url",this,c)),e=c.url.replace(/:([a-z]\w*)/gi,function(e,t){return c.params[t]?(a[t]=!0,o(c.params[t])):""}),"string"!=typeof c.root||e.match(/^(https?:)?\//)||(e=c.root+"/"+e),e=e.replace(/([^:])[\/]{2,}/g,"$1/"),e=e.replace(/(\w+)\/+$/,"$1"),s.each(c.params,function(e,t){a[t]||(i[t]=e)}),r=t.params(i),r&&(e+=(-1==e.indexOf("?")?"?":"&")+r),e}function r(e,t,n){var o,a=s.isArray(t),i=s.isPlainObject(t);s.each(t,function(t,c){o=s.isObject(t)||s.isArray(t),n&&(c=n+"["+(i||o?c:"")+"]"),!n&&a?e.add(t.name,t.value):o?r(e,t,c):e.add(c,t)})}function o(e){return a(e,!0).replace(/%26/gi,"&").replace(/%3D/gi,"=").replace(/%2B/gi,"+")}function a(e,t){return encodeURIComponent(e).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,t?"%20":"+")}var s=n(2)(e);return t.options={url:"",params:{}},t.params=function(e){var t=[];return t.add=function(e,t){s.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 s.extend(t.bind(this),t)}}),t}},function(e,t){e.exports=function(e){function t(e,r,o){for(var a in r)o&&(n.isPlainObject(r[a])||n.isArray(r[a]))?(n.isPlainObject(r[a])&&!n.isPlainObject(e[a])&&(e[a]={}),n.isArray(r[a])&&!n.isArray(e[a])&&(e[a]=[]),t(e[a],r[a],o)):void 0!==r[a]&&(e[a]=r[a])}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||{},s.isPlainObject(n)&&(i=n,n=""),c=s.extend({},t.headers.common,t.headers[i.method.toLowerCase()]),i=s.extend(!0,{url:n,headers:c},t.options,s.options("http",this,i)),s.isPlainObject(i.data)&&/^(get|jsonp)$/i.test(i.method)&&(s.extend(i.params,i.data),i.data=""),u=("jsonp"==i.method.toLowerCase()?o:r).call(this,this.$url||e.url,i),u.then(a,a),u.success=function(e){return u.then(function(t){e.call(p,t.data,t.status,t)},function(){}),u},u.error=function(e){return u["catch"](function(t){e.call(p,t.data,t.status,t)}),u},u.always=function(e){var t=function(t){e.call(p,t.data,t.status,t)};return u.then(t,t),u},i.success&&u.success(i.success),i.error&&u.error(i.error),u}function r(e,t){var n=new XMLHttpRequest;s.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&&s.isPlainObject(t.data)&&(t.headers["Content-Type"]="application/x-www-form-urlencoded",t.data=e.params(t.data)),s.isObject(t.data)&&/FormData/i.test(t.data.toString())&&delete t.headers["Content-Type"],s.isPlainObject(t.data)&&(t.data=JSON.stringify(t.data));var r=new i(function(r,o){n.open(t.method,e(t),!0),s.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 s.extend(r,{abort:function(){n.abort()}}),r}function o(e,t){var n,r,o="_jsonp"+Math.random().toString(36).substr(2);t.params[t.jsonp]=o,s.isFunction(t.beforeSend)&&t.beforeSend({},t);var a=new i(function(a,s){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?a:s)({responseText:t,status:i})};n.onload=i,n.onerror=i,document.body.appendChild(n)});return a}function a(e){try{e.data=JSON.parse(e.responseText)}catch(t){e.data=e.responseText}}var s=n(2)(e),i=n(4),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 s.isFunction(n)&&(o=r,r=n,n=void 0),this(t,s.extend({method:e,data:n,success:r},o))}}),Object.defineProperty(e.prototype,"$http",{get:function(){return s.extend(t.bind(this),t)}}),t}},function(e,t){function n(e){e(this.resolve.bind(this),this.reject.bind(this)),this._thens=[]}n.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:n},function(e,t,n){e.exports=function(e){function t(n,a,s){var i=this,c={};return s=o.extend({},t.actions,s),o.each(s,function(t,s){t=o.extend(!0,{url:n,params:a||{}},t),c[s]=function(){return(i.$http||e.http)(r(t,arguments))}}),c}function r(e,t){var n,r,a,s=o.extend({},e),i={};switch(t.length){case 4:a=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],a=t[1];break}r=t[1],a=t[2];case 1:o.isFunction(t[0])?r=t[0]:/^(POST|PUT|PATCH)$/i.test(s.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 s.url=e.url,s.data=n,s.params=o.extend({},e.params,i),r&&(s.success=r),a&&(s.error=a),s}var o=n(2)(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(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):"object"==typeof exports?exports.VueResource=e():t.VueResource=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){function r(t){t.url=n(1)(t),t.http=n(3)(t),t.resource=n(7)(t)}window.Vue&&Vue.use(r),t.exports=r},function(t,e,n){var r=n(2),o=document.createElement("a");t.exports=function(t){function e(t,n){var o,i={},s={},c=t;return r.isPlainObject(c)||(c={url:t,params:n}),c=r.extend({},e.options,r.options("url",this,c)),t=c.url.replace(/:([a-z]\w*)/gi,function(t,e){return c.params[e]?(i[e]=!0,a(c.params[e])):""}),"string"!=typeof c.root||t.match(/^(https?:)?\//)||(t=c.root+"/"+t),t=t.replace(/([^:])[\/]{2,}/g,"$1/"),t=t.replace(/(\w+)\/+$/,"$1"),r.each(c.params,function(t,e){i[e]||(s[e]=t)}),o=e.params(s),o&&(t+=(-1==t.indexOf("?")?"?":"&")+o),t}function n(t,e,o){var a,i=r.isArray(e),s=r.isPlainObject(e);r.each(e,function(e,c){a=r.isObject(e)||r.isArray(e),o&&(c=o+"["+(s||a?c:"")+"]"),!o&&i?t.add(e.name,e.value):a?n(t,e,c):t.add(c,e)})}function a(t){return i(t,!0).replace(/%26/gi,"&").replace(/%3D/gi,"=").replace(/%2B/gi,"+")}function i(t,e){return encodeURIComponent(t).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,e?"%20":"+")}return e.options={url:"",params:{}},e.params=function(t){var e=[];return e.add=function(t,e){r.isFunction(e)&&(e=e()),null===e&&(e=""),this.push(a(t)+"="+a(e))},n(e,t),e.join("&")},e.parse=function(t){return o.href=t,{href:o.href,protocol:o.protocol?o.protocol.replace(/:$/,""):"",port:o.port,host:o.host,hostname:o.hostname,pathname:"/"===o.pathname.charAt(0)?o.pathname:"/"+o.pathname,search:o.search?o.search.replace(/^\?/,""):"",hash:o.hash?o.hash.replace(/^#/,""):""}},Object.defineProperty(t.prototype,"$url",{get:function(){return r.extend(e.bind(this),e)}}),e}},function(t,e){function n(t,e,o){for(var a in e)o&&(r.isPlainObject(e[a])||r.isArray(e[a]))?(r.isPlainObject(e[a])&&!r.isPlainObject(t[a])&&(t[a]={}),r.isArray(e[a])&&!r.isArray(t[a])&&(t[a]=[]),n(t[a],e[a],o)):void 0!==e[a]&&(t[a]=e[a])}var r=e;r.isArray=Array.isArray,r.isFunction=function(t){return t&&"function"==typeof t},r.isObject=function(t){return null!==t&&"object"==typeof t},r.isPlainObject=function(t){return"[object Object]"===Object.prototype.toString.call(t)},r.options=function(t,e,n){var o=e.$options||{};return r.extend({},o[t],n)},r.each=function(t,e){var n,o;if("number"==typeof t.length)for(n=0;n<t.length;n++)e.call(t[n],t[n],n);else if(r.isObject(t))for(o in t)t.hasOwnProperty(o)&&e.call(t[o],t[o],o);return t},r.extend=function(t){var e,r=[],o=r.slice.call(arguments,1);return"boolean"==typeof t&&(e=t,t=o.shift()),o.forEach(function(r){n(t,r,e)}),t}},function(t,e,n){var r=n(2),o=n(4),a=n(6),i={"Content-Type":"application/json;charset=utf-8"};t.exports=function(t){function e(t,i){var u,f=this;return i=i||{},r.isPlainObject(t)&&(i=t,t=""),i=r.extend(!0,{url:t},e.options,r.options("http",this,i)),null===i.crossOrigin&&(i.crossOrigin=s(i.url)),i.headers=r.extend({},e.headers.common,i.crossOrigin?{}:e.headers.custom,e.headers[i.method.toLowerCase()],i.headers),r.isPlainObject(i.data)&&/^(get|jsonp)$/i.test(i.method)&&(r.extend(i.params,i.data),delete i.data),i.emulateHTTP&&!option.crossOrigin&&/^(put|patch|delete)$/i.test(i.method)&&(i.headers["X-HTTP-Method-Override"]=i.method,i.method="post"),i.emulateJSON&&r.isPlainObject(i.data)&&(i.headers["Content-Type"]="application/x-www-form-urlencoded",i.data=c.params(i.data)),r.isObject(i.data)&&/FormData/i.test(i.data.toString())&&delete i.headers["Content-Type"],r.isPlainObject(i.data)&&(i.data=JSON.stringify(i.data)),u=("jsonp"==i.method.toLowerCase()?a:o).call(this,this.$url||c,i),u.then(n,n),u.success=function(t){return u.then(function(e){t.call(f,e.data,e.status,e)},function(){}),u},u.error=function(t){return u["catch"](function(e){t.call(f,e.data,e.status,e)}),u},u.always=function(t){var e=function(e){t.call(f,e.data,e.status,e)};return u.then(e,e),u},i.success&&u.success(i.success),i.error&&u.error(i.error),u}function n(t){try{t.data=JSON.parse(t.responseText)}catch(e){t.data=t.responseText}}function s(t){var e=c.parse(t);return e.protocol!==u.protocol||e.host!==u.host}var c=t.url,u=c.parse(location.href);return e.options={method:"get",params:{},data:"",jsonp:"callback",beforeSend:null,crossOrigin:null,emulateHTTP:!1,emulateJSON:!1},e.headers={put:i,post:i,patch:i,"delete":i,common:{Accept:"application/json, text/plain, */*"},custom:{"X-Requested-With":"XMLHttpRequest"}},["get","put","post","patch","delete","jsonp"].forEach(function(t){e[t]=function(e,n,o,a){return r.isFunction(n)&&(a=o,o=n,n=void 0),this(e,r.extend({method:t,data:n,success:o},a))}}),Object.defineProperty(t.prototype,"$http",{get:function(){return r.extend(e.bind(this),e)}}),e}},function(t,e,n){var r=n(2),o=n(5);t.exports=function(t,e){var n,a=new XMLHttpRequest;return r.isFunction(e.beforeSend)&&e.beforeSend.call(this,a,e),n=new o(function(n,o){a.open(e.method,t(e),!0),r.each(e.headers,function(t,e){a.setRequestHeader(e,t)}),a.onreadystatechange=function(){4===this.readyState&&(this.status>=200&&this.status<300?n(this):o(this))},a.send(e.data)}),r.extend(n,{abort:function(){a.abort()}}),n}},function(t,e){function n(t){this.state=n.State.PENDING,this.value=void 0,this.deferred=[];var e=this;try{t(function(t){e.resolve(t)},function(t){e.reject(t)})}catch(r){e.reject(r)}}n.State={RESOLVED:0,REJECTED:1,PENDING:2},n.reject=function(t){return new n(function(e,n){n(t)})},n.resolve=function(t){return new n(function(e,n){e(t)})},n.all=function(t){return new n(function(e,n){function r(n){return function(r){a[n]=r,o+=1,o===t.length&&e(a)}}var o=0,a=[];0===t.length&&e(a);for(var i=0;i<t.length;i+=1)t[i].then(r(i),n)})},n.race=function(t){return new n(function(e,n){for(var r=0;r<t.length;r+=1)t[r].then(e,n)})};var r=n.prototype;r.resolve=function(t){var e=this;if(e.state===n.State.PENDING){if(t===e)throw new TypeError("Promise settled with itself.");var r=!1;try{var o=t&&t.then;if(null!==t&&"object"==typeof t&&"function"==typeof o)return void o.call(t,function(t){r||e.resolve(t),r=!0},function(t){r||e.reject(t),r=!0})}catch(a){return void(r||e.reject(a))}e.state=n.State.RESOLVED,e.value=t,e.notify()}},r.reject=function(t){var e=this;if(e.state===n.State.PENDING){if(t===e)throw new TypeError("Promise settled with itself.");e.state=n.State.REJECTED,e.value=t,e.notify()}},r.notify=function(){var t=this;a(function(){if(t.state!==n.State.PENDING)for(;t.deferred.length;){var e=t.deferred.shift(),r=e[0],o=e[1],a=e[2],i=e[3];try{t.state===n.State.RESOLVED?a("function"==typeof r?r.call(void 0,t.value):t.value):t.state===n.State.REJECTED&&("function"==typeof o?a(o.call(void 0,t.value)):i(t.value))}catch(s){i(s)}}})},r["catch"]=function(t){return this.then(void 0,t)},r.then=function(t,e){var r=this;return new n(function(n,o){r.deferred.push([t,e,n,o]),r.notify()})};var o=[],a=function(t){o.push(t),1===o.length&&a.async()};if(a.run=function(){for(;o.length;)o[0](),o.shift()},window.MutationObserver){var i=document.createElement("div"),s=new MutationObserver(a.run);s.observe(i,{attributes:!0}),a.async=function(){i.setAttribute("x",0)}}else a.async=function(){setTimeout(a.run)};t.exports=window.Promise||n},function(t,e,n){var r=n(2),o=n(5);t.exports=function(t,e){var n,a,i="_jsonp"+Math.random().toString(36).substr(2);return e.params[e.jsonp]=i,r.isFunction(e.beforeSend)&&e.beforeSend.call(this,{},e),new o(function(r,o){n=document.createElement("script"),n.src=t(e.url,e.params),n.type="text/javascript",n.async=!0,window[i]=function(t){a=t};var s=function(t){delete window[i],document.body.removeChild(n),"load"!==t.type||a||(t.type="error");var e=a?a:t.type,s="error"===t.type?404:200;(200===s?r:o)({responseText:e,status:s})};n.onload=s,n.onerror=s,document.body.appendChild(n)})}},function(t,e,n){var r=n(2);t.exports=function(t){function e(o,a,i){var s=this,c={};return i=r.extend({},e.actions,i),r.each(i,function(e,i){e=r.extend(!0,{url:o,params:a||{}},e),c[i]=function(){return(s.$http||t.http)(n(e,arguments))}}),c}function n(t,e){var n,o,a,i=r.extend({},t),s={};switch(e.length){case 4:a=e[3],o=e[2];case 3:case 2:if(!r.isFunction(e[1])){s=e[0],n=e[1],o=e[2];break}if(r.isFunction(e[0])){o=e[0],a=e[1];break}o=e[1],a=e[2];case 1:r.isFunction(e[0])?o=e[0]:/^(post|put|patch)$/i.test(i.method)?n=e[0]:s=e[0];break;case 0:break;default:throw"Expected up to 4 arguments [params, data, success, error], got "+e.length+" arguments"}return i.url=t.url,i.data=n,i.params=r.extend({},t.params,s),o&&(i.success=o),a&&(i.error=a),i}return e.actions={get:{method:"get"},save:{method:"post"},query:{method:"get"},remove:{method:"delete"},"delete":{method:"delete"}},Object.defineProperty(t.prototype,"$resource",{get:function(){return e.bind(this)}}),e}}])});
{
"name": "vue-resource",
"version": "0.1.8",
"version": "0.1.9",
"description": "A web request service for Vue.js",

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

@@ -0,15 +1,19 @@

/**
* Service for sending network requests.
*/
var _ = require('./lib/util');
var xhr = require('./lib/xhr');
var jsonp = require('./lib/jsonp');
var jsonType = {'Content-Type': 'application/json;charset=utf-8'};
module.exports = function (Vue) {
var _ = require('./util')(Vue);
var Promise = require('./promise');
var jsonType = { 'Content-Type': 'application/json;charset=utf-8' };
var Url = Vue.url;
var originUrl = Url.parse(location.href);
/**
* Http provides a service for sending XMLHttpRequests.
*/
function Http(url, options) {
function Http (url, options) {
var self = this, promise;
var self = this, headers, promise;
options = options || {};

@@ -22,9 +26,15 @@

headers = _.extend({},
Http.headers.common,
Http.headers[options.method.toLowerCase()]
options = _.extend(true, {url: url},
Http.options, _.options('http', this, options)
);
options = _.extend(true, {url: url, headers: headers},
Http.options, _.options('http', this, options)
if (options.crossOrigin === null) {
options.crossOrigin = crossOrigin(options.url);
}
options.headers = _.extend({},
Http.headers.common,
!options.crossOrigin ? Http.headers.custom : {},
Http.headers[options.method.toLowerCase()],
options.headers
);

@@ -34,7 +44,25 @@

_.extend(options.params, options.data);
options.data = '';
delete options.data;
}
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options);
if (options.emulateHTTP && !option.crossOrigin && /^(put|patch|delete)$/i.test(options.method)) {
options.headers['X-HTTP-Method-Override'] = options.method;
options.method = 'post';
}
if (options.emulateJSON && _.isPlainObject(options.data)) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
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)) {
options.data = JSON.stringify(options.data);
}
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Url, options);
promise.then(transformResponse, transformResponse);

@@ -82,106 +110,2 @@

function xhr(url, options) {
var request = new XMLHttpRequest();
if (_.isFunction(options.beforeSend)) {
options.beforeSend(request, options);
}
if (options.emulateHTTP && /^(put|patch|delete)$/i.test(options.method)) {
options.headers['X-HTTP-Method-Override'] = options.method;
options.method = 'post';
}
if (options.emulateJSON && _.isPlainObject(options.data)) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
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)) {
options.data = JSON.stringify(options.data);
}
var promise = new Promise(function (resolve, reject) {
request.open(options.method, url(options), true);
_.each(options.headers, function (value, header) {
request.setRequestHeader(header, value);
});
request.onreadystatechange = function () {
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();
}
});
return promise;
}
function jsonp(url, options) {
var callback = '_jsonp' + Math.random().toString(36).substr(2), script, result;
options.params[options.jsonp] = callback;
if (_.isFunction(options.beforeSend)) {
options.beforeSend({}, options);
}
var promise = new Promise(function (resolve, reject) {
script = document.createElement('script');
script.src = url(options.url, options.params);
script.type = 'text/javascript';
script.async = true;
window[callback] = function (data) {
result = data;
};
var handler = function (event) {
delete window[callback];
document.body.removeChild(script);
if (event.type === 'load' && !result) {
event.type = 'error';
}
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
(status === 200 ? resolve : reject)({ responseText: text, status: status });
};
script.onload = handler;
script.onerror = handler;
document.body.appendChild(script);
});
return promise;
}
function transformResponse(response) {

@@ -197,2 +121,9 @@

function crossOrigin(url) {
var requestUrl = Url.parse(url);
return (requestUrl.protocol !== originUrl.protocol || requestUrl.host !== originUrl.host);
}
Http.options = {

@@ -204,2 +135,3 @@ method: 'get',

beforeSend: null,
crossOrigin: null,
emulateHTTP: false,

@@ -214,6 +146,4 @@ emulateJSON: false

delete: jsonType,
common: {
'Accept': 'application/json, text/plain, */*',
'X-Requested-With': 'XMLHttpRequest'
}
common: {'Accept': 'application/json, text/plain, */*'},
custom: {'X-Requested-With': 'XMLHttpRequest'}
};

@@ -220,0 +150,0 @@

@@ -5,3 +5,3 @@ /**

function install (Vue) {
function install(Vue) {
Vue.url = require('./url')(Vue);

@@ -8,0 +8,0 @@ Vue.http = require('./http')(Vue);

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

module.exports = function (Vue) {
/**
* Service for interacting with RESTful services.
*/
var _ = require('./util')(Vue);
var _ = require('./lib/util');
/**
* Resource provides interaction support with RESTful services.
*/
module.exports = function (Vue) {
function Resource (url, params, actions) {
function Resource(url, params, actions) {

@@ -30,3 +30,3 @@ var self = this, resource = {};

function opts (action, args) {
function opts(action, args) {

@@ -71,3 +71,3 @@ var options = _.extend({}, action), params = {}, data, success, error;

success = args[0];
} else if (/^(POST|PUT|PATCH)$/i.test(options.method)) {
} else if (/^(post|put|patch)$/i.test(options.method)) {
data = args[0];

@@ -106,7 +106,7 @@ } else {

get: {method: 'GET'},
save: {method: 'POST'},
query: {method: 'GET'},
remove: {method: 'DELETE'},
delete: {method: 'DELETE'}
get: {method: 'get'},
save: {method: 'post'},
query: {method: 'get'},
remove: {method: 'delete'},
delete: {method: 'delete'}

@@ -113,0 +113,0 @@ };

@@ -1,13 +0,11 @@

module.exports = function (Vue) {
/**
* Service for URL templating.
*/
var _ = require('./util')(Vue);
var _ = require('./lib/util');
var el = document.createElement('a');
/**
* Url provides URL templating.
*
* @param {String} url
* @param {Object} params
*/
module.exports = function (Vue) {
function Url (url, params) {
function Url(url, params) {

@@ -99,16 +97,17 @@ var urlParams = {}, queryParams = {}, options = url, query;

var pattern = new RegExp("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?"),
matches = url.match(pattern);
el.href = url;
return {
url: url,
scheme: matches[1] || '',
host: matches[2] || '',
path: matches[3] || '',
query: matches[4] || '',
fragment: matches[5] || ''
href: el.href,
protocol: el.protocol ? el.protocol.replace(/:$/, '') : '',
port: el.port,
host: el.host,
hostname: el.hostname,
pathname: el.pathname.charAt(0) === '/' ? el.pathname : '/' + el.pathname,
search: el.search ? el.search.replace(/^\?/, '') : '',
hash: el.hash ? el.hash.replace(/^#/, '') : ''
};
};
function serialize (params, obj, scope) {
function serialize(params, obj, scope) {

@@ -135,3 +134,3 @@ var array = _.isArray(obj), plain = _.isPlainObject(obj), hash;

function encodeUriSegment (value) {
function encodeUriSegment(value) {

@@ -144,3 +143,3 @@ return encodeUriQuery(value, true).

function encodeUriQuery (value, spaces) {
function encodeUriQuery(value, spaces) {

@@ -147,0 +146,0 @@ return encodeURIComponent(value).

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