Effect CommunityEC
Effect Community10mo ago
50 replies
Afonso Matos

Routing Requests Based on API Key Environment in Effect Typescript

I want to have my API work like Stripe, in that the key passed as the header defines whether the environment is test or production.

How can I route it to V1Test or V1Live depending on a header?

const V1Test = HttpApiBuilder.group(MyApi, "v1", (handlers) =>
    handlers.handle("generate", (a) => Effect.succeed("this is a test!")).handle("health", () => Effect.succeed("OK"))
);

const V1Live = HttpApiBuilder.group(MyApi, "v1", (handlers) =>
    handlers
        .handle("generate", (a) => Effect.succeed("production response"))
        .handle("health", () => Effect.succeed("OK"))
);

const MyApiLive = HttpApiBuilder.api(MyApi).pipe(Layer.provide(V1Live));

const ServerLive = HttpApiBuilder.serve().pipe(
    Layer.provide(MyApiLive),
    Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 }))
);

Layer.launch(ServerLive).pipe(NodeRuntime.runMain);
Was this page helpful?