export class Express extends Context.Tag("Express")<
Express,
ReturnType<typeof express>
>() {
private static request =
(method: HttpMethod) =>
<A, E, R>(
path: string,
body: (
req: Request,
res: Response,
tx: DatabaseTx,
) => Effect.Effect<A, HttpError | E, R>,
) =>
Effect.gen(function* () {
const app = yield* Express
const run = yield* FiberSet.makeRuntime<R>()
const databaseClient = yield* DatabaseClient
yield* Effect.logInfo(
`[Express] Registered ${method.toUpperCase()} ${path}`,
)
app[method](path, (req, res) =>
run(
Effect.tryPromise(() =>
databaseClient.transaction(async (tx) => run(body(req, res, tx))),
).pipe(
Effect.andThen((data) => res.json(data)),
HttpErrorCatcher(res),
UnhandledErrorCatcher(res),
Effect.catchAllDefect((e) => Console.log(e)),
),
),
)
})
export class Express extends Context.Tag("Express")<
Express,
ReturnType<typeof express>
>() {
private static request =
(method: HttpMethod) =>
<A, E, R>(
path: string,
body: (
req: Request,
res: Response,
tx: DatabaseTx,
) => Effect.Effect<A, HttpError | E, R>,
) =>
Effect.gen(function* () {
const app = yield* Express
const run = yield* FiberSet.makeRuntime<R>()
const databaseClient = yield* DatabaseClient
yield* Effect.logInfo(
`[Express] Registered ${method.toUpperCase()} ${path}`,
)
app[method](path, (req, res) =>
run(
Effect.tryPromise(() =>
databaseClient.transaction(async (tx) => run(body(req, res, tx))),
).pipe(
Effect.andThen((data) => res.json(data)),
HttpErrorCatcher(res),
UnhandledErrorCatcher(res),
Effect.catchAllDefect((e) => Console.log(e)),
),
),
)
})