Repository navigation
refactor: unify header construction in sendLogs and sendOnClose #133
Copy link
Copy link
Closed
Labels
UseralePull requests that update Userale codePull requests that update Userale codemaintenancemaintain, eliminate vulnerabilities, reduce obsolescencemaintain, eliminate vulnerabilities, reduce obsolescence
Milestone
Description
Summary
sendLogs() and sendOnClose() in sendLogs.ts construct HTTP headers independently with diverging logic. This causes bugs and makes adding new headers error-prone.
Current Divergence
| Behavior | sendLogs() |
sendOnClose() |
|---|---|---|
Content-Type |
✅ | ✅ |
Authorization (string) |
✅ | ✅ |
Authorization (function callback) |
✅ calls the function | ❌ calls .toString() → sends function source code |
x-api-key (#124) |
✅ | ✅ |
config.headers (custom) |
✅ | ❌ missing |
updateAuthHeader() |
✅ | ❌ not called |
updateCustomHeaders() |
✅ | ❌ not called |
Three bugs from this divergence
- Function-type
authHeaderbroken insendOnClose— ifauthHeaderis a callback (e.g., for token refresh),sendOnClosecalls.toString()which sends the function source code as theAuthorizationheader value - Custom headers missing from pagehide flush — headers registered via
registerHeadersCallback()are never applied to the finalsendOnCloseflush - Future headers must be added in two places — every new header type (like
apiKeyin feat(auth): add support for api keys #124) requires changes to both functions
Proposed Fix
Extract a shared buildHeaders(config): Headers helper:
function buildHeaders(config: Configuration): Headers {
const headers = new Headers({ "Content-Type": "application/json;charset=UTF-8" });
updateAuthHeader(config);
if (config.authHeader) {
const value = typeof config.authHeader === "function" ? config.authHeader() : config.authHeader;
headers.set("Authorization", value);
}
if (config.apiKey) {
headers.set("x-api-key", config.apiKey);
}
updateCustomHeaders(config);
if (config.headers) {
for (const [header, value] of Object.entries(config.headers)) {
headers.set(header, value);
}
}
return headers;
}Both sendLogs() and sendOnClose() call buildHeaders(config).
Testing
Add tests for sendOnClose that verify:
-
x-api-keyheader is present (test exists after feat(auth): add support for api keys #124) -
Authorizationheader works with function-typeauthHeader - Custom headers from
registerHeadersCallback()are included - Headers from
config.headersare included
Context
Pre-existing issue, exposed during review of #124. The sendOnClose path is the pagehide event handler — it fires when users close tabs or navigate away. Logs sent via this path are currently missing custom headers and have broken auth callback support.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
UseralePull requests that update Userale codePull requests that update Userale codemaintenancemaintain, eliminate vulnerabilities, reduce obsolescencemaintain, eliminate vulnerabilities, reduce obsolescence
Activity
feat(userale): add pluggable LogExporter architecture, unified header…