Skip to content

Commit cc6b2c1

Browse files
committed
feat: enhance api request management system
Improve the API request management system by adding support for collections, environment variables, and enhanced request handling. - Introduced `Collection` and `ApiRequest` interfaces to manage grouped API requests. - Added methods to load, save, and update collections and environment variables. - Enhanced `handleApiRequest` to support query parameters and form data. - Updated the UI to allow for dynamic addition of query parameters, headers, and form data. - Improved the HTTP client to handle `FormData` and manage content-type headers automatically. These changes significantly enhance the user experience by allowing better organization and management of API requests.
1 parent 2f4be9f commit cc6b2c1

File tree

3 files changed

+332
-72
lines changed

3 files changed

+332
-72
lines changed

extensions/vscode/src/apiRequest/apiRequestProvider.ts

+95-10
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,79 @@ import * as vscode from 'vscode';
22
import { ApiRequestView } from './apiRequestView';
33
import { HttpClient } from '../utils/httpClient';
44

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+
518
export class ApiRequestProvider {
619
private context: vscode.ExtensionContext;
720
private view: ApiRequestView | undefined;
821
private httpClient: HttpClient;
22+
private collections: Collection[] = [];
23+
private environment: Record<string, string> = {};
924

1025
constructor(context: vscode.ExtensionContext) {
1126
this.context = context;
1227
this.httpClient = new HttpClient();
1328
this.handleApiRequest = this.handleApiRequest.bind(this);
29+
this.loadCollections();
30+
this.loadEnvironment();
1431
}
1532

1633
public openApiRequestView() {
17-
// Implementation to open the API Management view
1834
if (!this.view) {
1935
this.view = new ApiRequestView(this.context, this.handleApiRequest);
2036
}
2137
this.view.show();
22-
}
38+
this.updateCollectionsView();
39+
}
2340

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> {
2549
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+
2867
const startTime = Date.now();
2968
const response = await this.httpClient.sendRequest(
30-
url,
69+
urlObj.toString(),
3170
method,
32-
parsedHeaders,
33-
['GET', 'HEAD'].includes(method.toUpperCase()) ? undefined : body
71+
headers,
72+
requestBody
3473
);
35-
console.log("Working!");
3674
const endTime = Date.now();
3775
const responseTime = endTime - startTime;
3876
const responseSize = JSON.stringify(response).length;
39-
77+
4078
this.view?.postMessage({
4179
command: 'receiveResponse',
4280
response: response,
@@ -47,4 +85,51 @@ export class ApiRequestProvider {
4785
vscode.window.showErrorMessage(`Error sending request: ${error}`);
4886
}
4987
}
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+
}
50135
}

0 commit comments

Comments
 (0)