diff --git a/.changeset/blue-trees-draw.md b/.changeset/blue-trees-draw.md new file mode 100644 index 0000000..8e2e8d9 --- /dev/null +++ b/.changeset/blue-trees-draw.md @@ -0,0 +1,5 @@ +--- +"create-effect-app": patch +--- + +Fix TypeScript compilation errors in monorepo template diff --git a/templates/monorepo/packages/cli/src/Cli.ts b/templates/monorepo/packages/cli/src/Cli.ts index 943529e..a5b8dcd 100644 --- a/templates/monorepo/packages/cli/src/Cli.ts +++ b/templates/monorepo/packages/cli/src/Cli.ts @@ -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") ) diff --git a/templates/monorepo/packages/cli/src/TodosClient.ts b/templates/monorepo/packages/cli/src/TodosClient.ts index f3f68be..f77f444 100644 --- a/templates/monorepo/packages/cli/src/TodosClient.ts +++ b/templates/monorepo/packages/cli/src/TodosClient.ts @@ -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" @@ -19,14 +20,14 @@ export class TodosClient extends Effect.Service()("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}`)) diff --git a/templates/monorepo/packages/domain/src/TodosApi.ts b/templates/monorepo/packages/domain/src/TodosApi.ts index c289a2d..cac5d0c 100644 --- a/templates/monorepo/packages/domain/src/TodosApi.ts +++ b/templates/monorepo/packages/domain/src/TodosApi.ts @@ -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") @@ -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 })) ) {} diff --git a/templates/monorepo/packages/server/src/TodosRepository.ts b/templates/monorepo/packages/server/src/TodosRepository.ts index 7dde18b..384adf0 100644 --- a/templates/monorepo/packages/server/src/TodosRepository.ts +++ b/templates/monorepo/packages/server/src/TodosRepository.ts @@ -9,7 +9,7 @@ export class TodosRepository extends Effect.Service()("api/Todo Effect.map((todos) => Array.from(HashMap.values(todos))) ) - function getById(id: number): Effect.Effect { + function getById(id: TodoId): Effect.Effect { return Ref.get(todos).pipe( Effect.flatMap(HashMap.get(id)), Effect.catchTag("NoSuchElementException", () => new TodoNotFound({ id })) @@ -18,20 +18,20 @@ export class TodosRepository extends Effect.Service()("api/Todo function create(text: string): Effect.Effect { 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 { + function complete(id: TodoId): Effect.Effect { 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 { + function remove(id: TodoId): Effect.Effect { return getById(id).pipe( Effect.flatMap((todo) => Ref.update(todos, HashMap.remove(todo.id))) )