Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@snyk/code-client

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@snyk/code-client

Typescript consumer of SnykCode public API

  • 3.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.5K
decreased by-32.45%
Maintainers
1
Weekly downloads
 
Created
Source

code-client

Typescript consumer of public API

npm version npm downloads

Installation

$ npm install --save @snyk/code-client

Usage

Creates and initializes an instance

import codeClient from '@snyk/code-client';

// An address of server which will be used in order to send code and analyse it.
const baseURL = 'https://www.snyk.io';

Requests the creation of a new login session

const loginResponse = await codeClient.startSession({
  baseURL,
  // An identificator for the editor using the Snyk APIs
  source: 'atom',
});

if (loginResponse.type === 'error') {
  // Handle error and alert user
}

const { sessionToken, loginURL } = loginResponse.value;

Checks status of the login process

const sessionResponse = await codeClient.checkSession({ baseURL, sessionToken });
if (sessionResponse.type === 'error') {
  // Handle error and alert user
}

const isLoggedIn = sessionResponse.value; // boolean

Subscribe to events.

/** Building bundle process started with provided data */
codeClient.emitter.on('scanFilesProgress', (processed: number) = {
  console.log(`Indexed ${processed} files`);
});

/** Bundle upload process is started with provided data */
codeClient.emitter.on('uploadBundleProgress', (processed: number, total: number) => {
  console.log(`Upload bundle progress: ${processed}/${total}`);
});

/** Receives an error object and logs an error message */
codeClient.emitter.on('sendError', error => {
  console.log(error);
});

/** Logs HTTP requests sent to the API **/
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,
});

// bundle implements interface IFileBundle:
//   readonly baseURL: string;
//   readonly sessionToken: string;
//   readonly includeLint: boolean;
//   readonly severity: AnalysisSeverity;
//   readonly bundleId: string;
//   readonly analysisResults: IAnalysisResult;
//   readonly analysisURL: string;
//   readonly baseDir: string;
//   readonly paths: string[];
//   readonly supportedFiles: ISupportedFiles;

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,
});

// bundle implements interface IGitBundle
//   readonly baseURL: string;
//   readonly sessionToken: string;
//   readonly oAuthToken?: string;
//   readonly includeLint: boolean;
//   readonly severity: AnalysisSeverity;
//   readonly bundleId: string;
//   readonly analysisResults: IAnalysisResult;
//   readonly analysisURL: string;
//   readonly sarifResults?: Log;
//   readonly gitUri: string;

Errors

If there are any errors the result of every call will contain the following:

const { error, statusCode, statusText } = result;

Keywords

FAQs

Package last updated on 26 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc