All files / src/interceptor interceptor.ts

58.33% Statements 7/12
100% Branches 0/0
44.44% Functions 4/9
75% Lines 6/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39          1x                 1x           1x           1x       4x   12x            
/*
 * © Copyright 2022 HP Development Company, L.P.
 * SPDX-License-Identifier: MIT
 */
 
import { ClassReflection, DecoratorId, MethodReflection } from '@davinci/reflector';
import {
	Interceptor,
	InterceptorBag,
	InterceptorBagGenerics,
	InterceptorDecoratorMeta,
	InterceptorNext
} from './types';
 
export function getInterceptorsDecorators<I extends Interceptor = Interceptor, Meta = unknown>(
	reflection: MethodReflection | ClassReflection
): Array<InterceptorDecoratorMeta<I, Meta>> {
	return reflection.decorators.filter(decorator => decorator[DecoratorId] === 'interceptor');
}
 
export function getInterceptorsHandlers<I extends Interceptor = Interceptor>(
	reflection: MethodReflection | ClassReflection
): Array<I> {
	return reflection.decorators.filter(decorator => decorator[DecoratorId] === 'interceptor').map(d => d.handler);
}
 
export function executeInterceptorsStack<IBD extends InterceptorBagGenerics = InterceptorBagGenerics>(
	interceptors: Interceptor<IBD>[],
	interceptorBag: InterceptorBag<IBD>
): ReturnType<InterceptorNext<IBD>> {
	return interceptors.reverse().reduce(
		(wrapperFunction: InterceptorNext<IBD>, interceptor) => {
			return () => interceptor(wrapperFunction, interceptorBag);
		},
		// eslint-disable-next-line @typescript-eslint/no-empty-function
		() => {}
	)();
}