Giter Site home page Giter Site logo

stjordanis / code-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snyk/code-client

0.0 1.0 0.0 1.09 MB

Typescript consumer of public API

License: MIT License

JavaScript 20.62% TypeScript 76.37% Java 2.23% C++ 0.19% Dockerfile 0.60%

code-client's Introduction

code-client

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

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

Errors

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

const { error, statusCode, statusText } = result;

code-client's People

Contributors

arvi3d avatar shinin-light avatar j-sp4 avatar daminort avatar artursnyk avatar kliuchyk avatar orkamara avatar pkey avatar saark-snyk avatar snyk-bot avatar neonnoon avatar dependabot[bot] avatar novalex avatar solal-snyk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.