diff --git a/bower.json b/bower.json index c704622b..2a195b10 100644 --- a/bower.json +++ b/bower.json @@ -2,7 +2,7 @@ "name": "vue-resource", "main": "dist/vue-resource.js", "description": "A web request service for Vue.js", - "version": "0.7.1", + "version": "0.7.2", "homepage": "https://github.com/vuejs/vue-resource", "license": "MIT", "ignore": [ diff --git a/build/webpack.build.config.js b/build/webpack.build.config.js index 53d9df49..6d718f29 100644 --- a/build/webpack.build.config.js +++ b/build/webpack.build.config.js @@ -27,6 +27,24 @@ module.exports = [ ] }, + { + entry: "./src/index", + output: { + path: "./dist", + filename: "vue-resource.common.js", + library: "VueResource", + libraryTarget: "commonjs2" + }, + module: { + loaders: [ + {test: /.js/, exclude: /node_modules/, loader: 'babel', query: {presets: ['es2015-without-strict']}} + ] + }, + plugins: [ + new webpack.BannerPlugin(banner, {raw: true}) + ] + }, + { entry: "./src/index", output: { diff --git a/dist/vue-resource.common.js b/dist/vue-resource.common.js new file mode 100644 index 00000000..8f77595b --- /dev/null +++ b/dist/vue-resource.common.js @@ -0,0 +1,1657 @@ +/** + * vue-resource v0.7.2 + * https://github.com/vuejs/vue-resource + * Released under the MIT License. + */ + +module.exports = +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; + +/******/ // The require function +/******/ function __webpack_require__(moduleId) { + +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) +/******/ return installedModules[moduleId].exports; + +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ exports: {}, +/******/ id: moduleId, +/******/ loaded: false +/******/ }; + +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); + +/******/ // Flag the module as loaded +/******/ module.loaded = true; + +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } + + +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; + +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; + +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; + +/******/ // Load entry module and return exports +/******/ return __webpack_require__(0); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Install plugin. + */ + + function plugin(Vue) { + + if (plugin.installed) { + return; + } + + var _ = __webpack_require__(1); + + _.config = Vue.config; + _.warning = Vue.util.warn; + _.nextTick = Vue.util.nextTick; + + Vue.url = __webpack_require__(2); + Vue.http = __webpack_require__(8); + Vue.resource = __webpack_require__(23); + Vue.Promise = __webpack_require__(10); + + Object.defineProperties(Vue.prototype, { + + $url: { + get: function get() { + return _.options(Vue.url, this, this.$options.url); + } + }, + + $http: { + get: function get() { + return _.options(Vue.http, this, this.$options.http); + } + }, + + $resource: { + get: function get() { + return Vue.resource.bind(this); + } + }, + + $promise: { + get: function get() { + var _this = this; + + return function (executor) { + return new Vue.Promise(executor, _this); + }; + } + } + + }); + } + + if (typeof window !== 'undefined' && window.Vue) { + window.Vue.use(plugin); + } + + module.exports = plugin; + +/***/ }, +/* 1 */ +/***/ function(module, exports) { + + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; + + /** + * Utility functions. + */ + + var _ = exports, + array = [], + console = window.console; + + _.warn = function (msg) { + if (console && _.warning && (!_.config.silent || _.config.debug)) { + console.warn('[VueResource warn]: ' + msg); + } + }; + + _.error = function (msg) { + if (console) { + console.error(msg); + } + }; + + _.trim = function (str) { + return str.replace(/^\s*|\s*$/g, ''); + }; + + _.toLower = function (str) { + return str ? str.toLowerCase() : ''; + }; + + _.isArray = Array.isArray; + + _.isString = function (val) { + return typeof val === 'string'; + }; + + _.isFunction = function (val) { + return typeof val === 'function'; + }; + + _.isObject = function (obj) { + return obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object'; + }; + + _.isPlainObject = function (obj) { + return _.isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype; + }; + + _.options = function (fn, obj, options) { + + options = options || {}; + + if (_.isFunction(options)) { + options = options.call(obj); + } + + return _.merge(fn.bind({ $vm: obj, $options: options }), fn, { $options: 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); + } + } + } + + return obj; + }; + + _.defaults = function (target, source) { + + for (var key in source) { + if (target[key] === undefined) { + target[key] = source[key]; + } + } + + return target; + }; + + _.extend = function (target) { + + var args = array.slice.call(arguments, 1); + + args.forEach(function (arg) { + merge(target, arg); + }); + + return target; + }; + + _.merge = function (target) { + + var args = array.slice.call(arguments, 1); + + args.forEach(function (arg) { + merge(target, arg, true); + }); + + return target; + }; + + function merge(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] = []; + } + merge(target[key], source[key], deep); + } else if (source[key] !== undefined) { + target[key] = source[key]; + } + } + } + +/***/ }, +/* 2 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Service for URL templating. + */ + + var _ = __webpack_require__(1); + var ie = document.documentMode; + var el = document.createElement('a'); + + function Url(url, params) { + + var options = url, + transform; + + if (_.isString(url)) { + options = { url: url, params: params }; + } + + options = _.merge({}, Url.options, this.$options, options); + + Url.transforms.forEach(function (handler) { + transform = factory(handler, transform, this.$vm); + }, this); + + return transform(options); + }; + + /** + * Url options. + */ + + Url.options = { + url: '', + root: null, + params: {} + }; + + /** + * Url transforms. + */ + + Url.transforms = [__webpack_require__(3), __webpack_require__(5), __webpack_require__(6), __webpack_require__(7)]; + + /** + * Encodes a Url parameter string. + * + * @param {Object} obj + */ + + Url.params = function (obj) { + + var params = [], + escape = encodeURIComponent; + + params.add = function (key, value) { + + if (_.isFunction(value)) { + value = value(); + } + + if (value === null) { + value = ''; + } + + this.push(escape(key) + '=' + escape(value)); + }; + + serialize(params, obj); + + return params.join('&').replace(/%20/g, '+'); + }; + + /** + * Parse a URL and return its components. + * + * @param {String} url + */ + + Url.parse = function (url) { + + if (ie) { + el.href = url; + url = el.href; + } + + el.href = url; + + return { + 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 factory(handler, next, vm) { + return function (options) { + return handler.call(vm, options, next); + }; + } + + function serialize(params, obj, scope) { + + var array = _.isArray(obj), + plain = _.isPlainObject(obj), + hash; + + _.each(obj, function (value, key) { + + hash = _.isObject(value) || _.isArray(value); + + if (scope) { + key = scope + '[' + (plain || hash ? key : '') + ']'; + } + + if (!scope && array) { + params.add(value.name, value.value); + } else if (hash) { + serialize(params, value, key); + } else { + params.add(key, value); + } + }); + } + + module.exports = _.url = Url; + +/***/ }, +/* 3 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * URL Template (RFC 6570) Transform. + */ + + var UrlTemplate = __webpack_require__(4); + + module.exports = function (options) { + + var variables = [], + url = UrlTemplate.expand(options.url, options.params, variables); + + variables.forEach(function (key) { + delete options.params[key]; + }); + + return url; + }; + +/***/ }, +/* 4 */ +/***/ function(module, exports) { + + /** + * URL Template v2.0.6 (https://github.com/bramstein/url-template) + */ + + exports.expand = function (url, params, variables) { + + var tmpl = this.parse(url), + expanded = tmpl.expand(params); + + if (variables) { + variables.push.apply(variables, tmpl.vars); + } + + return expanded; + }; + + exports.parse = function (template) { + + var operators = ['+', '#', '.', '/', ';', '?', '&'], + variables = []; + + return { + vars: variables, + expand: function expand(context) { + return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) { + if (expression) { + + var operator = null, + values = []; + + if (operators.indexOf(expression.charAt(0)) !== -1) { + operator = expression.charAt(0); + expression = expression.substr(1); + } + + expression.split(/,/g).forEach(function (variable) { + var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable); + values.push.apply(values, exports.getValues(context, operator, tmp[1], tmp[2] || tmp[3])); + variables.push(tmp[1]); + }); + + if (operator && operator !== '+') { + + var separator = ','; + + if (operator === '?') { + separator = '&'; + } else if (operator !== '#') { + separator = operator; + } + + return (values.length !== 0 ? operator : '') + values.join(separator); + } else { + return values.join(','); + } + } else { + return exports.encodeReserved(literal); + } + }); + } + }; + }; + + exports.getValues = function (context, operator, key, modifier) { + + var value = context[key], + result = []; + + if (this.isDefined(value) && value !== '') { + if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { + value = value.toString(); + + if (modifier && modifier !== '*') { + value = value.substring(0, parseInt(modifier, 10)); + } + + result.push(this.encodeValue(operator, value, this.isKeyOperator(operator) ? key : null)); + } else { + if (modifier === '*') { + if (Array.isArray(value)) { + value.filter(this.isDefined).forEach(function (value) { + result.push(this.encodeValue(operator, value, this.isKeyOperator(operator) ? key : null)); + }, this); + } else { + Object.keys(value).forEach(function (k) { + if (this.isDefined(value[k])) { + result.push(this.encodeValue(operator, value[k], k)); + } + }, this); + } + } else { + var tmp = []; + + if (Array.isArray(value)) { + value.filter(this.isDefined).forEach(function (value) { + tmp.push(this.encodeValue(operator, value)); + }, this); + } else { + Object.keys(value).forEach(function (k) { + if (this.isDefined(value[k])) { + tmp.push(encodeURIComponent(k)); + tmp.push(this.encodeValue(operator, value[k].toString())); + } + }, this); + } + + if (this.isKeyOperator(operator)) { + result.push(encodeURIComponent(key) + '=' + tmp.join(',')); + } else if (tmp.length !== 0) { + result.push(tmp.join(',')); + } + } + } + } else { + if (operator === ';') { + result.push(encodeURIComponent(key)); + } else if (value === '' && (operator === '&' || operator === '?')) { + result.push(encodeURIComponent(key) + '='); + } else if (value === '') { + result.push(''); + } + } + + return result; + }; + + exports.isDefined = function (value) { + return value !== undefined && value !== null; + }; + + exports.isKeyOperator = function (operator) { + return operator === ';' || operator === '&' || operator === '?'; + }; + + exports.encodeValue = function (operator, value, key) { + + value = operator === '+' || operator === '#' ? this.encodeReserved(value) : encodeURIComponent(value); + + if (key) { + return encodeURIComponent(key) + '=' + value; + } else { + return value; + } + }; + + exports.encodeReserved = function (str) { + return str.split(/(%[0-9A-Fa-f]{2})/g).map(function (part) { + if (!/%[0-9A-Fa-f]/.test(part)) { + part = encodeURI(part); + } + return part; + }).join(''); + }; + +/***/ }, +/* 5 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Legacy Transform. + */ + + var _ = __webpack_require__(1); + + module.exports = function (options, next) { + + var variables = [], + url = next(options); + + url = url.replace(/(\/?):([a-z]\w*)/gi, function (match, slash, name) { + + _.warn('The `:' + name + '` parameter syntax has been deprecated. Use the `{' + name + '}` syntax instead.'); + + if (options.params[name]) { + variables.push(name); + return slash + encodeUriSegment(options.params[name]); + } + + return ''; + }); + + variables.forEach(function (key) { + delete options.params[key]; + }); + + return url; + }; + + function encodeUriSegment(value) { + + return encodeUriQuery(value, true).replace(/%26/gi, '&').replace(/%3D/gi, '=').replace(/%2B/gi, '+'); + } + + function encodeUriQuery(value, spaces) { + + return encodeURIComponent(value).replace(/%40/gi, '@').replace(/%3A/gi, ':').replace(/%24/g, '$').replace(/%2C/gi, ',').replace(/%20/g, spaces ? '%20' : '+'); + } + +/***/ }, +/* 6 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Query Parameter Transform. + */ + + var _ = __webpack_require__(1); + + module.exports = function (options, next) { + + var urlParams = Object.keys(_.url.options.params), + query = {}, + url = next(options); + + _.each(options.params, function (value, key) { + if (urlParams.indexOf(key) === -1) { + query[key] = value; + } + }); + + query = _.url.params(query); + + if (query) { + url += (url.indexOf('?') == -1 ? '?' : '&') + query; + } + + return url; + }; + +/***/ }, +/* 7 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Root Prefix Transform. + */ + + var _ = __webpack_require__(1); + + module.exports = function (options, next) { + + var url = next(options); + + if (_.isString(options.root) && !url.match(/^(https?:)?\//)) { + url = options.root + '/' + url; + } + + return url; + }; + +/***/ }, +/* 8 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Service for sending network requests. + */ + + var _ = __webpack_require__(1); + var Client = __webpack_require__(9); + var Promise = __webpack_require__(10); + var interceptor = __webpack_require__(13); + var jsonType = { 'Content-Type': 'application/json' }; + + function Http(url, options) { + var _this = this; + + var client = Client, + request, + promise; + + Http.interceptors.forEach(function (handler) { + client = interceptor(handler, _this.$vm)(client); + }); + + options = _.isObject(url) ? url : _.extend({ url: url }, options); + request = _.merge({}, Http.options, this.$options, options); + promise = client(request).bind(this.$vm).then(function (response) { + + return response.ok ? response : Promise.reject(response); + }, function (response) { + + if (response instanceof Error) { + _.error(response); + } + + return Promise.reject(response); + }); + + if (request.success) { + promise.success(request.success); + } + + if (request.error) { + promise.error(request.error); + } + + return promise; + } + + Http.options = { + method: 'get', + data: '', + params: {}, + headers: {}, + xhr: null, + upload: null, + jsonp: 'callback', + beforeSend: null, + crossOrigin: null, + emulateHTTP: false, + emulateJSON: false, + timeout: 0 + }; + + Http.interceptors = [__webpack_require__(14), __webpack_require__(15), __webpack_require__(16), __webpack_require__(18), __webpack_require__(19), __webpack_require__(20), __webpack_require__(21)]; + + Http.headers = { + put: jsonType, + post: jsonType, + patch: jsonType, + delete: jsonType, + common: { 'Accept': 'application/json, text/plain, */*' }, + custom: { 'X-Requested-With': 'XMLHttpRequest' } + }; + + ['get', 'put', 'post', 'patch', 'delete', 'jsonp'].forEach(function (method) { + + Http[method] = function (url, data, success, options) { + + if (_.isFunction(data)) { + options = success; + success = data; + data = undefined; + } + + if (_.isObject(success)) { + options = success; + success = undefined; + } + + return this(url, _.extend({ method: method, data: data, success: success }, options)); + }; + }); + + module.exports = _.http = Http; + +/***/ }, +/* 9 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Base client. + */ + + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); + var xhrClient = __webpack_require__(12); + + module.exports = function (request) { + + var response = (request.client || xhrClient)(request); + + return Promise.resolve(response).then(function (response) { + + if (response.headers) { + + var headers = parseHeaders(response.headers); + + response.headers = function (name) { + + if (name) { + return headers[_.toLower(name)]; + } + + return headers; + }; + } + + response.ok = response.status >= 200 && response.status < 300; + + return response; + }); + }; + + function parseHeaders(str) { + + var headers = {}, + value, + name, + i; + + if (_.isString(str)) { + _.each(str.split('\n'), function (row) { + + i = row.indexOf(':'); + name = _.trim(_.toLower(row.slice(0, i))); + value = _.trim(row.slice(i + 1)); + + if (headers[name]) { + + if (_.isArray(headers[name])) { + headers[name].push(value); + } else { + headers[name] = [headers[name], value]; + } + } else { + + headers[name] = value; + } + }); + } + + return headers; + } + +/***/ }, +/* 10 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Promise adapter. + */ + + var _ = __webpack_require__(1); + var PromiseObj = window.Promise || __webpack_require__(11); + + function Promise(executor, context) { + + if (executor instanceof PromiseObj) { + this.promise = executor; + } else { + this.promise = new PromiseObj(executor.bind(context)); + } + + this.context = context; + } + + Promise.all = function (iterable, context) { + return new Promise(PromiseObj.all(iterable), context); + }; + + Promise.resolve = function (value, context) { + return new Promise(PromiseObj.resolve(value), context); + }; + + Promise.reject = function (reason, context) { + return new Promise(PromiseObj.reject(reason), context); + }; + + Promise.race = function (iterable, context) { + return new Promise(PromiseObj.race(iterable), context); + }; + + var p = Promise.prototype; + + p.bind = function (context) { + this.context = context; + return this; + }; + + p.then = function (fulfilled, rejected) { + + if (fulfilled && fulfilled.bind && this.context) { + fulfilled = fulfilled.bind(this.context); + } + + if (rejected && rejected.bind && this.context) { + rejected = rejected.bind(this.context); + } + + this.promise = this.promise.then(fulfilled, rejected); + + return this; + }; + + p.catch = function (rejected) { + + if (rejected && rejected.bind && this.context) { + rejected = rejected.bind(this.context); + } + + this.promise = this.promise.catch(rejected); + + return this; + }; + + p.finally = function (callback) { + + return this.then(function (value) { + callback.call(this); + return value; + }, function (reason) { + callback.call(this); + return PromiseObj.reject(reason); + }); + }; + + p.success = function (callback) { + + _.warn('The `success` method has been deprecated. Use the `then` method instead.'); + + return this.then(function (response) { + return callback.call(this, response.data, response.status, response) || response; + }); + }; + + p.error = function (callback) { + + _.warn('The `error` method has been deprecated. Use the `catch` method instead.'); + + return this.catch(function (response) { + return callback.call(this, response.data, response.status, response) || response; + }); + }; + + p.always = function (callback) { + + _.warn('The `always` method has been deprecated. Use the `finally` method instead.'); + + var cb = function cb(response) { + return callback.call(this, response.data, response.status, response) || response; + }; + + return this.then(cb, cb); + }; + + module.exports = Promise; + +/***/ }, +/* 11 */ +/***/ function(module, exports, __webpack_require__) { + + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; + + /** + * Promises/A+ polyfill v1.1.4 (https://github.com/bramstein/promis) + */ + + var _ = __webpack_require__(1); + + var RESOLVED = 0; + var REJECTED = 1; + var PENDING = 2; + + function Promise(executor) { + + this.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.reject = function (r) { + return new Promise(function (resolve, reject) { + reject(r); + }); + }; + + Promise.resolve = function (x) { + return new Promise(function (resolve, reject) { + resolve(x); + }); + }; + + Promise.all = function all(iterable) { + return new Promise(function (resolve, reject) { + var count = 0, + result = []; + + if (iterable.length === 0) { + resolve(result); + } + + function resolver(i) { + return function (x) { + result[i] = x; + count += 1; + + if (count === iterable.length) { + resolve(result); + } + }; + } + + for (var i = 0; i < iterable.length; i += 1) { + Promise.resolve(iterable[i]).then(resolver(i), reject); + } + }); + }; + + Promise.race = function race(iterable) { + return new Promise(function (resolve, reject) { + for (var i = 0; i < iterable.length; i += 1) { + Promise.resolve(iterable[i]).then(resolve, reject); + } + }); + }; + + var p = Promise.prototype; + + p.resolve = function resolve(x) { + var promise = this; + + if (promise.state === PENDING) { + if (x === promise) { + throw new TypeError('Promise settled with itself.'); + } + + var called = false; + + try { + var then = x && x['then']; + + if (x !== null && (typeof x === 'undefined' ? 'undefined' : _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 = RESOLVED; + promise.value = x; + promise.notify(); + } + }; + + p.reject = function reject(reason) { + var promise = this; + + if (promise.state === PENDING) { + if (reason === promise) { + throw new TypeError('Promise settled with itself.'); + } + + promise.state = REJECTED; + promise.value = reason; + promise.notify(); + } + }; + + p.notify = function notify() { + var promise = this; + + _.nextTick(function () { + if (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 === RESOLVED) { + if (typeof onResolved === 'function') { + resolve(onResolved.call(undefined, promise.value)); + } else { + resolve(promise.value); + } + } else if (promise.state === REJECTED) { + if (typeof onRejected === 'function') { + resolve(onRejected.call(undefined, promise.value)); + } else { + reject(promise.value); + } + } + } catch (e) { + reject(e); + } + } + } + }); + }; + + p.then = function then(onResolved, onRejected) { + var promise = this; + + return new Promise(function (resolve, reject) { + promise.deferred.push([onResolved, onRejected, resolve, reject]); + promise.notify(); + }); + }; + + p.catch = function (onRejected) { + return this.then(undefined, onRejected); + }; + + module.exports = Promise; + +/***/ }, +/* 12 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * XMLHttp client. + */ + + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); + + module.exports = function (request) { + return new Promise(function (resolve) { + + var xhr = new XMLHttpRequest(), + response = { request: request }, + handler; + + request.cancel = function () { + xhr.abort(); + }; + + xhr.open(request.method, _.url(request), true); + + handler = function handler(event) { + + response.data = xhr.responseText; + response.status = xhr.status; + response.statusText = xhr.statusText; + response.headers = xhr.getAllResponseHeaders(); + + resolve(response); + }; + + xhr.timeout = 0; + xhr.onload = handler; + xhr.onabort = handler; + xhr.onerror = handler; + xhr.ontimeout = function () {}; + xhr.onprogress = function () {}; + + if (_.isPlainObject(request.xhr)) { + _.extend(xhr, request.xhr); + } + + if (_.isPlainObject(request.upload)) { + _.extend(xhr.upload, request.upload); + } + + _.each(request.headers || {}, function (value, header) { + xhr.setRequestHeader(header, value); + }); + + xhr.send(request.data); + }); + }; + +/***/ }, +/* 13 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Interceptor factory. + */ + + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); + + module.exports = function (handler, vm) { + + return function (client) { + + if (_.isFunction(handler)) { + handler = handler.call(vm, Promise); + } + + return function (request) { + + if (_.isFunction(handler.request)) { + request = handler.request.call(vm, request); + } + + return when(request, function (request) { + return when(client(request), function (response) { + + if (_.isFunction(handler.response)) { + response = handler.response.call(vm, response); + } + + return response; + }); + }); + }; + }; + }; + + function when(value, fulfilled, rejected) { + + var promise = Promise.resolve(value); + + if (arguments.length < 2) { + return promise; + } + + return promise.then(fulfilled, rejected); + } + +/***/ }, +/* 14 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Before Interceptor. + */ + + var _ = __webpack_require__(1); + + module.exports = { + + request: function request(_request) { + + if (_.isFunction(_request.beforeSend)) { + _request.beforeSend.call(this, _request); + } + + return _request; + } + + }; + +/***/ }, +/* 15 */ +/***/ function(module, exports) { + + /** + * Timeout Interceptor. + */ + + module.exports = function () { + + var timeout; + + return { + + request: function request(_request) { + + if (_request.timeout) { + timeout = setTimeout(function () { + _request.cancel(); + }, _request.timeout); + } + + return _request; + }, + + response: function response(_response) { + + clearTimeout(timeout); + + return _response; + } + + }; + }; + +/***/ }, +/* 16 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * JSONP Interceptor. + */ + + var jsonpClient = __webpack_require__(17); + + module.exports = { + + request: function request(_request) { + + if (_request.method == 'JSONP') { + _request.client = jsonpClient; + } + + return _request; + } + + }; + +/***/ }, +/* 17 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * JSONP client. + */ + + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); + + module.exports = function (request) { + return new Promise(function (resolve) { + + var callback = '_jsonp' + Math.random().toString(36).substr(2), + response = { request: request, data: null }, + handler, + script; + + request.params[request.jsonp] = callback; + request.cancel = function () { + handler({ type: 'cancel' }); + }; + + script = document.createElement('script'); + script.src = _.url(request); + script.type = 'text/javascript'; + script.async = true; + + window[callback] = function (data) { + response.data = data; + }; + + handler = function handler(event) { + + if (event.type === 'load' && response.data !== null) { + response.status = 200; + } else if (event.type === 'error') { + response.status = 404; + } else { + response.status = 0; + } + + resolve(response); + + delete window[callback]; + document.body.removeChild(script); + }; + + script.onload = handler; + script.onerror = handler; + + document.body.appendChild(script); + }); + }; + +/***/ }, +/* 18 */ +/***/ function(module, exports) { + + /** + * HTTP method override Interceptor. + */ + + module.exports = { + + request: function request(_request) { + + if (_request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(_request.method)) { + _request.headers['X-HTTP-Method-Override'] = _request.method; + _request.method = 'POST'; + } + + return _request; + } + + }; + +/***/ }, +/* 19 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Mime Interceptor. + */ + + var _ = __webpack_require__(1); + + module.exports = { + + request: function request(_request) { + + if (_request.emulateJSON && _.isPlainObject(_request.data)) { + _request.headers['Content-Type'] = 'application/x-www-form-urlencoded'; + _request.data = _.url.params(_request.data); + } + + if (_.isObject(_request.data) && /FormData/i.test(_request.data.toString())) { + delete _request.headers['Content-Type']; + } + + if (_.isPlainObject(_request.data)) { + _request.data = JSON.stringify(_request.data); + } + + return _request; + }, + + response: function response(_response) { + + try { + _response.data = JSON.parse(_response.data); + } catch (e) {} + + return _response; + } + + }; + +/***/ }, +/* 20 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Header Interceptor. + */ + + var _ = __webpack_require__(1); + + module.exports = { + + request: function request(_request) { + + _request.method = _request.method.toUpperCase(); + _request.headers = _.extend({}, _.http.headers.common, !_request.crossOrigin ? _.http.headers.custom : {}, _.http.headers[_request.method.toLowerCase()], _request.headers); + + if (_.isPlainObject(_request.data) && /^(GET|JSONP)$/i.test(_request.method)) { + _.extend(_request.params, _request.data); + delete _request.data; + } + + return _request; + } + + }; + +/***/ }, +/* 21 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * CORS Interceptor. + */ + + var _ = __webpack_require__(1); + var xdrClient = __webpack_require__(22); + var xhrCors = 'withCredentials' in new XMLHttpRequest(); + var originUrl = _.url.parse(location.href); + + module.exports = { + + request: function request(_request) { + + if (_request.crossOrigin === null) { + _request.crossOrigin = crossOrigin(_request); + } + + if (_request.crossOrigin) { + + if (!xhrCors) { + _request.client = xdrClient; + } + + _request.emulateHTTP = false; + } + + return _request; + } + + }; + + function crossOrigin(request) { + + var requestUrl = _.url.parse(_.url(request)); + + return requestUrl.protocol !== originUrl.protocol || requestUrl.host !== originUrl.host; + } + +/***/ }, +/* 22 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * XDomain client (Internet Explorer). + */ + + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); + + module.exports = function (request) { + return new Promise(function (resolve) { + + var xdr = new XDomainRequest(), + response = { request: request }, + handler; + + request.cancel = function () { + xdr.abort(); + }; + + xdr.open(request.method, _.url(request), true); + + handler = function handler(event) { + + response.data = xdr.responseText; + response.status = xdr.status; + response.statusText = xdr.statusText; + + resolve(response); + }; + + xdr.timeout = 0; + xdr.onload = handler; + xdr.onabort = handler; + xdr.onerror = handler; + xdr.ontimeout = function () {}; + xdr.onprogress = function () {}; + + xdr.send(request.data); + }); + }; + +/***/ }, +/* 23 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Service for interacting with RESTful services. + */ + + var _ = __webpack_require__(1); + + function Resource(url, params, actions, options) { + + var self = this, + resource = {}; + + actions = _.extend({}, Resource.actions, actions); + + _.each(actions, function (action, name) { + + action = _.merge({ url: url, params: params || {} }, options, action); + + resource[name] = function () { + return (self.$http || _.http)(opts(action, arguments)); + }; + }); + + return resource; + } + + function opts(action, args) { + + var options = _.extend({}, action), + params = {}, + data, + success, + error; + + switch (args.length) { + + case 4: + + error = args[3]; + success = args[2]; + + case 3: + case 2: + + if (_.isFunction(args[1])) { + + if (_.isFunction(args[0])) { + + success = args[0]; + error = args[1]; + + break; + } + + success = args[1]; + error = args[2]; + } else { + + params = args[0]; + data = args[1]; + success = args[2]; + + break; + } + + case 1: + + if (_.isFunction(args[0])) { + success = args[0]; + } else if (/^(POST|PUT|PATCH)$/i.test(options.method)) { + data = args[0]; + } else { + params = args[0]; + } + + break; + + case 0: + + break; + + default: + + throw 'Expected up to 4 arguments [params, data, success, error], got ' + args.length + ' arguments'; + } + + options.data = data; + options.params = _.extend({}, options.params, params); + + if (success) { + options.success = success; + } + + if (error) { + options.error = error; + } + + return options; + } + + Resource.actions = { + + get: { method: 'GET' }, + save: { method: 'POST' }, + query: { method: 'GET' }, + update: { method: 'PUT' }, + remove: { method: 'DELETE' }, + delete: { method: 'DELETE' } + + }; + + module.exports = _.resource = Resource; + +/***/ } +/******/ ]); \ No newline at end of file diff --git a/dist/vue-resource.js b/dist/vue-resource.js index cf69b805..bcd8c89e 100644 --- a/dist/vue-resource.js +++ b/dist/vue-resource.js @@ -1,5 +1,5 @@ /** - * vue-resource v0.7.1 + * vue-resource v0.7.2 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ diff --git a/dist/vue-resource.min.js b/dist/vue-resource.min.js index 330bf68a..a0510582 100644 --- a/dist/vue-resource.min.js +++ b/dist/vue-resource.min.js @@ -1,5 +1,5 @@ /** - * vue-resource v0.7.1 + * vue-resource v0.7.2 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ diff --git a/package.json b/package.json index 467bb3d0..8592813b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "vue-resource", - "version": "0.7.1", + "version": "0.7.2", "description": "A web request service for Vue.js", - "main": "src/index.js", + "main": "dist/vue-resource.common.js", "scripts": { "build": "webpack --config build/webpack.build.config.js", "test": "webpack --config test/webpack.config.js"