code-client
Typescript consumer of public API
Installation
$ npm install --save @snyk/code-client
Usage
Creates and initializes an instance
import codeClient from '@snyk/code-client';
const baseURL = 'https://www.snyk.io';
Requests the creation of a new login session
const loginResponse = await codeClient.startSession({
baseURL,
source: 'atom',
});
if (loginResponse.type === 'error') {
}
const { sessionToken, loginURL } = loginResponse.value;
Checks status of the login process
const sessionResponse = await codeClient.checkSession({ baseURL, sessionToken });
if (sessionResponse.type === 'error') {
}
const isLoggedIn = sessionResponse.value;
Subscribe to events.
codeClient.emitter.on('scanFilesProgress', (processed: number) = {
console.log(`Indexed ${processed} files`);
});
codeClient.emitter.on('uploadBundleProgress', (processed: number, total: number) => {
console.log(`Upload bundle progress: ${processed}/${total}`);
});
codeClient.emitter.on('sendError', error => {
console.log(error);
});
codeClient.emitter.on('apiRequestLog', (message) => {
console.log(message);
});
Complete list of events:
- supportedFilesLoaded: uploading supported file extensions, can be also used for instantiating file watcher
- scanFilesProgress: emits a number of files being found
- createBundleProgress: emits a progress in instantiating packages for analysis
- uploadBundleProgress: emits a progress in uploading files
- analyseProgress: emits a progress in analysis job
- error: emits in case of an error
Run analysis
const bundle = await codeClient.analyzeFolders({
baseURL,
sessionToken,
includeLint: false,
severity: 1,
paths: ['/home/user/repo'],
sarif,
source,
});
Creates a new bundle based on a previously uploaded one
const result = await codeClient.extendBundle({
sessionToken,
bundleId,
files: {
'/home/user/repo/main.js': '3e297985...',
'/home/user/repo/app.js': 'c8bc6452...',
},
removedFiles: [],
});
const { bundleId, missingFiles, uploadURL } = result;
Run analysis of remote git repository
const bundle = await analyzeGit({
baseURL,
sessionToken,
includeLint: false,
severity: 1,
gitUri: 'git@github.com:DeepCodeAI/cli.git@320d98a6896f5376efe6cefefb6e70b46b97d566',
sarif: true,
source,
});
Errors
If there are any errors the result of every call will contain the following:
const { error, statusCode, statusText } = result;