@@ -7,7 +7,6 @@ import { getGlobalTraceo } from "./utils";
77 * Http client for SDK requestst
88 */
99export class HttpClient {
10-
1110 protected constructor ( ) { }
1211
1312 /**
@@ -17,43 +16,52 @@ export class HttpClient {
1716 * URL is concatenation of passed host to client and pathanme to this method.
1817 * Use callback/onError callbacks to handle action after operation.
1918 */
20- public static request = ( { url, method = "POST" , body, onError, callback } : RequestOptionsType ) => {
21- const options = {
22- ...this . requestHeaders ( method ) ,
23- ...this . requestOptions ( url , method )
24- } ;
25-
26- const httpModule = this . requestModule ( ) ;
27-
28- const request = httpModule . request ( options , callback ) ;
29- request . on ( "error" , ( ) => onError ) ;
30-
31- this . requestWriteBody ( method , request , body ) ;
32-
33- request . end ( ) ;
19+ public static request = ( {
20+ url,
21+ method = "POST" ,
22+ body,
23+ onError,
24+ callback
25+ } : RequestOptionsType ) => {
26+ const options = {
27+ ...this . requestHeaders ( method ) ,
28+ ...this . requestOptions ( url , method )
3429 } ;
3530
31+ const httpModule = this . requestModule ( ) ;
32+
33+ const request = httpModule . request ( options , callback ) ;
34+ request . on ( "error" , ( ) => onError ) ;
3635
37- private static requestWriteBody = ( method : RequestType , request : http . ClientRequest , body : { } ) => {
36+ this . requestWriteBody ( method , request , body ) ;
37+
38+ request . end ( ) ;
39+ } ;
40+
41+ private static requestWriteBody = (
42+ method : RequestType ,
43+ request : http . ClientRequest ,
44+ body : { }
45+ ) => {
3846 if ( method === "POST" ) {
3947 request . write ( JSON . stringify ( body ) ) ;
4048 }
4149 } ;
42-
50+
4351 private static requestModule = ( ) : typeof http | typeof https => {
4452 const protocol = this . clientURL ( ) . protocol ;
4553 return protocol == "http:" ? http : https ;
4654 } ;
47-
55+
4856 private static clientURL = ( ) : URL => {
4957 return new URL ( getGlobalTraceo ( ) . options . host ) ;
5058 } ;
51-
59+
5260 private static requestOptions = ( url : string , method : RequestType ) : http . RequestOptions => {
5361 const host = getGlobalTraceo ( ) . options . host ;
5462 const reqUrl = new URL ( host ) ;
5563 const path = new URL ( url , host ) ;
56-
64+
5765 return {
5866 protocol : reqUrl . protocol ,
5967 port : reqUrl . port ,
@@ -62,84 +70,16 @@ export class HttpClient {
6270 path : path . pathname
6371 } ;
6472 } ;
65-
73+
6674 private static requestHeaders = ( method : RequestType ) => {
6775 const headers = getGlobalTraceo ( ) . headers ;
68-
76+
6977 if ( method !== "POST" ) return headers ;
7078 return {
7179 headers : {
7280 "Content-Type" : "application/json" ,
7381 ...headers
7482 }
7583 } ;
76- } ;
77- }
78-
79- const requestWriteBody = ( method : RequestType , request : http . ClientRequest , body : { } ) => {
80- if ( method === "POST" ) {
81- request . write ( JSON . stringify ( body ) ) ;
82- }
83- } ;
84-
85- const requestModule = ( ) : typeof http | typeof https => {
86- const protocol = clientURL ( ) . protocol ;
87- return protocol == "http:" ? http : https ;
88- } ;
89-
90- const clientURL = ( ) : URL => {
91- return new URL ( getGlobalTraceo ( ) . options . host ) ;
92- } ;
93-
94- const requestOptions = ( url : string , method : RequestType ) : http . RequestOptions => {
95- const host = getGlobalTraceo ( ) . options . host ;
96- const reqUrl = new URL ( host ) ;
97- const path = new URL ( url , host ) ;
98-
99- return {
100- protocol : reqUrl . protocol ,
101- port : reqUrl . port ,
102- host : reqUrl . hostname ,
103- method,
104- path : path . pathname
10584 } ;
106- } ;
107-
108- // const requestHeaders = (method: RequestType) => {
109- // const headers = getGlobalTraceo().headers;
110-
111- // if (method !== "POST") return headers;
112- // return {
113- // headers: {
114- // "Content-Type": "application/json",
115- // ...headers
116- // }
117- // };
118- // };
119-
120- // /**
121- // * Make http/s request to Traceo platoform.
122- // *
123- // * Default request method is POST, in this case in hedaers is passed "Content-Type": "application/json".
124- // * URL is concatenation of passed host to client and pathanme to this method.
125- // * Use callback/onError callbacks to handle action after operation.
126- // */
127- // const request = ({ url, method = "POST", body, onError, callback }: RequestOptionsType) => {
128- // const options = {
129- // ...requestHeaders(method),
130- // ...requestOptions(url, method)
131- // };
132-
133- // const httpModule = requestModule();
134-
135- // const request = httpModule.request(options, callback);
136- // request.on("error", () => onError);
137-
138- // requestWriteBody(method, request, body);
139-
140- // request.end();
141- // };
142-
143- // export const transport = {
144- // request
145- // };
85+ }
0 commit comments