Skip to content

Commit

Permalink
refactor: replace types with task based versions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKMarshall committed Sep 30, 2022
1 parent 2de0e14 commit 8d04366
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
4 changes: 2 additions & 2 deletions apps/domain/src/faction/create/createFaction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { pipe } from 'fp-ts/lib/function'
import { createEvents, validateFaction } from './implementation'
import * as TE from 'fp-ts/TaskEither'
import { CreateFactionT } from './types'
import { CreateFaction } from './types'

export const createFaction: CreateFactionT =
export const createFaction: CreateFaction =
({ checkFactionNameExists }) =>
(unvalidatedFaction) => {
return pipe(
Expand Down
13 changes: 3 additions & 10 deletions apps/domain/src/faction/create/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ValidatedFaction,
CreateFactionEvent,
CheckFactionNameExists,
CheckFactionNameExistsT,
} from './types'
import * as TE from 'fp-ts/TaskEither'
import * as T from 'fp-ts/Task'
Expand All @@ -23,12 +22,6 @@ type ValidateFaction = (
checkFactionNameExists: CheckFactionNameExists
) => (
unvalidatedFaction: UnvalidatedFaction
) => E.Either<FactionValidationError, ValidatedFaction>

type ValidateFactionT = (
checkFactionNameExists: CheckFactionNameExistsT
) => (
unvalidatedFaction: UnvalidatedFaction
) => TE.TaskEither<FactionValidationError, ValidatedFaction>

class FactionNameAlreadyExistsError extends Error {
Expand All @@ -49,7 +42,7 @@ const _tag = (name: FactionName.FactionName): UniqueFactionName =>
export type UniqueFactionName = Opaque<string, 'UniqueFactionName'>

const toUniqueFactionName =
(checkFactionNameExists: CheckFactionNameExistsT) =>
(checkFactionNameExists: CheckFactionNameExists) =>
(
name: FactionName.FactionName
): TE.TaskEither<FactionNameAlreadyExistsError, UniqueFactionName> => {
Expand All @@ -65,15 +58,15 @@ const toUniqueFactionName =
}

export const toValidFactionNameT = (
checkFactionNameExists: CheckFactionNameExistsT
checkFactionNameExists: CheckFactionNameExists
) =>
flow(
FactionName.parse('name'),
TE.fromEither,
TE.chainW(toUniqueFactionName(checkFactionNameExists))
)

export const validateFaction: ValidateFactionT =
export const validateFaction: ValidateFaction =
(checkFactionExists) => (unvalidatedFaction) => {
return pipe(unvalidatedFaction, ({ name }) =>
sequenceS(TE.ApplySeq)({
Expand Down
12 changes: 1 addition & 11 deletions apps/domain/src/faction/create/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import * as TE from 'fp-ts/TaskEither'
* External dependencies / DB Functions
*/

export type CheckFactionNameExists = (name: string) => boolean
export type CheckFactionNameExistsT = (name: string) => T.Task<boolean>
export type CheckFactionNameExists = (name: string) => T.Task<boolean>

/*
* Public API
Expand All @@ -36,18 +35,9 @@ export type CreateFactionError = FactionValidationError
export type CreateFactionDependencies = {
checkFactionNameExists: CheckFactionNameExists
}
export type CreateFactionDependenciesT = {
checkFactionNameExists: CheckFactionNameExistsT
}

export type CreateFaction = (
dependencies: CreateFactionDependencies
) => (
unvalidatedFaction: UnvalidatedFaction
) => E.Either<CreateFactionError, CreateFactionEvent[]>

export type CreateFactionT = (
dependencies: CreateFactionDependenciesT
) => (
unvalidatedFaction: UnvalidatedFaction
) => TE.TaskEither<CreateFactionError, CreateFactionEvent[]>

0 comments on commit 8d04366

Please sign in to comment.