1
- import * as vscode from ' vscode' ;
2
- import { ApiRequestView } from ' ./apiRequestView' ;
3
- import { HttpClient } from ' ../utils/httpClient' ;
1
+ import * as vscode from " vscode" ;
2
+ import { ApiRequestView } from " ./apiRequestView" ;
3
+ import { HttpClient } from " ../utils/httpClient" ;
4
4
5
5
interface Collection {
6
- name : string ;
7
- requests : ApiRequest [ ] ;
6
+ name : string ;
7
+ requests : ApiRequest [ ] ;
8
8
}
9
9
10
10
interface ApiRequest {
11
- name : string ;
12
- method : string ;
13
- url : string ;
14
- headers : Record < string , string > ;
15
- body : string ;
11
+ name : string ;
12
+ method : string ;
13
+ url : string ;
14
+ headers : Record < string , string > ;
15
+ body : string ;
16
16
}
17
17
18
18
export class ApiRequestProvider {
19
- private context : vscode . ExtensionContext ;
20
- private view : ApiRequestView | undefined ;
21
- private httpClient : HttpClient ;
22
- private collections : Collection [ ] = [ ] ;
23
- private environment : Record < string , string > = { } ;
24
-
25
- constructor ( context : vscode . ExtensionContext ) {
26
- this . context = context ;
27
- this . httpClient = new HttpClient ( ) ;
28
- this . handleApiRequest = this . handleApiRequest . bind ( this ) ;
29
- this . loadCollections ( ) ;
30
- this . loadEnvironment ( ) ;
19
+ private context : vscode . ExtensionContext ;
20
+ private view : ApiRequestView | undefined ;
21
+ private httpClient : HttpClient ;
22
+ private collections : Collection [ ] = [ ] ;
23
+ private environment : Record < string , string > = { } ;
24
+
25
+ constructor ( context : vscode . ExtensionContext ) {
26
+ this . context = context ;
27
+ this . httpClient = new HttpClient ( ) ;
28
+ this . handleApiRequest = this . handleApiRequest . bind ( this ) ;
29
+ this . loadCollections ( ) ;
30
+ this . loadEnvironment ( ) ;
31
+ }
32
+
33
+ public openApiRequestView ( ) {
34
+ console . log ( "API Request View : This is the request view" ) ;
35
+ this . view = new ApiRequestView ( this . context , this . handleApiRequest ) ;
36
+ this . view . show ( ) ;
37
+ this . updateCollectionsView ( ) ;
38
+ }
39
+
40
+ private async handleApiRequest (
41
+ method : string ,
42
+ url : string ,
43
+ headers : Record < string , string > ,
44
+ queryParams : Record < string , string > ,
45
+ formData : Record < string , string > ,
46
+ body : string ,
47
+ bodyType : string
48
+ ) : Promise < void > {
49
+ console . log ( "Handle API Request called" ) ;
50
+
51
+ // Log the incoming parameters
52
+ console . log ( "Parameters:" , {
53
+ method,
54
+ url,
55
+ headers,
56
+ queryParams,
57
+ formData,
58
+ body,
59
+ bodyType,
60
+ } ) ;
61
+
62
+ let urlObj : URL ;
63
+
64
+ try {
65
+ // Try to create a new URL object
66
+ urlObj = new URL ( url ) ;
67
+
68
+ // Append query params to URL
69
+ Object . entries ( queryParams ) . forEach ( ( [ key , value ] ) => {
70
+ urlObj . searchParams . append ( key , value ) ;
71
+ } ) ;
72
+ } catch ( error ) {
73
+ if ( url === "" ) {
74
+ vscode . window . showErrorMessage ( `Enter a URL!` ) ;
75
+ console . error ( "Empty URL" , error . message ) ;
76
+ } else if ( error instanceof TypeError ) {
77
+ // Handle the specific case for invalid URLs
78
+ vscode . window . showErrorMessage ( `Invalid URL: ${ url } ` ) ;
79
+ console . error ( "Error creating URL:" , error . message ) ;
80
+ return ; // Exit early if the URL is invalid
81
+ } else {
82
+ // Handle other types of errors
83
+ vscode . window . showErrorMessage ( `Unexpected error: ${ error . message } ` ) ;
84
+ console . error ( "Unexpected error:" , error ) ;
85
+ return ; // Exit early for unexpected errors
86
+ }
31
87
}
32
88
33
- public openApiRequestView ( ) {
34
- console . log ( "API Request View : This is the request view" ) ;
35
- // if (!this.view) {
36
- // this.view = new ApiRequestView(this.context, this.handleApiRequest);
37
- // }
38
- this . view = new ApiRequestView ( this . context , this . handleApiRequest ) ;
39
- this . view . show ( ) ;
40
- this . updateCollectionsView ( ) ;
89
+ // Prepare body
90
+ let requestBody : string | FormData | undefined ;
91
+ if ( bodyType === "form-data" ) {
92
+ requestBody = new FormData ( ) ;
93
+ Object . entries ( formData ) . forEach ( ( [ key , value ] ) => {
94
+ ( requestBody as FormData ) . append ( key , value ) ;
95
+ } ) ;
96
+ } else if ( bodyType === "raw" && body ) {
97
+ requestBody = body ;
41
98
}
42
99
43
- private async handleApiRequest (
44
- method : string ,
45
- url : string ,
46
- headers : Record < string , string > ,
47
- queryParams : Record < string , string > ,
48
- formData : Record < string , string > ,
49
- body : string ,
50
- bodyType : string
51
- ) : Promise < void > {
52
- console . log ( "Handle API Request called" ) ;
53
- try {
54
- // Append query params to URL
55
- const urlObj = new URL ( url ) ;
56
- Object . entries ( queryParams ) . forEach ( ( [ key , value ] ) => {
57
- urlObj . searchParams . append ( key , value ) ;
58
- } ) ;
59
-
60
- // Prepare body
61
- let requestBody : string | FormData | undefined ;
62
- if ( bodyType === 'form-data' ) {
63
- requestBody = new FormData ( ) ;
64
- Object . entries ( formData ) . forEach ( ( [ key , value ] ) => {
65
- ( requestBody as FormData ) . append ( key , value ) ;
66
- } ) ;
67
- } else if ( bodyType === 'raw' && body ) {
68
- requestBody = body ;
69
- }
70
-
71
- const startTime = Date . now ( ) ;
72
- const response = await this . httpClient . sendRequest (
73
- urlObj . toString ( ) ,
74
- method ,
75
- headers ,
76
- requestBody
77
- ) ;
78
- const endTime = Date . now ( ) ;
79
- const responseTime = endTime - startTime ;
80
- const responseSize = JSON . stringify ( response ) . length ;
81
-
82
- this . view ?. postMessage ( {
83
- command : 'receiveResponse' ,
84
- response : response ,
85
- time : responseTime ,
86
- size : responseSize
87
- } ) ;
88
- } catch ( error ) {
89
- vscode . window . showErrorMessage ( `Error sending request: ${ error } ` ) ;
90
- }
100
+ console . log ( "Final Request:" , {
101
+ url : urlObj . toString ( ) ,
102
+ method,
103
+ headers,
104
+ requestBody,
105
+ } ) ;
106
+
107
+ const startTime = Date . now ( ) ;
108
+
109
+ try {
110
+ const response = await this . httpClient . sendRequest (
111
+ urlObj . toString ( ) ,
112
+ method ,
113
+ headers ,
114
+ requestBody
115
+ ) ;
116
+
117
+ const endTime = Date . now ( ) ;
118
+ const responseTime = endTime - startTime ;
119
+ const responseSize = JSON . stringify ( response ) . length ;
120
+
121
+ this . view ?. postMessage ( {
122
+ command : "receiveResponse" ,
123
+ response : response ,
124
+ time : responseTime ,
125
+ size : responseSize ,
126
+ } ) ;
127
+ } catch ( error ) {
128
+ console . error ( "Error sending request:" , error ) ; // Log the error details
129
+ vscode . window . showErrorMessage ( `Error sending request: ${ error . message } ` ) ;
91
130
}
92
-
93
- private replaceEnvironmentVariables ( str : string ) : string {
94
- return str . replace ( / \{ \{ ( \w + ) \} \} / g, ( _ , key ) => this . environment [ key ] || '' ) ;
95
- }
96
-
97
- public addCollection ( name : string ) {
98
- console . log ( "api Request view : AddCollections called" ) ;
99
- this . collections . push ( { name, requests : [ ] } ) ;
100
- this . saveCollections ( ) ;
101
- this . updateCollectionsView ( ) ;
102
- }
103
-
104
- public addRequestToCollection ( collectionName : string , request : ApiRequest ) {
105
- console . log ( "api Request view : AddRequestToCollections called" ) ;
106
- const collection = this . collections . find ( c => c . name === collectionName ) ;
107
- if ( collection ) {
108
- collection . requests . push ( request ) ;
109
- this . saveCollections ( ) ;
110
- this . updateCollectionsView ( ) ;
111
- }
112
- }
113
-
114
- private loadCollections ( ) {
115
- this . collections = this . context . globalState . get ( 'apiCollections' , [ ] ) ;
116
- }
117
-
118
- private saveCollections ( ) {
119
- this . context . globalState . update ( 'apiCollections' , this . collections ) ;
131
+ }
132
+
133
+ private replaceEnvironmentVariables ( str : string ) : string {
134
+ return str . replace (
135
+ / \{ \{ ( \w + ) \} \} / g,
136
+ ( _ , key ) => this . environment [ key ] || ""
137
+ ) ;
138
+ }
139
+
140
+ public addCollection ( name : string ) {
141
+ console . log ( "api Request view : AddCollections called" ) ;
142
+ this . collections . push ( { name, requests : [ ] } ) ;
143
+ this . saveCollections ( ) ;
144
+ this . updateCollectionsView ( ) ;
145
+ }
146
+
147
+ public addRequestToCollection ( collectionName : string , request : ApiRequest ) {
148
+ console . log ( "api Request view : AddRequestToCollections called" ) ;
149
+ const collection = this . collections . find ( ( c ) => c . name === collectionName ) ;
150
+ if ( collection ) {
151
+ collection . requests . push ( request ) ;
152
+ this . saveCollections ( ) ;
153
+ this . updateCollectionsView ( ) ;
120
154
}
121
-
122
- private updateCollectionsView ( ) {
123
- this . view ?. postMessage ( {
124
- command : 'updateCollections' ,
125
- collections : this . collections
126
- } ) ;
127
- }
128
-
129
- public setEnvironmentVariable ( key : string , value : string ) {
130
- this . environment [ key ] = value ;
131
- this . saveEnvironment ( ) ;
132
- }
133
-
134
- private loadEnvironment ( ) {
135
- this . environment = this . context . globalState . get ( 'apiEnvironment' , { } ) ;
136
- }
137
-
138
- private saveEnvironment ( ) {
139
- this . context . globalState . update ( 'apiEnvironment' , this . environment ) ;
140
- }
141
- }
155
+ }
156
+
157
+ private loadCollections ( ) {
158
+ this . collections = this . context . globalState . get ( "apiCollections" , [ ] ) ;
159
+ }
160
+
161
+ private saveCollections ( ) {
162
+ this . context . globalState . update ( "apiCollections" , this . collections ) ;
163
+ }
164
+
165
+ private updateCollectionsView ( ) {
166
+ this . view ?. postMessage ( {
167
+ command : "updateCollections" ,
168
+ collections : this . collections ,
169
+ } ) ;
170
+ }
171
+
172
+ public setEnvironmentVariable ( key : string , value : string ) {
173
+ this . environment [ key ] = value ;
174
+ this . saveEnvironment ( ) ;
175
+ }
176
+
177
+ private loadEnvironment ( ) {
178
+ this . environment = this . context . globalState . get ( "apiEnvironment" , { } ) ;
179
+ }
180
+
181
+ private saveEnvironment ( ) {
182
+ this . context . globalState . update ( "apiEnvironment" , this . environment ) ;
183
+ }
184
+ }
0 commit comments