Replace axios with fetch to reduce bundle size and allow custom HTTP clients
This PR replaces the axios HTTP client with fetch in the Twilio Node.js Helper Library to significantly reduce bundle size and allow users to bring their own fetch implementation, directly addressing the bundle size concerns raised in issue #728.
Changes Made
Core Implementation:
- Replaced axios with fetch in
RequestClient.tswhile maintaining the exact same public API - Removed axios dependency from
package.json(saves ~100KB+ in bundle size) - Added
fetchoption toRequestClientOptionsto allow custom fetch implementations - Implemented retry logic using native promises instead of axios interceptors
- Maintained all existing functionality: HTTPS agents, validation, logging, error handling
Backward Compatibility:
- No breaking changes to the public API
- All existing RequestClient functionality preserved
- Works with any fetch-compatible implementation
Benefits
Bundle Size Reduction: Users can now significantly reduce bundle size by:
- Using native fetch in Node.js 18+ or browser environments
- Providing optimized HTTP clients like undici or node-fetch
- Avoiding the axios dependency entirely
Flexibility:
// Use undici for better performance
const { fetch } = require('undici');
const client = twilio(sid, token, {
httpClient: new twilio.RequestClient({ fetch })
});
// Use built-in fetch (Node.js 18+)
const client = twilio(sid, token); // Uses global fetch automatically
// Custom fetch with additional features
const customFetch = async (url, options) => {
console.log(`Making request to: ${url}`);
return fetch(url, options);
};
const client = twilio(sid, token, {
httpClient: new twilio.RequestClient({ fetch: customFetch })
});
Testing
Added comprehensive test suite (RequestClient.fetch.spec.js) with 18 tests covering:
- Basic HTTP operations (GET, POST with form data, query parameters)
- Authentication handling
- Retry logic for 429 responses
- Error handling and validation
- Response parsing (JSON and plain text)
- Request/response tracking
All tests pass and verify that the fetch implementation maintains feature parity with the original axios implementation.
Documentation
- Updated
advanced-examples/custom-http-client.mdto reflect fetch usage - Added
examples/custom-fetch.mdwith usage examples - Updated changelog with details about the change
This change enables users to dramatically reduce their bundle size while maintaining all existing functionality, directly addressing the core request in issue #728 to "let users bring in their own fetch implementation."
Fixes #898.
[!WARNING]
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.edge.region.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.edge.region2.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.edge.us1.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.edge2.region.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.region.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.region2.domain.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.region2.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)api.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)iam.twilio.com
- Triggering command:
node /home/REDACTED/work/twilio-node/twilio-node/node_modules/.bin/jest spec --coverage --detectOpenHandles --testPathIgnorePatterns=spec/cluster --testPathPattern=RequestClient.fetch.spec.js(dns block)If you need me to access, download, or install something from one of these locations, you can either:
- Configure Actions setup steps to set up my environment, which run before the firewall is enabled
- Add the appropriate URLs or hosts to the custom allowlist in this repository's Copilot coding agent settings (admins only)
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Is there a to-do list of what is left to release this?