The Endpoint class defines an HTTP endpoint with its method, path, serializers, and parsers.
const endpoint = new Endpoint({
method: 'GET',
pathname: '/users/(:id)',
// ...options
})HTTP method for the endpoint:
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'GET- Cannot have a body schemaPOST,PUT,PATCH,DELETE- Can have a body schema
URL path with optional dynamic segments:
pathname: '/users' // Static path
pathname: '/users/:id' // Required param
pathname: '/users/(:id)' // Optional param
pathname: '/posts/:id/comments/:commentId' // Multiple paramsSerializer for path parameters. See Serialization.
Serializer for query string parameters. See Serialization.
Serializer for request body. See Serialization.
Parser for successful response body. See Response Parsing.
Parser for error response body. See Response Parsing.
The Endpoint constructor takes a definition and optional default options:
const endpoint = new Endpoint({
method: 'GET',
pathname: '/users',
// Definition: method, pathname, params, query, body, data, error
}, {
headers: {
'X-API-Version': '2',
},
timeout: 5000,
retry: {
attempts: 3,
delay: 1000,
when: ({ response }) => response?.status === 503,
},
})The second argument accepts:
headers: Default headers for all requeststimeout: Request timeout in millisecondsretry: Default retry policy
These can be overridden per-request.
See Retry Policy for retry configuration.
Most users should use http_client instead of calling these methods directly. The HTTP client handles URL generation, body serialization, and response parsing automatically.
Generates a full URL with params and query serialized:
const url = await endpoint.generate_url({
base_url: 'https://api.example.com',
params: { id: '123' },
query: { include: 'posts' },
})Returns URL on success or SerializationError on validation failure.
Serializes the request body:
const { body, content_type } = await endpoint.serialize_body({
body: { name: 'John' },
})Returns { body, content_type } on success or SerializationError on validation failure.
Parses an HTTP response:
const result = await endpoint.parse_response(response)Returns typed result based on status code. See Response Parsing.