Skip to content

Commit 9f255fa

Browse files
committed
feat: added user-agent header
1 parent c598cb8 commit 9f255fa

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

sdk/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @stackoverflow/teams-sdk
22

3+
## 1.3.0
4+
5+
### Minor Changes
6+
7+
- Added user agent header to identify calls originating from the SDK
8+
39
## 1.2.2
410

511
### Patch Changes

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stackoverflow/teams-sdk",
3-
"version": "1.2.2",
3+
"version": "1.3.0",
44
"description": "SDK for the Stack Overflow Teams API",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

sdk/src/helper/fixedHttpLibrary.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
1518
export 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

Comments
 (0)