diff --git a/README.md b/README.md index c31aa17d..b5f6cb8a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The plugin provides services for making web requests and handle responses using Add `vue` and `vue-resource` to your `package.json`, then `npm install`, then add these lines in your code: -```javascript +```js var Vue = require('vue'); Vue.use(require('vue-resource')); @@ -20,14 +20,14 @@ Vue.use(require('vue-resource')); Set default values using the global configuration. -```javascript +```js Vue.http.options.root = '/root'; Vue.http.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk'; ``` Set default values inside your Vue component options. -```javascript +```js new Vue({ http: { @@ -70,7 +70,7 @@ The http service can be used globally `Vue.http` or in a Vue instance `this.$htt ### Example -```javascript +```js new Vue({ ready: function() { @@ -110,7 +110,7 @@ The resource service can be used globally `Vue.resource` or in a Vue instance `t ### Default Actions -```javascript +```js get: {method: 'GET'}, save: {method: 'POST'}, query: {method: 'GET'}, @@ -120,7 +120,7 @@ delete: {method: 'DELETE'} ``` ### Example -```javascript +```js new Vue({ ready: function() { @@ -155,7 +155,7 @@ new Vue({ Interceptors can be defined globally and are used for pre- and postprocessing of a request. -```javascript +```js Vue.http.interceptors.push({ request: function (request) { @@ -169,26 +169,20 @@ Vue.http.interceptors.push({ }); ``` -#### Interceptor Factory +A factory function can also be used. -If Promises are needed inside of a Interceptor, a factory function can be used. - -```javascript -Vue.http.interceptors.push(function (Promise) { +```js +Vue.http.interceptors.push(function () { return { - request: function (request) { - if (reject) { - return Promise.reject(); - } - }, + request: function (request) { + return request; + }, - response: function (response) { - if (reject) { - return Promise.reject(); - } - } + response: function (response) { + return response; + } - }; + }; }); ``` diff --git a/bower.json b/bower.json index 949afe89..25dd98e8 100644 --- a/bower.json +++ b/bower.json @@ -2,7 +2,7 @@ "name": "vue-resource", "main": "dist/vue-resource.js", "description": "A web request service for Vue.js", - "version": "0.6.0", + "version": "0.6.1", "homepage": "https://github.com/vuejs/vue-resource", "license": "MIT", "ignore": [ diff --git a/dist/vue-resource.js b/dist/vue-resource.js index 6bb3e3a3..227b22f1 100644 --- a/dist/vue-resource.js +++ b/dist/vue-resource.js @@ -1,5 +1,5 @@ /** - * vue-resource v0.6.0 + * vue-resource v0.6.1 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ @@ -75,7 +75,7 @@ return /******/ (function(modules) { // webpackBootstrap Vue.url = __webpack_require__(2); Vue.http = __webpack_require__(8); Vue.resource = __webpack_require__(23); - Vue.Promise = __webpack_require__(9); + Vue.Promise = __webpack_require__(10); Object.defineProperties(Vue.prototype, { @@ -670,14 +670,14 @@ return /******/ (function(modules) { // webpackBootstrap */ var _ = __webpack_require__(1); - var Promise = __webpack_require__(9); - var interceptor = __webpack_require__(11); - var defaultClient = __webpack_require__(12); + var Client = __webpack_require__(9); + var Promise = __webpack_require__(10); + var interceptor = __webpack_require__(13); var jsonType = {'Content-Type': 'application/json'}; function Http(url, options) { - var client = defaultClient, request, promise; + var client = Client, request, promise; Http.interceptors.forEach(function (handler) { client = interceptor(handler, this.$vm)(client); @@ -687,7 +687,6 @@ return /******/ (function(modules) { // webpackBootstrap request = _.merge({}, Http.options, this.$options, options); promise = client(request).bind(this.$vm).then(function (response) { - response.ok = response.status >= 200 && response.status < 300; return response.ok ? response : Promise.reject(response); }, function (response) { @@ -767,6 +766,77 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }, /* 9 */ +/***/ function(module, exports, __webpack_require__) { + + /** + * Base client. + */ + + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); + var xhrClient = __webpack_require__(12); + + module.exports = function (request) { + + var response = (request.client || xhrClient)(request); + + return Promise.resolve(response).then(function (response) { + + if (response.headers) { + + var headers = parseHeaders(response.headers); + + response.headers = function (name) { + + if (name) { + return headers[_.toLower(name)]; + } + + return headers; + }; + + } + + response.ok = response.status >= 200 && response.status < 300; + + return response; + }); + + }; + + function parseHeaders(str) { + + var headers = {}, value, name, i; + + if (_.isString(str)) { + _.each(str.split('\n'), function (row) { + + i = row.indexOf(':'); + name = _.trim(_.toLower(row.slice(0, i))); + value = _.trim(row.slice(i + 1)); + + if (headers[name]) { + + if (_.isArray(headers[name])) { + headers[name].push(value); + } else { + headers[name] = [headers[name], value]; + } + + } else { + + headers[name] = value; + } + + }); + } + + return headers; + } + + +/***/ }, +/* 10 */ /***/ function(module, exports, __webpack_require__) { /** @@ -774,7 +844,7 @@ return /******/ (function(modules) { // webpackBootstrap */ var _ = __webpack_require__(1); - var PromiseObj = window.Promise || __webpack_require__(10); + var PromiseObj = window.Promise || __webpack_require__(11); function Promise(executor, context) { @@ -881,7 +951,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }, -/* 10 */ +/* 11 */ /***/ function(module, exports, __webpack_require__) { /** @@ -1065,74 +1135,8 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = Promise; -/***/ }, -/* 11 */ -/***/ function(module, exports, __webpack_require__) { - - /** - * Interceptor factory. - */ - - var _ = __webpack_require__(1); - var Promise = __webpack_require__(9); - - module.exports = function (handler, vm) { - - return function (client) { - - if (_.isFunction(handler)) { - handler = handler.call(vm, Promise); - } - - return function (request) { - - if (_.isFunction(handler.request)) { - request = handler.request.call(vm, request); - } - - return when(request, function (request) { - return when(client(request), function (response) { - - if (_.isFunction(handler.response)) { - response = handler.response.call(vm, response); - } - - return response; - }); - }); - }; - }; - }; - - function when(value, fulfilled, rejected) { - - var promise = Promise.resolve(value); - - if (arguments.length < 2) { - return promise; - } - - return promise.then(fulfilled, rejected); - } - - /***/ }, /* 12 */ -/***/ function(module, exports, __webpack_require__) { - - /** - * Default client. - */ - - var xhrClient = __webpack_require__(13); - - module.exports = function (request) { - return (request.client || xhrClient)(request); - }; - - -/***/ }, -/* 13 */ /***/ function(module, exports, __webpack_require__) { /** @@ -1140,7 +1144,7 @@ return /******/ (function(modules) { // webpackBootstrap */ var _ = __webpack_require__(1); - var Promise = __webpack_require__(9); + var Promise = __webpack_require__(10); module.exports = function (request) { return new Promise(function (resolve) { @@ -1166,7 +1170,7 @@ return /******/ (function(modules) { // webpackBootstrap response.data = xhr.responseText; response.status = xhr.status; response.statusText = xhr.statusText; - response.headers = getHeaders(xhr); + response.headers = xhr.getAllResponseHeaders(); resolve(response); }; @@ -1179,52 +1183,55 @@ return /******/ (function(modules) { // webpackBootstrap }); }; - function getHeaders(xhr) { - var headers; +/***/ }, +/* 13 */ +/***/ function(module, exports, __webpack_require__) { - if (!headers) { - headers = parseHeaders(xhr.getAllResponseHeaders()); - } + /** + * Interceptor factory. + */ - return function (name) { + var _ = __webpack_require__(1); + var Promise = __webpack_require__(10); - if (name) { - return headers[_.toLower(name)]; - } + module.exports = function (handler, vm) { - return headers; - }; - } + return function (client) { - function parseHeaders(str) { + if (_.isFunction(handler)) { + handler = handler.call(vm, Promise); + } - var headers = {}, value, name, i; + return function (request) { - if (_.isString(str)) { - _.each(str.split('\n'), function (row) { + if (_.isFunction(handler.request)) { + request = handler.request.call(vm, request); + } - i = row.indexOf(':'); - name = _.trim(_.toLower(row.slice(0, i))); - value = _.trim(row.slice(i + 1)); + return when(request, function (request) { + return when(client(request), function (response) { - if (headers[name]) { + if (_.isFunction(handler.response)) { + response = handler.response.call(vm, response); + } - if (_.isArray(headers[name])) { - headers[name].push(value); - } else { - headers[name] = [headers[name], value]; - } + return response; + }); + }); + }; + }; + }; - } else { + function when(value, fulfilled, rejected) { - headers[name] = value; - } + var promise = Promise.resolve(value); - }); + if (arguments.length < 2) { + return promise; } - return headers; + return promise.then(fulfilled, rejected); } @@ -1321,7 +1328,7 @@ return /******/ (function(modules) { // webpackBootstrap */ var _ = __webpack_require__(1); - var Promise = __webpack_require__(9); + var Promise = __webpack_require__(10); module.exports = function (request) { return new Promise(function (resolve) { @@ -1515,7 +1522,7 @@ return /******/ (function(modules) { // webpackBootstrap */ var _ = __webpack_require__(1); - var Promise = __webpack_require__(9); + var Promise = __webpack_require__(10); module.exports = function (request) { return new Promise(function (resolve) { diff --git a/dist/vue-resource.min.js b/dist/vue-resource.min.js index 424def79..ffb78095 100644 --- a/dist/vue-resource.min.js +++ b/dist/vue-resource.min.js @@ -1,7 +1,7 @@ /** - * vue-resource v0.6.0 + * vue-resource v0.6.1 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.VueResource=e():t.VueResource=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){function r(t){var e=n(1);e.config=t.config,e.warning=t.util.warn,e.nextTick=t.util.nextTick,t.url=n(2),t.http=n(8),t.resource=n(23),t.Promise=n(9),Object.defineProperties(t.prototype,{$url:{get:function(){return e.options(t.url,this,this.$options.url)}},$http:{get:function(){return e.options(t.http,this,this.$options.http)}},$resource:{get:function(){return t.resource.bind(this)}},$promise:{get:function(){return function(e){return new t.Promise(e,this)}.bind(this)}}})}window.Vue&&Vue.use(r),t.exports=r},function(t,e){function n(t,e,o){for(var i in e)o&&(r.isPlainObject(e[i])||r.isArray(e[i]))?(r.isPlainObject(e[i])&&!r.isPlainObject(t[i])&&(t[i]={}),r.isArray(e[i])&&!r.isArray(t[i])&&(t[i]=[]),n(t[i],e[i],o)):void 0!==e[i]&&(t[i]=e[i])}var r=e,o=[],i=window.console;r.warn=function(t){i&&r.warning&&(!r.config.silent||r.config.debug)&&i.warn("[VueResource warn]: "+t)},r.error=function(t){i&&i.error(t)},r.trim=function(t){return t.replace(/^\s*|\s*$/g,"")},r.toLower=function(t){return t?t.toLowerCase():""},r.isArray=Array.isArray,r.isString=function(t){return"string"==typeof t},r.isFunction=function(t){return"function"==typeof t},r.isObject=function(t){return null!==t&&"object"==typeof t},r.isPlainObject=function(t){return r.isObject(t)&&Object.getPrototypeOf(t)==Object.prototype},r.options=function(t,e,n){return n=n||{},r.isFunction(n)&&(n=n.call(e)),r.merge(t.bind({$vm:e,$options:n}),t,{$options:n})},r.each=function(t,e){var n,o;if("number"==typeof t.length)for(n=0;n=200&&t.status<300,t.ok?t:i.reject(t)},function(t){return t instanceof Error&&o.error(t),i.reject(t)}),n.success&&u.success(n.success),n.error&&u.error(n.error),u}var o=n(1),i=n(9),s=n(11),a=n(12),u={"Content-Type":"application/json"};r.options={method:"get",data:"",params:{},headers:{},xhr:null,jsonp:"callback",beforeSend:null,crossOrigin:null,emulateHTTP:!1,emulateJSON:!1,timeout:0},r.interceptors=[n(14),n(15),n(16),n(18),n(19),n(20),n(21)],r.headers={put:u,post:u,patch:u,"delete":u,common:{Accept:"application/json, text/plain, */*"},custom:{"X-Requested-With":"XMLHttpRequest"}},["get","put","post","patch","delete","jsonp"].forEach(function(t){r[t]=function(e,n,r,i){return o.isFunction(n)&&(i=r,r=n,n=void 0),o.isObject(r)&&(i=r,r=void 0),this(e,o.extend({method:t,data:n,success:r},i))}}),t.exports=o.http=r},function(t,e,n){function r(t,e){t instanceof i?this.promise=t:this.promise=new i(t.bind(e)),this.context=e}var o=n(1),i=window.Promise||n(10);r.all=function(t,e){return new r(i.all(t),e)},r.resolve=function(t,e){return new r(i.resolve(t),e)},r.reject=function(t,e){return new r(i.reject(t),e)},r.race=function(t,e){return new r(i.race(t),e)};var s=r.prototype;s.bind=function(t){return this.context=t,this},s.then=function(t,e){return t&&t.bind&&this.context&&(t=t.bind(this.context)),e&&e.bind&&this.context&&(e=e.bind(this.context)),this.promise=this.promise.then(t,e),this},s["catch"]=function(t){return t&&t.bind&&this.context&&(t=t.bind(this.context)),this.promise=this.promise["catch"](t),this},s["finally"]=function(t){return this.then(function(e){return t.call(this),e},function(e){return t.call(this),i.reject(e)})},s.success=function(t){return o.warn("The `success` method has been deprecated. Use the `then` method instead."),this.then(function(e){return t.call(this,e.data,e.status,e)||e})},s.error=function(t){return o.warn("The `error` method has been deprecated. Use the `catch` method instead."),this["catch"](function(e){return t.call(this,e.data,e.status,e)||e})},s.always=function(t){o.warn("The `always` method has been deprecated. Use the `finally` method instead.");var e=function(e){return t.call(this,e.data,e.status,e)||e};return this.then(e,e)},t.exports=r},function(t,e,n){function r(t){this.state=a,this.value=void 0,this.deferred=[];var e=this;try{t(function(t){e.resolve(t)},function(t){e.reject(t)})}catch(n){e.reject(n)}}var o=n(1),i=0,s=1,a=2;r.reject=function(t){return new r(function(e,n){n(t)})},r.resolve=function(t){return new r(function(e,n){e(t)})},r.all=function(t){return new r(function(e,n){function o(n){return function(r){s[n]=r,i+=1,i===t.length&&e(s)}}var i=0,s=[];0===t.length&&e(s);for(var a=0;a=200&&t.status<300,t})}},function(t,e,n){function r(t,e){t instanceof i?this.promise=t:this.promise=new i(t.bind(e)),this.context=e}var o=n(1),i=window.Promise||n(11);r.all=function(t,e){return new r(i.all(t),e)},r.resolve=function(t,e){return new r(i.resolve(t),e)},r.reject=function(t,e){return new r(i.reject(t),e)},r.race=function(t,e){return new r(i.race(t),e)};var s=r.prototype;s.bind=function(t){return this.context=t,this},s.then=function(t,e){return t&&t.bind&&this.context&&(t=t.bind(this.context)),e&&e.bind&&this.context&&(e=e.bind(this.context)),this.promise=this.promise.then(t,e),this},s["catch"]=function(t){return t&&t.bind&&this.context&&(t=t.bind(this.context)),this.promise=this.promise["catch"](t),this},s["finally"]=function(t){return this.then(function(e){return t.call(this),e},function(e){return t.call(this),i.reject(e)})},s.success=function(t){return o.warn("The `success` method has been deprecated. Use the `then` method instead."),this.then(function(e){return t.call(this,e.data,e.status,e)||e})},s.error=function(t){return o.warn("The `error` method has been deprecated. Use the `catch` method instead."),this["catch"](function(e){return t.call(this,e.data,e.status,e)||e})},s.always=function(t){o.warn("The `always` method has been deprecated. Use the `finally` method instead.");var e=function(e){return t.call(this,e.data,e.status,e)||e};return this.then(e,e)},t.exports=r},function(t,e,n){function r(t){this.state=a,this.value=void 0,this.deferred=[];var e=this;try{t(function(t){e.resolve(t)},function(t){e.reject(t)})}catch(n){e.reject(n)}}var o=n(1),i=0,s=1,a=2;r.reject=function(t){return new r(function(e,n){n(t)})},r.resolve=function(t){return new r(function(e,n){e(t)})},r.all=function(t){return new r(function(e,n){function o(n){return function(r){s[n]=r,i+=1,i===t.length&&e(s)}}var i=0,s=[];0===t.length&&e(s);for(var a=0;a= 200 && response.status < 300; + + return response; + }); + +}; + +function parseHeaders(str) { + + var headers = {}, value, name, i; + + if (_.isString(str)) { + _.each(str.split('\n'), function (row) { + + i = row.indexOf(':'); + name = _.trim(_.toLower(row.slice(0, i))); + value = _.trim(row.slice(i + 1)); + + if (headers[name]) { + + if (_.isArray(headers[name])) { + headers[name].push(value); + } else { + headers[name] = [headers[name], value]; + } + + } else { + + headers[name] = value; + } + + }); + } + + return headers; +} diff --git a/src/http/client/xhr.js b/src/http/client/xhr.js index 11650129..24c86131 100644 --- a/src/http/client/xhr.js +++ b/src/http/client/xhr.js @@ -29,7 +29,7 @@ module.exports = function (request) { response.data = xhr.responseText; response.status = xhr.status; response.statusText = xhr.statusText; - response.headers = getHeaders(xhr); + response.headers = xhr.getAllResponseHeaders(); resolve(response); }; @@ -41,51 +41,3 @@ module.exports = function (request) { xhr.send(request.data); }); }; - -function getHeaders(xhr) { - - var headers; - - if (!headers) { - headers = parseHeaders(xhr.getAllResponseHeaders()); - } - - return function (name) { - - if (name) { - return headers[_.toLower(name)]; - } - - return headers; - }; -} - -function parseHeaders(str) { - - var headers = {}, value, name, i; - - if (_.isString(str)) { - _.each(str.split('\n'), function (row) { - - i = row.indexOf(':'); - name = _.trim(_.toLower(row.slice(0, i))); - value = _.trim(row.slice(i + 1)); - - if (headers[name]) { - - if (_.isArray(headers[name])) { - headers[name].push(value); - } else { - headers[name] = [headers[name], value]; - } - - } else { - - headers[name] = value; - } - - }); - } - - return headers; -} diff --git a/src/http/index.js b/src/http/index.js index 1f5d6a57..c0333a8e 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -3,14 +3,14 @@ */ var _ = require('../util'); +var Client = require('./client'); var Promise = require('../promise'); var interceptor = require('./interceptor'); -var defaultClient = require('./client/default'); var jsonType = {'Content-Type': 'application/json'}; function Http(url, options) { - var client = defaultClient, request, promise; + var client = Client, request, promise; Http.interceptors.forEach(function (handler) { client = interceptor(handler, this.$vm)(client); @@ -20,7 +20,6 @@ function Http(url, options) { request = _.merge({}, Http.options, this.$options, options); promise = client(request).bind(this.$vm).then(function (response) { - response.ok = response.status >= 200 && response.status < 300; return response.ok ? response : Promise.reject(response); }, function (response) {