4.23.5 • Published 4 months ago

@snyk/code-client v4.23.5

Weekly downloads
2,036
License
MIT
Repository
github
Last release
4 months ago

code-client

Typescript consumer of the Snyk Code public API

npm version npm downloads

This package is published using: semantic-release

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 results = await codeClient.analyzeFolders({
  connection: { baseURL, sessionToken, source },
  analysisOptions: {
    severity: 1,
  },
  fileOptions: {
    paths: ['/home/user/repo'],
    symlinksEnabled: false,
  },
});

Run analysis only for specific file, the one just changed for example

const results = await codeClient.analyzeFolders({
  connection: { baseURL, sessionToken, source },
  analysisOptions: {
    severity: 1,
    limitToFiles: ['recently-changed-file.js'],
  },
  fileOptions: {
    paths: ['/home/user/repo'],
    symlinksEnabled: false,
  },
});

Run analysis and report results to platform

const results = await codeClient.analyzeFolders({
  connection: { baseURL, sessionToken, source },
  analysisOptions: {
    severity: 1,
  },
  fileOptions: {
    paths: ['/home/user/repo'],
    symlinksEnabled: false,
  },
  reportOptions: {
    enabled: true,
    projectName: 'example-project',
  },
});

Creates a new bundle based on a previously uploaded one

const results = await codeClient.extendAnalysis({
  ...previousAnalysisResults,
  files: {
    '/home/user/repo/main.js',
    '/home/user/repo/app.js',
  },
});

Run analysis on an existing SCM project and report results to platform

const results = await codeClient.analyzeScmProject({
  connection: { baseURL, sessionToken, source },
  analysisOptions: {
    severity: 1,
  },
  reportOptions: {
    projectId: '<Snyk Project UUID>',
    commitId: '<Commit SHA to scan>',
  },
});

Errors

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

const { error, statusCode, statusText } = result;

CLI

There is a way to run separate calls using a CLI

Create bundle

Help manifest: time npm run cli -- help bundle:create

Usage: CLI bundle:create [options]

create a new bundle and return its ID with meta info

Options:
  --patterns [string...]     supported file patterns
  --ignore [path...]         ignored path glob
  --path [path...]           source code dir
  --url <url>                service URL
  --token <hash>             user token
  --org <string>             organization
  --source <string>          source identifier (default: "code-client")
  -H, --headers [string...]  custom headers e.g. "X-Custom-Header: some value". Can have multiple values diveded by space
  --debug                    enable debug mode
  -h, --help                 display help for command

Example call:

npm run cli -- bundle:create --url="<service url>" --token="<snyk token>" --headers="<extra>" --patterns=".*" --path="<absolute path>"
4.23.5

4 months ago

4.23.4

5 months ago

4.18.7

10 months ago

4.21.0

8 months ago

4.22.4

6 months ago

4.22.5

6 months ago

4.20.2

9 months ago

4.20.3

9 months ago

4.20.4

9 months ago

4.20.5

8 months ago

4.20.0

9 months ago

4.20.1

9 months ago

4.23.0

6 months ago

4.23.1

5 months ago

4.23.2

5 months ago

4.19.0

10 months ago

4.19.1

9 months ago

4.22.0

8 months ago

4.22.1

7 months ago

4.22.2

7 months ago

4.22.3

7 months ago

4.23.3

5 months ago

4.18.6

10 months ago

4.18.3

12 months ago

4.18.4

12 months ago

4.18.5

11 months ago

4.18.1

1 year ago

4.16.3

1 year ago

4.18.2

1 year ago

4.16.4

1 year ago

4.16.0

1 year ago

4.16.1

1 year ago

4.18.0

1 year ago

4.16.2

1 year ago

4.17.0

1 year ago

4.17.1

1 year ago

4.15.0

1 year ago

4.14.0

1 year ago

4.13.0

2 years ago

4.12.4

2 years ago

4.12.3

2 years ago

4.12.0

2 years ago

4.12.1

2 years ago

4.12.2

2 years ago

4.11.1

2 years ago

4.9.1

2 years ago

4.10.0

2 years ago

4.11.0

2 years ago

4.9.0

2 years ago

4.8.1

2 years ago

4.8.0

2 years ago

4.7.0

2 years ago

4.5.3

2 years ago

4.6.1

2 years ago

4.6.0

2 years ago

4.5.2

2 years ago

4.5.1

2 years ago

4.5.0

2 years ago

4.4.1

2 years ago

4.3.2

3 years ago

4.4.0

3 years ago

4.3.1

3 years ago

4.2.4

3 years ago

4.3.0

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

3.11.0

3 years ago

4.0.0

3 years ago

4.0.1-rc

3 years ago

3.10.0

3 years ago

4.0.0-alpha.0

3 years ago

3.9.0

3 years ago

3.8.1

3 years ago

3.8.0

3 years ago

3.7.0

3 years ago

3.5.3

3 years ago

3.6.0

3 years ago

3.5.2

3 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.5

3 years ago

3.1.4

3 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago