UNPKG

rest-chronicle

Version:
110 lines (108 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decorate = decorate; var _myrmidon = require("myrmidon"); var _constants = require("../constants"); function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class FunctionDecorator extends _myrmidon.FunctionDecorator { onParams(opts) { const { methodName } = opts; const { methods } = this.config; const injectMethodNames = (0, _myrmidon.getMethodNames)(methods); const onParamsMethod = injectMethodNames.find(m => m === `before_${methodName}`); if (onParamsMethod) return methods[onParamsMethod](opts); return super.onParams(opts); } onSuccess(opts) { const { methodName } = opts; const { methods } = this.config; const injectMethodNames = (0, _myrmidon.getMethodNames)(methods); const onSuccessMethod = injectMethodNames.find(m => m === `after_${methodName}`); if (onSuccessMethod) return methods[onSuccessMethod](opts); return super.onSuccess(opts); } } class ClassMethodDecorator extends _myrmidon.ClassMethodDecorator {} _defineProperty(ClassMethodDecorator, "FunctionDecorator", FunctionDecorator); class ClassDecorator extends _myrmidon.ClassDecorator { getClassMethodDecoratorConfig(params) { const { target } = params; return { ...super.getClassMethodDecoratorConfig(params), serviceName: target.constructor.name }; } getFunctionDecoratorConfig({ target }) { const { methods } = this.config; return { ...super.getFunctionDecoratorConfig({ target }), chronicle: methods._chronicle }; } get injectMethodNames() { const { methods } = this.config; return (0, _myrmidon.getMethodNames)(methods); } filterMethodName(name) { const injectMethodNames = this.injectMethodNames; const onParamsMethod = injectMethodNames.find(m => m === `before_${name}`); const onSuccessMethod = injectMethodNames.find(m => m === `after_${name}`); return onParamsMethod || onSuccessMethod; } decorateClass(target) { const { methods } = this.config; const injectMethodNames = (0, _myrmidon.getMethodNames)(methods); for (const methodName of injectMethodNames.filter(name => !name.includes('before_') && !name.includes('after_'))) { // eslint-disable-next-line no-param-reassign target[methodName] = methods[methodName]; } return super.decorateClass(target); } decorateFunction(target) { const { methods } = this.config; const injectMethodNames = (0, _myrmidon.getMethodNames)(methods); const decorated = super.decorateFunction(target); for (const methodName of injectMethodNames.filter(name => !name.includes('before_') && !name.includes('after_'))) { // eslint-disable-next-line no-param-reassign decorated[methodName] = methods[methodName]; } return decorated; } } _defineProperty(ClassDecorator, "ClassMethodDecorator", ClassMethodDecorator); function decorate(target, methods) { const decorator = new ClassDecorator({ config: { methods } }); // eslint-disable-next-line no-param-reassign target[_constants.isDecorated] = true; return decorator.decorate(target); }