Integrating Request-Dependent Services in Express Route Handlers

Hi, I'm playing around with the example code given in the Express integration docs. What I want to do is introduce a service that depends on a request's headers or other data that will be available to all route handlers. This is what I thought would work but the route layer still expects RequestContext as a requirement:

const get = <A, E, R>(
  path: string,
  body: (req: Request, res: Response) => Effect.Effect<A, E, R | RequestContext>,
) =>
  Effect.gen(function* () {
    const app = yield* Express;
    const run = yield* FiberSet.makeRuntime<R>();
    app.get(path, (req, res) =>
      run(
        Effect.provideService(body(req, res), RequestContext, {
          tenantId: Option.fromNullable(req.header("X-Tenant-Id")),
        }),
      ),
    );
  });

const IndexRouteLive = Layer.scopedDiscard(
  get("/", (_, res) =>
    Effect.gen(function* () {
      const context = yield* RequestContext;
      res.json({
        tenantId: context.tenantId,
      });
    }),
  ),
);
Was this page helpful?