/* * © 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( reflection: MethodReflection | ClassReflection ): Array> { return reflection.decorators.filter(decorator => decorator[DecoratorId] === 'interceptor'); } export function getInterceptorsHandlers( reflection: MethodReflection | ClassReflection ): Array { return reflection.decorators.filter(decorator => decorator[DecoratorId] === 'interceptor').map(d => d.handler); } export function executeInterceptorsStack( interceptors: Interceptor[], interceptorBag: InterceptorBag ): ReturnType> { return interceptors.reverse().reduce( (wrapperFunction: InterceptorNext, interceptor) => { return () => interceptor(wrapperFunction, interceptorBag); }, // eslint-disable-next-line @typescript-eslint/no-empty-function () => {} )(); }