You can also pass parameters to a request directly when creating the request, using the Options object:
import { FetchHttpClient, HttpClient, HttpClientRequest,} from "effect/unstable/http";import { Effect } from "effect";
function fetchUsersPaginated(options: { limit: number; sortBy: string }) { return Effect.gen(function* () { const client = yield* HttpClient.HttpClient;
// Pass urlParams directly in the options object const request = HttpClientRequest.get("https://dummyjson.com/users", { urlParams: { limit: options.limit.toString(), sortBy: options.sortBy, }, });
const response = yield* client.execute(request); const data = yield* response.json;
return data; }).pipe(Effect.provide(FetchHttpClient.layer));}
// Test itEffect.runPromise( fetchUsersPaginated({ limit: 5, sortBy: "firstName", }),).then((data) => { data.users.map((user: any) => { console.log(` ${user.firstName} ${user.lastName}`); });});Output:
Aaliyah HansonAaliyah MartinezAaron CookAbigail RiveraAddison WrightThe Options object isn’t limited to urlParams. You can set headers, accept types, and more, all in one place:
const request = HttpClientRequest.get("https://dummyjson.com/users", { urlParams: { limit: "5", sortBy: "firstName", }, headers: { "X-Request-Source": "effect-course", }, acceptJson: true,});This is equivalent to chaining setUrlParams, setHeaders, and acceptJson through .pipe(). The result is the same, use whichever style you prefer.
Here’s the full list of properties you can pass in the options object:
| Property | Type | Description |
|---|---|---|
urlParams | UrlParams.Input | Query string parameters to append to the URL endpoint. |
headers | Headers.Input | Custom HTTP headers to include in the request. |
body | HttpBody.HttpBody | The payload of the request (e.g., JSON, FormData, Stream). |
accept | string | Explicitly sets the Accept header to a specific media type. |
acceptJson | boolean | A convenient shorthand to automatically set the Accept header to application/json. |
hash | string | The URL fragment identifier (the part of the URL after the #). |
url | string | URL | The endpoint URL. |
method | HttpMethod | The specific HTTP method (e.g., "GET", "POST"). |
Note that while the base Options object contains all the properties above, Effect TS uses TypeScript utility types to restrict certain fields depending on how the request is constructed.
-
Options.NoBody: When usingHttpClientRequest.get()orHttpClientRequest.head(), the options object omitsmethod,url, andbody. You cannot pass a body to aGETrequest this way. -
Options.NoUrl: When usingHttpClientRequest.post(),HttpClientRequest.put(),HttpClientRequest.patch(), orHttpClientRequest.del(), the options object omitsmethodandurl(because they are already defined by the constructor and the first argument), but it does allowbody.
These same option variants apply to the client’s shorthand methods as well:
const client = yield* HttpClient.HttpClient;
// Options passed directly to client.getconst response = yield* client.get("https://dummyjson.com/users", { urlParams: { limit: "3" }, acceptJson: true, });The .pipe() pattern and the options object are interchangeable. The .pipe() approach is better when you’re building requests incrementally or conditionally. The options object is better when you know everything upfront and want it in one place.