@@ -2,41 +2,79 @@ import * as vscode from 'vscode';
2
2
import { ApiRequestView } from './apiRequestView' ;
3
3
import { HttpClient } from '../utils/httpClient' ;
4
4
5
+ interface Collection {
6
+ name : string ;
7
+ requests : ApiRequest [ ] ;
8
+ }
9
+
10
+ interface ApiRequest {
11
+ name : string ;
12
+ method : string ;
13
+ url : string ;
14
+ headers : Record < string , string > ;
15
+ body : string ;
16
+ }
17
+
5
18
export class ApiRequestProvider {
6
19
private context : vscode . ExtensionContext ;
7
20
private view : ApiRequestView | undefined ;
8
21
private httpClient : HttpClient ;
22
+ private collections : Collection [ ] = [ ] ;
23
+ private environment : Record < string , string > = { } ;
9
24
10
25
constructor ( context : vscode . ExtensionContext ) {
11
26
this . context = context ;
12
27
this . httpClient = new HttpClient ( ) ;
13
28
this . handleApiRequest = this . handleApiRequest . bind ( this ) ;
29
+ this . loadCollections ( ) ;
30
+ this . loadEnvironment ( ) ;
14
31
}
15
32
16
33
public openApiRequestView ( ) {
17
- // Implementation to open the API Management view
18
34
if ( ! this . view ) {
19
35
this . view = new ApiRequestView ( this . context , this . handleApiRequest ) ;
20
36
}
21
37
this . view . show ( ) ;
22
- }
38
+ this . updateCollectionsView ( ) ;
39
+ }
23
40
24
- private async handleApiRequest ( method : string , url : string , headers : string , body : string ) {
41
+ private async handleApiRequest (
42
+ method : string ,
43
+ url : string ,
44
+ headers : Record < string , string > ,
45
+ queryParams : Record < string , string > ,
46
+ formData : Record < string , string > ,
47
+ body : string
48
+ ) : Promise < void > {
25
49
try {
26
- console . log ( url , method , headers , body ) ;
27
- const parsedHeaders = JSON . parse ( headers ) ;
50
+ // Append query params to URL
51
+ const urlObj = new URL ( url ) ;
52
+ Object . entries ( queryParams ) . forEach ( ( [ key , value ] ) => {
53
+ urlObj . searchParams . append ( key , value ) ;
54
+ } ) ;
55
+
56
+ // Prepare body
57
+ let requestBody : string | FormData | undefined ;
58
+ if ( Object . keys ( formData ) . length > 0 ) {
59
+ requestBody = new FormData ( ) ;
60
+ Object . entries ( formData ) . forEach ( ( [ key , value ] ) => {
61
+ ( requestBody as FormData ) . append ( key , value ) ;
62
+ } ) ;
63
+ } else if ( body ) {
64
+ requestBody = body ;
65
+ }
66
+
28
67
const startTime = Date . now ( ) ;
29
68
const response = await this . httpClient . sendRequest (
30
- url ,
69
+ urlObj . toString ( ) ,
31
70
method ,
32
- parsedHeaders ,
33
- [ 'GET' , 'HEAD' ] . includes ( method . toUpperCase ( ) ) ? undefined : body
71
+ headers ,
72
+ requestBody
34
73
) ;
35
- console . log ( "Working!" ) ;
36
74
const endTime = Date . now ( ) ;
37
75
const responseTime = endTime - startTime ;
38
76
const responseSize = JSON . stringify ( response ) . length ;
39
-
77
+
40
78
this . view ?. postMessage ( {
41
79
command : 'receiveResponse' ,
42
80
response : response ,
@@ -47,4 +85,51 @@ export class ApiRequestProvider {
47
85
vscode . window . showErrorMessage ( `Error sending request: ${ error } ` ) ;
48
86
}
49
87
}
88
+
89
+ private replaceEnvironmentVariables ( str : string ) : string {
90
+ return str . replace ( / \{ \{ ( \w + ) \} \} / g, ( _ , key ) => this . environment [ key ] || '' ) ;
91
+ }
92
+
93
+ public addCollection ( name : string ) {
94
+ this . collections . push ( { name, requests : [ ] } ) ;
95
+ this . saveCollections ( ) ;
96
+ this . updateCollectionsView ( ) ;
97
+ }
98
+
99
+ public addRequestToCollection ( collectionName : string , request : ApiRequest ) {
100
+ const collection = this . collections . find ( c => c . name === collectionName ) ;
101
+ if ( collection ) {
102
+ collection . requests . push ( request ) ;
103
+ this . saveCollections ( ) ;
104
+ this . updateCollectionsView ( ) ;
105
+ }
106
+ }
107
+
108
+ private loadCollections ( ) {
109
+ this . collections = this . context . globalState . get ( 'apiCollections' , [ ] ) ;
110
+ }
111
+
112
+ private saveCollections ( ) {
113
+ this . context . globalState . update ( 'apiCollections' , this . collections ) ;
114
+ }
115
+
116
+ private updateCollectionsView ( ) {
117
+ this . view ?. postMessage ( {
118
+ command : 'updateCollections' ,
119
+ collections : this . collections
120
+ } ) ;
121
+ }
122
+
123
+ public setEnvironmentVariable ( key : string , value : string ) {
124
+ this . environment [ key ] = value ;
125
+ this . saveEnvironment ( ) ;
126
+ }
127
+
128
+ private loadEnvironment ( ) {
129
+ this . environment = this . context . globalState . get ( 'apiEnvironment' , { } ) ;
130
+ }
131
+
132
+ private saveEnvironment ( ) {
133
+ this . context . globalState . update ( 'apiEnvironment' , this . environment ) ;
134
+ }
50
135
}
0 commit comments