0.0.27 • Published 2 years ago

@shareid/core v0.0.27

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@shareid/core

OVERVIEW

This package heavily use fp-ts and io-ts

API


There is 7 steps in order to handle an API call.

  1. Validate request
  2. Create command
  3. Recreate state of aggregate
  4. Perform aggregate action / generate event
  5. save event
  6. publish event
  7. format response
  8. 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.

  1. Check event is already processed
  2. Create command from event processed
  3. Recreate state of aggregate
  4. Perform aggregate action / generate new event
  5. save new event
  6. publish new event
  7. format response
  8. Keep track of event processed
  9. 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);
0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago