@shareid/core v0.0.27
@shareid/core
OVERVIEW
This package heavily use fp-ts
and io-ts
API
There is 7 steps in order to handle an API call.
- Validate request
- Create command
- Recreate state of aggregate
- Perform aggregate action / generate event
- save event
- publish event
- format response
- respond
const api_controller =
<A extends RootEnv<any>, R, T, V>(
requestValidator: ValidatorRequestFn<R>,
fromRequestToCommand: FromRequestToCommand<R, T>,
commandHandler: CommandHandler<T, V>,
)
Step 1/2
In order to facilate creation of ValidatorRequestFn you can use the function requestWithAuth or requestWithoutAuth
const requestWithAuth =
<V extends { authorization: AuthorizationBearer }, R>(
codec: Codec<V>,
claims: string[],
f: FormatRequestWithAuthFn<V, R>,
)
const requestWithoutAuth =
<V, R>(codec: Codec<V>, f: FormatRequestWithoutAuthFn<V, R>)
fromRequestToCommand function must be :
type FromRequestToCommand<R, T> = <N extends RootEnvWithLogger<unknown>>(req: R, env?: N) => Command<T>;
Step 3/4/5/6/7 form command handler
type CommandHandler<T, V> = <A extends RootEnvWithLogger<V>>(
env: A,
command: Command<T>,
) => TE.TaskEither<Failed, Succeeded>;
To facilate creation of command handler you can use the function commandHandler
When you use this helper step 3/5/6 are handled for you.
const commandHandler =
<V, A extends RootEnvWithLogger<V>, T, E>(
aggregateFn: AggregateFn<V, T, E>,
formatFn: FormatResponseFn<E>,
aggregateIdentifier?: string,
)
Worker
There is 7 steps in order to handle an API call.
- Check event is already processed
- Create command from event processed
- Recreate state of aggregate
- Perform aggregate action / generate new event
- save new event
- publish new event
- format response
- Keep track of event processed
- Ack or Nack message
const worker_controller =
<A extends RootEnv<any>, R, T, V>(
fromEventToCommand: FromEventToCommand<R, T>,
commandHandler: CommandHandler<T, V>,
)
Step 1
This step is handled by worker controller
Step 2
export type FromEventToCommand<R, T> = <N extends RootEnvWithLogger<unknown>>(req: Event<R>, env?: N) => Command<T>;
Step 3/4/5/6/7 form command handler
type CommandHandler<T, V> = <A extends RootEnvWithLogger<V>>(
env: A,
command: Command<T>,
) => TE.TaskEither<Failed, Succeeded>;
To facilate creation of command handler you can use the function commandHandler
When you use this helper step 3/5/6 are handled for you.
const commandHandler =
<V, A extends RootEnvWithLogger<V>, T, E>(
aggregateFn: AggregateFn<V, T, E>,
formatFn: FormatResponseFn<E>,
aggregateIdentifier?: string,
)
Step 8/9
Those steps are handled by worker controller
API REFERENCE
hydrate
Recreate an aggregate state
const hydrate = <E>(fn: AggregationFn<E>, events: E[]): Aggregate
validateState
Validate state of an aggregate
const validateState = <T>(codec: Codec<T>, data: unknown, errMsg: string): TE.TaskEither<Failed, T>
runWorker
Initiate worker at start of server
const runWorker = async (amqpConfig: AmqpConfiguration, handlers: Handlers);
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago