@basenjs/base-http 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,10 +22,13 @@ class BaseHttp extends base.BaseObject {
22
22
  });
23
23
  }
24
24
  static buildOptions(options) {
25
+ options = typeof options === 'string' ? new URL(options) : options;
26
+ // console.debug('buildOptions:');
27
+ // console.debug(options);
25
28
  let headers = options.headers || {}, r = {
29
+ protocol: options.protocol || 'https:',
26
30
  hostname: options.hostname || 'localhost',
27
31
  path: options.path || '/',
28
- port: options.port || (options.protocol === 'https' ? 443 : 80),
29
32
  method: options.method || 'GET',
30
33
  headers: {
31
34
  'Content-Type': headers['Content-Type'] || 'application/json',
@@ -35,6 +38,7 @@ class BaseHttp extends base.BaseObject {
35
38
  ...headers
36
39
  }
37
40
  };
41
+ headers.port = (options.protocol === 'https:' ? 443 : 80);
38
42
  return r;
39
43
  }
40
44
  static head(options) {
@@ -64,9 +68,9 @@ class BaseHttp extends base.BaseObject {
64
68
  static async request(options, body = null, callbacks = { request: () => { }, data: () => { }, end: () => { } }) {
65
69
  var buffer = Buffer.alloc(0);
66
70
  const req = https.request(options, (res) => {
67
- console.log('<p>https://' + options.hostname + options.path + '</p>');
68
- console.log(`<p>STATUS: ${res.statusCode}</p>`);
69
- console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
71
+ // console.log('<p>https://' + options.hostname + options.path + '</p>');
72
+ // console.log(`<p>STATUS: ${res.statusCode}</p>`);
73
+ // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
70
74
  res.setEncoding('utf8');
71
75
  callbacks.request(res);
72
76
  res.on('data', (chunk) => {
@@ -20,10 +20,13 @@ class BaseHttp extends BaseObject {
20
20
  });
21
21
  }
22
22
  static buildOptions(options) {
23
+ options = typeof options === 'string' ? new URL(options) : options;
24
+ // console.debug('buildOptions:');
25
+ // console.debug(options);
23
26
  let headers = options.headers || {}, r = {
27
+ protocol: options.protocol || 'https:',
24
28
  hostname: options.hostname || 'localhost',
25
29
  path: options.path || '/',
26
- port: options.port || (options.protocol === 'https' ? 443 : 80),
27
30
  method: options.method || 'GET',
28
31
  headers: {
29
32
  'Content-Type': headers['Content-Type'] || 'application/json',
@@ -33,6 +36,7 @@ class BaseHttp extends BaseObject {
33
36
  ...headers
34
37
  }
35
38
  };
39
+ headers.port = (options.protocol === 'https:' ? 443 : 80);
36
40
  return r;
37
41
  }
38
42
  static head(options) {
@@ -62,9 +66,9 @@ class BaseHttp extends BaseObject {
62
66
  static async request(options, body = null, callbacks = { request: () => { }, data: () => { }, end: () => { } }) {
63
67
  var buffer = Buffer.alloc(0);
64
68
  const req = https.request(options, (res) => {
65
- console.log('<p>https://' + options.hostname + options.path + '</p>');
66
- console.log(`<p>STATUS: ${res.statusCode}</p>`);
67
- console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
69
+ // console.log('<p>https://' + options.hostname + options.path + '</p>');
70
+ // console.log(`<p>STATUS: ${res.statusCode}</p>`);
71
+ // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
68
72
  res.setEncoding('utf8');
69
73
  callbacks.request(res);
70
74
  res.on('data', (chunk) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basenjs/base-http",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "private": false,
5
5
  "description": "A base HTTP library for Node.js projects.",
6
6
  "type": "module",
@@ -23,11 +23,16 @@ export class BaseHttp extends BaseObject {
23
23
  }
24
24
 
25
25
  public static buildOptions(options: http.RequestOptions | any | Url): http.RequestOptions {
26
+ options = typeof options === 'string' ? new URL(options) : options;
27
+
28
+ // console.debug('buildOptions:');
29
+ // console.debug(options);
30
+
26
31
  let headers = options.headers || {},
27
32
  r : http.RequestOptions = {
33
+ protocol: options.protocol || 'https:',
28
34
  hostname: options.hostname || 'localhost',
29
35
  path: options.path || '/',
30
- port: options.port || (options.protocol==='https' ? 443 : 80),
31
36
  method: options.method || 'GET',
32
37
  headers: {
33
38
  'Content-Type': headers['Content-Type'] || 'application/json',
@@ -38,6 +43,8 @@ export class BaseHttp extends BaseObject {
38
43
  }
39
44
  };
40
45
 
46
+ headers.port = (options.protocol === 'https:' ? 443 : 80);
47
+
41
48
  return r;
42
49
  }
43
50
 
@@ -77,9 +84,9 @@ export class BaseHttp extends BaseObject {
77
84
  buffer: Buffer = Buffer.alloc(0);
78
85
 
79
86
  const req = https.request(options, (res) => {
80
- console.log('<p>https://' + options.hostname + options.path + '</p>');
81
- console.log(`<p>STATUS: ${res.statusCode}</p>`);
82
- console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
87
+ // console.log('<p>https://' + options.hostname + options.path + '</p>');
88
+ // console.log(`<p>STATUS: ${res.statusCode}</p>`);
89
+ // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
83
90
 
84
91
  res.setEncoding('utf8');
85
92