@basenjs/base-http 0.0.5 → 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.
@@ -117,23 +117,32 @@ class BaseHttp extends base.BaseObject {
117
117
  // console.log(`<p>STATUS: ${res.statusCode}</p>`);
118
118
  // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
119
119
  res.setEncoding('utf8');
120
- callbacks.request(req);
121
- callbacks.response(res);
120
+ if (callbacks.request) {
121
+ callbacks.request(req);
122
+ }
123
+ if (callbacks.response) {
124
+ callbacks.response(res);
125
+ }
122
126
  res.on('data', (chunk) => {
123
127
  // this.log.debug(`<p>BODY: ${chunk}</p>`);
124
128
  buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
125
- callbacks.data(buffer);
129
+ if (callbacks.data)
130
+ callbacks.data(buffer);
126
131
  });
127
132
  res.on('end', () => {
128
133
  // this.log.debug('No more data in response.');
129
- callbacks.end(buffer);
134
+ if (callbacks.end) {
135
+ callbacks.end(buffer);
136
+ }
130
137
  return buffer;
131
138
  });
132
139
  });
133
140
  req.on('error', (e) => {
134
141
  console.log(`problem with request: ${e.message}`);
135
142
  console.log(e.stack);
136
- callbacks.error(e);
143
+ if (callbacks.error) {
144
+ callbacks.error(e);
145
+ }
137
146
  throw (e);
138
147
  });
139
148
  if (body && (options.method?.toLowerCase() in ['post', 'put', 'patch'])) {
@@ -115,23 +115,32 @@ class BaseHttp extends BaseObject {
115
115
  // console.log(`<p>STATUS: ${res.statusCode}</p>`);
116
116
  // console.log(`<p>HEADERS: ${JSON.stringify(res.headers)}</p>`);
117
117
  res.setEncoding('utf8');
118
- callbacks.request(req);
119
- callbacks.response(res);
118
+ if (callbacks.request) {
119
+ callbacks.request(req);
120
+ }
121
+ if (callbacks.response) {
122
+ callbacks.response(res);
123
+ }
120
124
  res.on('data', (chunk) => {
121
125
  // this.log.debug(`<p>BODY: ${chunk}</p>`);
122
126
  buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
123
- callbacks.data(buffer);
127
+ if (callbacks.data)
128
+ callbacks.data(buffer);
124
129
  });
125
130
  res.on('end', () => {
126
131
  // this.log.debug('No more data in response.');
127
- callbacks.end(buffer);
132
+ if (callbacks.end) {
133
+ callbacks.end(buffer);
134
+ }
128
135
  return buffer;
129
136
  });
130
137
  });
131
138
  req.on('error', (e) => {
132
139
  console.log(`problem with request: ${e.message}`);
133
140
  console.log(e.stack);
134
- callbacks.error(e);
141
+ if (callbacks.error) {
142
+ callbacks.error(e);
143
+ }
135
144
  throw (e);
136
145
  });
137
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basenjs/base-http",
3
- "version": "0.0.5",
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,18 +91,25 @@ export class BaseHttp extends BaseObject {
91
91
 
92
92
  res.setEncoding('utf8');
93
93
 
94
- callbacks.request(req);
95
- callbacks.response(res);
94
+ if (callbacks.request) {
95
+ callbacks.request(req);
96
+ }
97
+
98
+ if (callbacks.response) {
99
+ callbacks.response(res);
100
+ }
96
101
 
97
102
  res.on('data', (chunk) => {
98
103
  // this.log.debug(`<p>BODY: ${chunk}</p>`);
99
104
  buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
100
- callbacks.data(buffer);
105
+ if (callbacks.data) callbacks.data(buffer);
101
106
  });
102
107
 
103
108
  res.on('end', () => {
104
109
  // this.log.debug('No more data in response.');
105
- callbacks.end(buffer);
110
+ if (callbacks.end) {
111
+ callbacks.end(buffer);
112
+ }
106
113
  return buffer;
107
114
  });
108
115
  });
@@ -110,7 +117,9 @@ export class BaseHttp extends BaseObject {
110
117
  req.on('error', (e) => {
111
118
  console.log(`problem with request: ${e.message}`);
112
119
  console.log(e.stack);
113
- callbacks.error(e);
120
+ if (callbacks.error) {
121
+ callbacks.error(e);
122
+ }
114
123
  throw(e);
115
124
  });
116
125