FetchRequest
A type representing a typed fetch request, which inherits from the native Request.
On top of the properties available in native requests, FetchRequest instances have their URL automatically prefixed
with the base URL of their fetch instance. Default options are also
applied, if present in the fetch instance.
The path of the request is extracted from the URL, excluding the base URL, and is available in the path property.
type FetchRequest<Schema, Method, Path>;
Type arguments:
-
Schema:
HttpSchemaThe HTTP schema used for the fetch instance.
-
Method:
stringThe HTTP method of the request that caused the error. Must be one of the methods of the path defined in the schema.
-
Path:
stringThe path of the request that caused the error. Must be one of the paths defined in the schema.
-
ErrorOnly:
booleanIf
true, the response will only include the status codes that are considered errors (4XX or 5XX). -
Redirect:
RequestRedirectThe redirect mode for the request, which can be one of
'follow','error', or'manual'. Defaults to'follow'. Iffolloworerror, the response will not include status codes that are considered redirects (300, 301, 302, 303, 307, and 308).
import { HttpSchema } from '@zimic/http';
import { createFetch } from '@zimic/fetch';
interface User {
id: string;
username: string;
}
type Schema = HttpSchema<{
'/users': {
POST: {
request: {
headers: { 'content-type': 'application/json' };
body: { username: string };
};
response: {
201: { body: User };
};
};
};
}>;
const fetch = createFetch<Schema>({
baseURL: 'http://localhost:3000',
});
const request = new fetch.Request('/users', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ username: 'me' }),
});
console.log(request); // FetchRequest<Schema, 'POST', '/users'>
Related:
request.path
The path of the request, excluding the base URL.
Type: string
const request = new fetch.Request('/users', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ username: 'me' }),
});
console.log(request.path); // '/users'
request.method
The HTTP method of the request.
Type: HttpMethod
const request = new fetch.Request('/users', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ username: 'me' }),
});
console.log(request.method); // 'POST'