effect-app / libs

The core libraries to build apps with Effect
44 stars 6 forks source link

Exploring fluent api for controllers #110

Closed patroza closed 17 hours ago

patroza commented 3 days ago

Following Effect's HttpApi fluent api router definitions:

diff --git a/api/src/Blog.controllers.ts b/api/src/Blog.controllers.ts
index bd28aba7..9c84337f 100644
--- a/api/src/Blog.controllers.ts
+++ b/api/src/Blog.controllers.ts
@@ -21,30 +21,26 @@ export default matchFor(BlogRsc)({
     const events = yield* Events
     const operations = yield* Operations

-    const { CreatePost, FindPost, GetPosts, PublishPost } = matchFor(BlogRsc)
-    return {
-      FindPost: FindPost((req) =>
+    return matchFor(BlogRsc)
+      .FindPost((req) =>
         blogPostRepo
           .find(req.id)
           .pipe(Effect.andThen(Option.getOrNull))
-      ),
-
-      GetPosts: GetPosts(
+      )
+      .GetPosts(
         blogPostRepo
           .all
           .pipe(Effect.andThen((items) => ({ items })))
-      ),
-
-      CreatePost: CreatePost((req) =>
+      )
+      .CreatePost((req) =>
         userRepo
           .getCurrentUser
           .pipe(
             Effect.andThen((author) => (new BlogPost({ ...req, author }, true))),
             Effect.tap(blogPostRepo.save)
           )
-      ),
-
-      PublishPost: PublishPost((req) =>
+      )
+      .PublishPost((req) =>
         Effect.gen(function*() {
           const post = yield* blogPostRepo.get(req.id)

@@ -91,6 +87,5 @@ export default matchFor(BlogRsc)({
           return op.id
         })
       )
-    }
   })
 })
patroza commented 2 days ago

note to self: explore return { FindPost: (req) => ... }, but probably can't be typed well and full of red squigles

patroza commented 1 day ago

I got this for now for testing.. https://github.com/effect-app/sample/commit/ff9c052a3d1b14ed85dbbaea741292342a35bc61#diff-0ec2f497c222ff30b5742762de94bf06e85961bea87037ce8d249ede988f3b3e

patroza commented 17 hours ago

final, I think! https://github.com/effect-app/sample/commit/58eca6371b6b65b51a5231194c72a44d370aed8f#diff-81c079b52add04c75d61d04d7754fa195c023bf20a7a0ed6450e2b4ce4fbfca3

patroza commented 15 hours ago

I think it's just simpler to return an Object by now.. https://github.com/effect-app/sample/commit/f2481dba78c86f2a3ccbecb4ca2d128f36d39d92