rest-chronicle
Version:
autodocumentate rest api
110 lines (108 loc) • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.decorate = decorate;
var _myrmidon = require("myrmidon");
var _constants = require("../constants");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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);
}