Send request body

This commit is contained in:
Gregory Schier
2023-02-24 16:09:19 -08:00
parent 76e398b8a1
commit 856d13c603
4 changed files with 45 additions and 11 deletions

View File

@@ -119,6 +119,7 @@ async fn send_request(
db_instance: State<'_, Mutex<Pool<Sqlite>>>, db_instance: State<'_, Mutex<Pool<Sqlite>>>,
url: &str, url: &str,
method: &str, method: &str,
body: Option<&str>,
) -> Result<CustomResponse, String> { ) -> Result<CustomResponse, String> {
let start = std::time::Instant::now(); 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 m = Method::from_bytes(method.to_uppercase().as_bytes()).unwrap();
let req = client let builder = client
.request(m, abs_url.to_string()) .request(m, abs_url.to_string())
.headers(headers) .headers(headers);
.build();
let req = match body {
Some(b) => builder.body(b.to_string()).build(),
None => builder.build(),
};
let req = match req { let req = match req {
Ok(v) => v, Ok(v) => v,

View File

@@ -23,6 +23,7 @@ function App() {
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [response, setResponse] = useState<Response | null>(null); const [response, setResponse] = useState<Response | null>(null);
const [url, setUrl] = useState<string>('https://go-server.schier.dev/debug'); const [url, setUrl] = useState<string>('https://go-server.schier.dev/debug');
const [body, setBody] = useState<string>('');
const [method, setMethod] = useState<{ label: string; value: string }>({ const [method, setMethod] = useState<{ label: string; value: string }>({
label: 'GET', label: 'GET',
value: 'GET', value: 'GET',
@@ -32,12 +33,15 @@ function App() {
setError(null); setError(null);
try { 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>')) { if (resp.body.includes('<head>')) {
resp.body = resp.body.replace(/<head>/gi, `<head><base href="${resp.url}"/>`); resp.body = resp.body.replace(/<head>/gi, `<head><base href="${resp.url}"/>`);
} }
setResponse(resp); setResponse(resp);
console.log('Response', resp.status, resp.url, { resp });
} catch (err) { } catch (err) {
setError(`${err}`); setError(`${err}`);
} }
@@ -60,7 +64,11 @@ function App() {
onUrlChange={setUrl} onUrlChange={setUrl}
sendRequest={sendRequest} 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> </VStack>
<VStack className="w-full"> <VStack className="w-full">

View File

@@ -4,9 +4,14 @@ import './Editor.css';
interface Props { interface Props {
contentType: string; contentType: string;
value: string; value: string;
onChange?: (value: string) => void;
} }
export default function Editor(props: Props) { 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" />; return <div ref={ref} className="cm-wrapper" />;
} }

View File

@@ -98,9 +98,11 @@ const extensions = [
export default function useCodeMirror({ export default function useCodeMirror({
value, value,
contentType, contentType,
onChange,
}: { }: {
value: string; value: string;
contentType: string; contentType: string;
onChange?: (value: string) => void;
}) { }) {
const [cm, setCm] = useState<EditorView | null>(null); const [cm, setCm] = useState<EditorView | null>(null);
const ref = useRef(null); const ref = useRef(null);
@@ -108,7 +110,7 @@ export default function useCodeMirror({
if (ref.current === null) return; if (ref.current === null) return;
const view = new EditorView({ const view = new EditorView({
extensions: getExtensions(contentType), extensions: getExtensions({ contentType, onChange }),
parent: ref.current, parent: ref.current,
}); });
@@ -122,7 +124,7 @@ export default function useCodeMirror({
const newState = EditorState.create({ const newState = EditorState.create({
doc: value, doc: value,
extensions: getExtensions(contentType), extensions: getExtensions({ contentType, onChange }),
}); });
cm.setState(newState); cm.setState(newState);
}, [cm, value]); }, [cm, value]);
@@ -130,7 +132,21 @@ export default function useCodeMirror({
return { ref, cm }; return { ref, cm };
} }
function getExtensions(contentType: string) { function getExtensions({
contentType,
onChange,
}: {
contentType: string;
onChange?: (value: string) => void;
}) {
const ext = syntaxExtensions[contentType]; const ext = syntaxExtensions[contentType];
return ext ? [...extensions, ext] : extensions; return ext
? [
...extensions,
...(onChange
? [EditorView.updateListener.of((update) => onChange(update.state.doc.toString()))]
: []),
ext,
]
: extensions;
} }