UNPKG

vue-rest-hooks

Version:

typescript restfull-api reactive @vue/composition-api

66 lines (65 loc) 2.69 kB
import { convertStore } from "../helper"; import { useValidator } from "../helper/useValidator"; var curVue = null; var DEFAULT_EXTRA_KEYS = ["route", "router"]; export var WrappedSetupPlugin = { install: function (Vue, options) { if (options === void 0) { options = {}; } if (curVue) { if (process.env.NODE_ENV !== "production") { // eslint-disable-next-line console.warn("Vue function api helper init duplicated !"); } } var pureVueProtoKeys = Object.keys(Vue.prototype); var pureVm = Object.keys(new Vue()); var extraKeys = (options.extraKeys || []).concat(DEFAULT_EXTRA_KEYS); function wrapperSetup() { var vm = this; var $options = vm.$options; var setup = $options.setup; if (!setup) { return; } if (typeof setup !== "function") { // eslint-disable-next-line console.warn('The "setup" option should be a function that returns a object in component definitions.', vm); return; } // wapper the setup option, so that we can use prototype properties and mixin properties in context $options.setup = function wrappedSetup(props, ctx) { // to extend context Object.keys(vm) .filter(function (x) { return /^\$/.test(x) && pureVm.indexOf(x) === -1; }) .forEach(function (x) { // @ts-ignore ctx[x.replace(/^\$/, "")] = vm[x]; }); Object.keys(vm.$root.constructor.prototype) .filter(function (x) { return /^\$/.test(x) && pureVueProtoKeys.indexOf(x) === -1; }) .forEach(function (x) { // @ts-ignore ctx[x.replace(/^\$/, "")] = vm[x]; }); // to extend context with router properties extraKeys.forEach(function (key) { // @ts-ignore var value = vm["$" + key]; if (value) { ctx[key] = value; } }); ctx.vuex = convertStore(ctx.store); ctx.utils = {}; ctx.utils.useValidator = function (params, fn) { return useValidator(params, fn, ctx.route.query); }; // @ts-ignore return setup(props, ctx); }; } Vue.mixin({ beforeCreate: wrapperSetup }); } };