Skip to content

Commit d6e7f89

Browse files
committed
Make agent option back compatible
1 parent c35ea30 commit d6e7f89

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ type SocketSdkResultType<T extends SocketSdkOperations> =
2222
| SocketSdkErrorType<T>
2323

2424
export interface SocketSdkOptions {
25-
agent?: Agent | undefined
25+
agent?:
26+
| Agent
27+
| {
28+
http?: Agent | undefined
29+
https?: Agent | undefined
30+
http2?: Agent | undefined
31+
}
32+
| undefined
2633
baseUrl?: string | undefined
2734
userAgent?: string | undefined
2835
}
@@ -206,6 +213,9 @@ export function createUserAgentFromPkgJson(pkgData: {
206213
return `${name}/${pkgData.version}${homepage ? ` (${homepage})` : ''}`
207214
}
208215

216+
// https://github.com/sindresorhus/got/blob/v14.4.6/documentation/2-options.md#agent
217+
const agentNames = new Set(['http', 'https', 'http2'])
218+
209219
export class SocketSdk {
210220
readonly #baseUrl: string
211221
readonly #reqOptions: RequestOptions
@@ -215,11 +225,16 @@ export class SocketSdk {
215225
*/
216226
constructor(apiToken: string, options?: SocketSdkOptions | undefined) {
217227
const {
218-
agent,
228+
agent: agentOrObj,
219229
baseUrl = 'https://api.socket.dev/v0/',
220230
userAgent
221231
} = { __proto__: null, ...options } as SocketSdkOptions
222-
232+
const agentKeys = agentOrObj ? Object.keys(agentOrObj) : []
233+
const agent = (
234+
agentKeys.length && agentKeys.every(k => agentNames.has(k))
235+
? (agentOrObj as any).https
236+
: agentOrObj
237+
) as Agent | undefined
223238
this.#baseUrl = baseUrl
224239
this.#reqOptions = {
225240
...(agent ? { agent } : {}),

0 commit comments

Comments
 (0)