HTTP save
This commit is contained in:
@@ -4,7 +4,8 @@ import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import { useAPI } from '@/providers/APIProvider';
|
||||
|
||||
const ServerManager = ({ onServerChange = () => {} }) => {
|
||||
const ServerManager = ({ onServerChange = () => {}, defaultValue = null }) => {
|
||||
|
||||
const { servers, loading, error, loadServers } = useAPI();
|
||||
const initialized = useRef(false);
|
||||
const [selectedServer, setSelectedServer] = useState(null);
|
||||
@@ -23,12 +24,17 @@ const ServerManager = ({ onServerChange = () => {} }) => {
|
||||
|
||||
// Handle server selection when servers are loaded
|
||||
useEffect(() => {
|
||||
if (servers?.length > 0 && !loading && !selectedServer) {
|
||||
const defaultServer = servers[0];
|
||||
if (servers?.length > 0 && !loading) {
|
||||
const defaultServer = defaultValue
|
||||
? servers.find(s => s.id === defaultValue)
|
||||
: servers[0];
|
||||
|
||||
if (defaultServer) {
|
||||
setSelectedServer(defaultServer);
|
||||
onServerChange(defaultServer.id);
|
||||
}
|
||||
}, [servers, loading, onServerChange, selectedServer]);
|
||||
}
|
||||
}, [servers, loading, onServerChange, defaultValue]);
|
||||
|
||||
const handleServerChange = (serverId) => {
|
||||
const server = servers.find(s => s.id === serverId);
|
||||
|
||||
@@ -19,7 +19,7 @@ const HTTP_METHODS = [
|
||||
|
||||
export const HTTPClientView = ({model, index}) => {
|
||||
const apiClient = new APIClient();
|
||||
const {getServer} = useAPI();
|
||||
const {getServer, updateAPIRequest} = useAPI();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [currentModel, setCurrentModel] = useState(model);
|
||||
const [basePath, setBasePath] = useState('');
|
||||
@@ -104,7 +104,9 @@ export const HTTPClientView = ({model, index}) => {
|
||||
);
|
||||
|
||||
const handleSendRequest = async () => {
|
||||
if (isLoading) return;
|
||||
if (isLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
setEditorContents(prev => ({...prev, consoleLog: ''}));
|
||||
@@ -132,14 +134,11 @@ export const HTTPClientView = ({model, index}) => {
|
||||
size: `Size: ${response.size}`
|
||||
});
|
||||
|
||||
console.log(responseData);
|
||||
|
||||
setEditorContents(prev => ({
|
||||
...prev,
|
||||
responseBody: response.body
|
||||
}));
|
||||
|
||||
console.log(editorContents.responseBody);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -152,6 +151,23 @@ export const HTTPClientView = ({model, index}) => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
const updatedModel = {
|
||||
...currentModel,
|
||||
headers: currentModel.headers || [],
|
||||
queryParams: currentModel.queryParams || [],
|
||||
preRequestScript: editorContents.preRequestScript,
|
||||
postRequestScript: editorContents.postRequestScript,
|
||||
requestBody: currentModel.requestBody
|
||||
};
|
||||
|
||||
await updateAPIRequest(updatedModel);
|
||||
} catch (error) {
|
||||
console.error('Failed to save API:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const renderResponseHeaders = () => (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
@@ -184,10 +200,7 @@ export const HTTPClientView = ({model, index}) => {
|
||||
|
||||
{/* Request Controls */}
|
||||
<div className="flex items-center gap-2">
|
||||
<Select
|
||||
value={currentModel.method}
|
||||
onValueChange={(value) => setCurrentModel(prev => ({...prev, method: value}))}
|
||||
>
|
||||
<Select value={currentModel.method} onValueChange={(value) => setCurrentModel(prev => ({...prev, method: value}))}>
|
||||
<SelectTrigger className="w-24">
|
||||
<SelectValue placeholder="Method"/>
|
||||
</SelectTrigger>
|
||||
@@ -201,25 +214,15 @@ export const HTTPClientView = ({model, index}) => {
|
||||
</Select>
|
||||
<ServerManager
|
||||
onServerChange={handleServerChange}
|
||||
defaultValue={currentModel.server}
|
||||
/>
|
||||
<Input
|
||||
value={basePath} // 선택된 서버의 basePath 표시
|
||||
readOnly
|
||||
className="w-40"
|
||||
/>
|
||||
<EditableInput
|
||||
value={currentModel.path}
|
||||
onChange={handlePathChange}
|
||||
className="flex-1"
|
||||
/>
|
||||
<Input value={basePath} readOnly className="w-40"/>
|
||||
<EditableInput value={currentModel.path} onChange={handlePathChange} className="flex-1" />
|
||||
|
||||
<Button
|
||||
disabled={isLoading}
|
||||
onClick={handleSendRequest}
|
||||
>
|
||||
<Button disabled={isLoading} onClick={handleSendRequest}>
|
||||
{isLoading ? 'Sending...' : 'Send'}
|
||||
</Button>
|
||||
<Button variant="secondary">Save</Button>
|
||||
<Button variant="secondary" onClick={handleSave}>Save</Button>
|
||||
</div>
|
||||
|
||||
{/* Request/Response Container */}
|
||||
@@ -241,7 +244,7 @@ export const HTTPClientView = ({model, index}) => {
|
||||
<div className="flex flex-col h-full gap-2">
|
||||
<Select
|
||||
value={currentModel.contentType}
|
||||
onValueChange={(value) => setCurrentModel(prev => ({ ...prev, contentType: value }))}
|
||||
onValueChange={(value) => setCurrentModel(prev => ({...prev, contentType: value}))}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Content Type"/>
|
||||
@@ -360,8 +363,6 @@ export const HTTPClientView = ({model, index}) => {
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -27,8 +27,6 @@ class APIClient {
|
||||
|
||||
const response = await this.makeRequest(model);
|
||||
const formattedResponse = this.formatResponse(response);
|
||||
onLog(formattedResponse);
|
||||
|
||||
const finalResponse = await this.executePostRequestScript(
|
||||
model.postRequestScript,
|
||||
variables,
|
||||
@@ -37,7 +35,6 @@ class APIClient {
|
||||
onLog
|
||||
);
|
||||
|
||||
|
||||
return finalResponse;
|
||||
|
||||
} catch (error) {
|
||||
|
||||
@@ -91,7 +91,6 @@ export class ScenarioExecutor {
|
||||
|
||||
try {
|
||||
const response = await this.apiClient.sendAPIRequest(data, this.appendLog.bind(this));
|
||||
console.log({response});
|
||||
this.logApiResponse(response, data.type);
|
||||
return response;
|
||||
} catch (error) {
|
||||
@@ -151,7 +150,7 @@ export class ScenarioExecutor {
|
||||
|
||||
logApiResponse(response, type) {
|
||||
const responseLog = type === 'HTTP'
|
||||
? `Response:\nStatus: ${response.status}\nHeaders:\n${this.formatHeaders(response.headers)}\nBody:\n${response.body}`
|
||||
? `Response:\n- Status: ${response.status}\n- Headers:\n${this.formatHeaders(response.headers)}\n- Body:\n${response.body}`
|
||||
: `Response:\n${response.body}`;
|
||||
|
||||
this.appendLog(responseLog);
|
||||
|
||||
Reference in New Issue
Block a user