strfry-policies/src/pipeline.ts

27 lines
712 B
TypeScript
Raw Normal View History

import { InputMessage, OutputMessage, Policy } from './types.ts';
2023-03-24 19:36:11 +00:00
2023-03-25 00:55:58 +00:00
type PolicyTuple<Opts = any> = [policy: Policy<Opts>, opts?: Opts];
type PolicyTuplesRest<T extends PolicyTuple[]> = {
[K in keyof T]: PolicyTuple<T[K]>
}
/** Processes messages through multiple policies, bailing early on rejection. */
2023-03-25 00:55:58 +00:00
async function pipeline<P extends any[]>(msg: InputMessage, policies: [...PolicyTuplesRest<P>]): Promise<OutputMessage> {
for (const tuple of policies) {
const [policy, opts] = tuple;
const result = await policy(msg, opts);
if (result.action !== 'accept') {
return result;
}
}
return {
id: msg.event.id,
action: 'accept',
msg: '',
};
}
export default pipeline;