| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563 |
- /*!
- * vue-resource v1.5.0
- * https://github.com/pagekit/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$1(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$1.reject = function (r) {
- return new Promise$1(function (resolve, reject) {
- reject(r);
- });
- };
- Promise$1.resolve = function (x) {
- return new Promise$1(function (resolve, reject) {
- resolve(x);
- });
- };
- Promise$1.all = function all(iterable) {
- return new Promise$1(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$1.resolve(iterable[i]).then(resolver(i), reject);
- }
- });
- };
- Promise$1.race = function race(iterable) {
- return new Promise$1(function (resolve, reject) {
- for (var i = 0; i < iterable.length; i += 1) {
- Promise$1.resolve(iterable[i]).then(resolve, reject);
- }
- });
- };
- var p = Promise$1.prototype;
- p.resolve = function resolve(x) {
- var promise = this;
- if (promise.state === PENDING) {
- if (x === promise) {
- throw new TypeError('Promise settled with itself.');
- }
- var called = false;
- try {
- var then = x && x['then'];
- if (x !== null && typeof x === 'object' && typeof then === 'function') {
- then.call(x, function (x) {
- if (!called) {
- promise.resolve(x);
- }
- called = true;
- }, function (r) {
- if (!called) {
- promise.reject(r);
- }
- called = true;
- });
- return;
- }
- } catch (e) {
- if (!called) {
- promise.reject(e);
- }
- return;
- }
- promise.state = RESOLVED;
- promise.value = x;
- promise.notify();
- }
- };
- p.reject = function reject(reason) {
- var promise = this;
- if (promise.state === PENDING) {
- if (reason === promise) {
- throw new TypeError('Promise settled with itself.');
- }
- promise.state = REJECTED;
- promise.value = reason;
- promise.notify();
- }
- };
- p.notify = function notify() {
- var promise = this;
- nextTick(function () {
- if (promise.state !== PENDING) {
- while (promise.deferred.length) {
- var deferred = promise.deferred.shift(),
- onResolved = deferred[0],
- onRejected = deferred[1],
- resolve = deferred[2],
- reject = deferred[3];
- try {
- if (promise.state === RESOLVED) {
- if (typeof onResolved === 'function') {
- resolve(onResolved.call(undefined, promise.value));
- } else {
- resolve(promise.value);
- }
- } else if (promise.state === REJECTED) {
- if (typeof onRejected === 'function') {
- resolve(onRejected.call(undefined, promise.value));
- } else {
- reject(promise.value);
- }
- }
- } catch (e) {
- reject(e);
- }
- }
- }
- });
- };
- p.then = function then(onResolved, onRejected) {
- var promise = this;
- return new Promise$1(function (resolve, reject) {
- promise.deferred.push([onResolved, onRejected, resolve, reject]);
- promise.notify();
- });
- };
- p.catch = function (onRejected) {
- return this.then(undefined, onRejected);
- };
- /**
- * Promise adapter.
- */
- if (typeof Promise === 'undefined') {
- window.Promise = Promise$1;
- }
- function PromiseObj(executor, context) {
- if (executor instanceof Promise) {
- this.promise = executor;
- } else {
- this.promise = new Promise(executor.bind(context));
- }
- this.context = context;
- }
- PromiseObj.all = function (iterable, context) {
- return new PromiseObj(Promise.all(iterable), context);
- };
- PromiseObj.resolve = function (value, context) {
- return new PromiseObj(Promise.resolve(value), context);
- };
- PromiseObj.reject = function (reason, context) {
- return new PromiseObj(Promise.reject(reason), context);
- };
- PromiseObj.race = function (iterable, context) {
- return new PromiseObj(Promise.race(iterable), context);
- };
- var p$1 = PromiseObj.prototype;
- p$1.bind = function (context) {
- this.context = context;
- return this;
- };
- p$1.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 PromiseObj(this.promise.then(fulfilled, rejected), this.context);
- };
- p$1.catch = function (rejected) {
- if (rejected && rejected.bind && this.context) {
- rejected = rejected.bind(this.context);
- }
- return new PromiseObj(this.promise.catch(rejected), this.context);
- };
- p$1.finally = function (callback) {
- return this.then(function (value) {
- callback.call(this);
- return value;
- }, function (reason) {
- callback.call(this);
- return Promise.reject(reason);
- }
- );
- };
- /**
- * Utility functions.
- */
- var ref = {};
- var hasOwnProperty = ref.hasOwnProperty;
- var ref$1 = [];
- var slice = ref$1.slice;
- var debug = false, ntick;
- var inBrowser = typeof window !== 'undefined';
- function Util (ref) {
- var config = ref.config;
- var nextTick = ref.nextTick;
- ntick = nextTick;
- debug = config.debug || !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 ntick(cb, ctx);
- }
- function trim(str) {
- return str ? str.replace(/^\s*|\s*$/g, '') : '';
- }
- function trimEnd(str, chars) {
- if (str && chars === undefined) {
- return str.replace(/\s+$/, '');
- }
- if (!str || !chars) {
- return str;
- }
- return str.replace(new RegExp(("[" + chars + "]+$")), '');
- }
- function toLower(str) {
- return str ? str.toLowerCase() : '';
- }
- function toUpper(str) {
- return str ? str.toUpperCase() : '';
- }
- var isArray = Array.isArray;
- function isString(val) {
- return typeof val === 'string';
- }
- 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 isBlob(obj) {
- return typeof Blob !== 'undefined' && obj instanceof Blob;
- }
- function isFormData(obj) {
- return typeof FormData !== 'undefined' && obj instanceof FormData;
- }
- function when(value, fulfilled, rejected) {
- var promise = PromiseObj.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 (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 (hasOwnProperty.call(obj, key)) {
- iterator.call(obj[key], obj[key], key);
- }
- }
- }
- return obj;
- }
- var assign = Object.assign || _assign;
- function merge(target) {
- var args = slice.call(arguments, 1);
- args.forEach(function (source) {
- _merge(target, source, true);
- });
- return target;
- }
- function defaults(target) {
- var args = 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 = 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];
- }
- }
- }
- /**
- * Root Prefix Transform.
- */
- function root (options$$1, next) {
- var url = next(options$$1);
- if (isString(options$$1.root) && !/^(https?:)?\//.test(url)) {
- url = trimEnd(options$$1.root, '/') + '/' + url;
- }
- return url;
- }
- /**
- * Query Parameter Transform.
- */
- function query (options$$1, next) {
- var urlParams = Object.keys(Url.options.params), query = {}, url = next(options$$1);
- each(options$$1.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 expand(context) {
- return template.replace(/\{([^{}]+)\}|([^{}]+)/g, function (_, expression, literal) {
- if (expression) {
- var operator = null, values = [];
- if (operators.indexOf(expression.charAt(0)) !== -1) {
- operator = expression.charAt(0);
- expression = expression.substr(1);
- }
- expression.split(/,/g).forEach(function (variable) {
- var tmp = /([^:*]*)(?::(\d+)|(\*))?/.exec(variable);
- values.push.apply(values, 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('');
- }
- /**
- * URL Template (RFC 6570) Transform.
- */
- 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.
- */
- function Url(url, params) {
- var self = this || {}, options$$1 = url, transform;
- if (isString(url)) {
- options$$1 = {url: url, params: params};
- }
- options$$1 = merge({}, Url.options, self.$options, options$$1);
- Url.transforms.forEach(function (handler) {
- if (isString(handler)) {
- handler = Url.transform[handler];
- }
- if (isFunction(handler)) {
- transform = factory(handler, transform, self.$vm);
- }
- });
- return transform(options$$1);
- }
- /**
- * Url options.
- */
- Url.options = {
- url: '',
- root: null,
- params: {}
- };
- /**
- * Url transforms.
- */
- Url.transform = {template: template, query: query, root: root};
- 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) {
- var el = document.createElement('a');
- if (document.documentMode) {
- 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$$1) {
- return handler.call(vm, options$$1, 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);
- }
- });
- }
- /**
- * XDomain client (Internet Explorer).
- */
- function xdrClient (request) {
- return new PromiseObj(function (resolve) {
- var xdr = new XDomainRequest(), handler = function (ref) {
- var type = ref.type;
- var status = 0;
- if (type === 'load') {
- status = 200;
- } else if (type === 'error') {
- status = 500;
- }
- resolve(request.respondWith(xdr.responseText, {status: status}));
- };
- request.abort = function () { return xdr.abort(); };
- xdr.open(request.method, request.getUrl());
- 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.
- */
- var SUPPORTS_CORS = inBrowser && 'withCredentials' in new XMLHttpRequest();
- function cors (request) {
- if (inBrowser) {
- var orgUrl = Url.parse(location.href);
- var reqUrl = Url.parse(request.getUrl());
- if (reqUrl.protocol !== orgUrl.protocol || reqUrl.host !== orgUrl.host) {
- request.crossOrigin = true;
- request.emulateHTTP = false;
- if (!SUPPORTS_CORS) {
- request.client = xdrClient;
- }
- }
- }
- }
- /**
- * Form data Interceptor.
- */
- function form (request) {
- if (isFormData(request.body)) {
- request.headers.delete('Content-Type');
- } else if (isObject(request.body) && request.emulateJSON) {
- request.body = Url.params(request.body);
- request.headers.set('Content-Type', 'application/x-www-form-urlencoded');
- }
- }
- /**
- * JSON Interceptor.
- */
- function json (request) {
- var type = request.headers.get('Content-Type') || '';
- if (isObject(request.body) && type.indexOf('application/json') === 0) {
- request.body = JSON.stringify(request.body);
- }
- return function (response) {
- return response.bodyText ? when(response.text(), function (text) {
- var type = response.headers.get('Content-Type') || '';
- if (type.indexOf('application/json') === 0 || isJson(text)) {
- try {
- response.body = JSON.parse(text);
- } catch (e) {
- response.body = null;
- }
- } else {
- response.body = text;
- }
- return response;
- }) : response;
- };
- }
- function isJson(str) {
- var start = str.match(/^\s*(\[|\{)/);
- var end = {'[': /]\s*$/, '{': /}\s*$/};
- return start && end[start[1]].test(str);
- }
- /**
- * JSONP client (Browser).
- */
- function jsonpClient (request) {
- return new PromiseObj(function (resolve) {
- 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;
- var status = 0;
- if (type === 'load' && body !== null) {
- status = 200;
- } else if (type === 'error') {
- status = 500;
- }
- if (status && window[callback]) {
- delete window[callback];
- document.body.removeChild(script);
- }
- resolve(request.respondWith(body, {status: status}));
- };
- 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';
- script.async = true;
- script.onload = handler;
- script.onerror = handler;
- document.body.appendChild(script);
- });
- }
- /**
- * JSONP Interceptor.
- */
- function jsonp (request) {
- if (request.method == 'JSONP') {
- request.client = jsonpClient;
- }
- }
- /**
- * Before Interceptor.
- */
- function before (request) {
- if (isFunction(request.before)) {
- request.before.call(this, request);
- }
- }
- /**
- * HTTP method override Interceptor.
- */
- function method (request) {
- if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) {
- request.headers.set('X-HTTP-Method-Override', request.method);
- request.method = 'POST';
- }
- }
- /**
- * Header Interceptor.
- */
- function header (request) {
- 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)) {
- request.headers.set(name, value);
- }
- });
- }
- /**
- * XMLHttp client (Browser).
- */
- function xhrClient (request) {
- return new PromiseObj(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)
- });
- each(trim(xhr.getAllResponseHeaders()).split('\n'), function (row) {
- response.headers.append(row.slice(0, row.indexOf(':')), row.slice(row.indexOf(':') + 1));
- });
- resolve(response);
- };
- request.abort = function () { return xhr.abort(); };
- xhr.open(request.method, request.getUrl(), true);
- if (request.timeout) {
- xhr.timeout = request.timeout;
- }
- if (request.responseType && 'responseType' in xhr) {
- xhr.responseType = request.responseType;
- }
- if (request.withCredentials || request.credentials) {
- xhr.withCredentials = true;
- }
- if (!request.crossOrigin) {
- request.headers.set('X-Requested-With', 'XMLHttpRequest');
- }
- // deprecated use downloadProgress
- if (isFunction(request.progress) && request.method === 'GET') {
- xhr.addEventListener('progress', request.progress);
- }
- if (isFunction(request.downloadProgress)) {
- xhr.addEventListener('progress', request.downloadProgress);
- }
- // deprecated use uploadProgress
- if (isFunction(request.progress) && /^(POST|PUT)$/i.test(request.method)) {
- xhr.upload.addEventListener('progress', request.progress);
- }
- if (isFunction(request.uploadProgress) && xhr.upload) {
- xhr.upload.addEventListener('progress', request.uploadProgress);
- }
- request.headers.forEach(function (value, name) {
- xhr.setRequestHeader(name, value);
- });
- xhr.onload = handler;
- xhr.onabort = handler;
- xhr.onerror = handler;
- xhr.ontimeout = handler;
- xhr.send(request.getBody());
- });
- }
- /**
- * Http client (Node).
- */
- function nodeClient (request) {
- var client = require('got');
- return new PromiseObj(function (resolve) {
- var url = request.getUrl();
- var body = request.getBody();
- var method = request.method;
- var headers = {}, handler;
- request.headers.forEach(function (value, name) {
- headers[name] = value;
- });
- client(url, {body: body, method: method, headers: headers}).then(handler = function (resp) {
- var response = request.respondWith(resp.body, {
- status: resp.statusCode,
- statusText: trim(resp.statusMessage)
- });
- each(resp.headers, function (value, name) {
- response.headers.set(name, value);
- });
- resolve(response);
- }, function (error$$1) { return handler(error$$1.response); });
- });
- }
- /**
- * Base client.
- */
- function Client (context) {
- var reqHandlers = [sendRequest], resHandlers = [];
- if (!isObject(context)) {
- context = null;
- }
- function Client(request) {
- while (reqHandlers.length) {
- var handler = reqHandlers.pop();
- if (isFunction(handler)) {
- var response = (void 0), next = (void 0);
- response = handler.call(context, request, function (val) { return next = val; }) || next;
- if (isObject(response)) {
- return new PromiseObj(function (resolve, reject) {
- resHandlers.forEach(function (handler) {
- response = when(response, function (response) {
- return handler.call(context, response) || response;
- }, reject);
- });
- when(response, resolve, reject);
- }, context);
- }
- if (isFunction(response)) {
- resHandlers.unshift(response);
- }
- } else {
- warn(("Invalid interceptor of type " + (typeof handler) + ", must be a function"));
- }
- }
- }
- Client.use = function (handler) {
- reqHandlers.push(handler);
- };
- return Client;
- }
- function sendRequest(request) {
- var client = request.client || (inBrowser ? xhrClient : nodeClient);
- return client(request);
- }
- /**
- * HTTP Headers.
- */
- var Headers = function Headers(headers) {
- var this$1 = this;
- this.map = {};
- 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.get = function get (name) {
- var list = this.map[getName(this.map, name)];
- return list ? list.join() : null;
- };
- 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.append = function append (name, value) {
- var list = this.map[getName(this.map, name)];
- if (list) {
- list.push(trim(value));
- } else {
- this.set(name, value);
- }
- };
- Headers.prototype.delete = function delete$1 (name) {
- delete this.map[getName(this.map, name)];
- };
- Headers.prototype.deleteAll = function deleteAll () {
- this.map = {};
- };
- Headers.prototype.forEach = function forEach (callback, thisArg) {
- var this$1 = this;
- 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) {
- return toLower(name) === toLower(curr) ? curr : prev;
- }, null);
- }
- function normalizeName(name) {
- if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
- throw new TypeError('Invalid character in header field name');
- }
- return trim(name);
- }
- /**
- * HTTP 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;
- if (isString(body)) {
- this.bodyText = body;
- } else if (isBlob(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.json = function json () {
- return when(this.text(), function (text) { return JSON.parse(text); });
- };
- Object.defineProperty(Response.prototype, 'data', {
- get: function get() {
- return this.body;
- },
- set: function set(body) {
- this.body = body;
- }
- });
- function blobText(body) {
- return new PromiseObj(function (resolve) {
- var reader = new FileReader();
- reader.readAsText(body);
- reader.onload = function () {
- resolve(reader.result);
- };
- });
- }
- function isBlobText(body) {
- return body.type.indexOf('text') === 0 || body.type.indexOf('json') !== -1;
- }
- /**
- * HTTP Request.
- */
- var Request = function Request(options$$1) {
- this.body = null;
- this.params = {};
- assign(this, options$$1, {
- method: toUpper(options$$1.method || 'GET')
- });
- 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.respondWith = function respondWith (body, options$$1) {
- return new Response(body, assign(options$$1 || {}, {url: this.getUrl()}));
- };
- /**
- * Service for sending network requests.
- */
- var COMMON_HEADERS = {'Accept': 'application/json, text/plain, */*'};
- var JSON_CONTENT_TYPE = {'Content-Type': 'application/json;charset=utf-8'};
- function Http(options$$1) {
- var self = this || {}, client = Client(self.$vm);
- defaults(options$$1 || {}, self.$options, Http.options);
- Http.interceptors.forEach(function (handler) {
- if (isString(handler)) {
- handler = Http.interceptor[handler];
- }
- if (isFunction(handler)) {
- client.use(handler);
- }
- });
- return client(new Request(options$$1)).then(function (response) {
- return response.ok ? response : PromiseObj.reject(response);
- }, function (response) {
- if (response instanceof Error) {
- error(response);
- }
- return PromiseObj.reject(response);
- });
- }
- Http.options = {};
- Http.headers = {
- put: JSON_CONTENT_TYPE,
- post: JSON_CONTENT_TYPE,
- patch: JSON_CONTENT_TYPE,
- delete: JSON_CONTENT_TYPE,
- common: COMMON_HEADERS,
- custom: {}
- };
- Http.interceptor = {before: before, method: method, jsonp: jsonp, json: json, form: form, header: header, cors: cors};
- Http.interceptors = ['before', 'method', 'jsonp', 'json', 'form', 'header', 'cors'];
- ['get', 'delete', 'head', 'jsonp'].forEach(function (method$$1) {
- Http[method$$1] = function (url, options$$1) {
- return this(assign(options$$1 || {}, {url: url, method: method$$1}));
- };
- });
- ['post', 'put', 'patch'].forEach(function (method$$1) {
- Http[method$$1] = function (url, body, options$$1) {
- return this(assign(options$$1 || {}, {url: url, method: method$$1, body: body}));
- };
- });
- /**
- * Service for interacting with RESTful services.
- */
- function Resource(url, params, actions, options$$1) {
- var self = this || {}, resource = {};
- actions = assign({},
- Resource.actions,
- actions
- );
- each(actions, function (action, name) {
- action = merge({url: url, params: assign({}, params)}, options$$1, action);
- resource[name] = function () {
- return (self.$http || Http)(opts(action, arguments));
- };
- });
- return resource;
- }
- function opts(action, args) {
- var options$$1 = 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$$1.method)) {
- body = args[0];
- } else {
- params = args[0];
- }
- break;
- case 0:
- break;
- default:
- throw 'Expected up to 2 arguments [params, body], got ' + args.length + ' arguments';
- }
- options$$1.body = body;
- options$$1.params = assign({}, options$$1.params, params);
- return options$$1;
- }
- Resource.actions = {
- get: {method: 'GET'},
- save: {method: 'POST'},
- query: {method: 'GET'},
- update: {method: 'PUT'},
- remove: {method: 'DELETE'},
- delete: {method: 'DELETE'}
- };
- /**
- * Install plugin.
- */
- function plugin(Vue) {
- if (plugin.installed) {
- return;
- }
- Util(Vue);
- Vue.url = Url;
- Vue.http = Http;
- Vue.resource = Resource;
- Vue.Promise = PromiseObj;
- Object.defineProperties(Vue.prototype, {
- $url: {
- get: function get() {
- return options(Vue.url, this, this.$options.url);
- }
- },
- $http: {
- get: function get() {
- return options(Vue.http, this, this.$options.http);
- }
- },
- $resource: {
- get: function get() {
- return Vue.resource.bind(this);
- }
- },
- $promise: {
- get: function get() {
- var this$1 = this;
- return function (executor) { return new Vue.Promise(executor, this$1); };
- }
- }
- });
- }
- if (typeof window !== 'undefined' && window.Vue) {
- window.Vue.use(plugin);
- }
- return plugin;
- })));
|