UNPKG

@cmtlyt/cl-utils

Version:
69 lines (68 loc) 1.72 kB
import * as baseFunc from './module/baseFunc'; import { createInstance } from './module/fetchRequest'; import * as lodash from './module/lodash'; function forEachObj(obj, callback) { Object.keys(obj).forEach((key) => { callback(obj[key], key, obj); }); } function setKeyReadonly(obj, key, value) { Object.defineProperty(obj, key, { value, writable: false, enumerable: false, configurable: false, }); } /** * 混入baseFunc工具函数 * @param {object} options * @param {boolean} options.toRoot * @param {any} options.target * @returns {undefined|object} */ export function useBaseFunc({ toRoot, target }) { const mixin = function (obj, tar) { tar = tar.prototype || tar; forEachObj(obj, function (value, key) { setKeyReadonly(target, '$' + key, value); }); }; if (target) mixin(baseFunc, target); else if (toRoot) mixin(baseFunc, global); else { return { install(vm) { mixin(baseFunc, vm.prototype); }, }; } } /** * 混入fetch * @param {object} param0 * @param {IInitRequestConfig} param0.config * @param {boolean} param0.toRoot * @returns {undefined|object} */ export function useFetch({ config, toRoot, name } = { config: {}, name: '_custom' }) { if (toRoot) { setKeyReadonly(global, '$fetch', createInstance(config, name)); } else { return { install(vm) { setKeyReadonly(vm.prototype, '$fetch', createInstance(config, name)); }, }; } } /** * 返回lodash对象 * @returns {lodash} */ export function useLodash() { return lodash; }