Skip to content

Commit 3a41357

Browse files
Merge pull request #209 from SocketDev/cg/uploadDependencies
Add dependencies upload
2 parents 74c5fc3 + e38c460 commit 3a41357

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,48 @@ class SocketSdk {
529529
}
530530
}
531531

532+
/**
533+
* @param {{[key: string]: string }} params
534+
* @param {string[]} filePaths
535+
* @param {string} pathsRelativeTo
536+
* @returns {Promise<SocketSdkResultType<'createDependenciesSnapshot'>>}
537+
*/
538+
async createDependenciesSnapshot (params, filePaths, pathsRelativeTo = '.') {
539+
const basePath = path.resolve(process.cwd(), pathsRelativeTo)
540+
const absoluteFilePaths = filePaths.map(filePath => path.resolve(basePath, filePath))
541+
const formattedQueryParams = new URLSearchParams(params)
542+
543+
const [
544+
{ FormData },
545+
{ fileFromPath },
546+
client
547+
] = await Promise.all([
548+
import('formdata-node'),
549+
import('formdata-node/file-from-path'),
550+
this.#getClient(),
551+
])
552+
553+
const body = new FormData()
554+
555+
const files = await Promise.all(absoluteFilePaths.map(absoluteFilePath => fileFromPath(absoluteFilePath)))
556+
557+
for (let i = 0, length = files.length; i < length; i++) {
558+
const absoluteFilePath = absoluteFilePaths[i]
559+
if (absoluteFilePath) {
560+
const relativeFilePath = path.relative(basePath, absoluteFilePath)
561+
body.set(relativeFilePath, files[i])
562+
}
563+
}
564+
565+
try {
566+
const data = await client.post(`dependencies/upload?${formattedQueryParams}`, { body }).json()
567+
568+
return { success: true, status: 200, data }
569+
} catch (err) {
570+
return /** @type {SocketSdkErrorType<'createDependenciesSnapshot'>} */ (this.#handleApiError(err))
571+
}
572+
}
573+
532574
/**
533575
* @param {Array<{ organization?: string }>} selectors
534576
* @returns {Promise<SocketSdkResultType<'postSettings'>>}

0 commit comments

Comments
 (0)