UNPKG

@ace-fetch/vue

Version:

vue adapter for @ace-fetch/core.

88 lines (87 loc) 3.46 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.defineRegistApi = void 0; var vue_demi_1 = require("vue-demi"); var core_1 = require("@ace-fetch/core"); var rootFetch_1 = require("./rootFetch"); 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 = (0, vue_demi_1.getCurrentInstance)(); fetch = fetch || (currentInstance && (0, vue_demi_1.inject)(rootFetch_1.FetchInjectKey)) || undefined; if (fetch) (0, rootFetch_1.setActiveFetch)(fetch); if (core_1.debug && !rootFetch_1.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 = rootFetch_1.activeFetch; if (!fetch._r.has(id)) { // creating regist apis register it to 'fetch._r' // FIXME: Build type error var registApis_1 = (0, core_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; } exports.defineRegistApi = defineRegistApi;