@basenjs/base-http 0.0.2 → 0.0.3

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