Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/context/src/__tests__/unit/interceptor-chain.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GenericInterceptorChain,
Next,
} from '../..';
import {Interceptor} from '../../interceptor';

describe('GenericInterceptorChain', () => {
let ctx: Context;
Expand Down Expand Up @@ -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<Interceptor>('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();
Expand Down
2 changes: 1 addition & 1 deletion packages/context/src/interceptor-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function invokeInterceptors<
* @param interceptors - A list of interceptor functions or binding keys
*/
export function composeInterceptors<C extends Context = Context>(
...interceptors: GenericInterceptor<C>[]
...interceptors: GenericInterceptorOrKey<C>[]
): GenericInterceptor<C> {
return (ctx, next) => {
const interceptor = new GenericInterceptorChain(
Expand Down