Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: typescriptify request.js
  • Loading branch information
Konstantin Yegupov committed Jun 11, 2019
1 parent 0babc68 commit a75c021
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -52,6 +52,7 @@
"dependencies": {
"@snyk/dep-graph": "1.4.1",
"@snyk/gemfile": "1.2.0",
"@types/agent-base": "^4.2.0",
"abbrev": "^1.1.1",
"ansi-escapes": "^4.1.0",
"chalk": "^2.4.2",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/args.ts
Expand Up @@ -2,7 +2,7 @@ import * as abbrev from 'abbrev';

import debugModule = require('debug');

declare interface Global extends NodeJS.Global {
export declare interface Global extends NodeJS.Global {
ignoreUnknownCA: boolean;
}

Expand Down
74 changes: 38 additions & 36 deletions src/lib/request/request.js → src/lib/request/request.ts
@@ -1,30 +1,32 @@
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
/* jshint camelcase: false */
module.exports = makeRequest;

var debug = require('debug')('snyk:req');
var snykDebug = require('debug')('snyk');
var needle = require('needle');
var parse = require('url').parse;
var format = require('url').format;
var querystring = require('querystring');
var zlib = require('zlib');
var config = require('../config');
var getProxyForUrl = require('proxy-from-env').getProxyForUrl;
var ProxyAgent = require('proxy-agent');
var analytics = require('../analytics');

function makeRequest(payload) {
return new Promise(function (resolve, reject) {
var body = payload.body;
var data;
import { debug as debugModule } from 'debug';
import * as needle from 'needle';
import { parse, format } from 'url';
import * as querystring from 'querystring';
import * as zlib from 'zlib';
import * as config from '../config';
import { getProxyForUrl } from 'proxy-from-env';
import * as ProxyAgent from 'proxy-agent';
import * as analytics from '../analytics';
import { Agent } from 'http';
import { Global } from '../../cli/args';
import { Payload } from './types';

const debug = debugModule('snyk:req');
const snykDebug = debugModule('snyk');

declare const global: Global;

export = function makeRequest(payload: Payload) {
return new Promise((resolve, reject) => {
const body = payload.body;
let data;

delete payload.body;

if (body) {
var json = JSON.stringify(body);
const json = JSON.stringify(body);
if (json.length < 1e4) {
debug(JSON.stringify(body, '', 2));
debug(JSON.stringify(body, null, 2));
}

// always compress going upstream
Expand All @@ -46,12 +48,12 @@ function makeRequest(payload) {
payload.headers['content-length'] = data.length;
}

var url = parse(payload.url);
const parsedUrl = parse(payload.url);

if (url.protocol === 'http:' && url.hostname !== 'localhost') {
if (parsedUrl.protocol === 'http:' && parsedUrl.hostname !== 'localhost') {
debug('forcing api request to https');
url.protocol = 'https:';
payload.url = format(url);
parsedUrl.protocol = 'https:';
payload.url = format(parsedUrl);
}

// prefer config timeout unless payload specified
Expand All @@ -61,25 +63,25 @@ function makeRequest(payload) {

debug('request payload: ', JSON.stringify(payload));

var method = (payload.method || 'get').toLowerCase();
url = payload.url;
const method = (payload.method || 'get').toLowerCase() as needle.NeedleHttpVerbs;
let url = payload.url;

if (payload.qs) {
url = url + '?' + querystring.stringify(payload.qs);
delete payload.qs;
}

var options = {
const options: needle.NeedleOptions = {
json: payload.json,
headers: payload.headers,
timeout: payload.timeout,
'follow_max': 5,
follow_max: 5,
};

var proxyUri = getProxyForUrl(url);
const proxyUri = getProxyForUrl(url);
if (proxyUri) {
snykDebug('using proxy:', proxyUri);
options.agent = new ProxyAgent(proxyUri);
options.agent = new ProxyAgent(proxyUri) as unknown as Agent;
} else {
snykDebug('not using proxy');
}
Expand All @@ -89,14 +91,14 @@ function makeRequest(payload) {
options.rejectUnauthorized = false;
}

needle.request(method, url, data, options, function (err, res, body) {
needle.request(method, url, data, options, (err, res, respBody) => {
debug(err);
debug('response (%s): ', (res || {}).statusCode, JSON.stringify(body));
debug('response (%s): ', (res || {}).statusCode, JSON.stringify(respBody));
if (err) {
return reject(err);
}

resolve({res: res, body: body});
resolve({res, body: respBody});
});
});
}
};
12 changes: 12 additions & 0 deletions src/lib/request/types.ts
@@ -0,0 +1,12 @@
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
import { NeedleHttpVerbs } from 'needle';

export interface Payload {
body: any;
url: string;
headers: OutgoingHttpHeaders;
method: NeedleHttpVerbs;
qs?: {};
json?: boolean;
timeout?: number;
}

0 comments on commit a75c021

Please sign in to comment.