Skip to main content
Version: v10.7.1

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.

What you'll learn
  1. How to accept any request with RequestInstance.
  2. How to constrain only the response, payload or other parts you need.
  3. The difference between RequestInstance and RequestModel.

Accept any request​

A generic parameter that extends RequestInstance accepts every request and keeps its exact type inside the function:

src/helpers/log-request.ts
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[]:

src/helpers/autocomplete.ts
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.

src/app.ts
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<{...}>
MeansAny request that matches this partial shapeThis one endpoint
Omitted keysany, so every request satisfies themStrict: no payload, no query params, Error as the error
Use forGeneric constraints in helpers and componentsLeaves of an SDK schema

In a schema, RequestInstance silently turns the omitted parts of an endpoint into any:

src/api/schema.ts
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" }>;
};
};

Congratulations!

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 RequestModel for schema leaves and RequestInstance for constraints.

Runnable example: Custom hooks.

Request extractors
Read types back from a request.
SDK
Schemas built from RequestModel leaves.