Using the Options Object

Avatar of Hemanta SundarayHemanta Sundaray

You can also pass parameters to a request directly when creating the request, using the Options object:

http.ts
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 it
Effect.runPromise(
fetchUsersPaginated({
limit: 5,
sortBy: "firstName",
}),
).then((data) => {
data.users.map((user: any) => {
console.log(` ${user.firstName} ${user.lastName}`);
});
});

Output:

Terminal
Aaliyah Hanson
Aaliyah Martinez
Aaron Cook
Abigail Rivera
Addison Wright

The 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:

PropertyTypeDescription
urlParamsUrlParams.InputQuery string parameters to append to the URL endpoint.
headersHeaders.InputCustom HTTP headers to include in the request.
bodyHttpBody.HttpBodyThe payload of the request (e.g., JSON, FormData, Stream).
acceptstringExplicitly sets the Accept header to a specific media type.
acceptJsonbooleanA convenient shorthand to automatically set the Accept header to application/json.
hashstringThe URL fragment identifier (the part of the URL after the #).
urlstring | URLThe endpoint URL.
methodHttpMethodThe 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 using HttpClientRequest.get() or HttpClientRequest.head(), the options object omits method, url, and body. You cannot pass a body to a GET request this way.

  • Options.NoUrl: When using HttpClientRequest.post(), HttpClientRequest.put(), HttpClientRequest.patch(), or HttpClientRequest.del(), the options object omits method and url (because they are already defined by the constructor and the first argument), but it does allow body.

These same option variants apply to the client’s shorthand methods as well:

const client = yield* HttpClient.HttpClient;
// Options passed directly to client.get
const 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.

Sign in to save progress

Stay in the loop

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