Effect CommunityEC
Effect Community2y ago
19 replies
wayne

Proposal for `HttpRouter.concat` Overload to Accept `Effect` for Shared Service Access Across Routes

Would it be possible to have an overload of HttpRouter.concat that takes an
Effect
?
I'd like to be able to access a service once and use it across multiple routes, e.g.:

const ContactRouter = Effect.gen(function* () {
    const contacts = yield* Contacts
    return HttpServer.router.empty.pipe(
        HttpServer.router.get(
            '/contact',
            Effect.gen(function* () {
                return yield* HttpServer.response.json(yield* contacts.getAll)
            }).pipe(Effect.withSpan('GET /contact')),
        ),
         HttpServer.router.put(
            '/contact/:id',
            Effect.gen(function* () {
                const { id } = yield* HttpServer.router.schemaParams(IdParams)
                return yield* Effect.gen(function* () {
                    const contact = yield* HttpServer.request.schemaBodyJson(ContactInput)
                    yield* contacts.update(id, contact)
                    return yield* HttpServer.response.empty()
                }).pipe(Effect.withSpan(`PUT /contact/${id}`))
            }),
        ),
   )
})

const HttpServerLive = HttpServer.router.empty.pipe(
    HttpServer.router.concat(ContactRouter),
    HttpServer.server.serve(HttpServer.middleware.logger),
    HttpServer.server.withLogAddress,
    Layer.provide(ServerLive),
    Layer.provide(NodeContext.layer),
    Layer.provide(Contacts.layer),
)
Was this page helpful?