Skip to content

Commit db388ca

Browse files
author
Danny McCormick
authored
Enable web usage without webpack (#153)
1 parent 5eb9c90 commit db388ca

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

lib/HttpClient.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import url = require("url");
55
import http = require("http");
66
import https = require("https");
7-
import tunnel = require("tunnel");
87
import ifm = require('./Interfaces');
9-
import fs = require('fs');
8+
let fs: any;
9+
let tunnel: any;
1010

1111
export enum HttpCodes {
1212
OK = 200,
@@ -122,17 +122,22 @@ export class HttpClient implements ifm.IHttpClient {
122122

123123
this._certConfig = requestOptions.cert;
124124

125-
// cache the cert content into memory, so we don't have to read it from disk every time
126-
if (this._certConfig && this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
127-
this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
128-
}
129-
130-
if (this._certConfig && this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
131-
this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
132-
}
125+
if (this._certConfig) {
126+
// If using cert, need fs
127+
fs = require('fs');
133128

134-
if (this._certConfig && this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
135-
this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
129+
// cache the cert content into memory, so we don't have to read it from disk every time
130+
if (this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
131+
this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
132+
}
133+
134+
if (this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
135+
this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
136+
}
137+
138+
if (this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
139+
this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
140+
}
136141
}
137142

138143
if (requestOptions.allowRedirects != null) {
@@ -437,7 +442,12 @@ export class HttpClient implements ifm.IHttpClient {
437442
}
438443

439444
if (useProxy) {
440-
const agentOptions: tunnel.TunnelOptions = {
445+
// If using proxy, need tunnel
446+
if (!tunnel) {
447+
tunnel = require('tunnel');
448+
}
449+
450+
const agentOptions = {
441451
maxSockets: maxSockets,
442452
keepAlive: this._keepAlive,
443453
proxy: {

package-lock.json

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typed-rest-client",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "Node Rest and Http Clients for use with TypeScript",
55
"main": "./RestClient.js",
66
"scripts": {

0 commit comments

Comments
 (0)