Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-trees-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-effect-app": patch
---

Fix TypeScript compilation errors in monorepo template
3 changes: 2 additions & 1 deletion templates/monorepo/packages/cli/src/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Args, Command, Options } from "@effect/cli"
import { TodoId } from "@template/domain/TodosApi"
import { TodosClient } from "./TodosClient.js"

const todoArg = Args.text({ name: "todo" }).pipe(
Args.withDescription("The message associated with a todo")
)

const todoId = Options.integer("id").pipe(
const todoId = Options.withSchema(Options.integer("id"), TodoId).pipe(
Options.withDescription("The identifier of the todo")
)

Expand Down
5 changes: 3 additions & 2 deletions templates/monorepo/packages/cli/src/TodosClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HttpApiClient } from "@effect/platform"
import type { TodoId } from "@template/domain/TodosApi"
import { TodosApi } from "@template/domain/TodosApi"
import { Effect } from "effect"

Expand All @@ -19,14 +20,14 @@ export class TodosClient extends Effect.Service<TodosClient>()("cli/TodosClient"
Effect.flatMap((todos) => Effect.logInfo(todos))
)

function complete(id: number) {
function complete(id: TodoId) {
return client.todos.completeTodo({ path: { id } }).pipe(
Effect.flatMap((todo) => Effect.logInfo("Marked todo completed: ", todo)),
Effect.catchTag("TodoNotFound", () => Effect.logError(`Failed to find todo with id: ${id}`))
)
}

function remove(id: number) {
function remove(id: TodoId) {
return client.todos.removeTodo({ path: { id } }).pipe(
Effect.flatMap(() => Effect.logInfo(`Deleted todo with id: ${id}`)),
Effect.catchTag("TodoNotFound", () => Effect.logError(`Failed to find todo with id: ${id}`))
Expand Down
6 changes: 3 additions & 3 deletions templates/monorepo/packages/domain/src/TodosApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class TodosApiGroup extends HttpApiGroup.make("todos")
HttpApiEndpoint.get("getTodoById", "/todos/:id")
.addSuccess(Todo)
.addError(TodoNotFound, { status: 404 })
.setPath(Schema.Struct({ id: Schema.NumberFromString }))
.setPath(Schema.Struct({ id: TodoIdFromString }))
)
.add(
HttpApiEndpoint.post("createTodo", "/todos")
Expand All @@ -35,13 +35,13 @@ export class TodosApiGroup extends HttpApiGroup.make("todos")
HttpApiEndpoint.patch("completeTodo", "/todos/:id")
.addSuccess(Todo)
.addError(TodoNotFound, { status: 404 })
.setPath(Schema.Struct({ id: Schema.NumberFromString }))
.setPath(Schema.Struct({ id: TodoIdFromString }))
)
.add(
HttpApiEndpoint.del("removeTodo", "/todos/:id")
.addSuccess(Schema.Void)
.addError(TodoNotFound, { status: 404 })
.setPath(Schema.Struct({ id: Schema.NumberFromString }))
.setPath(Schema.Struct({ id: TodoIdFromString }))
)
{}

Expand Down
8 changes: 4 additions & 4 deletions templates/monorepo/packages/server/src/TodosRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class TodosRepository extends Effect.Service<TodosRepository>()("api/Todo
Effect.map((todos) => Array.from(HashMap.values(todos)))
)

function getById(id: number): Effect.Effect<Todo, TodoNotFound> {
function getById(id: TodoId): Effect.Effect<Todo, TodoNotFound> {
return Ref.get(todos).pipe(
Effect.flatMap(HashMap.get(id)),
Effect.catchTag("NoSuchElementException", () => new TodoNotFound({ id }))
Expand All @@ -18,20 +18,20 @@ export class TodosRepository extends Effect.Service<TodosRepository>()("api/Todo

function create(text: string): Effect.Effect<Todo> {
return Ref.modify(todos, (map) => {
const id = TodoId.make(HashMap.reduce(map, 0, (max, todo) => todo.id > max ? todo.id : max))
const id = TodoId.make(HashMap.reduce(map, -1, (max, todo) => todo.id > max ? todo.id : max) + 1)
const todo = new Todo({ id, text, done: false })
return [todo, HashMap.set(map, id, todo)]
})
}

function complete(id: number): Effect.Effect<Todo, TodoNotFound> {
function complete(id: TodoId): Effect.Effect<Todo, TodoNotFound> {
return getById(id).pipe(
Effect.map((todo) => new Todo({ ...todo, done: true })),
Effect.tap((todo) => Ref.update(todos, HashMap.set(todo.id, todo)))
)
}

function remove(id: number): Effect.Effect<void, TodoNotFound> {
function remove(id: TodoId): Effect.Effect<void, TodoNotFound> {
return getById(id).pipe(
Effect.flatMap((todo) => Ref.update(todos, HashMap.remove(todo.id)))
)
Expand Down
Loading