UNPKG

@ace-fetch/vue

Version:

vue adapter for @ace-fetch/core.

40 lines (39 loc) 1.3 kB
import { isVue2, markRaw } from 'vue-demi'; import { FetchInjectKey, setActiveFetch } from './rootFetch'; export function createFetch(client) { var _p = []; // plugins added before calling app.use(pinia) var toBeInstalled = []; var fetch = markRaw({ install: function (app) { // this allows calling useFetch() outside of a component setup after setActiveFetch(fetch); if (!isVue2) { fetch._a = app; app.provide(FetchInjectKey, fetch); app.config.globalProperties.$afetch = fetch; app.config.globalProperties.$apiFetch = fetch; toBeInstalled.forEach(function (plugin) { return _p.push(plugin); }); toBeInstalled = []; } }, use: function (plugin) { if (!this._a && !isVue2) { toBeInstalled.push(plugin); } else { _p.push(plugin); } return this; }, // it's actually undefined here // @ts-expect-error set in install when using Vue 3 _a: null, // store regist apis _r: new Map(), // regist api plugins _p: _p, client: client, }); return fetch; }