@basenjs/base-http 0.0.4 → 0.0.6

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.
@@ -7,7 +7,8 @@ var qs = require('qs');
7
7
  var multiparty = require('multiparty');
8
8
 
9
9
  class BaseHttpRequestCallback {
10
- _request = (response) => { };
10
+ _request = (request) => { };
11
+ _response = (response) => { };
11
12
  _data = (buffer) => { };
12
13
  _end = (buffer) => { };
13
14
  _error = (buffer) => { };
@@ -17,6 +18,12 @@ class BaseHttpRequestCallback {
17
18
  set request(value) {
18
19
  this._request = value;
19
20
  }
21
+ get response() {
22
+ return this._response;
23
+ }
24
+ set response(value) {
25
+ this._response = value;
26
+ }
20
27
  get data() {
21
28
  return this._data;
22
29
  }
@@ -35,8 +42,9 @@ class BaseHttpRequestCallback {
35
42
  set error(value) {
36
43
  this._error = value;
37
44
  }
38
- constructor(request = () => { }, data = () => { }, end = () => { }, error = () => { }) {
45
+ constructor(request = () => { }, response = () => { }, data = () => { }, end = () => { }, error = () => { }) {
39
46
  this._request = request;
47
+ this._response = response;
40
48
  this._data = data;
41
49
  this._end = end;
42
50
  this._error = error;
@@ -109,22 +117,32 @@ class BaseHttp extends base.BaseObject {
109
117
  // console.log(`<p>STATUS: ${res.statusCode}</p>`);
110
118
  // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
111
119
  res.setEncoding('utf8');
112
- callbacks.request(res);
120
+ if (callbacks.request) {
121
+ callbacks.request(req);
122
+ }
123
+ if (callbacks.response) {
124
+ callbacks.response(res);
125
+ }
113
126
  res.on('data', (chunk) => {
114
127
  // this.log.debug(`<p>BODY: ${chunk}</p>`);
115
128
  buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
116
- callbacks.data(buffer);
129
+ if (callbacks.data)
130
+ callbacks.data(buffer);
117
131
  });
118
132
  res.on('end', () => {
119
133
  // this.log.debug('No more data in response.');
120
- callbacks.end(buffer);
134
+ if (callbacks.end) {
135
+ callbacks.end(buffer);
136
+ }
121
137
  return buffer;
122
138
  });
123
139
  });
124
140
  req.on('error', (e) => {
125
141
  console.log(`problem with request: ${e.message}`);
126
142
  console.log(e.stack);
127
- callbacks.error(e);
143
+ if (callbacks.error) {
144
+ callbacks.error(e);
145
+ }
128
146
  throw (e);
129
147
  });
130
148
  if (body && (options.method?.toLowerCase() in ['post', 'put', 'patch'])) {
@@ -5,7 +5,8 @@ import qs from 'qs';
5
5
  import multiparty from 'multiparty';
6
6
 
7
7
  class BaseHttpRequestCallback {
8
- _request = (response) => { };
8
+ _request = (request) => { };
9
+ _response = (response) => { };
9
10
  _data = (buffer) => { };
10
11
  _end = (buffer) => { };
11
12
  _error = (buffer) => { };
@@ -15,6 +16,12 @@ class BaseHttpRequestCallback {
15
16
  set request(value) {
16
17
  this._request = value;
17
18
  }
19
+ get response() {
20
+ return this._response;
21
+ }
22
+ set response(value) {
23
+ this._response = value;
24
+ }
18
25
  get data() {
19
26
  return this._data;
20
27
  }
@@ -33,8 +40,9 @@ class BaseHttpRequestCallback {
33
40
  set error(value) {
34
41
  this._error = value;
35
42
  }
36
- constructor(request = () => { }, data = () => { }, end = () => { }, error = () => { }) {
43
+ constructor(request = () => { }, response = () => { }, data = () => { }, end = () => { }, error = () => { }) {
37
44
  this._request = request;
45
+ this._response = response;
38
46
  this._data = data;
39
47
  this._end = end;
40
48
  this._error = error;
@@ -107,22 +115,32 @@ class BaseHttp extends BaseObject {
107
115
  // console.log(`<p>STATUS: ${res.statusCode}</p>`);
108
116
  // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
109
117
  res.setEncoding('utf8');
110
- callbacks.request(res);
118
+ if (callbacks.request) {
119
+ callbacks.request(req);
120
+ }
121
+ if (callbacks.response) {
122
+ callbacks.response(res);
123
+ }
111
124
  res.on('data', (chunk) => {
112
125
  // this.log.debug(`<p>BODY: ${chunk}</p>`);
113
126
  buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
114
- callbacks.data(buffer);
127
+ if (callbacks.data)
128
+ callbacks.data(buffer);
115
129
  });
116
130
  res.on('end', () => {
117
131
  // this.log.debug('No more data in response.');
118
- callbacks.end(buffer);
132
+ if (callbacks.end) {
133
+ callbacks.end(buffer);
134
+ }
119
135
  return buffer;
120
136
  });
121
137
  });
122
138
  req.on('error', (e) => {
123
139
  console.log(`problem with request: ${e.message}`);
124
140
  console.log(e.stack);
125
- callbacks.error(e);
141
+ if (callbacks.error) {
142
+ callbacks.error(e);
143
+ }
126
144
  throw (e);
127
145
  });
128
146
  if (body && (options.method?.toLowerCase() in ['post', 'put', 'patch'])) {
@@ -6,12 +6,60 @@ export declare class BaseHttp extends BaseObject {
6
6
  constructor();
7
7
  static processResponse(response: http.IncomingMessage): Promise<Buffer>;
8
8
  static buildOptions(options: http.RequestOptions | any | Url): http.RequestOptions;
9
- static head(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
10
- static options(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
11
- static get(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
12
- static post(options: http.RequestOptions | any | Url, body: any, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
13
- static put(options: http.RequestOptions | any | Url, body: any, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
14
- static patch(options: http.RequestOptions | any | Url, body: any, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
15
- static delete(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
16
- static request(options: http.RequestOptions | any | Url, body?: any, callbacks?: BaseHttpRequestCallback): Promise<http.ClientRequest | undefined>;
9
+ static head(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback | {
10
+ request?: Function;
11
+ response?: Function;
12
+ data?: Function;
13
+ end?: Function;
14
+ error?: Function;
15
+ }): Promise<http.ClientRequest | undefined>;
16
+ static options(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback | {
17
+ request?: Function;
18
+ response?: Function;
19
+ data?: Function;
20
+ end?: Function;
21
+ error?: Function;
22
+ }): Promise<http.ClientRequest | undefined>;
23
+ static get(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback | {
24
+ request?: Function;
25
+ response?: Function;
26
+ data?: Function;
27
+ end?: Function;
28
+ error?: Function;
29
+ }): Promise<http.ClientRequest | undefined>;
30
+ static post(options: http.RequestOptions | any | Url, body: any, callbacks?: BaseHttpRequestCallback | {
31
+ request?: Function;
32
+ response?: Function;
33
+ data?: Function;
34
+ end?: Function;
35
+ error?: Function;
36
+ }): Promise<http.ClientRequest | undefined>;
37
+ static put(options: http.RequestOptions | any | Url, body: any, callbacks?: BaseHttpRequestCallback | {
38
+ request?: Function;
39
+ response?: Function;
40
+ data?: Function;
41
+ end?: Function;
42
+ error?: Function;
43
+ }): Promise<http.ClientRequest | undefined>;
44
+ static patch(options: http.RequestOptions | any | Url, body: any, callbacks?: BaseHttpRequestCallback | {
45
+ request?: Function;
46
+ response?: Function;
47
+ data?: Function;
48
+ end?: Function;
49
+ error?: Function;
50
+ }): Promise<http.ClientRequest | undefined>;
51
+ static delete(options: http.RequestOptions | any | Url, callbacks?: BaseHttpRequestCallback | {
52
+ request?: Function;
53
+ response?: Function;
54
+ data?: Function;
55
+ end?: Function;
56
+ error?: Function;
57
+ }): Promise<http.ClientRequest | undefined>;
58
+ static request(options: http.RequestOptions | any | Url, body?: any, callbacks?: BaseHttpRequestCallback | {
59
+ request?: Function;
60
+ response?: Function;
61
+ data?: Function;
62
+ end?: Function;
63
+ error?: Function;
64
+ }): Promise<http.ClientRequest | undefined>;
17
65
  }
@@ -1,16 +1,19 @@
1
1
  import http from 'node:http';
2
2
  export declare class BaseHttpRequestCallback {
3
3
  _request: Function;
4
+ _response: Function;
4
5
  _data: Function;
5
6
  _end: Function;
6
7
  _error: Function;
7
8
  get request(): Function;
8
9
  set request(value: Function);
10
+ get response(): Function;
11
+ set response(value: Function);
9
12
  get data(): Function;
10
13
  set data(value: Function);
11
14
  get end(): Function;
12
15
  set end(value: Function);
13
16
  get error(): Function;
14
17
  set error(value: Function);
15
- constructor(request?: (response: http.IncomingMessage) => void, data?: (buffer: Buffer) => void, end?: (buffer: Buffer) => void, error?: (buffer: Buffer) => void);
18
+ constructor(request?: (request: http.ClientRequest) => void, response?: (response: http.IncomingMessage) => void, data?: (buffer: Buffer) => void, end?: (buffer: Buffer) => void, error?: (buffer: Buffer) => void);
16
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basenjs/base-http",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "private": false,
5
5
  "description": "A base HTTP library for Node.js projects.",
6
6
  "type": "module",
@@ -49,38 +49,38 @@ export class BaseHttp extends BaseObject {
49
49
  return r;
50
50
  }
51
51
 
52
- public static head(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
52
+ public static head(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
53
53
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'HEAD' }, null, callbacks);
54
54
  }
55
55
 
56
- public static options(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
56
+ public static options(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
57
57
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'OPTIONS' }, null, callbacks);
58
58
  }
59
59
 
60
- public static get(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
60
+ public static get(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
61
61
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'GET' }, null, callbacks);
62
62
  }
63
63
 
64
- public static post(options: http.RequestOptions | any | Url, body: any, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
64
+ public static post(options: http.RequestOptions | any | Url, body: any, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
65
65
  options.headers['Content-Length'] = body ? Buffer.byteLength(body) : 0;
66
66
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'POST' }, body, callbacks);
67
67
  }
68
68
 
69
- public static put(options: http.RequestOptions | any | Url, body: any, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
69
+ public static put(options: http.RequestOptions | any | Url, body: any, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
70
70
  options.headers['Content-Length'] = body ? Buffer.byteLength(body) : 0;
71
71
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'PUT' }, body, callbacks);
72
72
  }
73
73
 
74
- public static patch(options: http.RequestOptions | any | Url, body: any, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
74
+ public static patch(options: http.RequestOptions | any | Url, body: any, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
75
75
  options.headers['Content-Length'] = body ? Buffer.byteLength(body) : 0;
76
76
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'PATCH' }, body, callbacks);
77
77
  }
78
78
 
79
- public static delete(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
79
+ public static delete(options: http.RequestOptions | any | Url, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
80
80
  return BaseHttp.request({ ...BaseHttp.buildOptions(options), method: 'DELETE' }, null, callbacks);
81
81
  }
82
82
 
83
- public static async request(options: http.RequestOptions | any | Url, body: any = null, callbacks: BaseHttpRequestCallback = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
83
+ public static async request(options: http.RequestOptions | any | Url, body: any = null, callbacks: BaseHttpRequestCallback | { request?: Function, response?: Function, data?: Function, end?: Function, error?: Function } = new BaseHttpRequestCallback()): Promise<http.ClientRequest | undefined> {
84
84
  var _ = this,
85
85
  buffer: Buffer = Buffer.alloc(0);
86
86
 
@@ -91,17 +91,25 @@ export class BaseHttp extends BaseObject {
91
91
 
92
92
  res.setEncoding('utf8');
93
93
 
94
- callbacks.request(res);
94
+ if (callbacks.request) {
95
+ callbacks.request(req);
96
+ }
97
+
98
+ if (callbacks.response) {
99
+ callbacks.response(res);
100
+ }
95
101
 
96
102
  res.on('data', (chunk) => {
97
103
  // this.log.debug(`<p>BODY: ${chunk}</p>`);
98
104
  buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
99
- callbacks.data(buffer);
105
+ if (callbacks.data) callbacks.data(buffer);
100
106
  });
101
107
 
102
108
  res.on('end', () => {
103
109
  // this.log.debug('No more data in response.');
104
- callbacks.end(buffer);
110
+ if (callbacks.end) {
111
+ callbacks.end(buffer);
112
+ }
105
113
  return buffer;
106
114
  });
107
115
  });
@@ -109,7 +117,9 @@ export class BaseHttp extends BaseObject {
109
117
  req.on('error', (e) => {
110
118
  console.log(`problem with request: ${e.message}`);
111
119
  console.log(e.stack);
112
- callbacks.error(e);
120
+ if (callbacks.error) {
121
+ callbacks.error(e);
122
+ }
113
123
  throw(e);
114
124
  });
115
125
 
@@ -3,7 +3,8 @@ import https from 'node:https';
3
3
  import { Url } from 'node:url';
4
4
 
5
5
  export class BaseHttpRequestCallback {
6
- _request: Function = (response: http.IncomingMessage) => {};
6
+ _request: Function = (request: http.ClientRequest) => {};
7
+ _response: Function = (response: http.IncomingMessage) => {};
7
8
  _data: Function = (buffer: Buffer) => {};
8
9
  _end: Function = (buffer: Buffer) => {};
9
10
  _error: Function = (buffer: Buffer) => {};
@@ -16,6 +17,14 @@ export class BaseHttpRequestCallback {
16
17
  this._request = value;
17
18
  }
18
19
 
20
+ get response(): Function {
21
+ return this._response;
22
+ }
23
+
24
+ set response(value: Function) {
25
+ this._response = value;
26
+ }
27
+
19
28
  get data(): Function {
20
29
  return this._data;
21
30
  }
@@ -41,12 +50,14 @@ export class BaseHttpRequestCallback {
41
50
  }
42
51
 
43
52
  constructor(
44
- request: (response: http.IncomingMessage) => void = () => {},
53
+ request: (request: http.ClientRequest) => void = () => {},
54
+ response: (response: http.IncomingMessage) => void = () => {},
45
55
  data: (buffer: Buffer) => void = () => {},
46
56
  end: (buffer: Buffer) => void = () => {},
47
57
  error: (buffer: Buffer) => void = () => {}
48
58
  ) {
49
59
  this._request = request;
60
+ this._response = response;
50
61
  this._data = data;
51
62
  this._end = end;
52
63
  this._error = error;