feat(api): add User-Agent header to raw apiFetch calls#1320
Open
Bret Comnes (bcomnes) wants to merge 2 commits into
Open
feat(api): add User-Agent header to raw apiFetch calls#1320Bret Comnes (bcomnes) wants to merge 2 commits into
Bret Comnes (bcomnes) wants to merge 2 commits into
Conversation
Add getCliUserAgent() to sdk.mts that builds the CLI's full user agent string for direct (non-SDK) API calls, including the CLI product token, Node.js version, and OS platform/arch: socket/1.1.96 node/v22.0.0 linux/arm64 Add User-Agent header to queryApi() and sendApiRequest() which both go through apiFetch() bypassing the SDK. For SDK-routed calls the userAgent option already passes just the CLI product token (socket/1.1.96 ...) and the SDK constructs the full UA by prepending its own base including node/OS.
Adds the CLI's full user agent string to the env vars forwarded to the coana subprocess. Coana reads SOCKET_CALLER_USER_AGENT and appends it to its own base UA, producing a chain like: coana-tech-cli/15.3.0 node/v22.0.0 linux/arm64 socket/1.1.96 node/v22.0.0 linux/arm64
There was a problem hiding this comment.
Pull request overview
This PR adds a full CLI User-Agent string for direct (non-SDK) HTTP calls and wires it into the raw API request helpers (plus propagates it to the Coana dlx environment) so backend services can reliably identify the CLI runtime.
Changes:
- Add
getCliUserAgent()insrc/utils/sdk.mtsto build and cache a full UA string including CLI token + Node.js version + OS platform/arch. - Add
User-Agentheader to raw API helpers (queryApi()andsendApiRequest()) that useapiFetch()directly (bypassing the SDK). - Pass the full UA to Coana via
SOCKET_CALLER_USER_AGENTinspawnCoanaDlx().
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/utils/sdk.mts |
Adds getCliUserAgent() to generate/cached full CLI user agent string for non-SDK calls. |
src/utils/dlx.mts |
Propagates CLI UA to Coana via SOCKET_CALLER_USER_AGENT. |
src/utils/api.mts |
Sets User-Agent header on raw API calls made via apiFetch(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
39
to
43
| HTTP_STATUS_UNAUTHORIZED, | ||
| } from '../constants.mts' | ||
| import { getRequirements, getRequirementsKey } from './requirements.mts' | ||
| import { getDefaultApiToken, getExtraCaCerts } from './sdk.mts' | ||
| import { getCliUserAgent, getDefaultApiToken, getExtraCaCerts } from './sdk.mts' | ||
|
|
Comment on lines
+53
to
+71
| // Lazy-evaluated full CLI user agent for direct (non-SDK) API calls. | ||
| // Includes the CLI product token, Node.js version, and OS platform/arch. | ||
| // e.g. "socket/1.1.96 node/v22.0.0 linux/arm64" | ||
| let _cliUserAgent: string | undefined | ||
| export function getCliUserAgent(): string { | ||
| if (!_cliUserAgent) { | ||
| const name = constants.ENV.INLINED_SOCKET_CLI_NAME.replace('@', '').replace( | ||
| '/', | ||
| '-', | ||
| ) | ||
| const version = constants.ENV.INLINED_SOCKET_CLI_VERSION | ||
| _cliUserAgent = [ | ||
| `${name}/${version}`, | ||
| `node/${process.version}`, | ||
| `${process.platform}/${process.arch}`, | ||
| ].join(' ') | ||
| } | ||
| return _cliUserAgent | ||
| } |
Comment on lines
208
to
212
| const mixinsEnv: Record<string, string> = { | ||
| SOCKET_CLI_VERSION: constants.ENV.INLINED_SOCKET_CLI_VERSION, | ||
| // Pass the CLI's full user agent so coana can append it to its own UA. | ||
| SOCKET_CALLER_USER_AGENT: getCliUserAgent(), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add getCliUserAgent() to sdk.mts that builds the CLI's full user agent
string for direct (non-SDK) API calls, including the CLI product token,
Node.js version, and OS platform/arch:
socket/1.1.96 node/v22.0.0 linux/arm64
Add User-Agent header to queryApi() and sendApiRequest() which both go
through apiFetch() bypassing the SDK.
For SDK-routed calls the userAgent option already passes just the CLI
product token (socket/1.1.96 ...) and the SDK constructs the full UA
by prepending its own base including node/OS.
Depends on: SocketDev/socket-sdk-js#638 https://github.com/coana-tech/coana-package-manager/pull/2225
Note
Cursor Bugbot is generating a summary for commit 3778a64. Configure here.