Using Effect with Express for Sub-Route Handling
Hey, fist interaction with effect here, so bare with me if this is totally stupid.
I am trying/planning to use effect to handle a couple of sub routes in an express API.
So far, this is the approach (not registering any routes for a sub-route; plug effect API in
I am trying/planning to use effect to handle a couple of sub routes in an express API.
So far, this is the approach (not registering any routes for a sub-route; plug effect API in
route.use(async (req, res)) {}route.use(async (req, res)) {}) as below.import { json, Request, Response, Router, urlencoded } from "express";
import {
HttpApi,
HttpApiBuilder,
HttpApiEndpoint,
HttpApiGroup,
HttpServer
} from "@effect/platform";
import { Effect, Layer, Schema } from "effect";
const api = HttpApi.make("myApi").add(
HttpApiGroup.make("group").add(
HttpApiEndpoint.get("get", "/").addSuccess(Schema.String)
)
)
const groupLive = HttpApiBuilder.group(api, 'group', (handlers) =>
handlers.handle('get', () => Effect.succeed('Hello, world!'))
)
const MyApiLive = HttpApiBuilder.api(api).pipe(Layer.provide(groupLive))
// Convert the API to a web handler
const { dispose, handler } = HttpApiBuilder.toWebHandler(
Layer.mergeAll(MyApiLive, HttpServer.layerContext)
)
export const router = Router({
mergeParams: true
});
router.use(
urlencoded({
extended: true,
})
);
router.use(json());
// as we don't have any sub-routes, this is used to handle all requests
router.use(async function effectHandler(req: Request, res: Response) {
const url = `http://${req.headers.host}${req.url}`
const init: RequestInit = { method: req.method! }
const response = await handler(new Request(url, init))
const text = await response.text()
console.log("Text", text, text === '"Hello, world!"');
// / ^ ^
// ------------------------------- we should not have quotes
res
.status(response.status)
.send({ message: text })
});
export default routerimport { json, Request, Response, Router, urlencoded } from "express";
import {
HttpApi,
HttpApiBuilder,
HttpApiEndpoint,
HttpApiGroup,
HttpServer
} from "@effect/platform";
import { Effect, Layer, Schema } from "effect";
const api = HttpApi.make("myApi").add(
HttpApiGroup.make("group").add(
HttpApiEndpoint.get("get", "/").addSuccess(Schema.String)
)
)
const groupLive = HttpApiBuilder.group(api, 'group', (handlers) =>
handlers.handle('get', () => Effect.succeed('Hello, world!'))
)
const MyApiLive = HttpApiBuilder.api(api).pipe(Layer.provide(groupLive))
// Convert the API to a web handler
const { dispose, handler } = HttpApiBuilder.toWebHandler(
Layer.mergeAll(MyApiLive, HttpServer.layerContext)
)
export const router = Router({
mergeParams: true
});
router.use(
urlencoded({
extended: true,
})
);
router.use(json());
// as we don't have any sub-routes, this is used to handle all requests
router.use(async function effectHandler(req: Request, res: Response) {
const url = `http://${req.headers.host}${req.url}`
const init: RequestInit = { method: req.method! }
const response = await handler(new Request(url, init))
const text = await response.text()
console.log("Text", text, text === '"Hello, world!"');
// / ^ ^
// ------------------------------- we should not have quotes
res
.status(response.status)
.send({ message: text })
});
export default router