1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318 |
- /*!
- * vue-resource v0.9.3
- * 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());
- }(this, function () { 'use strict';
- /**
- * Promises/A+ polyfill v1.1.4 (https://github.com/bramstein/promis)
- */
- var RESOLVED = 0;
- var REJECTED = 1;
- var PENDING = 2;
- function Promise$2(executor) {
- this.state = PENDING;
- this.value = undefined;
- this.deferred = [];
- var promise = this;
- try {
- executor(function (x) {
- promise.resolve(x);
- }, function (r) {
- promise.reject(r);
- });
- } catch (e) {
- promise.reject(e);
- }
- }
- Promise$2.reject = function (r) {
- return new Promise$2(function (resolve, reject) {
- reject(r);
- });
- };
- Promise$2.resolve = function (x) {
- return new Promise$2(function (resolve, reject) {
- resolve(x);
- });
- };
- Promise$2.all = function all(iterable) {
- return new Promise$2(function (resolve, reject) {
- var count = 0,
- result = [];
- if (iterable.length === 0) {
- resolve(result);
- }
- function resolver(i) {
- return function (x) {
- result[i] = x;
- count += 1;
- if (count === iterable.length) {
- resolve(result);
- }
- };
- }
- for (var i = 0; i < iterable.length; i += 1) {
- Promise$2.resolve(iterable[i]).then(resolver(i), reject);
- }
- });
- };
- Promise$2.race = function race(iterable) {
- return new Promise$2(function (resolve, reject) {
- for (var i = 0; i < iterable.length; i += 1) {
- Promise$2.resolve(iterable[i]).then(resolve, reject);
- }
- });
- };
- var p$1 = Promise$2.prototype;
- p$1.resolve = function resolve(x) {
- var promise = this;
- if (promise.state === PENDING) {
- if (x === promise) {
- throw new TypeError('Promise settled with itself.');
- }
- var called = false;
- try {
- var then = x && x['then'];
- if (x !== null && typeof x === 'object' && typeof then === 'function') {
- then.call(x, function (x) {
- if (!called) {
- promise.resolve(x);
- }
- called = true;
- }, function (r) {
- if (!called) {
- promise.reject(r);
- }
- called = true;
- });
- return;
- }
- } catch (e) {
- if (!called) {
- promise.reject(e);
- }
- return;
- }
- promise.state = RESOLVED;
- promise.value = x;
- promise.notify();
- }
- };
- p$1.reject = function reject(reason) {
- var promise = this;
- if (promise.state === PENDING) {
- if (reason === promise) {
- throw new TypeError('Promise settled with itself.');
- }
- promise.state = REJECTED;
- promise.value = reason;
- promise.notify();
- }
- };
- p$1.notify = function notify() {
- var promise = this;
- nextTick(function () {
- if (promise.state !== PENDING) {
- while (promise.deferred.length) {
- var deferred = promise.deferred.shift(),
- onResolved = deferred[0],
- onRejected = deferred[1],
- resolve = deferred[2],
- reject = deferred[3];
- try {
- if (promise.state === RESOLVED) {
- if (typeof onResolved === 'function') {
- resolve(onResolved.call(undefined, promise.value));
- } else {
- resolve(promise.value);
- }
- } else if (promise.state === REJECTED) {
- if (typeof onRejected === 'function') {
- resolve(onRejected.call(undefined, promise.value));
- } else {
- reject(promise.value);
- }
- }
- } catch (e) {
- reject(e);
- }
- }
- }
- });
- };
- p$1.then = function then(onResolved, onRejected) {
- var promise = this;
- return new Promise$2(function (resolve, reject) {
- promise.deferred.push([onResolved, onRejected, resolve, reject]);
- promise.notify();
- });
- };
- p$1.catch = function (onRejected) {
- return this.then(undefined, onRejected);
- };
- var PromiseObj = window.Promise || Promise$2;
- function Promise$1(executor, context) {
- if (executor instanceof PromiseObj) {
- this.promise = executor;
- } else {
- this.promise = new PromiseObj(executor.bind(context));
- }
- this.context = context;
- }
- Promise$1.all = function (iterable, context) {
- return new Promise$1(PromiseObj.all(iterable), context);
- };
- Promise$1.resolve = function (value, context) {
- return new Promise$1(PromiseObj.resolve(value), context);
- };
- Promise$1.reject = function (reason, context) {
- return new Promise$1(PromiseObj.reject(reason), context);
- };
- Promise$1.race = function (iterable, context) {
- return new Promise$1(PromiseObj.race(iterable), context);
- };
- var p = Promise$1.prototype;
- p.bind = function (context) {
- this.context = context;
- return this;
- };
- p.then = function (fulfilled, rejected) {
- if (fulfilled && fulfilled.bind && this.context) {
- fulfilled = fulfilled.bind(this.context);
- }
- if (rejected && rejected.bind && this.context) {
- rejected = rejected.bind(this.context);
- }
- return new Promise$1(this.promise.then(fulfilled, rejected), this.context);
- };
- p.catch = function (rejected) {
- if (rejected && rejected.bind && this.context) {
- rejected = rejected.bind(this.context);
- }
- return new Promise$1(this.promise.catch(rejected), this.context);
- };
- p.finally = function (callback) {
- return this.then(function (value) {
- callback.call(this);
- return value;
- }, function (reason) {
- callback.call(this);
- return PromiseObj.reject(reason);
- });
- };
- var debug = false;
- var util = {};
- var array = [];
- function Util (Vue) {
- util = Vue.util;
- debug = Vue.config.debug || !Vue.config.silent;
- }
- function warn(msg) {
- if (typeof console !== 'undefined' && debug) {
- console.warn('[VueResource warn]: ' + msg);
- }
- }
- function error(msg) {
- if (typeof console !== 'undefined') {
- console.error(msg);
- }
- }
- function nextTick(cb, ctx) {
- return util.nextTick(cb, ctx);
- }
- function trim(str) {
- return str.replace(/^\s*|\s*$/g, '');
- }
- var isArray = Array.isArray;
- function isString(val) {
- return typeof val === 'string';
- }
- function isBoolean(val) {
- return val === true || val === false;
- }
- function isFunction(val) {
- return typeof val === 'function';
- }
- function isObject(obj) {
- return obj !== null && typeof obj === 'object';
- }
- function isPlainObject(obj) {
- return isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype;
- }
- function isFormData(obj) {
- return typeof FormData !== 'undefined' && obj instanceof FormData;
- }
- function when(value, fulfilled, rejected) {
- var promise = Promise$1.resolve(value);
- if (arguments.length < 2) {
- return promise;
- }
- return promise.then(fulfilled, rejected);
- }
- function options(fn, obj, opts) {
- opts = opts || {};
- if (isFunction(opts)) {
- opts = opts.call(obj);
- }
- return merge(fn.bind({ $vm: obj, $options: opts }), fn, { $options: opts });
- }
- function each(obj, iterator) {
- var i, key;
- if (typeof obj.length == 'number') {
- for (i = 0; i < obj.length; i++) {
- iterator.call(obj[i], obj[i], i);
- }
- } else if (isObject(obj)) {
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- iterator.call(obj[key], obj[key], key);
- }
- }
- }
- return obj;
- }
- var assign = Object.assign || _assign;
- function merge(target) {
- var args = array.slice.call(arguments, 1);
- args.forEach(function (source) {
- _merge(target, source, true);
- });
- return target;
- }
- function defaults(target) {
- var args = array.slice.call(arguments, 1);
- args.forEach(function (source) {
- for (var key in source) {
- if (target[key] === undefined) {
- target[key] = source[key];
- }
- }
- });
- return target;
- }
- function _assign(target) {
- var args = array.slice.call(arguments, 1);
- args.forEach(function (source) {
- _merge(target, source);
- });
- return target;
- }
- function _merge(target, source, deep) {
- for (var key in source) {
- if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
- if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
- target[key] = {};
- }
- if (isArray(source[key]) && !isArray(target[key])) {
- target[key] = [];
- }
- _merge(target[key], source[key], deep);
- } else if (source[key] !== undefined) {
- target[key] = source[key];
- }
- }
- }
- function root (options, next) {
- var url = next(options);
- if (isString(options.root) && !url.match(/^(https?:)?\//)) {
- url = options.root + '/' + url;
- }
- return url;
- }
- function query (options, next) {
- var urlParams = Object.keys(Url.options.params),
- query = {},
- url = next(options);
- each(options.params, function (value, key) {
- if (urlParams.indexOf(key) === -1) {
- query[key] = value;
- }
- });
- query = Url.params(query);
- if (query) {
- url += (url.indexOf('?') == -1 ? '?' : '&') + query;
- }
- return url;
- }
- /**
- * URL Template v2.0.6 (https://github.com/bramstein/url-template)
- */
- function expand(url, params, variables) {
- var tmpl = parse(url),
- expanded = tmpl.expand(params);
- if (variables) {
- variables.push.apply(variables, tmpl.vars);
- }
- return expanded;
- }
- function parse(template) {
- var operators = ['+', '#', '.', '/', ';', '?', '&'],
- variables = [];
- return {
- vars: variables,
- expand: function (context) {
- return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) {
- if (expression) {
- var operator = null,
- values = [];
- if (operators.indexOf(expression.charAt(0)) !== -1) {
- operator = expression.charAt(0);
- expression = expression.substr(1);
- }
- expression.split(/,/g).forEach(function (variable) {
- var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable);
- values.push.apply(values, getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
- variables.push(tmp[1]);
- });
- if (operator && operator !== '+') {
- var separator = ',';
- if (operator === '?') {
- separator = '&';
- } else if (operator !== '#') {
- separator = operator;
- }
- return (values.length !== 0 ? operator : '') + values.join(separator);
- } else {
- return values.join(',');
- }
- } else {
- return encodeReserved(literal);
- }
- });
- }
- };
- }
- function getValues(context, operator, key, modifier) {
- var value = context[key],
- result = [];
- if (isDefined(value) && value !== '') {
- if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
- value = value.toString();
- if (modifier && modifier !== '*') {
- value = value.substring(0, parseInt(modifier, 10));
- }
- result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
- } else {
- if (modifier === '*') {
- if (Array.isArray(value)) {
- value.filter(isDefined).forEach(function (value) {
- result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
- });
- } else {
- Object.keys(value).forEach(function (k) {
- if (isDefined(value[k])) {
- result.push(encodeValue(operator, value[k], k));
- }
- });
- }
- } else {
- var tmp = [];
- if (Array.isArray(value)) {
- value.filter(isDefined).forEach(function (value) {
- tmp.push(encodeValue(operator, value));
- });
- } else {
- Object.keys(value).forEach(function (k) {
- if (isDefined(value[k])) {
- tmp.push(encodeURIComponent(k));
- tmp.push(encodeValue(operator, value[k].toString()));
- }
- });
- }
- if (isKeyOperator(operator)) {
- result.push(encodeURIComponent(key) + '=' + tmp.join(','));
- } else if (tmp.length !== 0) {
- result.push(tmp.join(','));
- }
- }
- }
- } else {
- if (operator === ';') {
- result.push(encodeURIComponent(key));
- } else if (value === '' && (operator === '&' || operator === '?')) {
- result.push(encodeURIComponent(key) + '=');
- } else if (value === '') {
- result.push('');
- }
- }
- return result;
- }
- function isDefined(value) {
- return value !== undefined && value !== null;
- }
- function isKeyOperator(operator) {
- return operator === ';' || operator === '&' || operator === '?';
- }
- function encodeValue(operator, value, key) {
- value = operator === '+' || operator === '#' ? encodeReserved(value) : encodeURIComponent(value);
- if (key) {
- return encodeURIComponent(key) + '=' + value;
- } else {
- return value;
- }
- }
- function encodeReserved(str) {
- return str.split(/(%[0-9A-Fa-f]{2})/g).map(function (part) {
- if (!/%[0-9A-Fa-f]/.test(part)) {
- part = encodeURI(part);
- }
- return part;
- }).join('');
- }
- function template (options) {
- var variables = [],
- url = expand(options.url, options.params, variables);
- variables.forEach(function (key) {
- delete options.params[key];
- });
- return url;
- }
- /**
- * Service for URL templating.
- */
- var ie = document.documentMode;
- var el = document.createElement('a');
- function Url(url, params) {
- var self = this || {},
- options = url,
- transform;
- if (isString(url)) {
- options = { url: url, params: params };
- }
- options = merge({}, Url.options, self.$options, options);
- Url.transforms.forEach(function (handler) {
- transform = factory(handler, transform, self.$vm);
- });
- return transform(options);
- }
- /**
- * Url options.
- */
- Url.options = {
- url: '',
- root: null,
- params: {}
- };
- /**
- * Url transforms.
- */
- Url.transforms = [template, query, root];
- /**
- * Encodes a Url parameter string.
- *
- * @param {Object} obj
- */
- Url.params = function (obj) {
- var params = [],
- escape = encodeURIComponent;
- params.add = function (key, value) {
- if (isFunction(value)) {
- value = value();
- }
- if (value === null) {
- value = '';
- }
- this.push(escape(key) + '=' + escape(value));
- };
- serialize(params, obj);
- return params.join('&').replace(/%20/g, '+');
- };
- /**
- * Parse a URL and return its components.
- *
- * @param {String} url
- */
- Url.parse = function (url) {
- if (ie) {
- el.href = url;
- url = el.href;
- }
- el.href = url;
- return {
- href: el.href,
- protocol: el.protocol ? el.protocol.replace(/:$/, '') : '',
- port: el.port,
- host: el.host,
- hostname: el.hostname,
- pathname: el.pathname.charAt(0) === '/' ? el.pathname : '/' + el.pathname,
- search: el.search ? el.search.replace(/^\?/, '') : '',
- hash: el.hash ? el.hash.replace(/^#/, '') : ''
- };
- };
- function factory(handler, next, vm) {
- return function (options) {
- return handler.call(vm, options, next);
- };
- }
- function serialize(params, obj, scope) {
- var array = isArray(obj),
- plain = isPlainObject(obj),
- hash;
- each(obj, function (value, key) {
- hash = isObject(value) || isArray(value);
- if (scope) {
- key = scope + '[' + (plain || hash ? key : '') + ']';
- }
- if (!scope && array) {
- params.add(value.name, value.value);
- } else if (hash) {
- serialize(params, value, key);
- } else {
- params.add(key, value);
- }
- });
- }
- function xdrClient (request) {
- return new Promise$1(function (resolve) {
- var xdr = new XDomainRequest(),
- handler = function (event) {
- var response = request.respondWith(xdr.responseText, {
- status: xdr.status,
- statusText: xdr.statusText
- });
- resolve(response);
- };
- request.abort = function () {
- return xdr.abort();
- };
- xdr.open(request.method, request.getUrl(), true);
- xdr.timeout = 0;
- xdr.onload = handler;
- xdr.onerror = handler;
- xdr.ontimeout = function () {};
- xdr.onprogress = function () {};
- xdr.send(request.getBody());
- });
- }
- var ORIGIN_URL = Url.parse(location.href);
- var SUPPORTS_CORS = 'withCredentials' in new XMLHttpRequest();
- function cors (request, next) {
- if (!isBoolean(request.crossOrigin) && crossOrigin(request)) {
- request.crossOrigin = true;
- }
- if (request.crossOrigin) {
- if (!SUPPORTS_CORS) {
- request.client = xdrClient;
- }
- delete request.emulateHTTP;
- }
- next();
- }
- function crossOrigin(request) {
- var requestUrl = Url.parse(Url(request));
- return requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host;
- }
- function body (request, next) {
- if (request.emulateJSON && isPlainObject(request.body)) {
- request.body = Url.params(request.body);
- request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
- }
- if (isFormData(request.body)) {
- delete request.headers['Content-Type'];
- }
- if (isPlainObject(request.body)) {
- request.body = JSON.stringify(request.body);
- }
- next(function (response) {
- var contentType = response.headers['Content-Type'];
- if (isString(contentType) && contentType.indexOf('application/json') === 0) {
- try {
- response.data = response.json();
- } catch (e) {
- response.data = null;
- }
- } else {
- response.data = response.text();
- }
- });
- }
- function jsonpClient (request) {
- return new Promise$1(function (resolve) {
- var name = request.jsonp || 'callback',
- callback = '_jsonp' + Math.random().toString(36).substr(2),
- body = null,
- handler,
- script;
- handler = function (event) {
- var status = 0;
- if (event.type === 'load' && body !== null) {
- status = 200;
- } else if (event.type === 'error') {
- status = 404;
- }
- resolve(request.respondWith(body, { status: status }));
- delete window[callback];
- document.body.removeChild(script);
- };
- request.params[name] = callback;
- window[callback] = function (result) {
- body = JSON.stringify(result);
- };
- script = document.createElement('script');
- script.src = request.getUrl();
- script.type = 'text/javascript';
- script.async = true;
- script.onload = handler;
- script.onerror = handler;
- document.body.appendChild(script);
- });
- }
- function jsonp (request, next) {
- if (request.method == 'JSONP') {
- request.client = jsonpClient;
- }
- next(function (response) {
- if (request.method == 'JSONP') {
- response.data = response.json();
- }
- });
- }
- function before (request, next) {
- if (isFunction(request.before)) {
- request.before.call(this, request);
- }
- next();
- }
- /**
- * HTTP method override Interceptor.
- */
- function method (request, next) {
- if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) {
- request.headers['X-HTTP-Method-Override'] = request.method;
- request.method = 'POST';
- }
- next();
- }
- function header (request, next) {
- request.method = request.method.toUpperCase();
- request.headers = assign({}, Http.headers.common, !request.crossOrigin ? Http.headers.custom : {}, Http.headers[request.method.toLowerCase()], request.headers);
- next();
- }
- /**
- * Timeout Interceptor.
- */
- function timeout (request, next) {
- var timeout;
- if (request.timeout) {
- timeout = setTimeout(function () {
- request.abort();
- }, request.timeout);
- }
- next(function (response) {
- clearTimeout(timeout);
- });
- }
- function xhrClient (request) {
- return new Promise$1(function (resolve) {
- 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),
- headers: parseHeaders(xhr.getAllResponseHeaders())
- });
- resolve(response);
- };
- request.abort = function () {
- return xhr.abort();
- };
- xhr.open(request.method, request.getUrl(), true);
- xhr.timeout = 0;
- xhr.onload = handler;
- xhr.onerror = handler;
- if (request.progress) {
- if (request.method === 'GET') {
- xhr.addEventListener('progress', request.progress);
- } else if (/^(POST|PUT)$/i.test(request.method)) {
- xhr.upload.addEventListener('progress', request.progress);
- }
- }
- if (request.credentials === true) {
- xhr.withCredentials = true;
- }
- each(request.headers || {}, function (value, header) {
- xhr.setRequestHeader(header, value);
- });
- xhr.send(request.getBody());
- });
- }
- function parseHeaders(str) {
- var headers = {},
- value,
- name,
- i;
- each(trim(str).split('\n'), function (row) {
- i = row.indexOf(':');
- name = trim(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;
- }
- function Client (context) {
- var reqHandlers = [sendRequest],
- resHandlers = [],
- handler;
- if (!isObject(context)) {
- context = null;
- }
- function Client(request) {
- return new Promise$1(function (resolve) {
- function exec() {
- handler = reqHandlers.pop();
- if (isFunction(handler)) {
- handler.call(context, request, next);
- } else {
- warn('Invalid interceptor of type ' + typeof handler + ', must be a function');
- next();
- }
- }
- function next(response) {
- if (isFunction(response)) {
- resHandlers.unshift(response);
- } else if (isObject(response)) {
- resHandlers.forEach(function (handler) {
- response = when(response, function (response) {
- return handler.call(context, response) || response;
- });
- });
- when(response, resolve);
- return;
- }
- exec();
- }
- exec();
- }, context);
- }
- Client.use = function (handler) {
- reqHandlers.push(handler);
- };
- return Client;
- }
- function sendRequest(request, resolve) {
- var client = request.client || xhrClient;
- resolve(client(request));
- }
- var classCallCheck = function (instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- };
- /**
- * 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);
- this.url = url;
- this.body = body;
- this.headers = headers || {};
- this.status = status || 0;
- this.statusText = statusText || '';
- this.ok = status >= 200 && status < 300;
- }
- Response.prototype.text = function text() {
- return this.body;
- };
- Response.prototype.blob = function blob() {
- return new Blob([this.body]);
- };
- Response.prototype.json = function json() {
- return JSON.parse(this.body);
- };
- return Response;
- }();
- var Request = function () {
- function Request(options) {
- classCallCheck(this, Request);
- this.method = 'GET';
- this.body = null;
- this.params = {};
- this.headers = {};
- assign(this, options);
- }
- Request.prototype.getUrl = function getUrl() {
- return Url(this);
- };
- Request.prototype.getBody = function getBody() {
- return this.body;
- };
- Request.prototype.respondWith = function respondWith(body, options) {
- return new Response(body, assign(options || {}, { url: this.getUrl() }));
- };
- return Request;
- }();
- /**
- * 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' };
- function Http(options) {
- var self = this || {},
- client = Client(self.$vm);
- defaults(options || {}, self.$options, Http.options);
- Http.interceptors.forEach(function (handler) {
- client.use(handler);
- });
- return client(new Request(options)).then(function (response) {
- return response.ok ? response : Promise$1.reject(response);
- }, function (response) {
- if (response instanceof Error) {
- error(response);
- }
- return Promise$1.reject(response);
- });
- }
- Http.options = {};
- Http.headers = {
- put: JSON_CONTENT_TYPE,
- post: JSON_CONTENT_TYPE,
- patch: JSON_CONTENT_TYPE,
- delete: JSON_CONTENT_TYPE,
- custom: CUSTOM_HEADERS,
- common: COMMON_HEADERS
- };
- Http.interceptors = [before, timeout, method, body, jsonp, header, cors];
- ['get', 'delete', 'head', 'jsonp'].forEach(function (method) {
- Http[method] = function (url, options) {
- return this(assign(options || {}, { url: url, method: method }));
- };
- });
- ['post', 'put', 'patch'].forEach(function (method) {
- Http[method] = function (url, body, options) {
- return this(assign(options || {}, { url: url, method: method, body: body }));
- };
- });
- function Resource(url, params, actions, options) {
- var self = this || {},
- resource = {};
- actions = assign({}, Resource.actions, actions);
- each(actions, function (action, name) {
- action = merge({ url: url, params: params || {} }, options, action);
- resource[name] = function () {
- return (self.$http || Http)(opts(action, arguments));
- };
- });
- return resource;
- }
- function opts(action, args) {
- var options = assign({}, action),
- params = {},
- body;
- switch (args.length) {
- case 2:
- params = args[0];
- body = args[1];
- break;
- case 1:
- if (/^(POST|PUT|PATCH)$/i.test(options.method)) {
- body = args[0];
- } else {
- params = args[0];
- }
- break;
- case 0:
- break;
- default:
- throw 'Expected up to 4 arguments [params, body], got ' + args.length + ' arguments';
- }
- options.body = body;
- options.params = assign({}, options.params, params);
- return options;
- }
- Resource.actions = {
- get: { method: 'GET' },
- save: { method: 'POST' },
- query: { method: 'GET' },
- update: { method: 'PUT' },
- remove: { method: 'DELETE' },
- delete: { method: 'DELETE' }
- };
- function plugin(Vue) {
- if (plugin.installed) {
- return;
- }
- Util(Vue);
- Vue.url = Url;
- Vue.http = Http;
- Vue.resource = Resource;
- Vue.Promise = Promise$1;
- Object.defineProperties(Vue.prototype, {
- $url: {
- get: function () {
- return options(Vue.url, this, this.$options.url);
- }
- },
- $http: {
- get: function () {
- return options(Vue.http, this, this.$options.http);
- }
- },
- $resource: {
- get: function () {
- return Vue.resource.bind(this);
- }
- },
- $promise: {
- get: function () {
- var _this = this;
- return function (executor) {
- return new Vue.Promise(executor, _this);
- };
- }
- }
- });
- }
- if (typeof window !== 'undefined' && window.Vue) {
- window.Vue.use(plugin);
- }
- return plugin;
- }));
|