Prepending Transforms

Avatar of Hemanta SundarayHemanta Sundaray

HttpClient.mapRequestInput() is similar to mapRequest, but it prepends the transformation instead of appending it. The difference matters when you stack multiple transforms.

Consider this scenario: you have a client with a base URL transform (appended via mapRequest), and you want to add another transform that modifies the URL path. The order of execution determines the final URL.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function demonstrateOrder() {
return Effect.gen(function* () {
// mapRequest APPENDS — runs after existing transforms
const client1 = (yield* HttpClient.HttpClient).pipe(
HttpClient.mapRequest(
HttpClientRequest.prependUrl("https://dummyjson.com"),
),
// This runs AFTER prependUrl, so it sees the full URL
HttpClient.mapRequest((request) => {
console.log("[mapRequest] URL is:", request.url);
return request;
}),
);
// mapRequestInput PREPENDS — runs before existing transforms
const client2 = (yield* HttpClient.HttpClient).pipe(
HttpClient.mapRequest(
HttpClientRequest.prependUrl("https://dummyjson.com"),
),
// This runs BEFORE prependUrl, so it sees only the path
HttpClient.mapRequestInput((request) => {
console.log("[mapRequestInput] URL is:", request.url);
return request;
}),
);
console.log("--- Using mapRequest ---");
yield* client1.get("/users/1").pipe(Effect.ignore);
console.log("\n--- Using mapRequestInput ---");
yield* client2.get("/users/1").pipe(Effect.ignore);
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it
Effect.runPromise(demonstrateOrder());

Output:

Terminal
--- Using mapRequest ---
[mapRequest] URL is: https://dummyjson.com/users/1
--- Using mapRequestInput ---
[mapRequestInput] URL is: /users/1

With mapRequest, the log sees the full URL because it runs after prependUrl. With mapRequestInput, the log sees only /users/1 because it runs before prependUrl.

Use mapRequestInput when you need a transform to run before all other registered transforms. There’s also a mapRequestInputEffect variant for effectful prepended transforms.

In most cases, mapRequest is what you want. Reach for mapRequestInput only when execution order matters.

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.