Skip to content

Commit

Permalink
fix: typescriptify request and alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Yegupov committed Aug 6, 2019
1 parent efecb07 commit 7bd0260
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/cli/commands/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as open from 'opn';
import * as snyk from '../../../lib';
import * as config from '../../../lib/config';
import {isCI} from '../../../lib/is-ci';
import * as request from '../../../lib/request';
import request = require('../../../lib/request');
import * as url from 'url';
import * as uuid from 'uuid';
import * as spinner from '../../../lib/spinner';
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/auth/is-authed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as snyk from '../../../lib';
import * as config from '../../../lib/config';
import * as request from '../../../lib/request';
import request = require('../../../lib/request');

export function isAuthed() {
const token = snyk.config.get('api');
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import chalk from 'chalk';
import * as pathUtil from 'path';
import * as spinner from '../../lib/spinner';

import * as request from '../../lib/request';
import request = require('../../lib/request');
import * as detect from '../../lib/detect';
import * as plugins from '../../lib/plugins';
import {ModuleInfo} from '../../lib/module-info'; // TODO(kyegupov): fix import
Expand Down
33 changes: 17 additions & 16 deletions src/lib/alerts.js → src/lib/alerts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const chalk = require('chalk');
import chalk from 'chalk';

const registeredAlerts = [];
export type AlertType = 'info' | 'warning' | 'error';

function registerAlerts(alerts) {
export interface Alert {
type: AlertType;
name: string;
msg: string;
}

const registeredAlerts: Alert[] = [];

function registerAlerts(alerts: Alert[]) {
if (!alerts) {
return;
}
Expand All @@ -13,15 +21,8 @@ function registerAlerts(alerts) {
});
}

function hasAlert(name) {
let alertFound = false;
for (let i = 0; i < registeredAlerts.length; i++) {
if (registeredAlerts[i].name === name) {
alertFound = true;
break;
}
}
return alertFound;
function hasAlert(name: string) {
return registeredAlerts.find((a) => a.name === name);
}

function displayAlerts() {
Expand All @@ -39,8 +40,8 @@ function displayAlerts() {
return res;
}

module.exports = {
registerAlerts: registerAlerts,
hasAlert: hasAlert,
displayAlerts: displayAlerts,
export {
registerAlerts,
hasAlert,
displayAlerts,
};
2 changes: 1 addition & 1 deletion src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Debug from 'debug';
import * as depGraphLib from '@snyk/dep-graph';
import * as snyk from '../lib';
import {apiTokenExists} from './api-token';
import * as request from './request';
import request = require('./request');
import * as config from './config';
import * as os from 'os';
import * as _ from 'lodash';
Expand Down
22 changes: 0 additions & 22 deletions src/lib/request/index.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/lib/request/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import request = require('./request');
import alerts = require('../alerts');

// A hybrid async function: both returns a promise and takes a callback
export = async (payload: any, callback?: (err: Error|null, res?, body?) => void) => {
try {
const result = await request(payload);
if (result.body.alerts) {
alerts.registerAlerts(result.body.alerts);
}
// make callbacks and promises work
if (callback) {
callback(null, result.res, result.body);
}
return result;
} catch (error) {
if (callback) {
return callback(error);
}
throw error;
}
};
2 changes: 1 addition & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const snykDebug = debugModule('snyk');

declare const global: Global;

export = function makeRequest(payload: Payload) {
export = function makeRequest(payload: Payload): Promise<{res: needle.NeedleResponse, body: any}> {
return version().then((versionNumber) => (
new Promise((resolve, reject) => {
const body = payload.body;
Expand Down
2 changes: 1 addition & 1 deletion test/alerts.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {test} from 'tap';
import alerts = require('../src/lib/alerts');

const exampleAlert = (id, type = 'info') => {
const exampleAlert = (id, type: alerts.AlertType = 'info') => {
return {
msg: 'Example alert ' + id,
name: 'example-' + id,
Expand Down

0 comments on commit 7bd0260

Please sign in to comment.