npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snyk-request-manager

v1.8.6

Published

Rate controlled and retry enabled request manager to interact with Snyk APIs

Downloads

123,930

Readme

Snyk logo


Known Vulnerabilities Actively Maintained

Snyk helps you find, fix and monitor for known vulnerabilities in your dependencies, both on an ad hoc basis and as part of your CI (Build) system.

Snyk snyk-request-manager

Rate controlled and retry enabled request manager to interact with Snyk APIs.
No matter with request mode you decide to use, using the same client will ensure all requests are funneled through a leaky bucket style queue allowing specific burst and interval of requests.

Specific your queue settings in the constructor.

Failed requests will be put back into queue for retry till maximum number of attempts has been reached, in which case error will be thrown.

Installation

npm install snyk-request-manager

Usage

Check out available endpoints here: https://snyk.docs.apiary.io/#reference.
Any url used below omits the API base (https://snyk.io/api/v1). Example for base documentation endpoint:

  • GET request on https://snyk.io/api/v1/
  • await requestManager.request({verb: "GET", url: '/'})

0 - Setup your Snyk details if not already done

Following the same setup as snyk CLI, it uses the token stored in your system after a snyk auth or defined via env var SNYK_TOKEN.
Same thing if you need to designate a different API base url to your onprem instance via snyk config set endpoint or SNYK_API to https://yourhostname/api

Make sure to omit the base endpoint url when you define url to hit.

1 - Construct your manager

const requestManager = new requestsManager()

Default values if using new requestsManager():

snykToken = '', burstSize = 10, period = 500, maxRetryCount = 5

2 - Single shot request

Fire off your request and await it's result:

import { requestsManager } from 'snyk-request-manager';

const run = async () => {
    const requestManager = new requestsManager();

    // Fire off single shot request
    try {
    let requestSync = await requestManager.request({verb: "GET", url: '/url'})
    console.log(requestSync.data)
    } catch (err) {
        console.log(err)
    }
}

run()

3 - Bulk requests burst

Fire off you array of requests, await for all of them to complete to receive results in an Array in the same order. If some requests fails, retrieve the results in the catch, requests completed successfully will have the results.

import { requestsManager } from 'snyk-request-manager';

const run = async () => {
    const requestManager = new requestsManager();

    // Fire off single shot request
    try {
    let requestSync = await requestManager.request({verb: "GET", url: '/url'})
    console.log(requestSync.data)
    } catch (err) {
        console.log(err)
    }

    // Fire off multiple requests async/await
    const filters = `{
        "filters": {
            "severities": [
                "critical",
                "high",
                "medium",
                "low"
            ],
            "exploitMaturity": [
                "mature",
                "proof-of-concept",
                "no-known-exploit",
                "no-data"
            ],
            "types": [
                "vuln",
                "license"
            ],
            "ignored": false
        }
    }
    `
    try {
    const results = await requestManager.requestBulk([
        {verb: "GET", url: '/'},
        {verb: "POST", url: '/org/:orgID/project/:projectId/issues', body: filters },
        {verb: "GET", url: '/user/:id'}])
    console.log(results)
    } catch(resultsWithError) {
        console.log(resultsWithError)
    }
}

run()

4 - Stream mode requests

Define you listeners data and error to listen on your channel only. Define as many listeners as needed to use multiple parallel streams. If not defining custom channel name, default channel name is used in the backend stream

requestManager.on('data', {
    callback:(requestId, data) => {
        console.log("response for request on test-channel ", requestId)
        console.log(data.data)
    },
    channel: 'test-channel'
})

try {
requestManager.requestStream({verb: "GET", url: '/user/:id'}))
requestManager.requestStream({verb: "GET", url: '/'}, 'test-channel')
} catch (err) {
    console.log(err)
}

Above will only show result of call to / as listener is only for 'test-channel'

Customize queue/retry values and or snyk token

While instantiating your manager:

Customize queue size and intervals

const requestManager = new requestsManager({burstSize: 20, period: 100, maxRetryCount: 10})

Customize snyk token

const requestManager = new requestsManager({snykToken:'21346-1234-1234-1234')

Customize to use REST api endpoint

Each request can be opted in to use the new REST Snyk API, which defaults to 'https://api.snyk.io/rest/' and is automatically calculated from the SNYK_API or endpoint configuration by reusing the same host.

const res = await requestManager.request({verb: "GET", url: '/url', useRESTApi: true})

Customize snyk token and queue|intervals|retries

const requestManager = new requestsManager({snykToken:'21346-1234-1234-1234', burstSize: 20, period: 100, maxRetryCount: 10})