Skip to content

Commit ef7df81

Browse files
author
Pelle Wessman
committed
Prepend a CLI User-Agent to SDK requests
1 parent 44db799 commit ef7df81

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/utils/sdk.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { SocketSdk } from '@socketsecurity/sdk'
1+
import { readFile } from 'node:fs/promises'
2+
import { dirname, join } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
5+
import { SocketSdk, createUserAgentFromPkgJson } from '@socketsecurity/sdk'
26
import isInteractive from 'is-interactive'
37
import prompts from 'prompts'
48

@@ -34,11 +38,14 @@ export async function setupSdk () {
3438
https: new HttpsProxyAgent({ proxy: process.env['SOCKET_SECURITY_API_PROXY'] }),
3539
}
3640
}
41+
const packageJsonPath = join(dirname(fileURLToPath(import.meta.url)), '../../package.json')
42+
const packageJson = await readFile(packageJsonPath, 'utf8')
3743

3844
/** @type {import('@socketsecurity/sdk').SocketSdkOptions} */
3945
const sdkOptions = {
4046
agent,
4147
baseUrl: process.env['SOCKET_SECURITY_API_BASE_URL'],
48+
userAgent: createUserAgentFromPkgJson(JSON.parse(packageJson))
4249
}
4350

4451
return new SocketSdk(apiKey || '', sdkOptions)

0 commit comments

Comments
 (0)