Skip to content

Commit

Permalink
feat: remove fluture/sanctuary with neverthrow
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Oct 29, 2024
1 parent 01dc187 commit 535ce9d
Show file tree
Hide file tree
Showing 19 changed files with 1,382 additions and 8,754 deletions.
9,950 changes: 1,309 additions & 8,641 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions packages/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@
"cross-env": "^7.0.2",
"discord.js": "^12.5.1",
"dotenv": "^9.0.2",
"fluture": "^13.0.1",
"fluture-sanctuary-types": "^7.0.0",
"fs-extra": "^10.0.0",
"joi": "^17.4.0",
"sanctuary": "^3.1.0"
"neverthrow": "^8.1.1"
},
"devDependencies": {
"@swc-node/jest": "^1.2.1",
Expand All @@ -54,7 +52,7 @@
"@types/hapi__joi": "^17.1.4",
"@types/node": "^14.0.27",
"@types/sanctuary": "^3.0.3",
"nodemon": "^2.0.3",
"nodemon": "^3.1.7",
"prettier": "^2.2.1",
"pretty-quick": "^1.10.0",
"typescript": "^5.6.3"
Expand Down
7 changes: 3 additions & 4 deletions packages/bot/src/entity/character.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type Result, err, ok } from 'neverthrow'
import hapi, { ObjectSchema } from 'joi'
import type { Either } from '../utils/sanctuary'
import S from 'sanctuary'

const { object, string } = hapi
export interface IAttribute {
Expand Down Expand Up @@ -75,9 +74,9 @@ export type CreateCharacterEntity = Pick<Vampire | Human, 'name' | 'splat'>

export const makeCreateCharacterEntity =
(schema: ObjectSchema) =>
(c: CreateCharacterEntity): Either<string, CreateCharacterEntity> => {
(c: CreateCharacterEntity): Result<CreateCharacterEntity, string> => {
const { error, value } = schema.validate(c)
return error ? S.Left(error.message) : S.Right(value)
return error ? err(error.message) : ok(value)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/bot/src/entity/chronicle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Either, S } from '../utils/sanctuary.js'
import Hapi, { ObjectSchema } from 'joi'
import { type Result, err, ok } from 'neverthrow'

// const { alternatives, object, string, number } = hapi;

Expand Down Expand Up @@ -32,9 +32,9 @@ export const Validation = Hapi.object({

export const makeCreateChronicleEntity =
(schema: ObjectSchema) =>
(c: CreateChronicleEntity): Either<string, CreateChronicleEntity> => {
(c: CreateChronicleEntity): Result<CreateChronicleEntity, string> => {
const { error, value } = schema.validate(c)
return error ? S.Left(error.message) : S.Right(value)
return error ? err(error.message) : ok(value)
}

export const createChronicleEntity = makeCreateChronicleEntity(Validation)
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ICommand } from '../types'
import { resolve } from 'fluture'
import { type ICommand } from '../types'
import { okAsync } from 'neverthrow'

export default {
name: 'create-character',
description: 'Creates a character',
title: 'Create Character',
execute: () => {
return resolve('foo')
return okAsync('foo')
},
} as ICommand
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { ChronicleGateway } from '../../../gateway/chronicle/types'
import type { ICommand } from '../types'
import { type ChronicleGateway } from '../../../gateway/chronicle/types'
import { type ICommand } from '../types'
import { chronicleMessage } from '../messages/chronicle.js'
import { createChronicle } from '../../../use-case/create-chronicle.js'
import { map } from 'fluture'

/**
* Handles creating a chronicle (game). This game is tied to the discord server id.
Expand All @@ -24,6 +23,6 @@ export default {
referenceType: 'discord',
game: args[1],
version: args[2],
}).pipe(map(chronicleMessage))
}).map(chronicleMessage)
},
} as ICommand
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ICommand } from '../types'
import { resolve } from 'fluture'
import { type ICommand } from '../types'
import { okAsync } from 'neverthrow'

/**
* This will make a player if it doesn't exist and create a character for that player, make sure on subsequent calls that if they already
Expand All @@ -10,6 +10,6 @@ export default {
description: 'Creates a player character',
title: 'Create Player',
execute: () => {
return resolve('')
return okAsync('')
},
} as ICommand
4 changes: 2 additions & 2 deletions packages/bot/src/framework/discord/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ICommand } from '../types'
import { commandConfigurations } from '../configurations.js'
import { resolve } from 'fluture'
import { okAsync } from 'neverthrow'

/**
* This command returns a list of all possible commands to the caller.
Expand All @@ -18,7 +18,7 @@ export default {
${c.description}`,
}))

return resolve({
return okAsync({
embed: {
color: 3447003,
title: 'Commands',
Expand Down
6 changes: 3 additions & 3 deletions packages/bot/src/framework/discord/commands/roll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ICommand } from '../types'
import { resolve } from 'fluture'
import { type ICommand } from '../types'
import { okAsync } from 'neverthrow'

/**
* This command tests the ability of using other bots to handle things like rolling dice.
Expand All @@ -9,6 +9,6 @@ export default {
description: 'Roll Thirst!',
title: 'Roll',
execute: () => {
return resolve('!v 5 2 5')
return okAsync('!v 5 2 5')
},
} as ICommand
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ICommand } from '../types'
import { resolve } from 'fluture'
import { type ICommand } from '../types'
import { okAsync } from 'neverthrow'

export default {
name: 'update-character',
Expand All @@ -12,6 +12,6 @@ export default {
// 1) They could look up the character by name, so behind the scenes it would be
// chronicle id, and name
// 2) Or the user displays a list
return resolve('foo')
return okAsync('foo')
},
} as ICommand
5 changes: 2 additions & 3 deletions packages/bot/src/framework/discord/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { CommandResult, Result } from './types'
import { type CommandResult, type Result } from './types'
import Discord from 'discord.js'
import { chronicleGateway } from '../../gateway/chronicle/rest/index.js'
import { commands } from './configurations.js'
import { fork } from 'fluture'
import { isString } from '../../utils/string.js'
import { rest } from '../../service/rest/node.js'

Expand All @@ -18,7 +17,7 @@ const sendResult = (message: Discord.Message) => (result: Result) => {

const handleResult = (message: Discord.Message) => (result: CommandResult) => {
const sendMessageResult = sendResult(message)
fork(sendMessageResult)(sendMessageResult)(result)
result.map(sendMessageResult).mapErr(sendMessageResult)
}

// Initialize Discord Bot
Expand Down
6 changes: 3 additions & 3 deletions packages/bot/src/framework/discord/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { FutureInstance } from 'fluture'
import type { Message } from 'discord.js'
import { type Message } from 'discord.js'
import { type ResultAsync } from 'neverthrow'

export type Result = string | string[] | { embed: object }

export type CommandResult = FutureInstance<Result, Result>
export type CommandResult = ResultAsync<Result, Result>

export interface ICommand {
aliases?: string[]
Expand Down
23 changes: 8 additions & 15 deletions packages/bot/src/gateway/chronicle/rest/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type {
Chronicle,
CreateChronicleEntity,
} from '../../../entity/chronicle'
import type { ChronicleGateway, CreateChronicle } from '../types'
import { Rest } from '../../../service/rest/types'
import { chain } from 'fluture'
import { eitherToFuture } from '../../../utils/sanctuary.js'
import { type ChronicleGateway, type CreateChronicle } from '../types'
import { type Chronicle } from '../../../entity/chronicle'
import { type Rest } from '../../../service/rest/types'

/**
* This represents the raw format of the chronicle when selected from the table directly
Expand All @@ -26,21 +21,19 @@ import { eitherToFuture } from '../../../utils/sanctuary.js'
* @param db
*/
export const createChronicle =
(db: Rest): CreateChronicle =>
(options: Rest): CreateChronicle =>
(c) =>
eitherToFuture(c).pipe(
chain<string, CreateChronicleEntity, Chronicle>(
db.post('http://services:5101/chronicles')
)
c.asyncAndThen<Chronicle, string>(
options.post('http://services:5101/chronicles')
)

/**
* Complete gateway for accessing chronicle data from a postgres database
* @param db
*/
export const chronicleGateway = (db: Rest): ChronicleGateway => {
export const chronicleGateway = (options: Rest): ChronicleGateway => {
return {
create: createChronicle(db),
create: createChronicle(options),
// @ts-expect-error - fix later, this type will change now
getChronicle: () => {},
}
Expand Down
16 changes: 9 additions & 7 deletions packages/bot/src/gateway/chronicle/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import type { Chronicle, CreateChronicleEntity } from '../../entity/chronicle'
import type { Either } from '../../utils/sanctuary'
import type { FutureInstance } from 'fluture'
import {
type Chronicle,
type CreateChronicleEntity,
} from '../../entity/chronicle.js'
import { type Result, type ResultAsync } from 'neverthrow'

/**
* Creates a chronicle in the database and returns the id of the newly created chronicle
*/
export type CreateChronicle = (
c: Either<string, CreateChronicleEntity>
) => FutureInstance<string, Chronicle>
c: Result<CreateChronicleEntity, string>
) => ResultAsync<Chronicle, string>

export type ChronicleExistsByReference = (
t: 'discord'
) => (id: string) => FutureInstance<string, boolean>
) => (id: string) => ResultAsync<boolean, string>

export type GetChronicle = (id: string) => FutureInstance<string, Chronicle>
export type GetChronicle = (id: string) => ResultAsync<Chronicle, string>

export interface ChronicleGateway {
create: CreateChronicle
Expand Down
43 changes: 19 additions & 24 deletions packages/bot/src/service/rest/node.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import type { Rest } from './types'
import { attemptP } from 'fluture'
import fetch from 'node-fetch'
import { type FetchPost, type Rest } from './types'
import { fromAsyncThrowable } from 'neverthrow'

export const get = (url: string) => () => {
return fetch(url).then((r) => {
export const get = (url: string) => () =>
fetch(url).then((r) => {
if (r.ok) {
return r.json()
}
})
}

export const post =
(url: string) =>
<R, T extends object>(b: T) => {
return attemptP<string, R>(() => {
return fetch(url, {
method: 'post',
body: JSON.stringify(b),
headers: { 'Content-Type': 'application/json' },
}).then(async (r) => {
if (r.ok) {
return r.json()
} else {
return Promise.reject(await r.text())
}
})
})
}
const post = (url: string) => (b: object) =>
fetch(url, {
method: 'post',
body: JSON.stringify(b),
headers: { 'Content-Type': 'application/json' },
}).then(async (r) => {
if (r.ok) {
return r.json()
} else {
return Promise.reject(await r.text())
}
})

export const rest: Rest = {
get,
post,
// TODO: This is probably a string or an error
post: (url: string): FetchPost =>
fromAsyncThrowable(post(url), (e: string) => e),
}
8 changes: 4 additions & 4 deletions packages/bot/src/service/rest/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { FutureInstance } from 'fluture'
import { type ResultAsync } from 'neverthrow'

export type Post = (
url: string
) => <R, T extends object>(b: T) => FutureInstance<string, R>
export type Post = (url: string) => FetchPost

export type FetchPost = <R, T extends object>(b: T) => ResultAsync<R, string>

export interface Rest {
get: <T>(url: string) => () => Promise<T>
Expand Down
2 changes: 1 addition & 1 deletion packages/bot/src/use-case/create-chronicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
CreateChronicleEntity,
createChronicleEntity,
} from '../entity/chronicle.js'
import type { ChronicleGateway } from '../gateway/chronicle/types.js'
import { type ChronicleGateway } from '../gateway/chronicle/types.js'

export const createChronicle =
(gateway: ChronicleGateway) =>
Expand Down
26 changes: 0 additions & 26 deletions packages/bot/src/utils/sanctuary.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
},
"dependencies": {
"@material-ui/core": "^4.11.4",
"bot": "^0.0.3",
"neverthrow": "^8.1.1",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
Expand All @@ -45,7 +47,6 @@
"@swc/core": "^1.2.57",
"@types/jest": "^26.0.23",
"@types/react-dom": "^17.0.5",
"@webpack-cli/generators": "^2.1.0",
"html-webpack-plugin": "^5.3.1",
"jest": "^29.7.0",
"swc-loader": "^0.1.14",
Expand Down

0 comments on commit 535ce9d

Please sign in to comment.