Skip to content

Commit 9cc899d

Browse files
committed
Use queryToSearchParams to normalize query params
1 parent 3ad4dbc commit 9cc899d

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/index.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,30 @@ function promiseWithResolvers<T>(): ReturnType<
419419
return obj
420420
}
421421

422+
function queryToSearchParams(
423+
init?:
424+
| URLSearchParams
425+
| string
426+
| Record<string, string | readonly string[]>
427+
| Iterable<[string, string]>
428+
| ReadonlyArray<[string, string]>
429+
| null
430+
| undefined
431+
): URLSearchParams {
432+
const params = new URLSearchParams(init ?? '')
433+
const normalized: Record<string, string> = {}
434+
for (const entry of params.entries()) {
435+
let key = entry[0]
436+
if (key === 'defaultBranch') {
437+
key = 'default_branch'
438+
} else if (key === 'perPage') {
439+
key = 'per_page'
440+
}
441+
normalized[key] = entry[1]
442+
}
443+
return new URLSearchParams(normalized)
444+
}
445+
422446
function resolveAbsPaths(
423447
filepaths: string[],
424448
pathsRelativeTo?: string
@@ -491,13 +515,10 @@ export class SocketSdk {
491515
): Promise<IncomingMessage> {
492516
// Adds the first 'abort' listener to abortSignal.
493517
const req = getHttpModule(this.#baseUrl)
494-
.request(
495-
`${this.#baseUrl}purl?${new URLSearchParams(queryParams ?? '')}`,
496-
{
497-
method: 'POST',
498-
...this.#reqOptions
499-
}
500-
)
518+
.request(`${this.#baseUrl}purl?${queryToSearchParams(queryParams)}`, {
519+
method: 'POST',
520+
...this.#reqOptions
521+
})
501522
.end(JSON.stringify(componentsObj))
502523
return await getResponse(req)
503524
}
@@ -703,7 +724,7 @@ export class SocketSdk {
703724
}
704725

705726
async createDependenciesSnapshot(
706-
params: Record<string, string>,
727+
queryParams: Record<string, string>,
707728
filepaths: string[],
708729
pathsRelativeTo = '.'
709730
): Promise<SocketSdkResult<'createDependenciesSnapshot'>> {
@@ -713,7 +734,7 @@ export class SocketSdk {
713734
const data = await getResponseJson(
714735
await createUploadRequest(
715736
this.#baseUrl,
716-
`dependencies/upload?${new URLSearchParams(params)}`,
737+
`dependencies/upload?${queryToSearchParams(queryParams)}`,
717738
createRequestBodyForFilepaths(absFilepaths, basePath),
718739
this.#reqOptions
719740
)
@@ -736,7 +757,7 @@ export class SocketSdk {
736757
const data = await getResponseJson(
737758
await createUploadRequest(
738759
this.#baseUrl,
739-
`orgs/${encodeURIComponent(orgSlug)}/full-scans?${new URLSearchParams(queryParams ?? '')}`,
760+
`orgs/${encodeURIComponent(orgSlug)}/full-scans?${queryToSearchParams(queryParams)}`,
740761
createRequestBodyForFilepaths(absFilepaths, basePath),
741762
this.#reqOptions
742763
)
@@ -749,14 +770,14 @@ export class SocketSdk {
749770

750771
async createOrgRepo(
751772
orgSlug: string,
752-
params: Record<string, string>
773+
queryParams: Record<string, string>
753774
): Promise<SocketSdkResult<'createOrgRepo'>> {
754775
try {
755776
const data = await getResponseJson(
756777
await createPostRequest(
757778
this.#baseUrl,
758779
`orgs/${encodeURIComponent(orgSlug)}/repos`,
759-
params,
780+
queryParams,
760781
this.#reqOptions
761782
)
762783
)
@@ -838,7 +859,7 @@ export class SocketSdk {
838859
const data = await getResponseJson(
839860
await createGetRequest(
840861
this.#baseUrl,
841-
`orgs/${encodeURIComponent(orgSlug)}/audit-log?${new URLSearchParams(queryParams ?? '')}`,
862+
`orgs/${encodeURIComponent(orgSlug)}/audit-log?${queryToSearchParams(queryParams)}`,
842863
this.#reqOptions
843864
)
844865
)
@@ -929,7 +950,7 @@ export class SocketSdk {
929950
const data = await getResponseJson(
930951
await createGetRequest(
931952
this.#baseUrl,
932-
`orgs/${encodeURIComponent(orgSlug)}/full-scans?${new URLSearchParams(queryParams ?? '')}`,
953+
`orgs/${encodeURIComponent(orgSlug)}/full-scans?${queryToSearchParams(queryParams)}`,
933954
this.#reqOptions
934955
)
935956
)
@@ -1003,7 +1024,7 @@ export class SocketSdk {
10031024
const data = await getResponseJson(
10041025
await createGetRequest(
10051026
this.#baseUrl,
1006-
`orgs/${encodeURIComponent(orgSlug)}/repos?${new URLSearchParams(queryParams ?? '')}`,
1027+
`orgs/${encodeURIComponent(orgSlug)}/repos?${queryToSearchParams(queryParams)}`,
10071028
this.#reqOptions
10081029
)
10091030
)
@@ -1139,14 +1160,14 @@ export class SocketSdk {
11391160
}
11401161

11411162
async searchDependencies(
1142-
params: Record<string, number>
1163+
queryParams: Record<string, number>
11431164
): Promise<SocketSdkResult<'searchDependencies'>> {
11441165
try {
11451166
const data = await getResponseJson(
11461167
await createPostRequest(
11471168
this.#baseUrl,
11481169
'dependencies/search',
1149-
params,
1170+
queryParams,
11501171
this.#reqOptions
11511172
)
11521173
)
@@ -1159,14 +1180,14 @@ export class SocketSdk {
11591180
async updateOrgRepo(
11601181
orgSlug: string,
11611182
repoSlug: string,
1162-
params: Record<string, string>
1183+
queryParams: Record<string, string>
11631184
): Promise<SocketSdkResult<'updateOrgRepo'>> {
11641185
try {
11651186
const data = await getResponseJson(
11661187
await createPostRequest(
11671188
this.#baseUrl,
11681189
`orgs/${encodeURIComponent(orgSlug)}/repos/${encodeURIComponent(repoSlug)}`,
1169-
params,
1190+
queryParams,
11701191
this.#reqOptions
11711192
)
11721193
)

0 commit comments

Comments
 (0)