mirror of
https://github.com/DeBrosOfficial/network-ts-sdk.git
synced 2025-12-11 01:58:49 +00:00
Add TLS configuration for fetch in HttpClient
- Introduced a new function to create a fetch instance with TLS settings for staging certificates in Node.js environments. - Updated HttpClient to use this fetch function, allowing self-signed certificates in development and staging environments. - Enhanced security handling by ensuring that staging certificates are only accepted in non-production settings.
This commit is contained in:
parent
3db9f4d8b8
commit
681299efdd
@ -8,6 +8,29 @@ export interface HttpClientConfig {
|
|||||||
fetch?: typeof fetch;
|
fetch?: typeof fetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a fetch function with proper TLS configuration for staging certificates
|
||||||
|
* In Node.js, we need to configure TLS to accept Let's Encrypt staging certificates
|
||||||
|
*/
|
||||||
|
function createFetchWithTLSConfig(): typeof fetch {
|
||||||
|
// Check if we're in a Node.js environment
|
||||||
|
if (typeof process !== "undefined" && process.versions?.node) {
|
||||||
|
// For testing/staging/development: allow staging certificates
|
||||||
|
// Let's Encrypt staging certificates are self-signed and not trusted by default
|
||||||
|
const isDevelopmentOrStaging =
|
||||||
|
process.env.NODE_ENV !== "production" ||
|
||||||
|
process.env.DEBROS_ALLOW_STAGING_CERTS === "true" ||
|
||||||
|
process.env.DEBROS_USE_HTTPS === "true";
|
||||||
|
|
||||||
|
if (isDevelopmentOrStaging) {
|
||||||
|
// Allow self-signed/staging certificates
|
||||||
|
// WARNING: Only use this in development/testing environments
|
||||||
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return globalThis.fetch;
|
||||||
|
}
|
||||||
|
|
||||||
export class HttpClient {
|
export class HttpClient {
|
||||||
private baseURL: string;
|
private baseURL: string;
|
||||||
private timeout: number;
|
private timeout: number;
|
||||||
@ -22,7 +45,8 @@ export class HttpClient {
|
|||||||
this.timeout = config.timeout ?? 60000; // Increased from 30s to 60s for pub/sub operations
|
this.timeout = config.timeout ?? 60000; // Increased from 30s to 60s for pub/sub operations
|
||||||
this.maxRetries = config.maxRetries ?? 3;
|
this.maxRetries = config.maxRetries ?? 3;
|
||||||
this.retryDelayMs = config.retryDelayMs ?? 1000;
|
this.retryDelayMs = config.retryDelayMs ?? 1000;
|
||||||
this.fetch = config.fetch ?? globalThis.fetch;
|
// Use provided fetch or create one with proper TLS configuration for staging certificates
|
||||||
|
this.fetch = config.fetch ?? createFetchWithTLSConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
setApiKey(apiKey?: string) {
|
setApiKey(apiKey?: string) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user