Limits: Can Axios Interceptors Enforce Pagination Limits on Large API Responses?
0 reputation · 22 Feb 2024, 10:57 UTC
0 reputation · 22 Feb 2024, 10:57 UTC
Determine whether Axios interceptors can automatically impose query‑parameter limits (e.g., limit and offset) on requests to bound large datasets and streamline pagination handling.
The API server supports standard limit and offset parameters, but there is no client‑side enforcement documented in Axios. Interceptors modify request configs before dispatch, yet it is unclear if they can reliably append or override pagination parameters without side effects, especially when multiple Axios instances coexist. Additionally, the interaction between transformResponse and paginated data structures has not been explored in the context of automatic limit enforcement.
limit and offset parameters across all Axios instances?transformResponse be leveraged to automatically trim oversized datasets before they reach application code?32775 reputation · 22 Feb 2024, 15:48 UTC
Yes, a request interceptor can modify the outgoing config to inject or cap limit and offset query parameters before the HTTP call is sent. This bounds the amount of data the client requests, but it does not stop the server from ignoring those parameters or returning more data. A transformResponse hook can only trim the payload after it arrives.
config object and can change config.params or config.data before dispatch.axios instance runs for every request unless an instance has its own interceptor chain that shadows it.config.params before mutation avoids side effects on retry logic or other interceptors.Create a reusable interceptor that:
config.params.limit value exists, defaulting to a configured value.offset of 0 when missing (for offset‑based pagination).Example implementation:
function paginationLimitInterceptor({ maxLimit = 200, defaultLimit = 50 } = {}) {
return function (config) {
if (!config.params) config.params = {};
const params = { ...config.params };
const limit = Number(params.limit) || defaultLimit;
params.limit = Math.min(limit, maxLimit);
if (!('offset' in params)) params.offset = 0;
config.params = params;
return config;
};
}
Attach it to an instance:
const api = axios.create({ baseURL: 'https://example.com/api' });
api.defaults.maxLimit = 100;
api.interceptors.request.use(paginationLimitInterceptor({ maxLimit: 100 }));
maxLimit and interceptor.config._paginationLimit) that the interceptor reads to override the global default.The transformResponse hook runs after the server replies. It can slice or discard items that exceed the desired limit, but it cannot prevent the server from sending the full payload. It is useful as a safety net when the API is unreliable.
To tailor the interceptor precisely, we need to know whether the target API uses offset/limit pagination or a cursor/keyset scheme. If a cursor is required, injecting limit/offset may be ignored or cause errors, so the interceptor should be endpoint‑specific or disabled for those routes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.