Skip to content

feat(api): add User-Agent header to raw apiFetch calls#1320

Open
Bret Comnes (bcomnes) wants to merge 2 commits into
v1.xfrom
user-agent
Open

feat(api): add User-Agent header to raw apiFetch calls#1320
Bret Comnes (bcomnes) wants to merge 2 commits into
v1.xfrom
user-agent

Conversation

@bcomnes
Copy link
Copy Markdown
Member

@bcomnes Bret Comnes (bcomnes) commented May 20, 2026

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.

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
Copilot AI review requested due to automatic review settings May 20, 2026 01:12
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() in src/utils/sdk.mts to build and cache a full UA string including CLI token + Node.js version + OS platform/arch.
  • Add User-Agent header to raw API helpers (queryApi() and sendApiRequest()) that use apiFetch() directly (bypassing the SDK).
  • Pass the full UA to Coana via SOCKET_CALLER_USER_AGENT in spawnCoanaDlx().

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 thread src/utils/api.mts
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 thread src/utils/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 thread src/utils/dlx.mts
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(),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants