surrogate
Version:
Object method hooks made easy
124 lines • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextPreAndPost = exports.NextPost = exports.NextPre = exports.NextAsyncPreAndPost = exports.NextAsyncPost = exports.NextAsyncPre = exports.NextFor = void 0;
const manageDecorator_1 = require("./manageDecorator");
const constants_1 = require("../constants");
const which_1 = require("../which");
const asarray_1 = require("@jfrazx/asarray");
/**
* @description Designate decorated methods as next handler. Handler type must be assigned
*
* @export
* @decorator
* @template T
* @param {(NextForOptions<T> | NextForOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextFor = (nextOptions) => (target, event) => (0, asarray_1.asArray)(nextOptions).forEach((nextOption) => {
const { type, action, options = {} } = nextOption;
const which = (0, manageDecorator_1.determineWhich)(type);
const actions = (0, asarray_1.asArray)(action);
which.forEach((type) => actions.forEach((action) => (0, manageDecorator_1.manageDecorator)(type, {
handler: event,
options,
})(target, action)));
});
exports.NextFor = NextFor;
/**
* Designate decorated methods as async pre hooks.
*
* @export
* @decorator
* @template T
* @param {(NextDecoratorOptions<T> | NextDecoratorOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextAsyncPre = (nextOptions) => {
return nextAsyncHelper(nextOptions, which_1.PRE);
};
exports.NextAsyncPre = NextAsyncPre;
/**
* Designate decorated methods as async post hooks.
*
* @export
* @decorator
* @template T
* @param {(NextDecoratorOptions<T> | NextDecoratorOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextAsyncPost = (nextOptions) => {
return nextAsyncHelper(nextOptions, which_1.POST);
};
exports.NextAsyncPost = NextAsyncPost;
/**
* Designate decorated methods as async pre and post hooks.
*
* @export
* @decorator
* @template T
* @param {(NextDecoratorOptions<T> | NextDecoratorOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextAsyncPreAndPost = (nextOptions) => {
const which = [which_1.PRE, which_1.POST];
return (target, event, descriptor) => {
which.forEach((type) => nextAsyncHelper(nextOptions, type)(target, event, descriptor));
};
};
exports.NextAsyncPreAndPost = NextAsyncPreAndPost;
const nextAsyncHelper = (nextOptions, type) => {
return (target, event, descriptor) => (0, asarray_1.asArray)(nextOptions).forEach(({ action, options }) => nextHelper({
action,
options: Object.assign(Object.assign({}, options), { wrapper: constants_1.MethodWrapper.Async }),
}, type)(target, event, descriptor));
};
/**
* Designate decorated methods as pre hooks.
*
* @export
* @decorator
* @template T
* @param {(NextDecoratorOptions<T> | NextDecoratorOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextPre = (nextOptions) => {
return nextHelper(nextOptions, which_1.PRE);
};
exports.NextPre = NextPre;
/**
* Designate decorated methods as post hook
*
* @export
* @decorator
* @template T
* @param {(NextDecoratorOptions<T> | NextDecoratorOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextPost = (nextOptions) => {
return nextHelper(nextOptions, which_1.POST);
};
exports.NextPost = NextPost;
/**
* Designate decorated methods as pre and post hooks.
*
* @export
* @decorator
* @template T
* @param {(NextDecoratorOptions<T> | NextDecoratorOptions<T>[])} nextOptions
* @returns {PropertyDecorator<T>}
*/
const NextPreAndPost = (nextOptions) => {
const which = [which_1.PRE, which_1.POST];
return (target, event, descriptor) => {
which.forEach((type) => nextHelper(nextOptions, type)(target, event, descriptor));
};
};
exports.NextPreAndPost = NextPreAndPost;
const nextHelper = (hookOptions, type) => {
return (target, event, descriptor) => (0, asarray_1.asArray)(hookOptions).forEach(({ action, options = {} }) => (0, exports.NextFor)({
action,
type,
options,
})(target, event, descriptor));
};
//# sourceMappingURL=nextDecorators.js.map