@@ -4,41 +4,55 @@ import { Client } from "../client";
44import { RequestType } from "../types" ;
55
66type RequestOptionsType = {
7+ url : string ;
8+ method ?: RequestType ;
79 body : { } ;
810 onError : ( error : Error ) => void ;
11+ callback ?: ( resp : http . IncomingMessage ) => void ;
912} ;
1013export class HttpModule {
11- host : string ;
12- url : URL ;
13- method : RequestType ;
14+ private static instance : HttpModule ;
15+ private host : string ;
1416
15- constructor ( url : string , method : RequestType = "POST" ) {
17+ constructor ( ) {
1618 this . host = Client . config . url ;
17-
18- this . url = new URL ( url , this . host ) ;
19- this . method = method ;
2019 }
2120
22- public request ( options : RequestOptionsType ) {
23- const { body, onError } = options ;
21+ public static getInstance ( ) {
22+ if ( ! HttpModule . instance ) {
23+ HttpModule . instance = new HttpModule ( ) ;
24+ }
25+ return HttpModule . instance ;
26+ }
2427
28+ public request ( {
29+ url,
30+ method = "POST" ,
31+ body,
32+ onError,
33+ callback,
34+ } : RequestOptionsType ) {
2535 const requestOptions = {
26- ...this . requestHeaders ( ) ,
27- ...this . requestOptions ( ) ,
36+ ...this . requestHeaders ( method ) ,
37+ ...this . requestOptions ( url , method ) ,
2838 } ;
2939
3040 const httpModule = this . requestModule ( ) ;
3141
32- const request = httpModule . request ( requestOptions ) ;
42+ const request = httpModule . request ( requestOptions , callback ) ;
3343 request . on ( "error" , ( ) => onError ) ;
3444
35- this . requestWriteBody ( request , body ) ;
45+ this . requestWriteBody ( method , request , body ) ;
3646
3747 request . end ( ) ;
3848 }
3949
40- private requestWriteBody ( request : http . ClientRequest , body : { } ) {
41- if ( this . method === "POST" ) {
50+ private requestWriteBody (
51+ method : RequestType ,
52+ request : http . ClientRequest ,
53+ body : { }
54+ ) {
55+ if ( method === "POST" ) {
4256 request . write ( JSON . stringify ( body ) ) ;
4357 }
4458 }
@@ -51,28 +65,28 @@ export class HttpModule {
5165 return new URL ( this . host ) ;
5266 }
5367
54- private requestOptions ( ) : http . RequestOptions {
68+ private requestOptions (
69+ url : string ,
70+ method : RequestType
71+ ) : http . RequestOptions {
5572 const reqUrl = this . clientURL ;
73+ const path = new URL ( url , this . host ) ;
5674
5775 return {
5876 protocol : reqUrl . protocol ,
5977 port : reqUrl . port ,
6078 host : reqUrl . hostname ,
61- method : this . method ,
62- path : this . path ,
79+ method,
80+ path : ` ${ path . pathname } / ${ Client . config . appId } ` ,
6381 } ;
6482 }
6583
66- private get path ( ) {
67- return `${ this . url . pathname } /${ Client . config . appId } ` ;
68- }
69-
70- private requestHeaders ( ) {
84+ private requestHeaders ( method : RequestType ) {
7185 const headers = Client . client . headers ;
7286 const baseHeaders = {
7387 ...headers ,
7488 } ;
75- if ( this . method !== "POST" ) return baseHeaders ;
89+ if ( method !== "POST" ) return baseHeaders ;
7690 return {
7791 headers : {
7892 "Content-Type" : "application/json" ,
0 commit comments