Parameters
URL parameters are the dynamic segments of an endpoint path, such as the 1 in /users/1. In HyperFetch you write them
as :name in the endpoint, and TypeScript requires a value for each one before the request can be sent. For the
?key=value part of the URL, see query params.
- How to declare params in the endpoint.
- How to set them with
setParamsor pass them tosend(). - What happens when params are set twice.
- How param values are written into the URL.
Declare params in the endpoint
Prefix a path segment with :. HyperFetch reads the names from the endpoint string, so no generic is needed. Values are
string | number.
export const getProduct = client.createRequest<{ response: Product }>()({
endpoint: "/categories/:categoryId/products/:productId",
});
A param name starts with a letter or _. The colon of a port (http://localhost:3000/users/:userId) or of a GraphQL
variable like $id: ID! is not read as a param. Keep the endpoint a string literal: a widened string endpoint turns
the checks off.
Set params with setParams
setParams returns a new request with the values filled into the endpoint. Use it to prepare a request once and send it
from several places.
const request = getProduct.setParams({ categoryId: 12, productId: "Z24PBW443" });
console.log(request.endpoint); // "/categories/12/products/Z24PBW443"
await request.send();
TypeScript rejects a missing param, and send() needs params until every param is set:
getProduct.setParams({ categoryId: 12 }); // productId is missing
await getProduct.send(); // Expected 1 arguments, but got 0
Pass params to send()
Pass params to send() when the values come from user input or a route at call time. This is the usual choice for one
request sent with many values.
const { data } = await getProduct.send({
params: { categoryId: 12, productId: "Z24PBW443" },
});
Params set twice
Once setParams has filled the params, the returned request no longer accepts params in send(), so TypeScript
reports a second value. If you bypass the types, the value passed to send() wins for that call.
const userOne = getUser.setParams({ userId: 1 });
userOne.send({ params: { userId: 2 } }); // params are already set
To send the same request with another value, start from the original request: getUser.setParams({ userId: 2 }).
How values reach the URL
Each :name segment is replaced with String(value), as it is. Values are not URL-encoded, so encode those that may
contain /, ?, # or spaces:
const getFile = client.createRequest()({ endpoint: "/files/:path" });
await getFile.send({
params: { path: encodeURIComponent("docs/report 2024.pdf") },
});
// GET /files/docs%2Freport%202024.pdf
The filled endpoint is part of the request's cacheKey and queryKey, so GET /users/1 and GET /users/2 are cached
and queued separately.
When to use which
| Use | When |
|---|---|
send({ params }) | Values known at call time, the common case |
setParams(...) | A prepared request sent from several places, or passed to a hook |
You've learned how to work with URL parameters.
- You can declare params as
:namesegments and let TypeScript require them. - You can set them with
setParamsor pass them tosend(). - You can encode values that contain URL characters yourself.
Runnable examples: Basic data fetching and Dependent requests.