Accept any request
Reusable helpers and components often need "any request that returns string[]" rather than one specific request.
RequestInstance is that constraint: pin down the fields you care about and leave the rest open. This guide builds such
a helper and explains when to use RequestModel instead.
- How to accept any request with
RequestInstance. - How to constrain only the response, payload or other parts you need.
- The difference between
RequestInstanceandRequestModel.
Accept any request
A generic parameter that extends RequestInstance accepts every request and keeps its exact type inside the function:
import type { RequestInstance } from "@hyper-fetch/core";
export function logRequest<R extends RequestInstance>(request: R) {
console.log(request.method, request.endpoint);
return request;
}
Constrain the shape
RequestInstance takes the same keys as createRequest. Omitted keys stay any, so any request matches them. Here
the helper accepts only requests whose response is string[]:
import type { RequestInstance } from "@hyper-fetch/core";
export async function autocomplete(
request: RequestInstance<{ response: string[] }>,
) {
const { data } = await request.send();
return data ?? []; // data: string[] | null
}
Use a generic parameter, <R extends RequestInstance<{ response: string[] }>>(request: R), when the helper returns the
request or passes it on and must keep its exact type. Inside such a generic, send() cannot tell which options are
required, so send from a function typed with the plain constraint as above.
const getSuggestions = client.createRequest<{ response: string[] }>()({
endpoint: "/suggestions",
});
const getUsername = client.createRequest<{ response: string }>()({
endpoint: "/username",
});
await autocomplete(getSuggestions); // ok
await autocomplete(getUsername); // response is string, not string[]
A request with required params, payload or query params needs them before send(). Pass the request with its values
set, such as getSuggestions.setQueryParams(...), or constrain those keys too.
Use this pattern for:
- Generic components such as a
<DataTable request={...} />or a select that loads its options. - Helpers that send, log or transform requests of one shape.
- Callbacks in
sdk.$configure({...}), mocks and plugins.
RequestInstance or RequestModel
Both types take the same keys but treat omitted ones in opposite ways:
RequestInstance<{...}> | RequestModel<{...}> | |
|---|---|---|
| Means | Any request that matches this partial shape | This one endpoint |
| Omitted keys | any, so every request satisfies them | Strict: no payload, no query params, Error as the error |
| Use for | Generic constraints in helpers and components | Leaves of an SDK schema |
In a schema, RequestInstance silently turns the omitted parts of an endpoint into any:
import type { RequestInstance, RequestModel } from "@hyper-fetch/core";
type ApiSchema = {
users: {
// payload and queryParams become any
$get: RequestInstance<{ response: User[]; endpoint: "/users" }>;
// no payload, no query params
$get: RequestModel<{ response: User[]; endpoint: "/users" }>;
};
};
You've learned how to accept requests in reusable code.
- You can accept any request with
R extends RequestInstance. - You can constrain only the parts you need, such as the response.
- You can use
RequestModelfor schema leaves andRequestInstancefor constraints.
Runnable example: Custom hooks.