diff --git a/.snyk b/.snyk deleted file mode 100644 index 34e01e8..0000000 --- a/.snyk +++ /dev/null @@ -1,11 +0,0 @@ -# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities. -version: v1.14.1 -ignore: {} -# patches apply the minimum changes required to fix a vulnerability -patch: - SNYK-JS-LODASH-450202: - - lodash: - patched: '2019-07-05T10:44:37.780Z' - SNYK-JS-LODASH-567746: - - lodash: - patched: '2020-05-21T15:21:13.013Z' diff --git a/README.md b/README.md index 6d0b166..2a71221 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ 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. @@ -15,17 +16,19 @@ 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 there => https://snyk.docs.apiary.io/#reference\ -Any url used below omits the API base (https://snyk.io/api/v1):\ -Example for Base documentation endpoint: +Check out available endpoints here: [https://snyk.docs.apiary.io/#reference](https://snyk.docs.apiary.io/#reference).\ +Any url used below omits the API base ([https://snyk.io/api/v1](https://snyk.io/api/v1)). +Example for base documentation endpoint: * `GET` request on `https://snyk.io/api/v1/` -* await requestManager.request({verb: "GET", url: '/'}) +* `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` @@ -34,55 +37,62 @@ Same thing if you need to designate a different API base url to your onprem inst ### 1 - Construct your manager +```js +const requestManager = new requestsManager() +``` - const requestManager = new requestsManager() - -Default values if using `new requestsManager()`\ - `snykToken = '', burstSize = 10, period = 500, maxRetryCount = 5` - +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'; +Fire off your request and await it's result: - const run = async () => { - const requestManager = new requestsManager(); +```js +import { requestsManager } from 'snyk-request-manager'; - // Fire off single shot request - try{ - let requestSync = await requestManager.request({verb: "GET", url: '/url'}) - console.log(requestSync.data) - } catch (err) { - console.log(err) - } - } +const run = async () => { + const requestManager = new requestsManager(); - run() + // 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'; +```js +import { requestsManager } from 'snyk-request-manager'; - const run = async () => { - const requestManager = new requestsManager(); +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 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 = `{ + // Fire off multiple requests async/await + const filters = `{ "filters": { "severities": [ + "critical", "high", "medium", "low" @@ -98,25 +108,22 @@ If some requests fails, retrieve the results in the catch, requests completed su "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) } - - } + ` + 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() - - +run() +``` ### 4 - Stream mode requests @@ -124,40 +131,44 @@ 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) - } - +```js +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 + +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 snyk token and queue|intervals|retries + ``` const requestManager = new requestsManager({snykToken:'21346-1234-1234-1234', burstSize: 20, period: 100, maxRetryCount: 10}) ``` diff --git a/package.json b/package.json index 1d81d29..9301345 100644 --- a/package.json +++ b/package.json @@ -7,15 +7,14 @@ "format": "prettier --write '{''{lib,test}/!(fixtures)/**/*,*}.{js,ts,json,yml}'", "lint": "npm run format:check && npm run lint:eslint", "lint:eslint": "eslint --cache '{lib,test}/**/*.ts'", - "test": "snyk test && npm run lint && npm run test:unit", + "test": "npm run lint && npm run test:unit", "test:unit": "jest", "test:coverage": "npm run test:unit -- --coverage", "test:watch": "tsc-watch --onSuccess 'npm run test:unit'", "build": "tsc", "build-watch": "tsc -w", - "prepare": "npm run snyk-protect && npm run build", - "snyk-test": "snyk test", - "snyk-protect": "snyk protect" + "prepare": "npm run build", + "snyk-test": "snyk test" }, "types": "./dist/index.d.ts", "repository": { @@ -34,13 +33,13 @@ "homepage": "https://github.com/snyk-tech-services/snyk-request-manager#readme", "dependencies": { "@snyk/configstore": "^3.2.0-rc1", + "@types/debug": "^4.1.7", "@types/uuid": "^7.0.3", "axios": "^0.21.1", "chalk": "^4.0.0", "debug": "^4.1.1", "leaky-bucket-queue": "0.0.2", "lodash": "4.17.21", - "snyk": "^1.323.2", "snyk-config": "^4.0.0", "source-map-support": "^0.5.16", "tslib": "^1.10.0", @@ -71,6 +70,5 @@ "branches": [ "master" ] - }, - "snyk": true + } } diff --git a/src/lib/examples.ts b/src/lib/examples.ts index c195bde..8aee24a 100644 --- a/src/lib/examples.ts +++ b/src/lib/examples.ts @@ -51,6 +51,7 @@ const run = async () => { const filters = `{ "filters": { "severities": [ + "critical", "high", "medium", "low"