Skip to content

Commit f9080a5

Browse files
committed
Add uploadManifestFiles and aliases
1 parent 9838454 commit f9080a5

File tree

1 file changed

+83
-34
lines changed

1 file changed

+83
-34
lines changed

src/index.ts

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ export interface SocketSdkOptions {
7272
userAgent?: string | undefined
7373
}
7474

75+
export type UploadManifestFilesResponse = {
76+
tarHash: string
77+
unmatchedFiles: string[]
78+
}
79+
80+
export type UploadManifestFilesReturnType = {
81+
success: true
82+
status: 200
83+
data: UploadManifestFilesResponse
84+
}
85+
86+
export type UploadManifestFilesError = {
87+
success: false
88+
status: number
89+
error: string
90+
cause: string | undefined
91+
}
92+
7593
const DEFAULT_USER_AGENT = createUserAgentFromPkgJson(rootPkgJson)
7694

7795
class ResponseError extends Error {
@@ -317,6 +335,15 @@ async function getErrorResponseBody(
317335
}
318336
}
319337

338+
function desc(value: any) {
339+
return {
340+
__proto__: null,
341+
configurable: true,
342+
value,
343+
writable: true
344+
} as PropertyDescriptor
345+
}
346+
320347
function getHttpModule(baseUrl: string): typeof http | typeof https {
321348
const { protocol } = new URL(baseUrl)
322349
return protocol === 'https:' ? https : http
@@ -377,6 +404,23 @@ function isResponseOk(response: IncomingMessage): boolean {
377404
)
378405
}
379406

407+
function promiseWithResolvers<T>(): ReturnType<
408+
typeof Promise.withResolvers<T>
409+
> {
410+
if (Promise.withResolvers) {
411+
return Promise.withResolvers<T>()
412+
}
413+
414+
// This is what the above does but it's not available in node 20 (it is in node 22)
415+
// @ts-ignore -- sigh.
416+
const obj: ReturnType<typeof Promise.withResolvers<T>> = {}
417+
obj.promise = new Promise<T>((resolver, reject) => {
418+
obj.resolve = resolver
419+
obj.reject = reject
420+
})
421+
return obj
422+
}
423+
380424
function resolveAbsPaths(
381425
filepaths: string[],
382426
pathsRelativeTo?: string
@@ -512,7 +556,7 @@ export class SocketSdk {
512556
// The error payload may give a meaningful hint as to what went wrong.
513557
const bodyStr = await getErrorResponseBody(error.response)
514558
// Try to parse the body as JSON, fallback to treating as plain text.
515-
let body
559+
let body: string | undefined
516560
try {
517561
const parsed = JSON.parse(bodyStr)
518562
// A 400 should return an actionable message.
@@ -724,7 +768,7 @@ export class SocketSdk {
724768
}
725769
}
726770

727-
async createReportFromFilepaths(
771+
async createScanFromFilepaths(
728772
filepaths: string[],
729773
pathsRelativeTo: string = '.',
730774
issueRules?: Record<string, boolean>
@@ -752,19 +796,6 @@ export class SocketSdk {
752796
}
753797
}
754798

755-
// Alias to preserve backwards compatibility.
756-
async createReportFromFilePaths(
757-
filepaths: string[],
758-
pathsRelativeTo: string = '.',
759-
issueRules?: Record<string, boolean>
760-
): Promise<SocketSdkResultType<'createReport'>> {
761-
return await this.createReportFromFilepaths(
762-
filepaths,
763-
pathsRelativeTo,
764-
issueRules
765-
)
766-
}
767-
768799
async deleteOrgFullScan(
769800
orgSlug: string,
770801
fullScanId: string
@@ -1030,7 +1061,7 @@ export class SocketSdk {
10301061
}
10311062
}
10321063

1033-
async getReport(id: string): Promise<SocketSdkResultType<'getReport'>> {
1064+
async getScan(id: string): Promise<SocketSdkResultType<'getReport'>> {
10341065
try {
10351066
const data = await getResponseJson(
10361067
await createGetRequest(
@@ -1045,7 +1076,7 @@ export class SocketSdk {
10451076
}
10461077
}
10471078

1048-
async getReportList(): Promise<SocketSdkResultType<'getReportList'>> {
1079+
async getScanList(): Promise<SocketSdkResultType<'getReportList'>> {
10491080
try {
10501081
const data = await getResponseJson(
10511082
await createGetRequest(this.#baseUrl, 'report/list', this.#reqOptions)
@@ -1056,7 +1087,7 @@ export class SocketSdk {
10561087
}
10571088
}
10581089

1059-
async getReportSupportedFiles(): Promise<
1090+
async getSupportedScanFiles(): Promise<
10601091
SocketSdkResultType<'getReportSupportedFiles'>
10611092
> {
10621093
try {
@@ -1073,7 +1104,7 @@ export class SocketSdk {
10731104
}
10741105
}
10751106

1076-
async getScoreByNPMPackage(
1107+
async getScoreByNpmPackage(
10771108
pkgName: string,
10781109
version: string
10791110
): Promise<SocketSdkResultType<'getScoreByNPMPackage'>> {
@@ -1146,21 +1177,39 @@ export class SocketSdk {
11461177
return await this.#handleApiError<'updateOrgRepo'>(e)
11471178
}
11481179
}
1149-
}
11501180

1151-
function promiseWithResolvers<T>(): ReturnType<
1152-
typeof Promise.withResolvers<T>
1153-
> {
1154-
if (Promise.withResolvers) {
1155-
return Promise.withResolvers<T>()
1181+
async uploadManifestFiles(
1182+
orgSlug: string,
1183+
filepaths: string[],
1184+
pathsRelativeTo: string = '.'
1185+
): Promise<UploadManifestFilesReturnType | UploadManifestFilesError> {
1186+
const basePath = resolveBasePath(pathsRelativeTo)
1187+
const absFilepaths = resolveAbsPaths(filepaths, basePath)
1188+
try {
1189+
const data = await getResponseJson(
1190+
await createUploadRequest(
1191+
this.#baseUrl,
1192+
`orgs/${encodeURIComponent(orgSlug)}/upload-manifest-files`,
1193+
createRequestBodyForFilepaths(absFilepaths, basePath),
1194+
this.#reqOptions
1195+
)
1196+
)
1197+
return this.#handleApiSuccess<any>(
1198+
data
1199+
) as unknown as UploadManifestFilesReturnType
1200+
} catch (e) {
1201+
return (await this.#handleApiError<any>(
1202+
e
1203+
)) as unknown as UploadManifestFilesError
1204+
}
11561205
}
1157-
1158-
// This is what the above does but it's not available in node 20 (it is in node 22)
1159-
// @ts-ignore -- sigh.
1160-
const obj: ReturnType<typeof Promise.withResolvers<T>> = {}
1161-
obj.promise = new Promise<T>((resolver, reject) => {
1162-
obj.resolve = resolver
1163-
obj.reject = reject
1164-
})
1165-
return obj
11661206
}
1207+
// Add aliases.
1208+
Object.defineProperties(SocketSdk.prototype, {
1209+
createReportFromFilepaths: desc(SocketSdk.prototype.createScanFromFilepaths),
1210+
createReportFromFilePaths: desc(SocketSdk.prototype.createScanFromFilepaths),
1211+
getReport: desc(SocketSdk.prototype.getScan),
1212+
getReportList: desc(SocketSdk.prototype.getScanList),
1213+
getReportSupportedFiles: desc(SocketSdk.prototype.getSupportedScanFiles),
1214+
getScoreByNPMPackage: desc(SocketSdk.prototype.getScoreByNpmPackage)
1215+
})

0 commit comments

Comments
 (0)