@decaf-ts/db-decorators
Version:
Agnostic database decorators and repository
99 lines (98 loc) • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prefixMethod = prefixMethod;
exports.suffixMethod = suffixMethod;
exports.wrapMethodWithContext = wrapMethodWithContext;
const Context_1 = require("./Context.cjs");
const errors_1 = require("./errors.cjs");
/**
* @summary Util method to change a method of an object prefixing it with another
* @param {any} obj The Base Object
* @param {Function} after The original method
* @param {Function} prefix The Prefix method. The output will be used as arguments in the original method
* @param {string} [afterName] When the after function anme cannot be extracted, pass it here
*
* @function prefixMethod
*
* @memberOf module:db-decorators.Repository
*/
function prefixMethod(obj, after, prefix, afterName) {
async function wrapper(...args) {
const results = await Promise.resolve(prefix.call(this, ...args));
return Promise.resolve(after.apply(this, results));
}
const wrapped = wrapper.bind(obj);
const name = afterName ? afterName : after.name;
Object.defineProperty(wrapped, "name", {
enumerable: true,
configurable: true,
writable: false,
value: name,
});
obj[name] = wrapped;
}
/**
* @summary Util method to change a method of an object suffixing it with another
* @param {any} obj The Base Object
* @param {Function} before The original method
* @param {Function} suffix The Prefix method. The output will be used as arguments in the original method
* @param {string} [beforeName] When the after function anme cannot be extracted, pass it here
*
* @function suffixMethod
*
* @memberOf module:db-decorators.Repository
*/
function suffixMethod(obj, before, suffix, beforeName) {
async function wrapper(...args) {
const results = await Promise.resolve(before.call(this, ...args));
return suffix.call(this, ...results);
}
const wrapped = wrapper.bind(obj);
const name = beforeName ? beforeName : before.name;
Object.defineProperty(wrapped, "name", {
enumerable: true,
configurable: true,
writable: false,
value: name,
});
obj[name] = wrapped;
}
/**
* @summary Util method to wrap a method of an object with additional logic
*
* @param {any} obj The Base Object
* @param {Function} before the method to be prefixed
* @param {Function} method the method to be wrapped
* @param {Function} after The method to be suffixed
* @param {string} [methodName] When the after function anme cannot be extracted, pass it here
*
* @function wrapMethodWithContext
*
* @memberOf module:db-decorators.Repository
*/
function wrapMethodWithContext(obj, before, method, after, methodName) {
async function wrapper(...args) {
let transformedArgs = before.call(obj, ...args);
if (transformedArgs instanceof Promise)
transformedArgs = await transformedArgs;
const context = transformedArgs[transformedArgs.length - 1];
if (!(context instanceof Context_1.Context))
throw new errors_1.InternalError("Missing a context");
let results = await method.call(obj, ...transformedArgs);
if (results instanceof Promise)
results = await results;
results = after.call(this, results, context);
if (results instanceof Promise)
results = await results;
return results;
}
const wrapped = wrapper.bind(obj);
const name = methodName ? methodName : method.name;
Object.defineProperty(wrapped, "name", {
enumerable: true,
configurable: true,
writable: false,
value: name,
});
obj[name] = wrapped;
}