You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many applications need to make HTTP requests to external APIs or services from within their providers/services. While there are many HTTP client libraries in the JavaScript/TypeScript ecosystem (e.g., axios, node-fetch, undici, got), it would be useful to have a prepackaged, injectable HTTP module for NexusDI that provides a consistent, testable, and extensible API for making HTTP requests.
Note:
This module is not for running an HTTP server or registering controllers/routes. It is focused solely on outbound HTTP requests from your providers/services.
Motivation
Dependency injection for HTTP clients: Make it easy to inject and mock HTTP clients in services.
Consistent API: Provide a unified interface regardless of the underlying HTTP library.
Extensibility: Allow users to swap out the underlying HTTP client, add interceptors, or customize behavior.
Testability: Make it easy to mock or stub HTTP requests in tests.
Portability: Use the native fetch API by default for maximum compatibility across Node, browsers, React Native, edge runtimes, etc.
Proposed Features
Injectable HTTP client service (e.g., HttpService)
Support for common HTTP methods (GET, POST, PUT, DELETE, etc.)
Configurable base URL, headers, timeouts, etc. via config or configAsync
Support for request/response interceptors (middleware)
Uses the native fetch API by default for minimal dependencies and maximum portability
Allows passing a custom HTTP agent/dispatcher (in Node.js) for advanced use cases (e.g., certificate pinning, mTLS)
Easy to mock for testing
Open Questions
How should custom agent/dispatcher support be exposed in the API for Node.js?
Should we provide a fallback or polyfill for environments without native fetch?
Should we support advanced features like retries, circuit breakers, etc. out of the box?
How should configuration (base URL, headers, etc.) be provided—per-request, per-service, or both?
API Sketch
// Register the HTTP module (dynamic module) with configimport{Agent}from'node:https';container.set(HttpModule.config({baseURL: 'https://api.bankid.com',dispatcher: newAgent({cert: fs.readFileSync('client-cert.pem'),key: fs.readFileSync('client-key.pem'),ca: fs.readFileSync('ca.pem'),// ...other options for certificate pinning}),}));// Or with async configcontainer.set(HttpModule.configAsync(async()=>({baseURL: process.env.BANKID_API_URL,dispatcher: awaitcreateCustomAgent(),})));// Inject and use in a serviceclassBankIdService{constructor(privatehttp: HttpService){}asyncauthenticate(payload){returnthis.http.post('/auth',payload);}}
Call for Feedback
What use cases do you have for HTTP clients in NexusDI?
What features or configuration options would you like to see?
Do you have use cases that require a custom HTTP agent or dispatcher (e.g., for mTLS or certificate pinning)?
Are there environments you use where fetch is not available or not sufficient?
Any concerns or suggestions about extensibility or testability?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
Many applications need to make HTTP requests to external APIs or services from within their providers/services. While there are many HTTP client libraries in the JavaScript/TypeScript ecosystem (e.g., axios, node-fetch, undici, got), it would be useful to have a prepackaged, injectable HTTP module for NexusDI that provides a consistent, testable, and extensible API for making HTTP requests.
Note:
This module is not for running an HTTP server or registering controllers/routes. It is focused solely on outbound HTTP requests from your providers/services.
Motivation
fetchAPI by default for maximum compatibility across Node, browsers, React Native, edge runtimes, etc.Proposed Features
HttpService)configorconfigAsyncfetchAPI by default for minimal dependencies and maximum portabilityOpen Questions
fetch?API Sketch
Call for Feedback
Beta Was this translation helpful? Give feedback.
All reactions