@mobileaction/ui-modules
Version:
Mobile Action common modules for Vue projects
59 lines (52 loc) • 1.58 kB
JavaScript
import MaLogger from '../libs/MaLogger.js';
import { injectPlugin } from '../PluginUtils.js';
const { validate } = require('schema-utils');
// schema for options object
const schema = {
type: 'object',
properties: {
name: {
type: 'string',
},
isDebug: {
anyOf: [
{ type: 'boolean' },
{ type: 'object' },
],
},
},
};
const defaultOptions = {
name: 'ma-logger',
isDebug: false,
};
/**
* Use this when not using debug logs or don't care about debug logs
* @param app {Vue}
* @param name {string} name of the logger, for components component path is used
* @param isDebug {boolean|function} returning if debug is enabled to control debug endpoint
*/
export function MaLoggerPlugin(app, { name = 'ma-logger', isDebug = false } = {}) {
// default values for options
const options = {
...defaultOptions,
name,
isDebug,
};
// validate options
validate(schema, options, {
name: 'MaLoggerPlugin',
baseDataPath: 'options',
});
if (MaLoggerPlugin.app === app) {
console.warn(`${ options.name }: already added`);
return false;
}
MaLoggerPlugin.app = app;
MaLoggerPlugin.options = options;
// replace boolean value with function
options.isDebug = typeof options.isDebug === 'boolean' ? () => options.isDebug : options.isDebug;
const loggerIns = new MaLogger(app, options);
injectPlugin(app, loggerIns, '$log');
}
export default MaLoggerPlugin;