diff --git a/README.md b/README.md index 980c4e40..417369ec 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,21 @@ $ bower install vue-resource ``` ### CDN -Available on [jsdelivr](https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js), [cdnjs](https://cdnjs.com/libraries/vue-resource) or [unpkg](https://unpkg.com/vue-resource@1.0.3/dist/vue-resource.min.js). +Available on [jsdelivr](https://cdn.jsdelivr.net/vue.resource/1.1.0/vue-resource.min.js), [cdnjs](https://cdnjs.com/libraries/vue-resource) or [unpkg](https://unpkg.com/vue-resource@1.1.0/dist/vue-resource.min.js). ```html - + ``` ## Example ```js { // GET /someUrl - this.$http.get('/someUrl').then((response) => { - // success callback - }, (response) => { + this.$http.get('/someUrl').then(response => { + + // get body data + this.someData = response.body; + + }, response => { // error callback }); } diff --git a/bower.json b/bower.json index a30a0bd8..5de75b3e 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "vue-resource", "main": "dist/vue-resource.js", - "version": "1.0.3", + "version": "1.1.0", "description": "The HTTP client for Vue.js", "homepage": "https://github.com/vuejs/vue-resource", "license": "MIT", diff --git a/build/build.js b/build/build.js index 71e21a93..f4194e7d 100644 --- a/build/build.js +++ b/build/build.js @@ -1,7 +1,7 @@ var fs = require('fs'); var rollup = require('rollup'); var uglify = require('uglify-js'); -var babel = require('rollup-plugin-babel'); +var buble = require('rollup-plugin-buble'); var package = require('../package.json'); var banner = "/*!\n" + @@ -12,11 +12,7 @@ var banner = rollup.rollup({ entry: 'src/index.js', - plugins: [ - babel({ - presets: ['es2015-loose-rollup'] - }) - ] + plugins: [buble()] }) .then(function (bundle) { return write('dist/vue-resource.js', bundle.generate({ @@ -32,6 +28,7 @@ rollup.rollup({ }) .then(function (bundle) { return write('dist/vue-resource.es2015.js', bundle.generate({ + format: 'es', banner: banner, footer: 'export { Url, Http, Resource };' }).code, bundle); diff --git a/dist/vue-resource.common.js b/dist/vue-resource.common.js index b792ef01..13ffa743 100644 --- a/dist/vue-resource.common.js +++ b/dist/vue-resource.common.js @@ -1,5 +1,5 @@ /*! - * vue-resource v1.0.3 + * vue-resource v1.1.0 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ @@ -12,7 +12,7 @@ var RESOLVED = 0; var REJECTED = 1; -var PENDING = 2; +var PENDING = 2; function Promise$1(executor) { @@ -47,8 +47,7 @@ Promise$1.resolve = function (x) { Promise$1.all = function all(iterable) { return new Promise$1(function (resolve, reject) { - var count = 0, - result = []; + var count = 0, result = []; if (iterable.length === 0) { resolve(result); @@ -100,6 +99,7 @@ p$1.resolve = function resolve(x) { promise.resolve(x); } called = true; + }, function (r) { if (!called) { promise.reject(r); @@ -249,25 +249,31 @@ p.catch = function (rejected) { p.finally = function (callback) { return this.then(function (value) { - callback.call(this); - return value; - }, function (reason) { - callback.call(this); - return Promise.reject(reason); - }); + callback.call(this); + return value; + }, function (reason) { + callback.call(this); + return Promise.reject(reason); + } + ); }; /** * Utility functions. */ -var debug = false;var util = {};var slice = [].slice; +var debug = false; +var util = {}; +var ref = {}; +var hasOwnProperty = ref.hasOwnProperty; +var ref$1 = []; +var slice = ref$1.slice; -function Util (Vue) { +var Util = function (Vue) { util = Vue.util; debug = Vue.config.debug || !Vue.config.silent; -} +}; function warn(msg) { if (typeof console !== 'undefined' && debug) { @@ -286,7 +292,7 @@ function nextTick(cb, ctx) { } function trim(str) { - return str.replace(/^\s*|\s*$/g, ''); + return str ? str.replace(/^\s*|\s*$/g, '') : ''; } function toLower(str) { @@ -346,20 +352,20 @@ function options(fn, obj, opts) { opts = opts.call(obj); } - return merge(fn.bind({ $vm: obj, $options: opts }), fn, { $options: opts }); + return merge(fn.bind({$vm: obj, $options: opts}), fn, {$options: opts}); } function each(obj, iterator) { var i, key; - if (obj && typeof obj.length == 'number') { + if (isArray(obj)) { 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)) { + if (hasOwnProperty.call(obj, key)) { iterator.call(obj[key], obj[key], key); } } @@ -392,6 +398,7 @@ function defaults(target) { target[key] = source[key]; } } + }); return target; @@ -428,28 +435,26 @@ function _merge(target, source, deep) { * Root Prefix Transform. */ -function root (options, next) { +var root = function (options$$1, next) { - var url = next(options); + var url = next(options$$1); - if (isString(options.root) && !url.match(/^(https?:)?\//)) { - url = options.root + '/' + url; + if (isString(options$$1.root) && !url.match(/^(https?:)?\//)) { + url = options$$1.root + '/' + url; } return url; -} +}; /** * Query Parameter Transform. */ -function query (options, next) { +var query = function (options$$1, next) { - var urlParams = Object.keys(Url.options.params), - query = {}, - url = next(options); + var urlParams = Object.keys(Url.options.params), query = {}, url = next(options$$1); - each(options.params, function (value, key) { + each(options$$1.params, function (value, key) { if (urlParams.indexOf(key) === -1) { query[key] = value; } @@ -462,7 +467,7 @@ function query (options, next) { } return url; -} +}; /** * URL Template v2.0.6 (https://github.com/bramstein/url-template) @@ -470,8 +475,7 @@ function query (options, next) { function expand(url, params, variables) { - var tmpl = parse(url), - expanded = tmpl.expand(params); + var tmpl = parse(url), expanded = tmpl.expand(params); if (variables) { variables.push.apply(variables, tmpl.vars); @@ -482,17 +486,15 @@ function expand(url, params, variables) { function parse(template) { - var operators = ['+', '#', '.', '/', ';', '?', '&'], - variables = []; + var operators = ['+', '#', '.', '/', ';', '?', '&'], variables = []; return { vars: variables, - expand: function (context) { + expand: function expand(context) { return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) { if (expression) { - var operator = null, - values = []; + var operator = null, values = []; if (operators.indexOf(expression.charAt(0)) !== -1) { operator = expression.charAt(0); @@ -519,6 +521,7 @@ function parse(template) { } else { return values.join(','); } + } else { return encodeReserved(literal); } @@ -529,8 +532,7 @@ function parse(template) { function getValues(context, operator, key, modifier) { - var value = context[key], - result = []; + var value = context[key], result = []; if (isDefined(value) && value !== '') { if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { @@ -600,7 +602,7 @@ function isKeyOperator(operator) { function encodeValue(operator, value, key) { - value = operator === '+' || operator === '#' ? encodeReserved(value) : encodeURIComponent(value); + value = (operator === '+' || operator === '#') ? encodeReserved(value) : encodeURIComponent(value); if (key) { return encodeURIComponent(key) + '=' + value; @@ -622,17 +624,16 @@ function encodeReserved(str) { * URL Template (RFC 6570) Transform. */ -function template (options) { +var template = function (options) { - var variables = [], - url = expand(options.url, options.params, variables); + var variables = [], url = expand(options.url, options.params, variables); variables.forEach(function (key) { delete options.params[key]; }); return url; -} +}; /** * Service for URL templating. @@ -643,21 +644,19 @@ var el = document.createElement('a'); function Url(url, params) { - var self = this || {}, - options = url, - transform; + var self = this || {}, options$$1 = url, transform; if (isString(url)) { - options = { url: url, params: params }; + options$$1 = {url: url, params: params}; } - options = merge({}, Url.options, self.$options, options); + options$$1 = merge({}, Url.options, self.$options, options$$1); Url.transforms.forEach(function (handler) { transform = factory(handler, transform, self.$vm); }); - return transform(options); + return transform(options$$1); } /** @@ -684,8 +683,7 @@ Url.transforms = [template, query, root]; Url.params = function (obj) { - var params = [], - escape = encodeURIComponent; + var params = [], escape = encodeURIComponent; params.add = function (key, value) { @@ -733,16 +731,14 @@ Url.parse = function (url) { }; function factory(handler, next, vm) { - return function (options) { - return handler.call(vm, options, next); + return function (options$$1) { + return handler.call(vm, options$$1, next); }; } function serialize(params, obj, scope) { - var array = isArray(obj), - plain = isPlainObject(obj), - hash; + var array = isArray(obj), plain = isPlainObject(obj), hash; each(obj, function (value, key) { @@ -766,12 +762,11 @@ function serialize(params, obj, scope) { * XDomain client (Internet Explorer). */ -function xdrClient (request) { +var xdrClient = function (request) { return new PromiseObj(function (resolve) { - var xdr = new XDomainRequest(), - handler = function (_ref) { - var type = _ref.type; + var xdr = new XDomainRequest(), handler = function (ref) { + var type = ref.type; var status = 0; @@ -782,22 +777,25 @@ function xdrClient (request) { status = 500; } - resolve(request.respondWith(xdr.responseText, { status: status })); + resolve(request.respondWith(xdr.responseText, {status: status})); }; - request.abort = function () { - return xdr.abort(); - }; + request.abort = function () { return xdr.abort(); }; xdr.open(request.method, request.getUrl()); - xdr.timeout = 0; + + if (request.timeout) { + xdr.timeout = request.timeout; + } + xdr.onload = handler; + xdr.onabort = handler; xdr.onerror = handler; xdr.ontimeout = handler; xdr.onprogress = function () {}; xdr.send(request.getBody()); }); -} +}; /** * CORS Interceptor. @@ -806,7 +804,7 @@ function xdrClient (request) { var ORIGIN_URL = Url.parse(location.href); var SUPPORTS_CORS = 'withCredentials' in new XMLHttpRequest(); -function cors (request, next) { +var cors = function (request, next) { if (!isBoolean(request.crossOrigin) && crossOrigin(request)) { request.crossOrigin = true; @@ -822,24 +820,25 @@ function cors (request, next) { } next(); -} +}; function crossOrigin(request) { var requestUrl = Url.parse(Url(request)); - return requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host; + return (requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host); } /** * Body Interceptor. */ -function body (request, next) { +var body = function (request, next) { if (isFormData(request.body)) { request.headers.delete('Content-Type'); + } else if (isObject(request.body) || isArray(request.body)) { if (request.emulateJSON) { @@ -853,12 +852,15 @@ function body (request, next) { next(function (response) { Object.defineProperty(response, 'data', { - get: function () { + + get: function get() { return this.body; }, - set: function (body) { + + set: function set(body) { this.body = body; } + }); return response.bodyText ? when(response.text(), function (text) { @@ -872,30 +874,29 @@ function body (request, next) { } catch (e) { response.body = null; } + } else { response.body = text; } return response; + }) : response; + }); -} +}; /** * JSONP client. */ -function jsonpClient (request) { +var jsonpClient = function (request) { return new PromiseObj(function (resolve) { - var name = request.jsonp || 'callback', - callback = '_jsonp' + Math.random().toString(36).substr(2), - body = null, - handler, - script; + var name = request.jsonp || 'callback', callback = request.jsonpCallback || '_jsonp' + Math.random().toString(36).substr(2), body = null, handler, script; - handler = function (_ref) { - var type = _ref.type; + handler = function (ref) { + var type = ref.type; var status = 0; @@ -906,18 +907,28 @@ function jsonpClient (request) { status = 500; } - resolve(request.respondWith(body, { status: status })); + if (status && window[callback]) { + delete window[callback]; + document.body.removeChild(script); + } - delete window[callback]; - document.body.removeChild(script); + resolve(request.respondWith(body, {status: status})); }; - request.params[name] = callback; - window[callback] = function (result) { body = JSON.stringify(result); }; + request.abort = function () { + handler({type: 'abort'}); + }; + + request.params[name] = callback; + + if (request.timeout) { + setTimeout(request.abort, request.timeout); + } + script = document.createElement('script'); script.src = request.getUrl(); script.type = 'text/javascript'; @@ -927,13 +938,13 @@ function jsonpClient (request) { document.body.appendChild(script); }); -} +}; /** * JSONP Interceptor. */ -function jsonp (request, next) { +var jsonp = function (request, next) { if (request.method == 'JSONP') { request.client = jsonpClient; @@ -943,34 +954,35 @@ function jsonp (request, next) { if (request.method == 'JSONP') { - return when(response.json(), function (json) { - - response.body = json; + try { + response.body = JSON.parse(response.body); + } catch (e) { + response.body = null; + } - return response; - }); } + }); -} +}; /** * Before Interceptor. */ -function before (request, next) { +var before = function (request, next) { if (isFunction(request.before)) { request.before.call(this, request); } next(); -} +}; /** * HTTP method override Interceptor. */ -function method (request, next) { +var method = function (request, next) { if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) { request.headers.set('X-HTTP-Method-Override', request.method); @@ -978,15 +990,18 @@ function method (request, next) { } next(); -} +}; /** * Header Interceptor. */ -function header (request, next) { +var header = function (request, next) { - var headers = assign({}, Http.headers.common, !request.crossOrigin ? Http.headers.custom : {}, Http.headers[toLower(request.method)]); + var headers = assign({}, Http.headers.common, + !request.crossOrigin ? Http.headers.custom : {}, + Http.headers[toLower(request.method)] + ); each(headers, function (value, name) { if (!request.headers.has(name)) { @@ -995,42 +1010,23 @@ function header (request, next) { }); next(); -} - -/** - * Timeout Interceptor. - */ - -function timeout (request, next) { - - var timeout; - - if (request.timeout) { - timeout = setTimeout(function () { - request.abort(); - }, request.timeout); - } - - next(function (response) { - - clearTimeout(timeout); - }); -} +}; /** * XMLHttp client. */ -function xhrClient (request) { +var xhrClient = function (request) { return new PromiseObj(function (resolve) { - var xhr = new XMLHttpRequest(), - handler = function (event) { + var xhr = new XMLHttpRequest(), handler = function (event) { - var response = request.respondWith('response' in xhr ? xhr.response : xhr.responseText, { - status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug - statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText) - }); + var response = request.respondWith( + 'response' in xhr ? xhr.response : xhr.responseText, { + status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug + statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText) + } + ); each(trim(xhr.getAllResponseHeaders()).split('\n'), function (row) { response.headers.append(row.slice(0, row.indexOf(':')), row.slice(row.indexOf(':') + 1)); @@ -1039,9 +1035,7 @@ function xhrClient (request) { resolve(response); }; - request.abort = function () { - return xhr.abort(); - }; + request.abort = function () { return xhr.abort(); }; if (request.progress) { if (request.method === 'GET') { @@ -1057,6 +1051,10 @@ function xhrClient (request) { xhr.responseType = 'blob'; } + if (request.timeout) { + xhr.timeout = request.timeout; + } + if (request.credentials === true) { xhr.withCredentials = true; } @@ -1065,22 +1063,21 @@ function xhrClient (request) { xhr.setRequestHeader(name, value); }); - xhr.timeout = 0; xhr.onload = handler; + xhr.onabort = handler; xhr.onerror = handler; + xhr.ontimeout = handler; xhr.send(request.getBody()); }); -} +}; /** * Base client. */ -function Client (context) { +var Client = function (context) { - var reqHandlers = [sendRequest], - resHandlers = [], - handler; + var reqHandlers = [sendRequest], resHandlers = [], handler; if (!isObject(context)) { context = null; @@ -1096,7 +1093,7 @@ function Client (context) { if (isFunction(handler)) { handler.call(context, request, next); } else { - warn('Invalid interceptor of type ' + typeof handler + ', must be a function'); + warn(("Invalid interceptor of type " + (typeof handler) + ", must be a function")); next(); } } @@ -1106,6 +1103,7 @@ function Client (context) { if (isFunction(response)) { resHandlers.unshift(response); + } else if (isObject(response)) { resHandlers.forEach(function (handler) { @@ -1123,6 +1121,7 @@ function Client (context) { } exec(); + }, context); } @@ -1131,7 +1130,7 @@ function Client (context) { }; return Client; -} +}; function sendRequest(request, resolve) { @@ -1140,76 +1139,64 @@ function sendRequest(request, resolve) { resolve(client(request)); } -var classCallCheck = function (instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -}; - /** * HTTP Headers. */ -var Headers = function () { - function Headers(headers) { - var _this = this; - - classCallCheck(this, Headers); +var Headers = function Headers(headers) { + var this$1 = this; - this.map = {}; + this.map = {}; - each(headers, function (value, name) { - return _this.append(name, value); - }); - } + each(headers, function (value, name) { return this$1.append(name, value); }); +}; - Headers.prototype.has = function has(name) { - return getName(this.map, name) !== null; - }; +Headers.prototype.has = function has (name) { + return getName(this.map, name) !== null; +}; - Headers.prototype.get = function get(name) { +Headers.prototype.get = function get (name) { - var list = this.map[getName(this.map, name)]; + var list = this.map[getName(this.map, name)]; - return list ? list[0] : null; - }; + return list ? list[0] : null; +}; - Headers.prototype.getAll = function getAll(name) { - return this.map[getName(this.map, name)] || []; - }; +Headers.prototype.getAll = function getAll (name) { + return this.map[getName(this.map, name)] || []; +}; - Headers.prototype.set = function set(name, value) { - this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)]; - }; +Headers.prototype.set = function set (name, value) { + this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)]; +}; - Headers.prototype.append = function append(name, value) { +Headers.prototype.append = function append (name, value){ - var list = this.getAll(name); + var list = this.getAll(name); - if (list.length) { - list.push(trim(value)); - } else { - this.set(name, value); - } - }; + if (list.length) { + list.push(trim(value)); + } else { + this.set(name, value); + } +}; - Headers.prototype.delete = function _delete(name) { - delete this.map[getName(this.map, name)]; - }; +Headers.prototype.delete = function delete$1 (name){ + delete this.map[getName(this.map, name)]; +}; - Headers.prototype.forEach = function forEach(callback, thisArg) { - var _this2 = this; +Headers.prototype.deleteAll = function deleteAll (){ + this.map = {}; +}; - each(this.map, function (list, name) { - each(list, function (value) { - return callback.call(thisArg, value, name, _this2); - }); - }); - }; +Headers.prototype.forEach = function forEach (callback, thisArg) { + var this$1 = this; - return Headers; -}(); + each(this.map, function (list, name) { + each(list, function (value) { return callback.call(thisArg, value, name, this$1); }); + }); +}; function getName(map, name) { return Object.keys(map).reduce(function (prev, curr) { @@ -1230,51 +1217,45 @@ function normalizeName(name) { * HTTP Response. */ -var Response = function () { - function Response(body, _ref) { - var url = _ref.url; - var headers = _ref.headers; - var status = _ref.status; - var statusText = _ref.statusText; - classCallCheck(this, Response); +var Response = function Response(body, ref) { + var url = ref.url; + var headers = ref.headers; + var status = ref.status; + var statusText = ref.statusText; - this.url = url; - this.ok = status >= 200 && status < 300; - this.status = status || 0; - this.statusText = statusText || ''; - this.headers = new Headers(headers); - this.body = body; + this.url = url; + this.ok = status >= 200 && status < 300; + this.status = status || 0; + this.statusText = statusText || ''; + this.headers = new Headers(headers); + this.body = body; - if (isString(body)) { + if (isString(body)) { - this.bodyText = body; - } else if (isBlob(body)) { + this.bodyText = body; - this.bodyBlob = body; + } else if (isBlob(body)) { - if (isBlobText(body)) { - this.bodyText = blobText(body); - } + this.bodyBlob = body; + + if (isBlobText(body)) { + this.bodyText = blobText(body); } } +}; - Response.prototype.blob = function blob() { - return when(this.bodyBlob); - }; - - Response.prototype.text = function text() { - return when(this.bodyText); - }; +Response.prototype.blob = function blob () { + return when(this.bodyBlob); +}; - Response.prototype.json = function json() { - return when(this.text(), function (text) { - return JSON.parse(text); - }); - }; +Response.prototype.text = function text () { + return when(this.bodyText); +}; - return Response; -}(); +Response.prototype.json = function json () { + return when(this.text(), function (text) { return JSON.parse(text); }); +}; function blobText(body) { return new PromiseObj(function (resolve) { @@ -1285,6 +1266,7 @@ function blobText(body) { reader.onload = function () { resolve(reader.result); }; + }); } @@ -1296,60 +1278,54 @@ function isBlobText(body) { * HTTP Request. */ -var Request = function () { - function Request(options) { - classCallCheck(this, Request); - +var Request = function Request(options$$1) { - this.body = null; - this.params = {}; + this.body = null; + this.params = {}; - assign(this, options, { - method: toUpper(options.method || 'GET') - }); + assign(this, options$$1, { + method: toUpper(options$$1.method || 'GET') + }); - if (!(this.headers instanceof Headers)) { - this.headers = new Headers(this.headers); - } + if (!(this.headers instanceof Headers)) { + this.headers = new Headers(this.headers); } +}; - Request.prototype.getUrl = function getUrl() { - return Url(this); - }; - - Request.prototype.getBody = function getBody() { - return this.body; - }; +Request.prototype.getUrl = function getUrl (){ + return Url(this); +}; - Request.prototype.respondWith = function respondWith(body, options) { - return new Response(body, assign(options || {}, { url: this.getUrl() })); - }; +Request.prototype.getBody = function getBody (){ + return this.body; +}; - return Request; -}(); +Request.prototype.respondWith = function respondWith (body, options$$1) { + return new Response(body, assign(options$$1 || {}, {url: this.getUrl()})); +}; /** * Service for sending network requests. */ -var CUSTOM_HEADERS = { 'X-Requested-With': 'XMLHttpRequest' }; -var COMMON_HEADERS = { 'Accept': 'application/json, text/plain, */*' }; -var JSON_CONTENT_TYPE = { 'Content-Type': 'application/json;charset=utf-8' }; +var CUSTOM_HEADERS = {'X-Requested-With': 'XMLHttpRequest'}; +var COMMON_HEADERS = {'Accept': 'application/json, text/plain, */*'}; +var JSON_CONTENT_TYPE = {'Content-Type': 'application/json;charset=utf-8'}; -function Http(options) { +function Http(options$$1) { - var self = this || {}, - client = Client(self.$vm); + var self = this || {}, client = Client(self.$vm); - defaults(options || {}, self.$options, Http.options); + defaults(options$$1 || {}, self.$options, Http.options); Http.interceptors.forEach(function (handler) { client.use(handler); }); - return client(new Request(options)).then(function (response) { + return client(new Request(options$$1)).then(function (response) { return response.ok ? response : PromiseObj.reject(response); + }, function (response) { if (response instanceof Error) { @@ -1371,36 +1347,40 @@ Http.headers = { common: COMMON_HEADERS }; -Http.interceptors = [before, timeout, method, body, jsonp, header, cors]; +Http.interceptors = [before, method, body, jsonp, header, cors]; -['get', 'delete', 'head', 'jsonp'].forEach(function (method) { +['get', 'delete', 'head', 'jsonp'].forEach(function (method$$1) { - Http[method] = function (url, options) { - return this(assign(options || {}, { url: url, method: method })); + Http[method$$1] = function (url, options$$1) { + return this(assign(options$$1 || {}, {url: url, method: method$$1})); }; + }); -['post', 'put', 'patch'].forEach(function (method) { +['post', 'put', 'patch'].forEach(function (method$$1) { - Http[method] = function (url, body, options) { - return this(assign(options || {}, { url: url, method: method, body: body })); + Http[method$$1] = function (url, body$$1, options$$1) { + return this(assign(options$$1 || {}, {url: url, method: method$$1, body: body$$1})); }; + }); /** * Service for interacting with RESTful services. */ -function Resource(url, params, actions, options) { +function Resource(url, params, actions, options$$1) { - var self = this || {}, - resource = {}; + var self = this || {}, resource = {}; - actions = assign({}, Resource.actions, actions); + actions = assign({}, + Resource.actions, + actions + ); each(actions, function (action, name) { - action = merge({ url: url, params: assign({}, params) }, options, action); + action = merge({url: url, params: assign({}, params)}, options$$1, action); resource[name] = function () { return (self.$http || Http)(opts(action, arguments)); @@ -1412,9 +1392,7 @@ function Resource(url, params, actions, options) { function opts(action, args) { - var options = assign({}, action), - params = {}, - body; + var options$$1 = assign({}, action), params = {}, body; switch (args.length) { @@ -1427,7 +1405,7 @@ function opts(action, args) { case 1: - if (/^(POST|PUT|PATCH)$/i.test(options.method)) { + if (/^(POST|PUT|PATCH)$/i.test(options$$1.method)) { body = args[0]; } else { params = args[0]; @@ -1441,23 +1419,23 @@ function opts(action, args) { default: - throw 'Expected up to 4 arguments [params, body], got ' + args.length + ' arguments'; + throw 'Expected up to 2 arguments [params, body], got ' + args.length + ' arguments'; } - options.body = body; - options.params = assign({}, options.params, params); + options$$1.body = body; + options$$1.params = assign({}, options$$1.params, params); - return options; + return options$$1; } Resource.actions = { - get: { method: 'GET' }, - save: { method: 'POST' }, - query: { method: 'GET' }, - update: { method: 'PUT' }, - remove: { method: 'DELETE' }, - delete: { method: 'DELETE' } + get: {method: 'GET'}, + save: {method: 'POST'}, + query: {method: 'GET'}, + update: {method: 'PUT'}, + remove: {method: 'DELETE'}, + delete: {method: 'DELETE'} }; @@ -1481,30 +1459,28 @@ function plugin(Vue) { Object.defineProperties(Vue.prototype, { $url: { - get: function () { + get: function get() { return options(Vue.url, this, this.$options.url); } }, $http: { - get: function () { + get: function get() { return options(Vue.http, this, this.$options.http); } }, $resource: { - get: function () { + get: function get() { return Vue.resource.bind(this); } }, $promise: { - get: function () { - var _this = this; + get: function get() { + var this$1 = this; - return function (executor) { - return new Vue.Promise(executor, _this); - }; + return function (executor) { return new Vue.Promise(executor, this$1); }; } } @@ -1515,4 +1491,4 @@ if (typeof window !== 'undefined' && window.Vue) { window.Vue.use(plugin); } -module.exports = plugin; \ No newline at end of file +module.exports = plugin; diff --git a/dist/vue-resource.es2015.js b/dist/vue-resource.es2015.js index b445e4dd..05fe8be9 100644 --- a/dist/vue-resource.es2015.js +++ b/dist/vue-resource.es2015.js @@ -1,5 +1,5 @@ /*! - * vue-resource v1.0.3 + * vue-resource v1.1.0 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ @@ -10,7 +10,7 @@ var RESOLVED = 0; var REJECTED = 1; -var PENDING = 2; +var PENDING = 2; function Promise$1(executor) { @@ -45,8 +45,7 @@ Promise$1.resolve = function (x) { Promise$1.all = function all(iterable) { return new Promise$1(function (resolve, reject) { - var count = 0, - result = []; + var count = 0, result = []; if (iterable.length === 0) { resolve(result); @@ -98,6 +97,7 @@ p$1.resolve = function resolve(x) { promise.resolve(x); } called = true; + }, function (r) { if (!called) { promise.reject(r); @@ -247,25 +247,31 @@ p.catch = function (rejected) { p.finally = function (callback) { return this.then(function (value) { - callback.call(this); - return value; - }, function (reason) { - callback.call(this); - return Promise.reject(reason); - }); + callback.call(this); + return value; + }, function (reason) { + callback.call(this); + return Promise.reject(reason); + } + ); }; /** * Utility functions. */ -var debug = false;var util = {};var slice = [].slice; +var debug = false; +var util = {}; +var ref = {}; +var hasOwnProperty = ref.hasOwnProperty; +var ref$1 = []; +var slice = ref$1.slice; -function Util (Vue) { +var Util = function (Vue) { util = Vue.util; debug = Vue.config.debug || !Vue.config.silent; -} +}; function warn(msg) { if (typeof console !== 'undefined' && debug) { @@ -284,7 +290,7 @@ function nextTick(cb, ctx) { } function trim(str) { - return str.replace(/^\s*|\s*$/g, ''); + return str ? str.replace(/^\s*|\s*$/g, '') : ''; } function toLower(str) { @@ -344,20 +350,20 @@ function options(fn, obj, opts) { opts = opts.call(obj); } - return merge(fn.bind({ $vm: obj, $options: opts }), fn, { $options: opts }); + return merge(fn.bind({$vm: obj, $options: opts}), fn, {$options: opts}); } function each(obj, iterator) { var i, key; - if (obj && typeof obj.length == 'number') { + if (isArray(obj)) { 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)) { + if (hasOwnProperty.call(obj, key)) { iterator.call(obj[key], obj[key], key); } } @@ -390,6 +396,7 @@ function defaults(target) { target[key] = source[key]; } } + }); return target; @@ -426,28 +433,26 @@ function _merge(target, source, deep) { * Root Prefix Transform. */ -function root (options, next) { +var root = function (options$$1, next) { - var url = next(options); + var url = next(options$$1); - if (isString(options.root) && !url.match(/^(https?:)?\//)) { - url = options.root + '/' + url; + if (isString(options$$1.root) && !url.match(/^(https?:)?\//)) { + url = options$$1.root + '/' + url; } return url; -} +}; /** * Query Parameter Transform. */ -function query (options, next) { +var query = function (options$$1, next) { - var urlParams = Object.keys(Url.options.params), - query = {}, - url = next(options); + var urlParams = Object.keys(Url.options.params), query = {}, url = next(options$$1); - each(options.params, function (value, key) { + each(options$$1.params, function (value, key) { if (urlParams.indexOf(key) === -1) { query[key] = value; } @@ -460,7 +465,7 @@ function query (options, next) { } return url; -} +}; /** * URL Template v2.0.6 (https://github.com/bramstein/url-template) @@ -468,8 +473,7 @@ function query (options, next) { function expand(url, params, variables) { - var tmpl = parse(url), - expanded = tmpl.expand(params); + var tmpl = parse(url), expanded = tmpl.expand(params); if (variables) { variables.push.apply(variables, tmpl.vars); @@ -480,17 +484,15 @@ function expand(url, params, variables) { function parse(template) { - var operators = ['+', '#', '.', '/', ';', '?', '&'], - variables = []; + var operators = ['+', '#', '.', '/', ';', '?', '&'], variables = []; return { vars: variables, - expand: function (context) { + expand: function expand(context) { return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) { if (expression) { - var operator = null, - values = []; + var operator = null, values = []; if (operators.indexOf(expression.charAt(0)) !== -1) { operator = expression.charAt(0); @@ -517,6 +519,7 @@ function parse(template) { } else { return values.join(','); } + } else { return encodeReserved(literal); } @@ -527,8 +530,7 @@ function parse(template) { function getValues(context, operator, key, modifier) { - var value = context[key], - result = []; + var value = context[key], result = []; if (isDefined(value) && value !== '') { if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { @@ -598,7 +600,7 @@ function isKeyOperator(operator) { function encodeValue(operator, value, key) { - value = operator === '+' || operator === '#' ? encodeReserved(value) : encodeURIComponent(value); + value = (operator === '+' || operator === '#') ? encodeReserved(value) : encodeURIComponent(value); if (key) { return encodeURIComponent(key) + '=' + value; @@ -620,17 +622,16 @@ function encodeReserved(str) { * URL Template (RFC 6570) Transform. */ -function template (options) { +var template = function (options) { - var variables = [], - url = expand(options.url, options.params, variables); + var variables = [], url = expand(options.url, options.params, variables); variables.forEach(function (key) { delete options.params[key]; }); return url; -} +}; /** * Service for URL templating. @@ -641,21 +642,19 @@ var el = document.createElement('a'); function Url(url, params) { - var self = this || {}, - options = url, - transform; + var self = this || {}, options$$1 = url, transform; if (isString(url)) { - options = { url: url, params: params }; + options$$1 = {url: url, params: params}; } - options = merge({}, Url.options, self.$options, options); + options$$1 = merge({}, Url.options, self.$options, options$$1); Url.transforms.forEach(function (handler) { transform = factory(handler, transform, self.$vm); }); - return transform(options); + return transform(options$$1); } /** @@ -682,8 +681,7 @@ Url.transforms = [template, query, root]; Url.params = function (obj) { - var params = [], - escape = encodeURIComponent; + var params = [], escape = encodeURIComponent; params.add = function (key, value) { @@ -731,16 +729,14 @@ Url.parse = function (url) { }; function factory(handler, next, vm) { - return function (options) { - return handler.call(vm, options, next); + return function (options$$1) { + return handler.call(vm, options$$1, next); }; } function serialize(params, obj, scope) { - var array = isArray(obj), - plain = isPlainObject(obj), - hash; + var array = isArray(obj), plain = isPlainObject(obj), hash; each(obj, function (value, key) { @@ -764,12 +760,11 @@ function serialize(params, obj, scope) { * XDomain client (Internet Explorer). */ -function xdrClient (request) { +var xdrClient = function (request) { return new PromiseObj(function (resolve) { - var xdr = new XDomainRequest(), - handler = function (_ref) { - var type = _ref.type; + var xdr = new XDomainRequest(), handler = function (ref) { + var type = ref.type; var status = 0; @@ -780,22 +775,25 @@ function xdrClient (request) { status = 500; } - resolve(request.respondWith(xdr.responseText, { status: status })); + resolve(request.respondWith(xdr.responseText, {status: status})); }; - request.abort = function () { - return xdr.abort(); - }; + request.abort = function () { return xdr.abort(); }; xdr.open(request.method, request.getUrl()); - xdr.timeout = 0; + + if (request.timeout) { + xdr.timeout = request.timeout; + } + xdr.onload = handler; + xdr.onabort = handler; xdr.onerror = handler; xdr.ontimeout = handler; xdr.onprogress = function () {}; xdr.send(request.getBody()); }); -} +}; /** * CORS Interceptor. @@ -804,7 +802,7 @@ function xdrClient (request) { var ORIGIN_URL = Url.parse(location.href); var SUPPORTS_CORS = 'withCredentials' in new XMLHttpRequest(); -function cors (request, next) { +var cors = function (request, next) { if (!isBoolean(request.crossOrigin) && crossOrigin(request)) { request.crossOrigin = true; @@ -820,24 +818,25 @@ function cors (request, next) { } next(); -} +}; function crossOrigin(request) { var requestUrl = Url.parse(Url(request)); - return requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host; + return (requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host); } /** * Body Interceptor. */ -function body (request, next) { +var body = function (request, next) { if (isFormData(request.body)) { request.headers.delete('Content-Type'); + } else if (isObject(request.body) || isArray(request.body)) { if (request.emulateJSON) { @@ -851,12 +850,15 @@ function body (request, next) { next(function (response) { Object.defineProperty(response, 'data', { - get: function () { + + get: function get() { return this.body; }, - set: function (body) { + + set: function set(body) { this.body = body; } + }); return response.bodyText ? when(response.text(), function (text) { @@ -870,30 +872,29 @@ function body (request, next) { } catch (e) { response.body = null; } + } else { response.body = text; } return response; + }) : response; + }); -} +}; /** * JSONP client. */ -function jsonpClient (request) { +var jsonpClient = function (request) { return new PromiseObj(function (resolve) { - var name = request.jsonp || 'callback', - callback = '_jsonp' + Math.random().toString(36).substr(2), - body = null, - handler, - script; + var name = request.jsonp || 'callback', callback = request.jsonpCallback || '_jsonp' + Math.random().toString(36).substr(2), body = null, handler, script; - handler = function (_ref) { - var type = _ref.type; + handler = function (ref) { + var type = ref.type; var status = 0; @@ -904,18 +905,28 @@ function jsonpClient (request) { status = 500; } - resolve(request.respondWith(body, { status: status })); + if (status && window[callback]) { + delete window[callback]; + document.body.removeChild(script); + } - delete window[callback]; - document.body.removeChild(script); + resolve(request.respondWith(body, {status: status})); }; - request.params[name] = callback; - window[callback] = function (result) { body = JSON.stringify(result); }; + request.abort = function () { + handler({type: 'abort'}); + }; + + request.params[name] = callback; + + if (request.timeout) { + setTimeout(request.abort, request.timeout); + } + script = document.createElement('script'); script.src = request.getUrl(); script.type = 'text/javascript'; @@ -925,13 +936,13 @@ function jsonpClient (request) { document.body.appendChild(script); }); -} +}; /** * JSONP Interceptor. */ -function jsonp (request, next) { +var jsonp = function (request, next) { if (request.method == 'JSONP') { request.client = jsonpClient; @@ -941,34 +952,35 @@ function jsonp (request, next) { if (request.method == 'JSONP') { - return when(response.json(), function (json) { - - response.body = json; + try { + response.body = JSON.parse(response.body); + } catch (e) { + response.body = null; + } - return response; - }); } + }); -} +}; /** * Before Interceptor. */ -function before (request, next) { +var before = function (request, next) { if (isFunction(request.before)) { request.before.call(this, request); } next(); -} +}; /** * HTTP method override Interceptor. */ -function method (request, next) { +var method = function (request, next) { if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) { request.headers.set('X-HTTP-Method-Override', request.method); @@ -976,15 +988,18 @@ function method (request, next) { } next(); -} +}; /** * Header Interceptor. */ -function header (request, next) { +var header = function (request, next) { - var headers = assign({}, Http.headers.common, !request.crossOrigin ? Http.headers.custom : {}, Http.headers[toLower(request.method)]); + var headers = assign({}, Http.headers.common, + !request.crossOrigin ? Http.headers.custom : {}, + Http.headers[toLower(request.method)] + ); each(headers, function (value, name) { if (!request.headers.has(name)) { @@ -993,42 +1008,23 @@ function header (request, next) { }); next(); -} - -/** - * Timeout Interceptor. - */ - -function timeout (request, next) { - - var timeout; - - if (request.timeout) { - timeout = setTimeout(function () { - request.abort(); - }, request.timeout); - } - - next(function (response) { - - clearTimeout(timeout); - }); -} +}; /** * XMLHttp client. */ -function xhrClient (request) { +var xhrClient = function (request) { return new PromiseObj(function (resolve) { - var xhr = new XMLHttpRequest(), - handler = function (event) { + var xhr = new XMLHttpRequest(), handler = function (event) { - var response = request.respondWith('response' in xhr ? xhr.response : xhr.responseText, { - status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug - statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText) - }); + var response = request.respondWith( + 'response' in xhr ? xhr.response : xhr.responseText, { + status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug + statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText) + } + ); each(trim(xhr.getAllResponseHeaders()).split('\n'), function (row) { response.headers.append(row.slice(0, row.indexOf(':')), row.slice(row.indexOf(':') + 1)); @@ -1037,9 +1033,7 @@ function xhrClient (request) { resolve(response); }; - request.abort = function () { - return xhr.abort(); - }; + request.abort = function () { return xhr.abort(); }; if (request.progress) { if (request.method === 'GET') { @@ -1055,6 +1049,10 @@ function xhrClient (request) { xhr.responseType = 'blob'; } + if (request.timeout) { + xhr.timeout = request.timeout; + } + if (request.credentials === true) { xhr.withCredentials = true; } @@ -1063,22 +1061,21 @@ function xhrClient (request) { xhr.setRequestHeader(name, value); }); - xhr.timeout = 0; xhr.onload = handler; + xhr.onabort = handler; xhr.onerror = handler; + xhr.ontimeout = handler; xhr.send(request.getBody()); }); -} +}; /** * Base client. */ -function Client (context) { +var Client = function (context) { - var reqHandlers = [sendRequest], - resHandlers = [], - handler; + var reqHandlers = [sendRequest], resHandlers = [], handler; if (!isObject(context)) { context = null; @@ -1094,7 +1091,7 @@ function Client (context) { if (isFunction(handler)) { handler.call(context, request, next); } else { - warn('Invalid interceptor of type ' + typeof handler + ', must be a function'); + warn(("Invalid interceptor of type " + (typeof handler) + ", must be a function")); next(); } } @@ -1104,6 +1101,7 @@ function Client (context) { if (isFunction(response)) { resHandlers.unshift(response); + } else if (isObject(response)) { resHandlers.forEach(function (handler) { @@ -1121,6 +1119,7 @@ function Client (context) { } exec(); + }, context); } @@ -1129,7 +1128,7 @@ function Client (context) { }; return Client; -} +}; function sendRequest(request, resolve) { @@ -1138,76 +1137,64 @@ function sendRequest(request, resolve) { resolve(client(request)); } -var classCallCheck = function (instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -}; - /** * HTTP Headers. */ -var Headers = function () { - function Headers(headers) { - var _this = this; - - classCallCheck(this, Headers); +var Headers = function Headers(headers) { + var this$1 = this; - this.map = {}; + this.map = {}; - each(headers, function (value, name) { - return _this.append(name, value); - }); - } + each(headers, function (value, name) { return this$1.append(name, value); }); +}; - Headers.prototype.has = function has(name) { - return getName(this.map, name) !== null; - }; +Headers.prototype.has = function has (name) { + return getName(this.map, name) !== null; +}; - Headers.prototype.get = function get(name) { +Headers.prototype.get = function get (name) { - var list = this.map[getName(this.map, name)]; + var list = this.map[getName(this.map, name)]; - return list ? list[0] : null; - }; + return list ? list[0] : null; +}; - Headers.prototype.getAll = function getAll(name) { - return this.map[getName(this.map, name)] || []; - }; +Headers.prototype.getAll = function getAll (name) { + return this.map[getName(this.map, name)] || []; +}; - Headers.prototype.set = function set(name, value) { - this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)]; - }; +Headers.prototype.set = function set (name, value) { + this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)]; +}; - Headers.prototype.append = function append(name, value) { +Headers.prototype.append = function append (name, value){ - var list = this.getAll(name); + var list = this.getAll(name); - if (list.length) { - list.push(trim(value)); - } else { - this.set(name, value); - } - }; + if (list.length) { + list.push(trim(value)); + } else { + this.set(name, value); + } +}; - Headers.prototype.delete = function _delete(name) { - delete this.map[getName(this.map, name)]; - }; +Headers.prototype.delete = function delete$1 (name){ + delete this.map[getName(this.map, name)]; +}; - Headers.prototype.forEach = function forEach(callback, thisArg) { - var _this2 = this; +Headers.prototype.deleteAll = function deleteAll (){ + this.map = {}; +}; - each(this.map, function (list, name) { - each(list, function (value) { - return callback.call(thisArg, value, name, _this2); - }); - }); - }; +Headers.prototype.forEach = function forEach (callback, thisArg) { + var this$1 = this; - return Headers; -}(); + each(this.map, function (list, name) { + each(list, function (value) { return callback.call(thisArg, value, name, this$1); }); + }); +}; function getName(map, name) { return Object.keys(map).reduce(function (prev, curr) { @@ -1228,51 +1215,45 @@ function normalizeName(name) { * HTTP Response. */ -var Response = function () { - function Response(body, _ref) { - var url = _ref.url; - var headers = _ref.headers; - var status = _ref.status; - var statusText = _ref.statusText; - classCallCheck(this, Response); +var Response = function Response(body, ref) { + var url = ref.url; + var headers = ref.headers; + var status = ref.status; + var statusText = ref.statusText; - this.url = url; - this.ok = status >= 200 && status < 300; - this.status = status || 0; - this.statusText = statusText || ''; - this.headers = new Headers(headers); - this.body = body; + this.url = url; + this.ok = status >= 200 && status < 300; + this.status = status || 0; + this.statusText = statusText || ''; + this.headers = new Headers(headers); + this.body = body; - if (isString(body)) { + if (isString(body)) { - this.bodyText = body; - } else if (isBlob(body)) { + this.bodyText = body; - this.bodyBlob = body; + } else if (isBlob(body)) { - if (isBlobText(body)) { - this.bodyText = blobText(body); - } + this.bodyBlob = body; + + if (isBlobText(body)) { + this.bodyText = blobText(body); } } +}; - Response.prototype.blob = function blob() { - return when(this.bodyBlob); - }; - - Response.prototype.text = function text() { - return when(this.bodyText); - }; +Response.prototype.blob = function blob () { + return when(this.bodyBlob); +}; - Response.prototype.json = function json() { - return when(this.text(), function (text) { - return JSON.parse(text); - }); - }; +Response.prototype.text = function text () { + return when(this.bodyText); +}; - return Response; -}(); +Response.prototype.json = function json () { + return when(this.text(), function (text) { return JSON.parse(text); }); +}; function blobText(body) { return new PromiseObj(function (resolve) { @@ -1283,6 +1264,7 @@ function blobText(body) { reader.onload = function () { resolve(reader.result); }; + }); } @@ -1294,60 +1276,54 @@ function isBlobText(body) { * HTTP Request. */ -var Request = function () { - function Request(options) { - classCallCheck(this, Request); - +var Request = function Request(options$$1) { - this.body = null; - this.params = {}; + this.body = null; + this.params = {}; - assign(this, options, { - method: toUpper(options.method || 'GET') - }); + assign(this, options$$1, { + method: toUpper(options$$1.method || 'GET') + }); - if (!(this.headers instanceof Headers)) { - this.headers = new Headers(this.headers); - } + if (!(this.headers instanceof Headers)) { + this.headers = new Headers(this.headers); } +}; - Request.prototype.getUrl = function getUrl() { - return Url(this); - }; - - Request.prototype.getBody = function getBody() { - return this.body; - }; +Request.prototype.getUrl = function getUrl (){ + return Url(this); +}; - Request.prototype.respondWith = function respondWith(body, options) { - return new Response(body, assign(options || {}, { url: this.getUrl() })); - }; +Request.prototype.getBody = function getBody (){ + return this.body; +}; - return Request; -}(); +Request.prototype.respondWith = function respondWith (body, options$$1) { + return new Response(body, assign(options$$1 || {}, {url: this.getUrl()})); +}; /** * Service for sending network requests. */ -var CUSTOM_HEADERS = { 'X-Requested-With': 'XMLHttpRequest' }; -var COMMON_HEADERS = { 'Accept': 'application/json, text/plain, */*' }; -var JSON_CONTENT_TYPE = { 'Content-Type': 'application/json;charset=utf-8' }; +var CUSTOM_HEADERS = {'X-Requested-With': 'XMLHttpRequest'}; +var COMMON_HEADERS = {'Accept': 'application/json, text/plain, */*'}; +var JSON_CONTENT_TYPE = {'Content-Type': 'application/json;charset=utf-8'}; -function Http(options) { +function Http(options$$1) { - var self = this || {}, - client = Client(self.$vm); + var self = this || {}, client = Client(self.$vm); - defaults(options || {}, self.$options, Http.options); + defaults(options$$1 || {}, self.$options, Http.options); Http.interceptors.forEach(function (handler) { client.use(handler); }); - return client(new Request(options)).then(function (response) { + return client(new Request(options$$1)).then(function (response) { return response.ok ? response : PromiseObj.reject(response); + }, function (response) { if (response instanceof Error) { @@ -1369,36 +1345,40 @@ Http.headers = { common: COMMON_HEADERS }; -Http.interceptors = [before, timeout, method, body, jsonp, header, cors]; +Http.interceptors = [before, method, body, jsonp, header, cors]; -['get', 'delete', 'head', 'jsonp'].forEach(function (method) { +['get', 'delete', 'head', 'jsonp'].forEach(function (method$$1) { - Http[method] = function (url, options) { - return this(assign(options || {}, { url: url, method: method })); + Http[method$$1] = function (url, options$$1) { + return this(assign(options$$1 || {}, {url: url, method: method$$1})); }; + }); -['post', 'put', 'patch'].forEach(function (method) { +['post', 'put', 'patch'].forEach(function (method$$1) { - Http[method] = function (url, body, options) { - return this(assign(options || {}, { url: url, method: method, body: body })); + Http[method$$1] = function (url, body$$1, options$$1) { + return this(assign(options$$1 || {}, {url: url, method: method$$1, body: body$$1})); }; + }); /** * Service for interacting with RESTful services. */ -function Resource(url, params, actions, options) { +function Resource(url, params, actions, options$$1) { - var self = this || {}, - resource = {}; + var self = this || {}, resource = {}; - actions = assign({}, Resource.actions, actions); + actions = assign({}, + Resource.actions, + actions + ); each(actions, function (action, name) { - action = merge({ url: url, params: assign({}, params) }, options, action); + action = merge({url: url, params: assign({}, params)}, options$$1, action); resource[name] = function () { return (self.$http || Http)(opts(action, arguments)); @@ -1410,9 +1390,7 @@ function Resource(url, params, actions, options) { function opts(action, args) { - var options = assign({}, action), - params = {}, - body; + var options$$1 = assign({}, action), params = {}, body; switch (args.length) { @@ -1425,7 +1403,7 @@ function opts(action, args) { case 1: - if (/^(POST|PUT|PATCH)$/i.test(options.method)) { + if (/^(POST|PUT|PATCH)$/i.test(options$$1.method)) { body = args[0]; } else { params = args[0]; @@ -1439,23 +1417,23 @@ function opts(action, args) { default: - throw 'Expected up to 4 arguments [params, body], got ' + args.length + ' arguments'; + throw 'Expected up to 2 arguments [params, body], got ' + args.length + ' arguments'; } - options.body = body; - options.params = assign({}, options.params, params); + options$$1.body = body; + options$$1.params = assign({}, options$$1.params, params); - return options; + return options$$1; } Resource.actions = { - get: { method: 'GET' }, - save: { method: 'POST' }, - query: { method: 'GET' }, - update: { method: 'PUT' }, - remove: { method: 'DELETE' }, - delete: { method: 'DELETE' } + get: {method: 'GET'}, + save: {method: 'POST'}, + query: {method: 'GET'}, + update: {method: 'PUT'}, + remove: {method: 'DELETE'}, + delete: {method: 'DELETE'} }; @@ -1479,30 +1457,28 @@ function plugin(Vue) { Object.defineProperties(Vue.prototype, { $url: { - get: function () { + get: function get() { return options(Vue.url, this, this.$options.url); } }, $http: { - get: function () { + get: function get() { return options(Vue.http, this, this.$options.http); } }, $resource: { - get: function () { + get: function get() { return Vue.resource.bind(this); } }, $promise: { - get: function () { - var _this = this; + get: function get() { + var this$1 = this; - return function (executor) { - return new Vue.Promise(executor, _this); - }; + return function (executor) { return new Vue.Promise(executor, this$1); }; } } @@ -1514,4 +1490,4 @@ if (typeof window !== 'undefined' && window.Vue) { } export default plugin; -export { Url, Http, Resource }; \ No newline at end of file +export { Url, Http, Resource }; diff --git a/dist/vue-resource.js b/dist/vue-resource.js index 2937451d..7f7cc054 100644 --- a/dist/vue-resource.js +++ b/dist/vue-resource.js @@ -1,13 +1,13 @@ /*! - * vue-resource v1.0.3 + * vue-resource v1.1.0 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global.VueResource = factory()); + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global.VueResource = factory()); }(this, (function () { 'use strict'; /** @@ -16,7 +16,7 @@ var RESOLVED = 0; var REJECTED = 1; -var PENDING = 2; +var PENDING = 2; function Promise$1(executor) { @@ -51,8 +51,7 @@ Promise$1.resolve = function (x) { Promise$1.all = function all(iterable) { return new Promise$1(function (resolve, reject) { - var count = 0, - result = []; + var count = 0, result = []; if (iterable.length === 0) { resolve(result); @@ -104,6 +103,7 @@ p$1.resolve = function resolve(x) { promise.resolve(x); } called = true; + }, function (r) { if (!called) { promise.reject(r); @@ -253,25 +253,31 @@ p.catch = function (rejected) { p.finally = function (callback) { return this.then(function (value) { - callback.call(this); - return value; - }, function (reason) { - callback.call(this); - return Promise.reject(reason); - }); + callback.call(this); + return value; + }, function (reason) { + callback.call(this); + return Promise.reject(reason); + } + ); }; /** * Utility functions. */ -var debug = false;var util = {};var slice = [].slice; +var debug = false; +var util = {}; +var ref = {}; +var hasOwnProperty = ref.hasOwnProperty; +var ref$1 = []; +var slice = ref$1.slice; -function Util (Vue) { +var Util = function (Vue) { util = Vue.util; debug = Vue.config.debug || !Vue.config.silent; -} +}; function warn(msg) { if (typeof console !== 'undefined' && debug) { @@ -290,7 +296,7 @@ function nextTick(cb, ctx) { } function trim(str) { - return str.replace(/^\s*|\s*$/g, ''); + return str ? str.replace(/^\s*|\s*$/g, '') : ''; } function toLower(str) { @@ -350,20 +356,20 @@ function options(fn, obj, opts) { opts = opts.call(obj); } - return merge(fn.bind({ $vm: obj, $options: opts }), fn, { $options: opts }); + return merge(fn.bind({$vm: obj, $options: opts}), fn, {$options: opts}); } function each(obj, iterator) { var i, key; - if (obj && typeof obj.length == 'number') { + if (isArray(obj)) { 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)) { + if (hasOwnProperty.call(obj, key)) { iterator.call(obj[key], obj[key], key); } } @@ -396,6 +402,7 @@ function defaults(target) { target[key] = source[key]; } } + }); return target; @@ -432,28 +439,26 @@ function _merge(target, source, deep) { * Root Prefix Transform. */ -function root (options, next) { +var root = function (options$$1, next) { - var url = next(options); + var url = next(options$$1); - if (isString(options.root) && !url.match(/^(https?:)?\//)) { - url = options.root + '/' + url; + if (isString(options$$1.root) && !url.match(/^(https?:)?\//)) { + url = options$$1.root + '/' + url; } return url; -} +}; /** * Query Parameter Transform. */ -function query (options, next) { +var query = function (options$$1, next) { - var urlParams = Object.keys(Url.options.params), - query = {}, - url = next(options); + var urlParams = Object.keys(Url.options.params), query = {}, url = next(options$$1); - each(options.params, function (value, key) { + each(options$$1.params, function (value, key) { if (urlParams.indexOf(key) === -1) { query[key] = value; } @@ -466,7 +471,7 @@ function query (options, next) { } return url; -} +}; /** * URL Template v2.0.6 (https://github.com/bramstein/url-template) @@ -474,8 +479,7 @@ function query (options, next) { function expand(url, params, variables) { - var tmpl = parse(url), - expanded = tmpl.expand(params); + var tmpl = parse(url), expanded = tmpl.expand(params); if (variables) { variables.push.apply(variables, tmpl.vars); @@ -486,17 +490,15 @@ function expand(url, params, variables) { function parse(template) { - var operators = ['+', '#', '.', '/', ';', '?', '&'], - variables = []; + var operators = ['+', '#', '.', '/', ';', '?', '&'], variables = []; return { vars: variables, - expand: function (context) { + expand: function expand(context) { return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) { if (expression) { - var operator = null, - values = []; + var operator = null, values = []; if (operators.indexOf(expression.charAt(0)) !== -1) { operator = expression.charAt(0); @@ -523,6 +525,7 @@ function parse(template) { } else { return values.join(','); } + } else { return encodeReserved(literal); } @@ -533,8 +536,7 @@ function parse(template) { function getValues(context, operator, key, modifier) { - var value = context[key], - result = []; + var value = context[key], result = []; if (isDefined(value) && value !== '') { if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { @@ -604,7 +606,7 @@ function isKeyOperator(operator) { function encodeValue(operator, value, key) { - value = operator === '+' || operator === '#' ? encodeReserved(value) : encodeURIComponent(value); + value = (operator === '+' || operator === '#') ? encodeReserved(value) : encodeURIComponent(value); if (key) { return encodeURIComponent(key) + '=' + value; @@ -626,17 +628,16 @@ function encodeReserved(str) { * URL Template (RFC 6570) Transform. */ -function template (options) { +var template = function (options) { - var variables = [], - url = expand(options.url, options.params, variables); + var variables = [], url = expand(options.url, options.params, variables); variables.forEach(function (key) { delete options.params[key]; }); return url; -} +}; /** * Service for URL templating. @@ -647,21 +648,19 @@ var el = document.createElement('a'); function Url(url, params) { - var self = this || {}, - options = url, - transform; + var self = this || {}, options$$1 = url, transform; if (isString(url)) { - options = { url: url, params: params }; + options$$1 = {url: url, params: params}; } - options = merge({}, Url.options, self.$options, options); + options$$1 = merge({}, Url.options, self.$options, options$$1); Url.transforms.forEach(function (handler) { transform = factory(handler, transform, self.$vm); }); - return transform(options); + return transform(options$$1); } /** @@ -688,8 +687,7 @@ Url.transforms = [template, query, root]; Url.params = function (obj) { - var params = [], - escape = encodeURIComponent; + var params = [], escape = encodeURIComponent; params.add = function (key, value) { @@ -737,16 +735,14 @@ Url.parse = function (url) { }; function factory(handler, next, vm) { - return function (options) { - return handler.call(vm, options, next); + return function (options$$1) { + return handler.call(vm, options$$1, next); }; } function serialize(params, obj, scope) { - var array = isArray(obj), - plain = isPlainObject(obj), - hash; + var array = isArray(obj), plain = isPlainObject(obj), hash; each(obj, function (value, key) { @@ -770,12 +766,11 @@ function serialize(params, obj, scope) { * XDomain client (Internet Explorer). */ -function xdrClient (request) { +var xdrClient = function (request) { return new PromiseObj(function (resolve) { - var xdr = new XDomainRequest(), - handler = function (_ref) { - var type = _ref.type; + var xdr = new XDomainRequest(), handler = function (ref) { + var type = ref.type; var status = 0; @@ -786,22 +781,25 @@ function xdrClient (request) { status = 500; } - resolve(request.respondWith(xdr.responseText, { status: status })); + resolve(request.respondWith(xdr.responseText, {status: status})); }; - request.abort = function () { - return xdr.abort(); - }; + request.abort = function () { return xdr.abort(); }; xdr.open(request.method, request.getUrl()); - xdr.timeout = 0; + + if (request.timeout) { + xdr.timeout = request.timeout; + } + xdr.onload = handler; + xdr.onabort = handler; xdr.onerror = handler; xdr.ontimeout = handler; xdr.onprogress = function () {}; xdr.send(request.getBody()); }); -} +}; /** * CORS Interceptor. @@ -810,7 +808,7 @@ function xdrClient (request) { var ORIGIN_URL = Url.parse(location.href); var SUPPORTS_CORS = 'withCredentials' in new XMLHttpRequest(); -function cors (request, next) { +var cors = function (request, next) { if (!isBoolean(request.crossOrigin) && crossOrigin(request)) { request.crossOrigin = true; @@ -826,24 +824,25 @@ function cors (request, next) { } next(); -} +}; function crossOrigin(request) { var requestUrl = Url.parse(Url(request)); - return requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host; + return (requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host); } /** * Body Interceptor. */ -function body (request, next) { +var body = function (request, next) { if (isFormData(request.body)) { request.headers.delete('Content-Type'); + } else if (isObject(request.body) || isArray(request.body)) { if (request.emulateJSON) { @@ -857,12 +856,15 @@ function body (request, next) { next(function (response) { Object.defineProperty(response, 'data', { - get: function () { + + get: function get() { return this.body; }, - set: function (body) { + + set: function set(body) { this.body = body; } + }); return response.bodyText ? when(response.text(), function (text) { @@ -876,30 +878,29 @@ function body (request, next) { } catch (e) { response.body = null; } + } else { response.body = text; } return response; + }) : response; + }); -} +}; /** * JSONP client. */ -function jsonpClient (request) { +var jsonpClient = function (request) { return new PromiseObj(function (resolve) { - var name = request.jsonp || 'callback', - callback = '_jsonp' + Math.random().toString(36).substr(2), - body = null, - handler, - script; + var name = request.jsonp || 'callback', callback = request.jsonpCallback || '_jsonp' + Math.random().toString(36).substr(2), body = null, handler, script; - handler = function (_ref) { - var type = _ref.type; + handler = function (ref) { + var type = ref.type; var status = 0; @@ -910,18 +911,28 @@ function jsonpClient (request) { status = 500; } - resolve(request.respondWith(body, { status: status })); + if (status && window[callback]) { + delete window[callback]; + document.body.removeChild(script); + } - delete window[callback]; - document.body.removeChild(script); + resolve(request.respondWith(body, {status: status})); }; - request.params[name] = callback; - window[callback] = function (result) { body = JSON.stringify(result); }; + request.abort = function () { + handler({type: 'abort'}); + }; + + request.params[name] = callback; + + if (request.timeout) { + setTimeout(request.abort, request.timeout); + } + script = document.createElement('script'); script.src = request.getUrl(); script.type = 'text/javascript'; @@ -931,13 +942,13 @@ function jsonpClient (request) { document.body.appendChild(script); }); -} +}; /** * JSONP Interceptor. */ -function jsonp (request, next) { +var jsonp = function (request, next) { if (request.method == 'JSONP') { request.client = jsonpClient; @@ -947,34 +958,35 @@ function jsonp (request, next) { if (request.method == 'JSONP') { - return when(response.json(), function (json) { - - response.body = json; + try { + response.body = JSON.parse(response.body); + } catch (e) { + response.body = null; + } - return response; - }); } + }); -} +}; /** * Before Interceptor. */ -function before (request, next) { +var before = function (request, next) { if (isFunction(request.before)) { request.before.call(this, request); } next(); -} +}; /** * HTTP method override Interceptor. */ -function method (request, next) { +var method = function (request, next) { if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) { request.headers.set('X-HTTP-Method-Override', request.method); @@ -982,15 +994,18 @@ function method (request, next) { } next(); -} +}; /** * Header Interceptor. */ -function header (request, next) { +var header = function (request, next) { - var headers = assign({}, Http.headers.common, !request.crossOrigin ? Http.headers.custom : {}, Http.headers[toLower(request.method)]); + var headers = assign({}, Http.headers.common, + !request.crossOrigin ? Http.headers.custom : {}, + Http.headers[toLower(request.method)] + ); each(headers, function (value, name) { if (!request.headers.has(name)) { @@ -999,42 +1014,23 @@ function header (request, next) { }); next(); -} - -/** - * Timeout Interceptor. - */ - -function timeout (request, next) { - - var timeout; - - if (request.timeout) { - timeout = setTimeout(function () { - request.abort(); - }, request.timeout); - } - - next(function (response) { - - clearTimeout(timeout); - }); -} +}; /** * XMLHttp client. */ -function xhrClient (request) { +var xhrClient = function (request) { return new PromiseObj(function (resolve) { - var xhr = new XMLHttpRequest(), - handler = function (event) { + var xhr = new XMLHttpRequest(), handler = function (event) { - var response = request.respondWith('response' in xhr ? xhr.response : xhr.responseText, { - status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug - statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText) - }); + var response = request.respondWith( + 'response' in xhr ? xhr.response : xhr.responseText, { + status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug + statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText) + } + ); each(trim(xhr.getAllResponseHeaders()).split('\n'), function (row) { response.headers.append(row.slice(0, row.indexOf(':')), row.slice(row.indexOf(':') + 1)); @@ -1043,9 +1039,7 @@ function xhrClient (request) { resolve(response); }; - request.abort = function () { - return xhr.abort(); - }; + request.abort = function () { return xhr.abort(); }; if (request.progress) { if (request.method === 'GET') { @@ -1061,6 +1055,10 @@ function xhrClient (request) { xhr.responseType = 'blob'; } + if (request.timeout) { + xhr.timeout = request.timeout; + } + if (request.credentials === true) { xhr.withCredentials = true; } @@ -1069,22 +1067,21 @@ function xhrClient (request) { xhr.setRequestHeader(name, value); }); - xhr.timeout = 0; xhr.onload = handler; + xhr.onabort = handler; xhr.onerror = handler; + xhr.ontimeout = handler; xhr.send(request.getBody()); }); -} +}; /** * Base client. */ -function Client (context) { +var Client = function (context) { - var reqHandlers = [sendRequest], - resHandlers = [], - handler; + var reqHandlers = [sendRequest], resHandlers = [], handler; if (!isObject(context)) { context = null; @@ -1100,7 +1097,7 @@ function Client (context) { if (isFunction(handler)) { handler.call(context, request, next); } else { - warn('Invalid interceptor of type ' + typeof handler + ', must be a function'); + warn(("Invalid interceptor of type " + (typeof handler) + ", must be a function")); next(); } } @@ -1110,6 +1107,7 @@ function Client (context) { if (isFunction(response)) { resHandlers.unshift(response); + } else if (isObject(response)) { resHandlers.forEach(function (handler) { @@ -1127,6 +1125,7 @@ function Client (context) { } exec(); + }, context); } @@ -1135,7 +1134,7 @@ function Client (context) { }; return Client; -} +}; function sendRequest(request, resolve) { @@ -1144,76 +1143,64 @@ function sendRequest(request, resolve) { resolve(client(request)); } -var classCallCheck = function (instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -}; - /** * HTTP Headers. */ -var Headers = function () { - function Headers(headers) { - var _this = this; - - classCallCheck(this, Headers); +var Headers = function Headers(headers) { + var this$1 = this; - this.map = {}; + this.map = {}; - each(headers, function (value, name) { - return _this.append(name, value); - }); - } + each(headers, function (value, name) { return this$1.append(name, value); }); +}; - Headers.prototype.has = function has(name) { - return getName(this.map, name) !== null; - }; +Headers.prototype.has = function has (name) { + return getName(this.map, name) !== null; +}; - Headers.prototype.get = function get(name) { +Headers.prototype.get = function get (name) { - var list = this.map[getName(this.map, name)]; + var list = this.map[getName(this.map, name)]; - return list ? list[0] : null; - }; + return list ? list[0] : null; +}; - Headers.prototype.getAll = function getAll(name) { - return this.map[getName(this.map, name)] || []; - }; +Headers.prototype.getAll = function getAll (name) { + return this.map[getName(this.map, name)] || []; +}; - Headers.prototype.set = function set(name, value) { - this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)]; - }; +Headers.prototype.set = function set (name, value) { + this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)]; +}; - Headers.prototype.append = function append(name, value) { +Headers.prototype.append = function append (name, value){ - var list = this.getAll(name); + var list = this.getAll(name); - if (list.length) { - list.push(trim(value)); - } else { - this.set(name, value); - } - }; + if (list.length) { + list.push(trim(value)); + } else { + this.set(name, value); + } +}; - Headers.prototype.delete = function _delete(name) { - delete this.map[getName(this.map, name)]; - }; +Headers.prototype.delete = function delete$1 (name){ + delete this.map[getName(this.map, name)]; +}; - Headers.prototype.forEach = function forEach(callback, thisArg) { - var _this2 = this; +Headers.prototype.deleteAll = function deleteAll (){ + this.map = {}; +}; - each(this.map, function (list, name) { - each(list, function (value) { - return callback.call(thisArg, value, name, _this2); - }); - }); - }; +Headers.prototype.forEach = function forEach (callback, thisArg) { + var this$1 = this; - return Headers; -}(); + each(this.map, function (list, name) { + each(list, function (value) { return callback.call(thisArg, value, name, this$1); }); + }); +}; function getName(map, name) { return Object.keys(map).reduce(function (prev, curr) { @@ -1234,51 +1221,45 @@ function normalizeName(name) { * HTTP Response. */ -var Response = function () { - function Response(body, _ref) { - var url = _ref.url; - var headers = _ref.headers; - var status = _ref.status; - var statusText = _ref.statusText; - classCallCheck(this, Response); +var Response = function Response(body, ref) { + var url = ref.url; + var headers = ref.headers; + var status = ref.status; + var statusText = ref.statusText; - this.url = url; - this.ok = status >= 200 && status < 300; - this.status = status || 0; - this.statusText = statusText || ''; - this.headers = new Headers(headers); - this.body = body; + this.url = url; + this.ok = status >= 200 && status < 300; + this.status = status || 0; + this.statusText = statusText || ''; + this.headers = new Headers(headers); + this.body = body; - if (isString(body)) { + if (isString(body)) { - this.bodyText = body; - } else if (isBlob(body)) { + this.bodyText = body; - this.bodyBlob = body; + } else if (isBlob(body)) { - if (isBlobText(body)) { - this.bodyText = blobText(body); - } + this.bodyBlob = body; + + if (isBlobText(body)) { + this.bodyText = blobText(body); } } +}; - Response.prototype.blob = function blob() { - return when(this.bodyBlob); - }; - - Response.prototype.text = function text() { - return when(this.bodyText); - }; +Response.prototype.blob = function blob () { + return when(this.bodyBlob); +}; - Response.prototype.json = function json() { - return when(this.text(), function (text) { - return JSON.parse(text); - }); - }; +Response.prototype.text = function text () { + return when(this.bodyText); +}; - return Response; -}(); +Response.prototype.json = function json () { + return when(this.text(), function (text) { return JSON.parse(text); }); +}; function blobText(body) { return new PromiseObj(function (resolve) { @@ -1289,6 +1270,7 @@ function blobText(body) { reader.onload = function () { resolve(reader.result); }; + }); } @@ -1300,60 +1282,54 @@ function isBlobText(body) { * HTTP Request. */ -var Request = function () { - function Request(options) { - classCallCheck(this, Request); - +var Request = function Request(options$$1) { - this.body = null; - this.params = {}; + this.body = null; + this.params = {}; - assign(this, options, { - method: toUpper(options.method || 'GET') - }); + assign(this, options$$1, { + method: toUpper(options$$1.method || 'GET') + }); - if (!(this.headers instanceof Headers)) { - this.headers = new Headers(this.headers); - } + if (!(this.headers instanceof Headers)) { + this.headers = new Headers(this.headers); } +}; - Request.prototype.getUrl = function getUrl() { - return Url(this); - }; - - Request.prototype.getBody = function getBody() { - return this.body; - }; +Request.prototype.getUrl = function getUrl (){ + return Url(this); +}; - Request.prototype.respondWith = function respondWith(body, options) { - return new Response(body, assign(options || {}, { url: this.getUrl() })); - }; +Request.prototype.getBody = function getBody (){ + return this.body; +}; - return Request; -}(); +Request.prototype.respondWith = function respondWith (body, options$$1) { + return new Response(body, assign(options$$1 || {}, {url: this.getUrl()})); +}; /** * Service for sending network requests. */ -var CUSTOM_HEADERS = { 'X-Requested-With': 'XMLHttpRequest' }; -var COMMON_HEADERS = { 'Accept': 'application/json, text/plain, */*' }; -var JSON_CONTENT_TYPE = { 'Content-Type': 'application/json;charset=utf-8' }; +var CUSTOM_HEADERS = {'X-Requested-With': 'XMLHttpRequest'}; +var COMMON_HEADERS = {'Accept': 'application/json, text/plain, */*'}; +var JSON_CONTENT_TYPE = {'Content-Type': 'application/json;charset=utf-8'}; -function Http(options) { +function Http(options$$1) { - var self = this || {}, - client = Client(self.$vm); + var self = this || {}, client = Client(self.$vm); - defaults(options || {}, self.$options, Http.options); + defaults(options$$1 || {}, self.$options, Http.options); Http.interceptors.forEach(function (handler) { client.use(handler); }); - return client(new Request(options)).then(function (response) { + return client(new Request(options$$1)).then(function (response) { return response.ok ? response : PromiseObj.reject(response); + }, function (response) { if (response instanceof Error) { @@ -1375,36 +1351,40 @@ Http.headers = { common: COMMON_HEADERS }; -Http.interceptors = [before, timeout, method, body, jsonp, header, cors]; +Http.interceptors = [before, method, body, jsonp, header, cors]; -['get', 'delete', 'head', 'jsonp'].forEach(function (method) { +['get', 'delete', 'head', 'jsonp'].forEach(function (method$$1) { - Http[method] = function (url, options) { - return this(assign(options || {}, { url: url, method: method })); + Http[method$$1] = function (url, options$$1) { + return this(assign(options$$1 || {}, {url: url, method: method$$1})); }; + }); -['post', 'put', 'patch'].forEach(function (method) { +['post', 'put', 'patch'].forEach(function (method$$1) { - Http[method] = function (url, body, options) { - return this(assign(options || {}, { url: url, method: method, body: body })); + Http[method$$1] = function (url, body$$1, options$$1) { + return this(assign(options$$1 || {}, {url: url, method: method$$1, body: body$$1})); }; + }); /** * Service for interacting with RESTful services. */ -function Resource(url, params, actions, options) { +function Resource(url, params, actions, options$$1) { - var self = this || {}, - resource = {}; + var self = this || {}, resource = {}; - actions = assign({}, Resource.actions, actions); + actions = assign({}, + Resource.actions, + actions + ); each(actions, function (action, name) { - action = merge({ url: url, params: assign({}, params) }, options, action); + action = merge({url: url, params: assign({}, params)}, options$$1, action); resource[name] = function () { return (self.$http || Http)(opts(action, arguments)); @@ -1416,9 +1396,7 @@ function Resource(url, params, actions, options) { function opts(action, args) { - var options = assign({}, action), - params = {}, - body; + var options$$1 = assign({}, action), params = {}, body; switch (args.length) { @@ -1431,7 +1409,7 @@ function opts(action, args) { case 1: - if (/^(POST|PUT|PATCH)$/i.test(options.method)) { + if (/^(POST|PUT|PATCH)$/i.test(options$$1.method)) { body = args[0]; } else { params = args[0]; @@ -1445,23 +1423,23 @@ function opts(action, args) { default: - throw 'Expected up to 4 arguments [params, body], got ' + args.length + ' arguments'; + throw 'Expected up to 2 arguments [params, body], got ' + args.length + ' arguments'; } - options.body = body; - options.params = assign({}, options.params, params); + options$$1.body = body; + options$$1.params = assign({}, options$$1.params, params); - return options; + return options$$1; } Resource.actions = { - get: { method: 'GET' }, - save: { method: 'POST' }, - query: { method: 'GET' }, - update: { method: 'PUT' }, - remove: { method: 'DELETE' }, - delete: { method: 'DELETE' } + get: {method: 'GET'}, + save: {method: 'POST'}, + query: {method: 'GET'}, + update: {method: 'PUT'}, + remove: {method: 'DELETE'}, + delete: {method: 'DELETE'} }; @@ -1485,30 +1463,28 @@ function plugin(Vue) { Object.defineProperties(Vue.prototype, { $url: { - get: function () { + get: function get() { return options(Vue.url, this, this.$options.url); } }, $http: { - get: function () { + get: function get() { return options(Vue.http, this, this.$options.http); } }, $resource: { - get: function () { + get: function get() { return Vue.resource.bind(this); } }, $promise: { - get: function () { - var _this = this; + get: function get() { + var this$1 = this; - return function (executor) { - return new Vue.Promise(executor, _this); - }; + return function (executor) { return new Vue.Promise(executor, this$1); }; } } @@ -1521,4 +1497,4 @@ if (typeof window !== 'undefined' && window.Vue) { return plugin; -}))); \ No newline at end of file +}))); diff --git a/dist/vue-resource.min.js b/dist/vue-resource.min.js index 5a6f05f7..757e48b0 100644 --- a/dist/vue-resource.min.js +++ b/dist/vue-resource.min.js @@ -1,7 +1,7 @@ /*! - * vue-resource v1.0.3 + * vue-resource v1.1.0 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ -!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.VueResource=n()}(this,function(){"use strict";function t(t){this.state=it,this.value=void 0,this.deferred=[];var n=this;try{t(function(t){n.resolve(t)},function(t){n.reject(t)})}catch(t){n.reject(t)}}function n(t,n){t instanceof Promise?this.promise=t:this.promise=new Promise(t.bind(n)),this.context=n}function e(t){at=t.util,ct=t.config.debug||!t.config.silent}function o(t){"undefined"!=typeof console&&ct&&console.warn("[VueResource warn]: "+t)}function r(t){"undefined"!=typeof console&&console.error(t)}function i(t,n){return at.nextTick(t,n)}function u(t){return t.replace(/^\s*|\s*$/g,"")}function s(t){return t?t.toLowerCase():""}function c(t){return t?t.toUpperCase():""}function a(t){return"string"==typeof t}function f(t){return t===!0||t===!1}function h(t){return"function"==typeof t}function p(t){return null!==t&&"object"==typeof t}function l(t){return p(t)&&Object.getPrototypeOf(t)==Object.prototype}function d(t){return"undefined"!=typeof Blob&&t instanceof Blob}function m(t){return"undefined"!=typeof FormData&&t instanceof FormData}function y(t,e,o){var r=n.resolve(t);return arguments.length<2?r:r.then(e,o)}function v(t,n,e){return e=e||{},h(e)&&(e=e.call(n)),g(t.bind({$vm:n,$options:e}),t,{$options:e})}function b(t,n){var e,o;if(t&&"number"==typeof t.length)for(e=0;e=200&&i<300,this.status=i||0,this.statusText=u||"",this.headers=new bt(r),this.body=n,a(n)?this.bodyText=n:d(n)&&(this.bodyBlob=n,Y(n)&&(this.bodyText=Q(n)))}return t.prototype.blob=function(){return y(this.bodyBlob)},t.prototype.text=function(){return y(this.bodyText)},t.prototype.json=function(){return y(this.text(),function(t){return JSON.parse(t)})},t}(),wt=function(){function t(n){vt(this,t),this.body=null,this.params={},pt(this,n,{method:c(n.method||"GET")}),this.headers instanceof bt||(this.headers=new bt(this.headers))}return t.prototype.getUrl=function(){return k(this)},t.prototype.getBody=function(){return this.body},t.prototype.respondWith=function(t,n){return new gt(t,pt(n||{},{url:this.getUrl()}))},t}(),Tt={"X-Requested-With":"XMLHttpRequest"},xt={Accept:"application/json, text/plain, */*"},jt={"Content-Type":"application/json;charset=utf-8"};return Z.options={},Z.headers={put:jt,post:jt,patch:jt,delete:jt,custom:Tt,common:xt},Z.interceptors=[M,W,X,B,D,F,N],["get","delete","head","jsonp"].forEach(function(t){Z[t]=function(n,e){return this(pt(e||{},{url:n,method:t}))}}),["post","put","patch"].forEach(function(t){Z[t]=function(n,e,o){return this(pt(o||{},{url:n,method:t,body:e}))}}),tt.actions={get:{method:"GET"},save:{method:"POST"},query:{method:"GET"},update:{method:"PUT"},remove:{method:"DELETE"},delete:{method:"DELETE"}},"undefined"!=typeof window&&window.Vue&&window.Vue.use(et),et}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.VueResource=e()}(this,function(){"use strict";function t(t){this.state=F,this.value=void 0,this.deferred=[];var e=this;try{t(function(t){e.resolve(t)},function(t){e.reject(t)})}catch(t){e.reject(t)}}function e(t,e){t instanceof Promise?this.promise=t:this.promise=new Promise(t.bind(e)),this.context=e}function n(t){"undefined"!=typeof console&&V&&console.warn("[VueResource warn]: "+t)}function o(t){"undefined"!=typeof console&&console.error(t)}function r(t,e){return _.nextTick(t,e)}function i(t){return t?t.replace(/^\s*|\s*$/g,""):""}function u(t){return t?t.toLowerCase():""}function s(t){return t?t.toUpperCase():""}function c(t){return"string"==typeof t}function a(t){return t===!0||t===!1}function f(t){return"function"==typeof t}function p(t){return null!==t&&"object"==typeof t}function h(t){return p(t)&&Object.getPrototypeOf(t)==Object.prototype}function l(t){return"undefined"!=typeof Blob&&t instanceof Blob}function d(t){return"undefined"!=typeof FormData&&t instanceof FormData}function m(t,n,o){var r=e.resolve(t);return arguments.length<2?r:r.then(n,o)}function y(t,e,n){return n=n||{},f(n)&&(n=n.call(e)),b(t.bind({$vm:e,$options:n}),t,{$options:n})}function v(t,e){var n,o;if(tt(t))for(n=0;n=200&&r<300,this.status=r||0,this.statusText=i||"",this.headers=new gt(o),this.body=t,c(t)?this.bodyText=t:l(t)&&(this.bodyBlob=t,N(t)&&(this.bodyText=L(t)))};wt.prototype.blob=function(){return m(this.bodyBlob)},wt.prototype.text=function(){return m(this.bodyText)},wt.prototype.json=function(){return m(this.text(),function(t){return JSON.parse(t)})};var Tt=function(t){this.body=null,this.params={},et(this,t,{method:s(t.method||"GET")}),this.headers instanceof gt||(this.headers=new gt(this.headers))};Tt.prototype.getUrl=function(){return A(this)},Tt.prototype.getBody=function(){return this.body},Tt.prototype.respondWith=function(t,e){return new wt(t,et(e||{},{url:this.getUrl()}))};var xt={"X-Requested-With":"XMLHttpRequest"},jt={Accept:"application/json, text/plain, */*"},Et={"Content-Type":"application/json;charset=utf-8"};return J.options={},J.headers={put:Et,post:Et,patch:Et,delete:Et,custom:xt,common:jt},J.interceptors=[dt,mt,pt,lt,yt,ft],["get","delete","head","jsonp"].forEach(function(t){J[t]=function(e,n){return this(et(n||{},{url:e,method:t}))}}),["post","put","patch"].forEach(function(t){J[t]=function(e,n,o){return this(et(o||{},{url:e,method:t,body:n}))}}),q.actions={get:{method:"GET"},save:{method:"POST"},query:{method:"GET"},update:{method:"PUT"},remove:{method:"DELETE"},delete:{method:"DELETE"}},"undefined"!=typeof window&&window.Vue&&window.Vue.use(D),D}); \ No newline at end of file diff --git a/docs/api.md b/docs/api.md index 9f7bfd22..c78f95f1 100644 --- a/docs/api.md +++ b/docs/api.md @@ -24,6 +24,7 @@ getUrl() (string) getBody() (any) respondWith(any: body, object: options) (Response) + abort() } ``` diff --git a/docs/config.md b/docs/config.md index 4a0e11b1..2f4e32de 100644 --- a/docs/config.md +++ b/docs/config.md @@ -22,6 +22,8 @@ new Vue({ }) ``` +Note that for the root option to work, the path of the request must be relative. This will use this the root option: `Vue.http.get('someUrl')` while this will not: `Vue.http.get('/someUrl')`. + ## Webpack/Browserify Add `vue` and `vue-resource` to your `package.json`, then `npm install`, then add these lines in your code: diff --git a/docs/http.md b/docs/http.md index 41ca2fdb..75e54454 100644 --- a/docs/http.md +++ b/docs/http.md @@ -9,9 +9,9 @@ A Vue instance provides the `this.$http` service which can send HTTP requests. A ```js { // GET /someUrl - this.$http.get('/someUrl').then((response) => { + this.$http.get('/someUrl').then(response => { // success callback - }, (response) => { + }, response => { // error callback }); } @@ -78,7 +78,7 @@ blob() | `Promise` | Resolves the body as Blob object ```js { // POST /someUrl - this.$http.post('/someUrl', {foo: 'bar'}).then((response) => { + this.$http.post('/someUrl', {foo: 'bar'}).then(response => { // get status response.status; @@ -89,10 +89,10 @@ blob() | `Promise` | Resolves the body as Blob object // get 'Expires' header response.headers.get('Expires'); - // set data on vm - this.$set('someData', response.body); + // get body data + this.someData = response.body; - }, (response) => { + }, response => { // error callback }); } @@ -103,12 +103,12 @@ Fetch an image and use the blob() method to extract the image body content from ```js { // GET /image.jpg - this.$http.get('/image.jpg').then((response) => { + this.$http.get('/image.jpg').then(response => { // resolve to Blob return response.blob(); - }).then(blob) => { + }).then(blob => { // use image Blob }); } @@ -122,9 +122,13 @@ Interceptors can be defined globally and are used for pre- and postprocessing of ```js Vue.http.interceptors.push((request, next) => { - // modify request + // modify method request.method = 'POST'; + // modify headers + request.headers.set('X-CSRF-TOKEN', 'TOKEN'); + request.headers.set('Authorization', 'Bearer TOKEN'); + // continue to next interceptor next(); }); @@ -132,13 +136,13 @@ Vue.http.interceptors.push((request, next) => { ### Request and Response processing ```js -Vue.http.interceptors.push((request, next) => { +Vue.http.interceptors.push((request, next) => { // modify request request.method = 'POST'; // continue to next interceptor - next((response) => { + next(response => { // modify response response.body = '...'; diff --git a/docs/recipes.md b/docs/recipes.md index 201692db..7d215568 100644 --- a/docs/recipes.md +++ b/docs/recipes.md @@ -17,9 +17,9 @@ Sending forms using [FormData](https://developer.mozilla.org/en-US/docs/Web/API/ formData.append('pic', fileInput, 'mypic.jpg'); // POST /someUrl - this.$http.post('/someUrl', formData).then((response) => { + this.$http.post('/someUrl', formData).then(response => { // success callback - }, (response) => { + }, response => { // error callback }); } @@ -46,9 +46,9 @@ Abort a previous request when a new request is about to be sent. For example whe this.previousRequest = request; } - }).then((response) => { + }).then(response => { // success callback - }, (response) => { + }, response => { // error callback }); } diff --git a/docs/resource.md b/docs/resource.md index cc390fd1..405dcbb9 100644 --- a/docs/resource.md +++ b/docs/resource.md @@ -24,21 +24,21 @@ delete: {method: 'DELETE'} var resource = this.$resource('someItem{/id}'); // GET someItem/1 - resource.get({id: 1}).then((response) => { - this.$set('item', response.json()) + resource.get({id: 1}).then(response => { + this.$set('item', response.body) }); // POST someItem/1 - resource.save({id: 1}, {item: this.item}).then((response) => { + resource.save({id: 1}, {item: this.item}).then(response => { // success callback - }, (response) => { + }, response => { // error callback }); // DELETE someItem/1 - resource.delete({id: 1}).then((response) => { + resource.delete({id: 1}).then(response => { // success callback - }, (response) => { + }, response => { // error callback }); } @@ -56,14 +56,14 @@ delete: {method: 'DELETE'} var resource = this.$resource('someItem{/id}', {}, customActions); // GET someItem/foo/1 - resource.foo({id: 1}).then((response) => { - this.$set('item', response.json()) + resource.foo({id: 1}).then(response => { + this.$set('item', response.body) }); // POST someItem/bar/1 - resource.bar({id: 1}, {item: this.item}).then((response) => { + resource.bar({id: 1}, {item: this.item}).then(response => { // success callback - }, (response) => { + }, response => { // error callback }); } diff --git a/package.json b/package.json index e8e37445..5cf7d346 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-resource", - "version": "1.0.3", + "version": "1.1.0", "main": "dist/vue-resource.common.js", "jsnext:main": "dist/vue-resource.es2015.js", "description": "The HTTP client for Vue.js", @@ -25,18 +25,15 @@ "release": "node build/release.js" }, "devDependencies": { - "babel-core": "^6.14.0", - "babel-loader": "^6.2.5", - "babel-plugin-transform-runtime": "^6.15.0", - "babel-preset-es2015": "^6.14.0", - "babel-preset-es2015-loose-rollup": "^7.0.0", + "buble": "^0.15.2", + "buble-loader": "^0.4.0", "generate-release": "^0.10.1", - "jasmine-core": "^2.5.0", + "jasmine-core": "^2.5.2", "replace-in-file": "^2.0.1", - "rollup": "^0.34.13", - "rollup-plugin-babel": "^2.6.1", - "uglify-js": "^2.7.3", - "vue": "^1.0.26", - "webpack": "2.1.0-beta.21" + "rollup": "^0.41.4", + "rollup-plugin-buble": "^0.15.0", + "uglify-js": "^2.7.5", + "vue": "^2.1.10", + "webpack": "^2.2.0" } } diff --git a/src/http/client/index.js b/src/http/client/index.js index 3d995c16..0fb3e6b0 100644 --- a/src/http/client/index.js +++ b/src/http/client/index.js @@ -15,7 +15,7 @@ export default function (context) { } function Client(request) { - return new Promise((resolve) => { + return new Promise(resolve => { function exec() { @@ -56,7 +56,7 @@ export default function (context) { }, context); } - Client.use = (handler) => { + Client.use = handler => { reqHandlers.push(handler); }; diff --git a/src/http/client/jsonp.js b/src/http/client/jsonp.js index 87dba2c1..b90ec408 100644 --- a/src/http/client/jsonp.js +++ b/src/http/client/jsonp.js @@ -5,9 +5,9 @@ import Promise from '../../promise'; export default function (request) { - return new Promise((resolve) => { + return new Promise(resolve => { - var name = request.jsonp || 'callback', callback = '_jsonp' + Math.random().toString(36).substr(2), body = null, handler, script; + var name = request.jsonp || 'callback', callback = request.jsonpCallback || '_jsonp' + Math.random().toString(36).substr(2), body = null, handler, script; handler = ({type}) => { @@ -19,17 +19,27 @@ export default function (request) { status = 500; } + if (status && window[callback]) { + delete window[callback]; + document.body.removeChild(script); + } + resolve(request.respondWith(body, {status})); + }; + + window[callback] = result => { + body = JSON.stringify(result); + }; - delete window[callback]; - document.body.removeChild(script); + request.abort = () => { + handler({type: 'abort'}); }; request.params[name] = callback; - window[callback] = (result) => { - body = JSON.stringify(result); - }; + if (request.timeout) { + setTimeout(request.abort, request.timeout); + } script = document.createElement('script'); script.src = request.getUrl(); diff --git a/src/http/client/xdr.js b/src/http/client/xdr.js index 6d35339e..91165fc9 100644 --- a/src/http/client/xdr.js +++ b/src/http/client/xdr.js @@ -5,7 +5,7 @@ import Promise from '../../promise'; export default function (request) { - return new Promise((resolve) => { + return new Promise(resolve => { var xdr = new XDomainRequest(), handler = ({type}) => { @@ -23,8 +23,13 @@ export default function (request) { request.abort = () => xdr.abort(); xdr.open(request.method, request.getUrl()); - xdr.timeout = 0; + + if (request.timeout) { + xdr.timeout = request.timeout; + } + xdr.onload = handler; + xdr.onabort = handler; xdr.onerror = handler; xdr.ontimeout = handler; xdr.onprogress = () => {}; diff --git a/src/http/client/xhr.js b/src/http/client/xhr.js index b9cf2933..60f2d7a0 100644 --- a/src/http/client/xhr.js +++ b/src/http/client/xhr.js @@ -6,7 +6,7 @@ import Promise from '../../promise'; import { each, trim } from '../../util'; export default function (request) { - return new Promise((resolve) => { + return new Promise(resolve => { var xhr = new XMLHttpRequest(), handler = (event) => { @@ -40,6 +40,10 @@ export default function (request) { xhr.responseType = 'blob'; } + if (request.timeout) { + xhr.timeout = request.timeout; + } + if (request.credentials === true) { xhr.withCredentials = true; } @@ -48,9 +52,10 @@ export default function (request) { xhr.setRequestHeader(name, value); }); - xhr.timeout = 0; xhr.onload = handler; + xhr.onabort = handler; xhr.onerror = handler; + xhr.ontimeout = handler; xhr.send(request.getBody()); }); } diff --git a/src/http/headers.js b/src/http/headers.js index d34763b5..fa76e928 100644 --- a/src/http/headers.js +++ b/src/http/headers.js @@ -47,6 +47,10 @@ export default class Headers { delete this.map[getName(this.map, name)]; } + deleteAll(){ + this.map = {}; + } + forEach(callback, thisArg) { each(this.map, (list, name) => { each(list, value => callback.call(thisArg, value, name, this)); diff --git a/src/http/index.js b/src/http/index.js index 1d61deb6..ade5cecd 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -12,7 +12,6 @@ import jsonp from './interceptor/jsonp'; import before from './interceptor/before'; import method from './interceptor/method'; import header from './interceptor/header'; -import timeout from './interceptor/timeout'; import Client from './client/index'; import Request from './request'; import Promise from '../promise'; @@ -53,7 +52,7 @@ Http.headers = { common: COMMON_HEADERS }; -Http.interceptors = [before, timeout, method, body, jsonp, header, cors]; +Http.interceptors = [before, method, body, jsonp, header, cors]; ['get', 'delete', 'head', 'jsonp'].forEach((method) => { diff --git a/src/http/interceptor/body.js b/src/http/interceptor/body.js index 71c2a7f8..4f49cb17 100644 --- a/src/http/interceptor/body.js +++ b/src/http/interceptor/body.js @@ -21,7 +21,7 @@ export default function (request, next) { } } - next((response) => { + next(response => { Object.defineProperty(response, 'data', { diff --git a/src/http/interceptor/jsonp.js b/src/http/interceptor/jsonp.js index 160c508a..7294d651 100644 --- a/src/http/interceptor/jsonp.js +++ b/src/http/interceptor/jsonp.js @@ -11,16 +11,16 @@ export default function (request, next) { request.client = jsonpClient; } - next((response) => { + next(response => { if (request.method == 'JSONP') { - return when(response.json(), json => { + try { + response.body = JSON.parse(response.body); + } catch (e) { + response.body = null; + } - response.body = json; - - return response; - }); } }); diff --git a/src/http/interceptor/timeout.js b/src/http/interceptor/timeout.js deleted file mode 100644 index 3a75da71..00000000 --- a/src/http/interceptor/timeout.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Timeout Interceptor. - */ - -export default function (request, next) { - - var timeout; - - if (request.timeout) { - timeout = setTimeout(() => { - request.abort(); - }, request.timeout); - } - - next((response) => { - - clearTimeout(timeout); - - }); -} diff --git a/src/resource.js b/src/resource.js index dd90d8df..6cabd1b7 100644 --- a/src/resource.js +++ b/src/resource.js @@ -55,7 +55,7 @@ function opts(action, args) { default: - throw 'Expected up to 4 arguments [params, body], got ' + args.length + ' arguments'; + throw 'Expected up to 2 arguments [params, body], got ' + args.length + ' arguments'; } options.body = body; diff --git a/src/util.js b/src/util.js index 33b4ded6..018e8320 100644 --- a/src/util.js +++ b/src/util.js @@ -4,7 +4,7 @@ import Promise from './promise'; -var debug = false, util = {}, { slice } = []; +var debug = false, util = {}, {hasOwnProperty} = {}, {slice} = []; export default function (Vue) { util = Vue.util; @@ -28,7 +28,7 @@ export function nextTick(cb, ctx) { } export function trim(str) { - return str.replace(/^\s*|\s*$/g, ''); + return str ? str.replace(/^\s*|\s*$/g, '') : ''; } export function toLower(str) { @@ -95,13 +95,13 @@ export function each(obj, iterator) { var i, key; - if (obj && typeof obj.length == 'number') { + if (isArray(obj)) { 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)) { + if (hasOwnProperty.call(obj, key)) { iterator.call(obj[key], obj[key], key); } } diff --git a/test/http.js b/test/http.js index f0262157..d7ace7d0 100644 --- a/test/http.js +++ b/test/http.js @@ -2,9 +2,9 @@ import Vue from 'vue'; describe('Vue.http', function () { - it('get("data/text.txt")', (done) => { + it('get("data/text.txt")', done => { - Vue.http.get('data/text.txt').then((res) => { + Vue.http.get('data/text.txt').then(res => { expect(res.ok).toBe(true); expect(res.status).toBe(200); @@ -17,9 +17,9 @@ describe('Vue.http', function () { }); - it('get("data/valid.json")', (done) => { + it('get("data/valid.json")', done => { - Vue.http.get('data/valid.json').then((res) => { + Vue.http.get('data/valid.json').then(res => { expect(res.ok).toBe(true); expect(res.status).toBe(200); @@ -31,9 +31,9 @@ describe('Vue.http', function () { }); - it('get("data/invalid.json")', (done) => { + it('get("data/invalid.json")', done => { - Vue.http.get('data/invalid.json').then((res) => { + Vue.http.get('data/invalid.json').then(res => { expect(res.ok).toBe(true); expect(res.status).toBe(200); @@ -44,9 +44,9 @@ describe('Vue.http', function () { }); - it('get("github.com/avatar")', (done) => { + it('get("github.com/avatar")', done => { - Vue.http.get('https://avatars1.githubusercontent.com/u/6128107').then((res) => { + Vue.http.get('https://avatars1.githubusercontent.com/u/6128107').then(res => { expect(res.ok).toBe(true); expect(res.status).toBe(200); @@ -58,9 +58,9 @@ describe('Vue.http', function () { }); - it('get("cors-api.appspot.com")', (done) => { + it('get("cors-api.appspot.com")', done => { - Vue.http.get('http://server.cors-api.appspot.com/server?id=1&enable=true').then((res) => { + Vue.http.get('http://server.cors-api.appspot.com/server?id=1&enable=true').then(res => { expect(res.ok).toBe(true); expect(res.status).toBe(200); @@ -73,9 +73,9 @@ describe('Vue.http', function () { }); - it('jsonp("jsfiddle.net/jsonp")', (done) => { + it('jsonp("jsfiddle.net/jsonp")', done => { - Vue.http.jsonp('http://jsfiddle.net/echo/jsonp/', {params: {foo: 'bar'}}).then((res) => { + Vue.http.jsonp('http://jsfiddle.net/echo/jsonp/', {params: {foo: 'bar'}}).then(res => { expect(res.ok).toBe(true); expect(res.status).toBe(200); @@ -91,13 +91,13 @@ describe('Vue.http', function () { describe('this.$http', function () { - it('get("data/valid.json")', (done) => { + it('get("data/valid.json")', done => { var vm = new Vue({ created() { - this.$http.get('data/valid.json').then((res) => { + this.$http.get('data/valid.json').then(res => { expect(this).toBe(vm); expect(res.ok).toBe(true); @@ -115,7 +115,7 @@ describe('this.$http', function () { }); - it('get("data/valid.json") with abort()', (done) => { + it('get("data/valid.json") with timeout', done => { var vm = new Vue({ @@ -123,7 +123,36 @@ describe('this.$http', function () { var random = Math.random().toString(36).substr(2); - this.$http.get('data/valid.json?' + random, { + this.$http.get(`data/valid.json?${random}`, {timeout: 1}).then(res => { + + console.log(res); + + fail('Callback has been called'); + + }, res => { + + expect(res.ok).toBe(false); + expect(res.status).toBe(0); + + done(); + + }); + + } + + }); + + }); + + it('get("data/valid.json") with abort()', done => { + + var vm = new Vue({ + + created() { + + var random = Math.random().toString(36).substr(2); + + this.$http.get(`data/valid.json?${random}`, { before(req) { setTimeout(() => { @@ -132,13 +161,13 @@ describe('this.$http', function () { req.abort(); - done(); - }, 0); } - }).then((res) => { + }).then(res => { fail('Callback has been called'); + }, res => { + done(); }); } @@ -146,13 +175,13 @@ describe('this.$http', function () { }); - it('get("data/notfound.json") using catch()', (done) => { + it('get("data/notfound.json") using catch()', done => { var vm = new Vue({ created() { - this.$http.get('data/notfound.json').catch((res) => { + this.$http.get('data/notfound.json').catch(res => { expect(this).toBe(vm); expect(res.ok).toBe(false); diff --git a/test/webpack.config.js b/test/webpack.config.js index a7c5910c..c6d830d9 100644 --- a/test/webpack.config.js +++ b/test/webpack.config.js @@ -1,12 +1,12 @@ module.exports = { - entry: __dirname + "/index.js", + entry: __dirname + '/index.js', output: { - path: __dirname + "/", - filename: "specs.js" + path: __dirname + '/', + filename: 'specs.js' }, module: { loaders: [ - {test: /.js/, exclude: /node_modules/, loader: 'babel', query: {presets: [['es2015', {modules: false}]]}} + {test: /\.js$/, loader: 'buble-loader', exclude: /node_modules/} ] } };