-
Notifications
You must be signed in to change notification settings - Fork 5
/
invoke.ts
31 lines (30 loc) · 805 Bytes
/
invoke.ts
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
import type {
CommandAdapterFactory,
Messages,
Snapshot,
State
} from "../types";
import { validate } from "../utils/validation";
import command from "./command";
/**
* Validates and and adapts messages from external systems into internal command invocations
*
* **Note:** Can be used as webhooks and anti-corruption layers
*
* @param factory the command adapter factory
* @param payload the message payload
* @returns last snapshot
*/
export default async function invoke<
P extends State,
S extends State,
C extends Messages,
E extends Messages
>(
factory: CommandAdapterFactory<P, C>,
payload: P
): Promise<Snapshot<S, E> | undefined> {
const adapter = factory();
const validated = validate(payload, adapter.schemas.message);
return command(adapter.on(validated));
}