Skip to content

Commit 0c53d73

Browse files
committed
Update types
1 parent a3fa84b commit 0c53d73

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/index.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,41 @@ import rootPkgJson from '../package.json' with { type: 'json' }
1313

1414
import type { operations } from '../types/api'
1515
import type { OpErrorType, OpReturnType } from '../types/api-helpers'
16+
import type { ClientHttp2Session } from 'http2-wrapper'
1617
import type { ReadStream } from 'node:fs'
17-
import type { ClientRequest, IncomingMessage } from 'node:http'
18-
import type { Agent, RequestOptions } from 'node:https'
19-
20-
type BatchPackageFetchResultType = SocketSdkResultType<'batchPackageFetch'>
21-
22-
type BatchPackageStreamOptions = {
18+
import type {
19+
ClientRequest,
20+
Agent as HttpAgent,
21+
RequestOptions as HttpRequestOptions,
22+
IncomingMessage
23+
} from 'node:http'
24+
import type { ClientSessionRequestOptions } from 'node:http2'
25+
import type {
26+
Agent as HttpsAgent,
27+
RequestOptions as HttpsRequestOptions
28+
} from 'node:https'
29+
30+
export type Agent = HttpsAgent | HttpAgent | ClientHttp2Session
31+
32+
export type BatchPackageFetchResultType =
33+
SocketSdkResultType<'batchPackageFetch'>
34+
35+
export type BatchPackageStreamOptions = {
2336
chunkSize: number
2437
concurrencyLimit: number
2538
}
2639

40+
export type GotOptions = {
41+
http?: HttpAgent | undefined
42+
https?: HttpsAgent | undefined
43+
http2?: ClientHttp2Session | undefined
44+
}
45+
46+
export type RequestOptions =
47+
| HttpsRequestOptions
48+
| HttpRequestOptions
49+
| ClientSessionRequestOptions
50+
2751
export type SocketSdkOperations = keyof operations
2852

2953
export type SocketSdkReturnType<T extends SocketSdkOperations> = OpReturnType<
@@ -43,14 +67,7 @@ export type SocketSdkResultType<T extends SocketSdkOperations> =
4367
| SocketSdkErrorType<T>
4468

4569
export interface SocketSdkOptions {
46-
agent?:
47-
| Agent
48-
| {
49-
http?: Agent | undefined
50-
https?: Agent | undefined
51-
http2?: Agent | undefined
52-
}
53-
| undefined
70+
agent?: Agent | GotOptions | undefined
5471
baseUrl?: string | undefined
5572
userAgent?: string | undefined
5673
}
@@ -183,7 +200,7 @@ async function createUploadRequest(
183200
method: 'POST',
184201
...options,
185202
headers: {
186-
...options?.headers,
203+
...(options as HttpsRequestOptions)?.headers,
187204
'Content-Type': `multipart/form-data; boundary=${boundary}`
188205
}
189206
})
@@ -407,9 +424,12 @@ export class SocketSdk {
407424
userAgent
408425
} = { __proto__: null, ...options } as SocketSdkOptions
409426
const agentKeys = agentOrObj ? Object.keys(agentOrObj) : []
427+
const agentAsGotOptions = agentOrObj as GotOptions
410428
const agent = (
411429
agentKeys.length && agentKeys.every(k => agentNames.has(k))
412-
? (agentOrObj as any).https
430+
? agentAsGotOptions.https ||
431+
agentAsGotOptions.http ||
432+
agentAsGotOptions.http2
413433
: agentOrObj
414434
) as Agent | undefined
415435
this.#baseUrl = baseUrl

0 commit comments

Comments
 (0)