@@ -11,26 +11,35 @@ import "whatwg-fetch";
1111 * Stack Overflow APIs often return valid JSON or plain text responses without Content-Type headers,
1212 * which causes the auto-generated SDK's ObjectSerializer to fail. This library
1313 * intercepts responses and adds the appropriate Content-Type header based on content detection.
14+ *
15+ * Additionally, this library automatically adds the 'SOInternalSDK' User-Agent header
16+ * to all outgoing requests for analytics tracking.
1417 */
1518export class FixedIsomorphicFetchHttpLibrary implements HttpLibrary {
1619 public send ( request : RequestContext ) : Observable < ResponseContext > {
1720 let method = request . getHttpMethod ( ) . toString ( ) ;
1821 let body = request . getBody ( ) ;
1922
23+ // Get existing headers
24+ const headers = request . getHeaders ( ) ;
25+
26+ // Add User-Agent header to all requests
27+ headers [ 'User-Agent' ] = 'SOInternalSDK' ;
28+
2029 const resultPromise = fetch ( request . getUrl ( ) , {
2130 method : method ,
2231 body : body as any ,
23- headers : request . getHeaders ( ) ,
32+ headers : headers ,
2433 signal : request . getSignal ( ) ,
2534 credentials : "same-origin"
2635 } ) . then ( async ( resp : any ) => {
27- const headers : { [ name : string ] : string } = { } ;
36+ const responseHeaders : { [ name : string ] : string } = { } ;
2837 resp . headers . forEach ( ( value : string , name : string ) => {
29- headers [ name ] = value ;
38+ responseHeaders [ name ] = value ;
3039 } ) ;
3140
3241 // CORE FIX: Add Content-Type header if missing
33- const hasContentType = Object . keys ( headers ) . some (
42+ const hasContentType = Object . keys ( responseHeaders ) . some (
3443 key => key . toLowerCase ( ) === 'content-type'
3544 ) ;
3645
@@ -42,9 +51,9 @@ export class FixedIsomorphicFetchHttpLibrary implements HttpLibrary {
4251
4352 // Detect content type based on content
4453 if ( trimmed . startsWith ( '{' ) || trimmed . startsWith ( '[' ) ) {
45- headers [ 'content-type' ] = 'application/json' ;
54+ responseHeaders [ 'content-type' ] = 'application/json' ;
4655 } else {
47- headers [ 'content-type' ] = 'text/plain' ;
56+ responseHeaders [ 'content-type' ] = 'text/plain' ;
4857 }
4958 }
5059
@@ -53,7 +62,7 @@ export class FixedIsomorphicFetchHttpLibrary implements HttpLibrary {
5362 binary : ( ) => resp . blob ( )
5463 } ;
5564
56- return new ResponseContext ( resp . status , headers , body ) ;
65+ return new ResponseContext ( resp . status , responseHeaders , body ) ;
5766 } ) ;
5867 return from < Promise < ResponseContext > > ( resultPromise ) ;
5968 }
0 commit comments