Skip to content

Commit

Permalink
Merge pull request #2115 from snyk/refactor/use-named-export-for-requ…
Browse files Browse the repository at this point in the history
…est-module

Use named export in request module
  • Loading branch information
maxjeffos committed Jul 28, 2021
2 parents f0d86d3 + fe97014 commit a235a72
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 206 deletions.
6 changes: 3 additions & 3 deletions src/cli/commands/auth/index.ts
Expand Up @@ -9,7 +9,7 @@ import { isCI } from '../../../lib/is-ci';
import { isDocker } from '../../../lib/is-docker';
import { args as argsLib } from '../../args';
import * as config from '../../../lib/config';
import request = require('../../../lib/request');
import { makeRequest } from '../../../lib/request';
import { CustomError } from '../../../lib/errors';
import { AuthFailedError } from '../../../lib/errors';
import { TokenExpiredError } from '../../../lib/errors/token-expired-error';
Expand Down Expand Up @@ -94,7 +94,7 @@ async function testAuthComplete(

return new Promise((resolve, reject) => {
debug(payload);
request(payload, (error, res, body) => {
makeRequest(payload, (error, res, body) => {
debug(error, (res || {}).statusCode, body);
if (error) {
return reject(error);
Expand Down Expand Up @@ -175,7 +175,7 @@ async function getIpFamily(): Promise<6 | undefined> {
const family = 6;
try {
// Dispatch a FORCED IPv6 request to test client's ISP and network capability
await request({
await makeRequest({
url: config.API + '/verify/callback',
family, // family param forces the handler to dispatch a request using IP at "family" version
method: 'post',
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/auth/is-authed.ts
@@ -1,6 +1,6 @@
import * as snyk from '../../../lib';
import * as config from '../../../lib/config';
import request = require('../../../lib/request');
import { makeRequest } from '../../../lib/request';

export function isAuthed() {
const token = snyk.config.get('api');
Expand All @@ -20,7 +20,7 @@ export function verifyAPI(api) {
};

return new Promise((resolve, reject) => {
request(payload, (error, res, body) => {
makeRequest(payload, (error, res, body) => {
if (error) {
return reject(error);
}
Expand Down
Expand Up @@ -3,7 +3,7 @@ import { Payload } from '../../../../../lib/snyk-test/types';
import * as config from '../../../../../lib/config';
import { isCI } from '../../../../../lib/is-ci';
import { api } from '../../../../../lib/api-token';
import request = require('../../../../../lib/request');
import { makeRequest } from '../../../../../lib/request';
import { CustomError } from '../../../../../lib/errors';
import { getErrorStringCode } from '../error-utils';

Expand All @@ -22,7 +22,7 @@ export function getIacOrgSettings(
};

return new Promise((resolve, reject) => {
request(payload, (error, res) => {
makeRequest(payload, (error, res) => {
if (error) {
return reject(error);
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/test/iac-local-execution/usage-tracking.ts
@@ -1,4 +1,4 @@
import request = require('../../../../lib/request');
import { makeRequest } from '../../../../lib/request';
import config = require('../../../../lib/config');
import { api as getApiToken } from '../../../../lib/api-token';
import { CustomError } from '../../../../lib/errors';
Expand All @@ -12,7 +12,7 @@ export async function trackUsage(
issuesPrevented: res.result.cloudConfigResults.length,
};
});
const trackingResponse = await request({
const trackingResponse = await makeRequest({
method: 'POST',
headers: {
Authorization: `token ${getApiToken()}`,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/analytics/index.ts
Expand Up @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid';
const snyk = require('../../lib');
const config = require('../config');
const version = require('../version');
const request = require('../request');
import { makeRequest } from '../request';
const {
getIntegrationName,
getIntegrationVersion,
Expand Down Expand Up @@ -123,7 +123,7 @@ export async function postAnalytics(
const queryString =
Object.keys(queryStringParams).length > 0 ? queryStringParams : undefined;

return request({
return makeRequest({
body: {
data: data,
},
Expand Down
4 changes: 2 additions & 2 deletions src/lib/authorization.ts
@@ -1,5 +1,5 @@
import * as snyk from './';
import request = require('./request');
import { makeRequest } from './request';
import * as config from './config';

export async function actionAllowed(
Expand All @@ -9,7 +9,7 @@ export async function actionAllowed(
const org = options.org || config.org || null;

try {
const res = await request({
const res = await makeRequest({
method: 'GET',
url: config.API + '/authorization/' + action,
json: true,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/feature-flags.ts
@@ -1,4 +1,4 @@
import request = require('./request');
import { makeRequest } from './request';
import { api as getApiToken } from './api-token';
import * as config from './config';
import { assembleQueryString } from './snyk-test/common';
Expand All @@ -14,7 +14,7 @@ export async function isFeatureFlagSupportedForOrg(
featureFlag: string,
org,
): Promise<OrgFeatureFlagResponse> {
const response = await request({
const response = await makeRequest({
method: 'GET',
headers: {
Authorization: `token ${getApiToken()}`,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/iac/iac-parser.ts
Expand Up @@ -6,7 +6,7 @@ import {
MissingApiTokenError,
NotSupportedIacFileErrorMsg,
} from '../errors';
import request = require('../request');
import { makeRequest } from '../request';
import { api as getApiToken } from '../api-token';
import * as config from './../config';
import {
Expand Down Expand Up @@ -93,7 +93,7 @@ export function validateK8sFile(
export async function makeValidateTerraformRequest(
terraformFileContent: string,
): Promise<IacValidationResponse> {
const response = await request({
const response = await makeRequest({
body: {
contentBase64: Buffer.from(terraformFileContent).toString('base64'),
},
Expand Down
8 changes: 4 additions & 4 deletions src/lib/monitor/index.ts
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
import * as depGraphLib from '@snyk/dep-graph';
import * as snyk from '..';
import { apiOrOAuthTokenExists, getAuthHeader } from '../api-token';
import request = require('../request');
import { makeRequest } from '../request';
import * as config from '../config';
import * as os from 'os';
const get = require('lodash.get');
Expand Down Expand Up @@ -270,7 +270,7 @@ async function monitorDepTree(
),
);
}
request(
makeRequest(
{
body: {
meta: {
Expand Down Expand Up @@ -439,7 +439,7 @@ export async function monitorDepGraph(
),
);
}
request(
makeRequest(
{
body: {
meta: {
Expand Down Expand Up @@ -584,7 +584,7 @@ async function experimentalMonitorDepGraphFromDepTree(
),
);
}
request(
makeRequest(
{
body: {
meta: {
Expand Down
7 changes: 4 additions & 3 deletions src/lib/plugins/sast/checks.ts
@@ -1,4 +1,5 @@
import request = require('../../request');
import { makeRequest } from '../../request';

import { api as getApiToken } from '../../api-token';
import * as config from '../../config';
import { assembleQueryString } from '../../snyk-test/common';
Expand All @@ -15,7 +16,7 @@ interface TrackUsageResponse {
}

export async function getSastSettingsForOrg(org): Promise<SastSettings> {
const response = await request({
const response = await makeRequest({
method: 'GET',
headers: {
Authorization: `token ${getApiToken()}`,
Expand All @@ -30,7 +31,7 @@ export async function getSastSettingsForOrg(org): Promise<SastSettings> {
}

export async function trackUsage(org): Promise<TrackUsageResponse> {
const response = await request({
const response = await makeRequest({
method: 'POST',
headers: {
Authorization: `token ${getApiToken()}`,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/protect/fetch-patch.ts
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as analytics from '../analytics';
import * as debugModule from 'debug';
import request = require('../request');
import { makeRequest } from '../request';

const debug = debugModule('snyk:fetch-patch');

Expand All @@ -10,7 +10,7 @@ async function getPatchFile(
patchFilename: string,
): Promise<string> {
try {
const response = await request({ url: patchUrl });
const response = await makeRequest({ url: patchUrl });
if (
!response ||
!response.res ||
Expand Down
12 changes: 6 additions & 6 deletions src/lib/request/index.ts
@@ -1,24 +1,24 @@
import request = require('./request');
import { makeRequest } from './request';
import alerts = require('../alerts');
import { MetricsCollector } from '../metrics';
import * as needle from 'needle';

// A hybrid async function: both returns a promise and takes a callback
async function requestWrapper(
async function makeRequestWrapper(
payload: any,
): Promise<{ res: needle.NeedleResponse; body: any }>;
async function requestWrapper(
async function makeRequestWrapper(
payload: any,
callback: (err: Error | null, res?, body?) => void,
): Promise<void>;
async function requestWrapper(
async function makeRequestWrapper(
payload: any,
callback?: (err: Error | null, res?, body?) => void,
): Promise<void | { res: needle.NeedleResponse; body: any }> {
const totalNetworkTimeTimer = MetricsCollector.NETWORK_TIME.createInstance();
totalNetworkTimeTimer.start();
try {
const result = await request(payload);
const result = await makeRequest(payload);
if (result.body.alerts) {
alerts.registerAlerts(result.body.alerts);
}
Expand All @@ -37,4 +37,4 @@ async function requestWrapper(
}
}

export = requestWrapper;
export { makeRequestWrapper as makeRequest };
4 changes: 2 additions & 2 deletions src/lib/request/promise.ts
@@ -1,8 +1,8 @@
import request = require('./index');
import * as request from './index';

export async function makeRequest<T>(payload: any): Promise<T> {
return new Promise((resolve, reject) => {
request(payload, (error, res, body) => {
request.makeRequest(payload, (error, res, body) => {
if (error) {
return reject(error);
}
Expand Down

0 comments on commit a235a72

Please sign in to comment.