UNPKG

@caikengren/uni-use-request

Version:
168 lines (163 loc) 6.73 kB
import { a as clearCache, i as store_default, n as USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY, r as getRequestTagBg, t as useRequest_default } from "./useRequest-CQFXizun.js"; import { provide, unref } from "vue"; import { setupDevtoolsPlugin } from "@vue/devtools-api"; //#region src/devtools/devtools.ts const pluginId = "vue-hooks-plus"; const pluginName = "Vue Hooks Plus 🍭"; const pluginLogo = "https://raw.githubusercontent.com/InhiblabCore/vue-hooks-plus/c3b984112610ef3fb21140a0beb27b4a228fe0b3/packages/hooks/docs/public/logo.svg"; let currentStateId; const controlMap = /* @__PURE__ */ new Map(); function setupDevtools(app) { setupDevtoolsPlugin({ id: pluginId, label: pluginName, packageName: "vue-hooks-plus", homepage: "https://inhiblabcore.github.io/docs/hooks", logo: pluginLogo, app, settings: { baseSort: { type: "choice", component: "button-group", label: "Sort Cache Entries", options: [{ label: "ASC", value: 1 }, { label: "DESC", value: -1 }], defaultValue: 1 } } }, (api) => { api.addTimelineLayer({ id: pluginId, label: pluginName, color: 16767308 }); api.addInspector({ id: pluginId, label: pluginName, icon: "api", treeFilterPlaceholder: "Search useRequest", actions: [{ icon: "delete", tooltip: "Clear useRequest root ", action: () => { store_default.reset(currentStateId); api.sendInspectorTree(pluginId); api.sendInspectorState(pluginId); } }] }); store_default.subscribe((event) => { store_default.update(event.key, { time: event.time, type: event.type }); api.sendInspectorTree(pluginId); api.sendInspectorState(pluginId); api.addTimelineEvent({ layerId: pluginId, event: { title: event.type, subtitle: `data: ${JSON.stringify(event.data)}`, time: api.now(), data: { ...event } } }); }); api.on.getInspectorTree((payload) => { if (payload.inspectorId === pluginId) { controlMap.clear(); const settings = api.getSettings(); const queries = store_default.getAll(); let sortedArray = []; if (settings.baseSort === 1) sortedArray = Array.from(queries.entries()).sort((a, b) => { var _b$1$time, _b$, _a$1$time, _a$; return ((_b$1$time = (_b$ = b[1]) === null || _b$ === void 0 ? void 0 : _b$.time) !== null && _b$1$time !== void 0 ? _b$1$time : 0) - ((_a$1$time = (_a$ = a[1]) === null || _a$ === void 0 ? void 0 : _a$.time) !== null && _a$1$time !== void 0 ? _a$1$time : 0); }); else sortedArray = Array.from(queries.entries()).sort((a, b) => { var _a$1$time2, _a$2, _b$1$time2, _b$2; return ((_a$1$time2 = (_a$2 = a[1]) === null || _a$2 === void 0 ? void 0 : _a$2.time) !== null && _a$1$time2 !== void 0 ? _a$1$time2 : 0) - ((_b$1$time2 = (_b$2 = b[1]) === null || _b$2 === void 0 ? void 0 : _b$2.time) !== null && _b$1$time2 !== void 0 ? _b$1$time2 : 0); }); const filtered = sortedArray.filter((item) => new RegExp(payload.filter, "g").test(item[0])).map((item) => { var _item$, _item$2, _item$3; return { id: item[0], label: item[0], tags: ((_item$ = item[1]) === null || _item$ === void 0 ? void 0 : _item$.type) ? [{ label: `${(_item$2 = item[1]) === null || _item$2 === void 0 ? void 0 : _item$2.type}`, textColor: 16777215, backgroundColor: getRequestTagBg((_item$3 = item[1]) === null || _item$3 === void 0 ? void 0 : _item$3.type) }] : [] }; }); payload.rootNodes = [{ id: "vue-hooks-plus-useRequest", label: "useRequest", tags: [{ label: "Root", textColor: 16777215, backgroundColor: 4372611 }], children: filtered !== null && filtered !== void 0 ? filtered : [] }]; } }); api.on.getInspectorState((payload) => { currentStateId = payload.nodeId; let pluginsIndex = 0; if (payload.inspectorId === pluginId) { const queries = store_default.getAll(); if (payload.nodeId) { var _currentSource$instan, _currentSource$instan2; const currentSource = queries.get(payload.nodeId); if (!currentSource) return; payload.state = { "Details": [{ key: "Key", value: payload.nodeId }, { key: "Request Name", value: currentSource.requestName }], "Data Explorer": Object.keys(currentSource.instance.state).map((item) => ({ key: item, value: unref(currentSource.instance.state[item]) })), "Option": Object.keys(currentSource.instance.options).map((item) => ({ key: item, value: currentSource.instance.options[item] })), "Plugins 🧩": (_currentSource$instan = (_currentSource$instan2 = currentSource.instance.pluginImpls) === null || _currentSource$instan2 === void 0 || (_currentSource$instan2 = _currentSource$instan2.map((_, index) => { var _currentSource$instan3, _currentSource$instan4, _currentSource$instan5; const pluginName$1 = currentSource === null || currentSource === void 0 || (_currentSource$instan3 = currentSource.instance) === null || _currentSource$instan3 === void 0 || (_currentSource$instan3 = _currentSource$instan3.pluginImpls) === null || _currentSource$instan3 === void 0 || (_currentSource$instan3 = _currentSource$instan3[index]) === null || _currentSource$instan3 === void 0 ? void 0 : _currentSource$instan3.name; if (!pluginName$1) { if (index !== pluginsIndex) pluginsIndex++; } return { key: pluginName$1 ? pluginName$1 : `plugin ${pluginsIndex}`, value: (_currentSource$instan4 = currentSource === null || currentSource === void 0 || (_currentSource$instan5 = currentSource.instance) === null || _currentSource$instan5 === void 0 || (_currentSource$instan5 = _currentSource$instan5.pluginImpls) === null || _currentSource$instan5 === void 0 ? void 0 : _currentSource$instan5[index]) !== null && _currentSource$instan4 !== void 0 ? _currentSource$instan4 : null }; })) === null || _currentSource$instan2 === void 0 ? void 0 : _currentSource$instan2.filter((item) => Object.keys(item.value).length !== 0)) !== null && _currentSource$instan !== void 0 ? _currentSource$instan : [] }; } } }); }); } //#endregion //#region src/devtools/index.ts var devtools_default = { install(app) { setupDevtools(app); } }; //#endregion //#region src/useRequestProvider.ts function useRequestProvider(config) { provide(USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY, config); } //#endregion //#region src/index.ts var src_default = useRequest_default; //#endregion export { clearCache as clearUseRequestCache, src_default as default, devtools_default as useRequestDevToolsPlugin, useRequestProvider };