Skip to content

Commit

Permalink
Merge pull request #209 from SocketDev/cg/uploadDependencies
Browse files Browse the repository at this point in the history
Add dependencies upload
  • Loading branch information
charliegerard authored Jul 31, 2024
2 parents 74c5fc3 + e38c460 commit 3a41357
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,48 @@ class SocketSdk {
}
}

/**
* @param {{[key: string]: string }} params
* @param {string[]} filePaths
* @param {string} pathsRelativeTo
* @returns {Promise<SocketSdkResultType<'createDependenciesSnapshot'>>}
*/
async createDependenciesSnapshot (params, filePaths, pathsRelativeTo = '.') {
const basePath = path.resolve(process.cwd(), pathsRelativeTo)
const absoluteFilePaths = filePaths.map(filePath => path.resolve(basePath, filePath))
const formattedQueryParams = new URLSearchParams(params)

const [
{ FormData },
{ fileFromPath },
client
] = await Promise.all([
import('formdata-node'),
import('formdata-node/file-from-path'),
this.#getClient(),
])

const body = new FormData()

const files = await Promise.all(absoluteFilePaths.map(absoluteFilePath => fileFromPath(absoluteFilePath)))

for (let i = 0, length = files.length; i < length; i++) {
const absoluteFilePath = absoluteFilePaths[i]
if (absoluteFilePath) {
const relativeFilePath = path.relative(basePath, absoluteFilePath)
body.set(relativeFilePath, files[i])
}
}

try {
const data = await client.post(`dependencies/upload?${formattedQueryParams}`, { body }).json()

return { success: true, status: 200, data }
} catch (err) {
return /** @type {SocketSdkErrorType<'createDependenciesSnapshot'>} */ (this.#handleApiError(err))
}
}

/**
* @param {Array<{ organization?: string }>} selectors
* @returns {Promise<SocketSdkResultType<'postSettings'>>}
Expand Down

0 comments on commit 3a41357

Please sign in to comment.