Skip to content

Commit ca280e4

Browse files
committed
Enhance RequestBuilder and useApiTester to manage path parameters dynamically; add placeholder extraction for URLs
1 parent 3f4b3ea commit ca280e4

3 files changed

Lines changed: 265 additions & 247 deletions

File tree

spa/src/components/api-tester/RequestBuilder.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ export function RequestBuilder({
8484
}
8585
}, [method, activeTab, hasPathParams]);
8686

87+
// When path params disappear, switch away from path tab
88+
useEffect(() => {
89+
if (activeTab === 'path' && !hasPathParams) {
90+
setActiveTab('params');
91+
}
92+
}, [activeTab, hasPathParams]);
93+
8794
// Resize select to fit the selected method text
8895
useEffect(() => {
8996
if (measureRef.current && selectRef.current) {

spa/src/components/api-tester/useApiTester.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ function detectResponseContentType(endpoint: Endpoint): string {
7575
return 'application/json';
7676
}
7777

78+
function extractPlaceholders(url: string): string[] {
79+
const names: string[] = [];
80+
const pattern = /\{([^}]+)\}/g;
81+
let match;
82+
while ((match = pattern.exec(url)) !== null) {
83+
const name = match[1]!;
84+
if (!names.includes(name)) {
85+
names.push(name);
86+
}
87+
}
88+
return names;
89+
}
90+
7891
function buildInitialRequest(endpoint: Endpoint, baseUrl: string, spec: OpenApiSpec): RequestState {
7992
const origin = baseUrl && baseUrl !== '/' ? baseUrl.replace(/\/$/, '') : window.location.origin;
8093
const url = `${origin}${endpoint.path}`;
@@ -234,21 +247,19 @@ export function useApiTester(endpoint: Endpoint, spec: OpenApiSpec): UseApiTeste
234247
}, [baseUrl, compiledPatterns, spec]);
235248

236249
const setUrl = useCallback((url: string) => {
237-
setRequest(prev => ({ ...prev, url }));
250+
setRequest(prev => {
251+
const newNames = extractPlaceholders(url);
252+
const existing = new Map(prev.pathParams.map(p => [p.key, p]));
253+
const pathParams = newNames.map(name =>
254+
existing.get(name) ?? makePair(name, '', true)
255+
);
256+
return { ...prev, url, pathParams };
257+
});
238258
}, []);
239259

240260
const setPathParams = useCallback((pathParams: KeyValuePair[]) => {
241-
setRequest(prev => {
242-
const origin = baseUrl && baseUrl !== '/' ? baseUrl.replace(/\/$/, '') : window.location.origin;
243-
let url = `${origin}${endpoint.path}`;
244-
for (const p of pathParams) {
245-
if (p.value) {
246-
url = url.replace(`{${p.key}}`, p.value);
247-
}
248-
}
249-
return { ...prev, pathParams, url };
250-
});
251-
}, [baseUrl, endpoint.path]);
261+
setRequest(prev => ({ ...prev, pathParams }));
262+
}, []);
252263

253264
const setQueryParams = useCallback((queryParams: KeyValuePair[]) => {
254265
setRequest(prev => ({ ...prev, queryParams }));

templates/doc.html.php

Lines changed: 235 additions & 235 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)