From 5a328ecf0daa387d56c93583275bb235183a558a Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 5 May 2020 12:51:09 -0700 Subject: [PATCH] fix(context): allow binding keys to be used with composeInterceptors() --- .../__tests__/unit/interceptor-chain.unit.ts | 22 +++++++++++++++++++ packages/context/src/interceptor-chain.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/context/src/__tests__/unit/interceptor-chain.unit.ts b/packages/context/src/__tests__/unit/interceptor-chain.unit.ts index 953c0bfc503c..762935038171 100644 --- a/packages/context/src/__tests__/unit/interceptor-chain.unit.ts +++ b/packages/context/src/__tests__/unit/interceptor-chain.unit.ts @@ -13,6 +13,7 @@ import { GenericInterceptorChain, Next, } from '../..'; +import {Interceptor} from '../../interceptor'; describe('GenericInterceptorChain', () => { let ctx: Context; @@ -206,6 +207,27 @@ describe('GenericInterceptorChain', () => { expect(result).to.eql('ABC'); }); + it('composes multiple interceptors or keys as a single interceptor', async () => { + const binding = ctx + .bind('interceptors.abc') + .to(async (context, next) => { + await next(); + return 'ABC'; + }); + const childCtx = new Context(ctx); + const interceptor = composeInterceptors( + givenNamedInterceptor('interceptor1'), + binding.key, + ); + let invoked = false; + const result = await interceptor(childCtx, () => { + invoked = true; + return invoked; + }); + expect(invoked).to.be.true(); + expect(result).to.eql('ABC'); + }); + function givenContext() { events = []; ctx = new Context(); diff --git a/packages/context/src/interceptor-chain.ts b/packages/context/src/interceptor-chain.ts index 91ebe6d323f6..4d5887b8d48a 100644 --- a/packages/context/src/interceptor-chain.ts +++ b/packages/context/src/interceptor-chain.ts @@ -228,7 +228,7 @@ export function invokeInterceptors< * @param interceptors - A list of interceptor functions or binding keys */ export function composeInterceptors( - ...interceptors: GenericInterceptor[] + ...interceptors: GenericInterceptorOrKey[] ): GenericInterceptor { return (ctx, next) => { const interceptor = new GenericInterceptorChain(