mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-20 16:43:53 +01:00
Send request body
This commit is contained in:
@@ -119,6 +119,7 @@ async fn send_request(
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
url: &str,
|
||||
method: &str,
|
||||
body: Option<&str>,
|
||||
) -> Result<CustomResponse, String> {
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
@@ -141,10 +142,14 @@ async fn send_request(
|
||||
);
|
||||
|
||||
let m = Method::from_bytes(method.to_uppercase().as_bytes()).unwrap();
|
||||
let req = client
|
||||
let builder = client
|
||||
.request(m, abs_url.to_string())
|
||||
.headers(headers)
|
||||
.build();
|
||||
.headers(headers);
|
||||
|
||||
let req = match body {
|
||||
Some(b) => builder.body(b.to_string()).build(),
|
||||
None => builder.build(),
|
||||
};
|
||||
|
||||
let req = match req {
|
||||
Ok(v) => v,
|
||||
|
||||
@@ -23,6 +23,7 @@ function App() {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [response, setResponse] = useState<Response | null>(null);
|
||||
const [url, setUrl] = useState<string>('https://go-server.schier.dev/debug');
|
||||
const [body, setBody] = useState<string>('');
|
||||
const [method, setMethod] = useState<{ label: string; value: string }>({
|
||||
label: 'GET',
|
||||
value: 'GET',
|
||||
@@ -32,12 +33,15 @@ function App() {
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const resp = (await invoke('send_request', { method: method.value, url })) as Response;
|
||||
const resp = (await invoke('send_request', {
|
||||
method: method.value,
|
||||
url,
|
||||
body: body || undefined,
|
||||
})) as Response;
|
||||
if (resp.body.includes('<head>')) {
|
||||
resp.body = resp.body.replace(/<head>/gi, `<head><base href="${resp.url}"/>`);
|
||||
}
|
||||
setResponse(resp);
|
||||
console.log('Response', resp.status, resp.url, { resp });
|
||||
} catch (err) {
|
||||
setError(`${err}`);
|
||||
}
|
||||
@@ -60,7 +64,11 @@ function App() {
|
||||
onUrlChange={setUrl}
|
||||
sendRequest={sendRequest}
|
||||
/>
|
||||
<Editor value={`{\n "foo": "bar"\n}`} contentType="application/json" />
|
||||
<Editor
|
||||
value={`{\n "foo": "bar"\n}`}
|
||||
contentType="application/json"
|
||||
onChange={setBody}
|
||||
/>
|
||||
</VStack>
|
||||
</VStack>
|
||||
<VStack className="w-full">
|
||||
|
||||
@@ -4,9 +4,14 @@ import './Editor.css';
|
||||
interface Props {
|
||||
contentType: string;
|
||||
value: string;
|
||||
onChange?: (value: string) => void;
|
||||
}
|
||||
|
||||
export default function Editor(props: Props) {
|
||||
const { ref } = useCodeMirror({ value: props.value, contentType: props.contentType });
|
||||
const { ref } = useCodeMirror({
|
||||
value: props.value,
|
||||
contentType: props.contentType,
|
||||
onChange: props.onChange,
|
||||
});
|
||||
return <div ref={ref} className="cm-wrapper" />;
|
||||
}
|
||||
|
||||
@@ -98,9 +98,11 @@ const extensions = [
|
||||
export default function useCodeMirror({
|
||||
value,
|
||||
contentType,
|
||||
onChange,
|
||||
}: {
|
||||
value: string;
|
||||
contentType: string;
|
||||
onChange?: (value: string) => void;
|
||||
}) {
|
||||
const [cm, setCm] = useState<EditorView | null>(null);
|
||||
const ref = useRef(null);
|
||||
@@ -108,7 +110,7 @@ export default function useCodeMirror({
|
||||
if (ref.current === null) return;
|
||||
|
||||
const view = new EditorView({
|
||||
extensions: getExtensions(contentType),
|
||||
extensions: getExtensions({ contentType, onChange }),
|
||||
parent: ref.current,
|
||||
});
|
||||
|
||||
@@ -122,7 +124,7 @@ export default function useCodeMirror({
|
||||
|
||||
const newState = EditorState.create({
|
||||
doc: value,
|
||||
extensions: getExtensions(contentType),
|
||||
extensions: getExtensions({ contentType, onChange }),
|
||||
});
|
||||
cm.setState(newState);
|
||||
}, [cm, value]);
|
||||
@@ -130,7 +132,21 @@ export default function useCodeMirror({
|
||||
return { ref, cm };
|
||||
}
|
||||
|
||||
function getExtensions(contentType: string) {
|
||||
function getExtensions({
|
||||
contentType,
|
||||
onChange,
|
||||
}: {
|
||||
contentType: string;
|
||||
onChange?: (value: string) => void;
|
||||
}) {
|
||||
const ext = syntaxExtensions[contentType];
|
||||
return ext ? [...extensions, ext] : extensions;
|
||||
return ext
|
||||
? [
|
||||
...extensions,
|
||||
...(onChange
|
||||
? [EditorView.updateListener.of((update) => onChange(update.state.doc.toString()))]
|
||||
: []),
|
||||
ext,
|
||||
]
|
||||
: extensions;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user