@ace-fetch/vue
Version:
vue adapter for @ace-fetch/core.
84 lines (83 loc) • 3.27 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { getCurrentInstance, inject } from 'vue-demi';
import { registApi, debug } from '@ace-fetch/core';
import { FetchInjectKey, setActiveFetch, activeFetch } from './rootFetch';
export function defineRegistApi(idOrOptions, registOptions) {
var id;
var options;
if (typeof idOrOptions === 'string' || idOrOptions instanceof Symbol) {
id = idOrOptions;
options = registOptions;
}
else {
var _id = idOrOptions.id, restOptions = __rest(idOrOptions, ["id"]);
id = _id;
options = restOptions;
}
var optionsForPlugin = __assign({}, options);
function useRegistApi(fetch) {
var _b;
var currentInstance = getCurrentInstance();
fetch = fetch || (currentInstance && inject(FetchInjectKey)) || undefined;
if (fetch)
setActiveFetch(fetch);
if (debug && !activeFetch) {
throw new Error("getActiveFetch was called with no active Fetch. Did you forget to install fetch?\n" +
"\tconst fetch = createFetch()\n" +
"\tapp.use(fetch)\n" +
"This will fail in production.");
}
fetch = activeFetch;
if (!fetch._r.has(id)) {
// creating regist apis register it to 'fetch._r'
// FIXME: Build type error
var registApis_1 = registApi(fetch.client, options.definition || options.apis, options.prefix, id);
// apply all local plugins
(_b = options.plugins) === null || _b === void 0 ? void 0 : _b.forEach(function (extender) {
Object.assign(registApis_1, extender({
id: id,
registApis: registApis_1,
fetch: fetch,
app: fetch._a,
options: optionsForPlugin,
}));
});
// apply all global plugins
fetch._p.forEach(function (extender) {
Object.assign(registApis_1, extender({
id: id,
registApis: registApis_1,
fetch: fetch,
app: fetch._a,
options: optionsForPlugin,
}));
});
fetch._r.set(id, registApis_1);
}
// get from store
var apis = fetch._r.get(id);
return apis;
}
return useRegistApi;
}