Skip to content

Commit fb461f1

Browse files
author
Pelle Wessman
committed
Improve User-Agent of SDK
1 parent 626c309 commit fb461f1

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,42 @@ const { SocketSdk } = require('@socketsecurity/sdk')
6060

6161
* `getQuota()`
6262

63+
## Additional exports
64+
65+
* `createUserAgentFromPkgJson(pkgJson)`
66+
* `pkgJson`: The content of the `package.json` you want to create a `User-Agent` string for
67+
68+
## Advanced
69+
70+
### Specifying custom user agent
71+
72+
The `SocketSdk` constructor accepts an `options` object as its second argument and there a `userAgent` key with a string value can be specified. If specified then that user agent will be prepended to the SDK user agent. See this example:
73+
74+
```js
75+
const client = new SocketSdk('yourApiKeyHere', {
76+
userAgent: 'example/1.2.3 (http://example.com/)'
77+
})
78+
```
79+
80+
Which results in the [HTTP `User-Agent` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent):
81+
82+
```
83+
User-Agent: example/1.2.3 (http://example.com/) socketsecurity-sdk/0.5.2 (https://github.com/SocketDev/socket-sdk-js)
84+
```
85+
86+
To easily create a user agent for your code you can use the additional export `createUserAgentFromPkgJson()` like this, assuming `pkgJson` contains your parsed `package.json`:
87+
88+
```js
89+
const client = new SocketSdk('yourApiKeyHere', {
90+
userAgent: createUserAgentFromPkgJson(pkgJson)
91+
})
92+
```
93+
94+
Specifying a custom user agent is good practice when shipping a piece of code that others can use to make requests. Eg. [our CLI](https://github.com/SocketDev/socket-cli-js) uses this option to identify requests coming from it + mentioning which version of it that is used.
95+
6396
## See also
6497

6598
* [Socket API Reference](https://docs.socket.dev/reference)
6699
* [Socket.dev](https://socket.dev/)
67100
* [Socket GitHub App](https://github.com/apps/socket-security)
101+
* [Socket CLI](https://github.com/SocketDev/socket-cli-js)

index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const path = require('node:path')
44

55
const { ErrorWithCause } = require('pony-cause')
66

7+
const pkg = require('./package.json')
8+
79
/**
810
* @template {keyof import('./types/api').operations} T
911
* @typedef {import('./types/api-helpers').OpReturnType<import('./types/api').operations[T]>} SocketSdkReturnType
@@ -23,6 +25,7 @@ const { ErrorWithCause } = require('pony-cause')
2325
* @typedef SocketSdkOptions
2426
* @property {import('got').Agents} [agent]
2527
* @property {string} [baseUrl]
28+
* @property {string} [userAgent]
2629
*/
2730

2831
class SocketSdk {
@@ -44,12 +47,16 @@ class SocketSdk {
4447
const {
4548
agent,
4649
baseUrl = 'https://api.socket.dev/v0/',
50+
userAgent,
4751
} = options
4852

4953
this.#gotOptions = {
5054
prefixUrl: baseUrl,
5155
retry: { limit: 0 },
5256
username: apiKey,
57+
headers: {
58+
'user-agent': (userAgent ? userAgent + ' ' : '') + createUserAgentFromPkgJson(pkg),
59+
},
5360
...(agent ? { agent } : {}),
5461
}
5562
}
@@ -243,4 +250,15 @@ function ensureObject (value) {
243250
return !!(value && typeof value === 'object' && !Array.isArray(value))
244251
}
245252

246-
module.exports = { SocketSdk }
253+
/**
254+
* @param {{ name: string, version: string, homepage?: string }} pkgData Package.json data to base the User-Agent on
255+
* @returns {string}
256+
*/
257+
function createUserAgentFromPkgJson (pkgData) {
258+
return `${pkgData.name.replace('@', '').replace('/', '-')}/${pkgData.version}` + (pkgData.homepage ? ` (${pkgData.homepage})` : '')
259+
}
260+
261+
module.exports = {
262+
createUserAgentFromPkgJson,
263+
SocketSdk,
264+
}

0 commit comments

Comments
 (0)