UNPKG

@ace-fetch/vue

Version:

vue adapter for @ace-fetch/core.

52 lines (51 loc) 2.23 kB
import { FetchInjectKey, setActiveFetch } from './rootFetch'; export function FetchVuePlugin(Vue) { // Used to avoid multiple mixins being setup // when in dev mode and hot module reload // https://github.com/vuejs/vue/issues/5089#issuecomment-284260111 if (Vue.$__api_fetch_installed__) return; // eslint-disable-next-line @typescript-eslint/camelcase Vue.$__api_fetch_installed__ = true; // Equivalent of // app.config.globalProperties.$fetch = fetch Vue.mixin({ beforeCreate: function () { var options = this.$options; if (options.apiFetch || options.afetch) { var fetch_1 = options.apiFetch || options.afetch; // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/main/src/apis/inject.ts#L31 if (!this._provided) { var provideCache_1 = {}; Object.defineProperty(this, '_provided', { get: function () { return provideCache_1; }, set: function (v) { return Object.assign(provideCache_1, v); }, }); } this._provided[FetchInjectKey] = fetch_1; // propagate the fetch instance in an SSR friendly way // avoid adding it to nuxt twice // @Deprecated if (!this.$afetch) { this.$afetch = fetch_1; } if (!this.$apiFetch) { this.$apiFetch = fetch_1; } // this allows calling useFetch() outside of a component setup after setActiveFetch(fetch_1); fetch_1._a = this; } else { // @Deprecated this.$afetch = (options.parent && options.parent.$afetch) || this; this.$apiFetch = (options.parent && options.parent.$apiFetch) || this; } }, }); } // Auto install if it is not done yet and `window` has `Vue` in Vue2. // To allow users to avoid auto-installation in some cases, if (typeof window !== 'undefined' && window.Vue) { window.Vue.use(FetchVuePlugin); }