Skip to content

Commit c9dbaec

Browse files
committed
Update arg signatures
1 parent 7183746 commit c9dbaec

File tree

1 file changed

+78
-31
lines changed

1 file changed

+78
-31
lines changed

src/index.ts

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import path from 'node:path'
66
import readline from 'node:readline'
77

88
import abortSignal from '@socketsecurity/registry/lib/constants/abort-signal'
9+
import { hasOwn, isObjectObject } from '@socketsecurity/registry/lib/objects'
910
import { pRetry } from '@socketsecurity/registry/lib/promises'
1011

1112
// @ts-ignore: Avoid TS import attributes error.
@@ -32,8 +33,9 @@ export type Agent = HttpsAgent | HttpAgent | ClientHttp2Session
3233
export type BatchPackageFetchResultType = SocketSdkResult<'batchPackageFetch'>
3334

3435
export type BatchPackageStreamOptions = {
35-
chunkSize: number
36-
concurrencyLimit: number
36+
chunkSize?: number | undefined
37+
concurrencyLimit?: number | undefined
38+
queryParams?: QueryParams | undefined
3739
}
3840

3941
export type GotOptions = {
@@ -42,6 +44,8 @@ export type GotOptions = {
4244
http2?: ClientHttp2Session | undefined
4345
}
4446

47+
export type QueryParams = Record<string, any>
48+
4549
export type RequestOptions =
4650
| HttpsRequestOptions
4751
| HttpRequestOptions
@@ -423,14 +427,14 @@ function queryToSearchParams(
423427
init?:
424428
| URLSearchParams
425429
| string
426-
| Record<string, any>
430+
| QueryParams
427431
| Iterable<[string, any]>
428432
| ReadonlyArray<[string, any]>
429433
| null
430434
| undefined
431435
): URLSearchParams {
432436
const params = new URLSearchParams(init ?? '')
433-
const normalized = { __proto__: null } as unknown as Record<string, string>
437+
const normalized = { __proto__: null } as unknown as QueryParams
434438
const entries: Iterable<[string, any]> = params.entries()
435439
for (const entry of entries) {
436440
let key = entry[0]
@@ -514,8 +518,8 @@ export class SocketSdk {
514518
}
515519

516520
async #createBatchPurlRequest(
517-
queryParams: Record<string, string> | null | undefined,
518-
componentsObj: { components: Array<{ purl: string }> }
521+
componentsObj: { components: Array<{ purl: string }> },
522+
queryParams?: QueryParams | undefined
519523
): Promise<IncomingMessage> {
520524
// Adds the first 'abort' listener to abortSignal.
521525
const req = getHttpModule(this.#baseUrl)
@@ -528,13 +532,13 @@ export class SocketSdk {
528532
}
529533

530534
async *#createBatchPurlGenerator(
531-
queryParams: Record<string, string> | null | undefined,
532-
componentsObj: { components: Array<{ purl: string }> }
535+
componentsObj: { components: Array<{ purl: string }> },
536+
queryParams?: QueryParams | undefined
533537
): AsyncGenerator<BatchPackageFetchResultType> {
534538
let res: IncomingMessage | undefined
535539
try {
536540
res = await pRetry(
537-
() => this.#createBatchPurlRequest(queryParams, componentsObj),
541+
() => this.#createBatchPurlRequest(componentsObj, queryParams),
538542
{
539543
retries: 4,
540544
onRetryRethrow: true,
@@ -609,12 +613,19 @@ export class SocketSdk {
609613
}
610614

611615
async batchPackageFetch(
612-
queryParams: Record<string, string> | null | undefined,
613-
componentsObj: { components: Array<{ purl: string }> }
616+
componentsObj: { components: Array<{ purl: string }> },
617+
queryParams?: QueryParams | undefined
614618
): Promise<BatchPackageFetchResultType> {
619+
// Support previous argument signature.
620+
if (isObjectObject(componentsObj) && !hasOwn(componentsObj, 'components')) {
621+
const oldParam1 = componentsObj
622+
const oldParam2 = queryParams
623+
queryParams = oldParam1 as typeof oldParam2
624+
componentsObj = oldParam2 as unknown as typeof oldParam1
625+
}
615626
let res: IncomingMessage | undefined
616627
try {
617-
res = await this.#createBatchPurlRequest(queryParams, componentsObj)
628+
res = await this.#createBatchPurlRequest(componentsObj, queryParams)
618629
} catch (e) {
619630
return await this.#handleApiError<'batchPackageFetch'>(e)
620631
}
@@ -633,10 +644,29 @@ export class SocketSdk {
633644
}
634645

635646
async *batchPackageStream(
636-
queryParams: Record<string, string> | null | undefined,
637647
componentsObj: { components: Array<{ purl: string }> },
638648
options?: BatchPackageStreamOptions | undefined
639649
): AsyncGenerator<BatchPackageFetchResultType> {
650+
// Support previous argument signature.
651+
if (isObjectObject(componentsObj) && !hasOwn(componentsObj, 'components')) {
652+
const oldParam1 = componentsObj
653+
const oldParam2 = options
654+
componentsObj = oldParam2 as unknown as typeof oldParam1
655+
options = {
656+
queryParams: oldParam1 as QueryParams,
657+
...arguments[2]
658+
} as BatchPackageStreamOptions
659+
}
660+
661+
const {
662+
chunkSize = 100,
663+
concurrencyLimit = 10,
664+
queryParams
665+
} = {
666+
__proto__: null,
667+
...options
668+
} as BatchPackageStreamOptions
669+
640670
type GeneratorStep = {
641671
generator: AsyncGenerator<BatchPackageFetchResultType>
642672
iteratorResult: IteratorResult<BatchPackageFetchResultType>
@@ -646,10 +676,6 @@ export class SocketSdk {
646676
promise: Promise<GeneratorStep>
647677
}
648678

649-
const { chunkSize = 100, concurrencyLimit = 10 } = {
650-
__proto__: null,
651-
...options
652-
} as BatchPackageStreamOptions
653679
// The createBatchPurlGenerator method will add 2 'abort' event listeners to
654680
// abortSignal so we multiply the concurrencyLimit by 2.
655681
const neededMaxListeners = concurrencyLimit * 2
@@ -669,10 +695,13 @@ export class SocketSdk {
669695
// No more work to do.
670696
return
671697
}
672-
const generator = this.#createBatchPurlGenerator(queryParams, {
673-
// Chunk components.
674-
components: components.slice(index, index + chunkSize)
675-
})
698+
const generator = this.#createBatchPurlGenerator(
699+
{
700+
// Chunk components.
701+
components: components.slice(index, index + chunkSize)
702+
},
703+
queryParams
704+
)
676705
continueGen(generator)
677706
index += chunkSize
678707
}
@@ -728,10 +757,19 @@ export class SocketSdk {
728757
}
729758

730759
async createDependenciesSnapshot(
731-
queryParams: Record<string, string>,
732760
filepaths: string[],
733-
pathsRelativeTo = '.'
761+
pathsRelativeTo = '.',
762+
queryParams?: QueryParams | undefined
734763
): Promise<SocketSdkResult<'createDependenciesSnapshot'>> {
764+
// Support previous argument signature.
765+
if (isObjectObject(filepaths)) {
766+
const oldParam1 = filepaths
767+
const oldParam2 = pathsRelativeTo
768+
const oldParam3 = queryParams
769+
queryParams = oldParam1 as typeof oldParam3
770+
filepaths = oldParam2 as unknown as typeof oldParam1
771+
pathsRelativeTo = oldParam3 as unknown as typeof oldParam2
772+
}
735773
const basePath = resolveBasePath(pathsRelativeTo)
736774
const absFilepaths = resolveAbsPaths(filepaths, basePath)
737775
try {
@@ -751,10 +789,19 @@ export class SocketSdk {
751789

752790
async createOrgFullScan(
753791
orgSlug: string,
754-
queryParams: Record<string, string> | null | undefined,
755792
filepaths: string[],
756-
pathsRelativeTo: string = '.'
793+
pathsRelativeTo: string = '.',
794+
queryParams?: QueryParams | undefined
757795
): Promise<SocketSdkResult<'CreateOrgFullScan'>> {
796+
// Support previous argument signature.
797+
if (isObjectObject(filepaths)) {
798+
const oldParam2 = filepaths
799+
const oldParam3 = pathsRelativeTo
800+
const oldParam4 = queryParams
801+
queryParams = oldParam2 as typeof oldParam4
802+
filepaths = oldParam3 as unknown as typeof oldParam2
803+
pathsRelativeTo = oldParam4 as unknown as typeof oldParam3
804+
}
758805
const basePath = resolveBasePath(pathsRelativeTo)
759806
const absFilepaths = resolveAbsPaths(filepaths, basePath)
760807
try {
@@ -774,7 +821,7 @@ export class SocketSdk {
774821

775822
async createOrgRepo(
776823
orgSlug: string,
777-
queryParams: Record<string, string>
824+
queryParams?: QueryParams | undefined
778825
): Promise<SocketSdkResult<'createOrgRepo'>> {
779826
try {
780827
const data = await getResponseJson(
@@ -857,7 +904,7 @@ export class SocketSdk {
857904

858905
async getAuditLogEvents(
859906
orgSlug: string,
860-
queryParams?: Record<string, string> | null | undefined
907+
queryParams?: QueryParams | undefined
861908
): Promise<SocketSdkResult<'getAuditLogEvents'>> {
862909
try {
863910
const data = await getResponseJson(
@@ -948,7 +995,7 @@ export class SocketSdk {
948995

949996
async getOrgFullScanList(
950997
orgSlug: string,
951-
queryParams?: Record<string, string> | null | undefined
998+
queryParams?: QueryParams | undefined
952999
): Promise<SocketSdkResult<'getOrgFullScanList'>> {
9531000
try {
9541001
const data = await getResponseJson(
@@ -1022,7 +1069,7 @@ export class SocketSdk {
10221069

10231070
async getOrgRepoList(
10241071
orgSlug: string,
1025-
queryParams?: Record<string, string> | null | undefined
1072+
queryParams?: QueryParams | undefined
10261073
): Promise<SocketSdkResult<'getOrgRepoList'>> {
10271074
try {
10281075
const data = await getResponseJson(
@@ -1164,7 +1211,7 @@ export class SocketSdk {
11641211
}
11651212

11661213
async searchDependencies(
1167-
queryParams: Record<string, number>
1214+
queryParams?: QueryParams | undefined
11681215
): Promise<SocketSdkResult<'searchDependencies'>> {
11691216
try {
11701217
const data = await getResponseJson(
@@ -1184,7 +1231,7 @@ export class SocketSdk {
11841231
async updateOrgRepo(
11851232
orgSlug: string,
11861233
repoSlug: string,
1187-
queryParams: Record<string, string>
1234+
queryParams?: QueryParams | undefined
11881235
): Promise<SocketSdkResult<'updateOrgRepo'>> {
11891236
try {
11901237
const data = await getResponseJson(

0 commit comments

Comments
 (0)