Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MeshDB Creds in query form #146

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/components/QueryForm/QueryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function QueryForm() {
return QueryFormInput.parse(data);
}

// TODO: Check if we need to log in the user
async function sendForm(event: FormEvent<HTMLFormElement>) {
// Clear previous query results
setLegacyQueryResults([]);
Expand Down Expand Up @@ -90,7 +91,6 @@ export function QueryForm() {
route,
queryForm.query_type,
queryForm.data,
queryForm.password,
);
console.log("response is:");
console.log(resp);
Expand Down Expand Up @@ -252,12 +252,6 @@ export function QueryForm() {
/>
<div className={styles.horizontal}>
<input type="text" name="data" placeholder={queryLabel} required />
<input
type="password"
name="password"
placeholder="Password"
required
/>
</div>
<div className={styles.centered}>
<label>
Expand Down
37 changes: 3 additions & 34 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@/lib/io";
import { z } from "zod";
import { getMeshDBAPIEndpoint } from "./endpoint";
import { parse } from "path";

const get = async <S extends z.Schema>(
url: string,
Expand All @@ -15,51 +16,19 @@ const get = async <S extends z.Schema>(
): Promise<ReturnType<S["parse"]>> => {
const api_base = new URL(`${await getMeshDBAPIEndpoint()}/api/v1/`);
const res = await fetch(new URL(url, api_base), {
headers: {
...(auth && { Authorization: `Bearer ${auth}` }),
},
credentials: "include",
next: nextOptions,
}).catch(console.warn);
if (!res?.ok) throw res;
return schema.parse(await res.json());
};

const post = async <S extends z.Schema>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting this because we don't use it

url: string,
schema: S,
input: unknown,
auth?: string,
method = "POST",
): Promise<ReturnType<S["parse"]>> => {
console.log("Will POST: " + input);
const api_base = new URL(`${await getMeshDBAPIEndpoint()}/api/v1/`);
const res = await fetch(new URL(url, api_base), {
method,
headers: {
"Content-Type": "application/json",
...(auth && { Authorization: `Bearer ${auth}` }),
},
body: JSON.stringify(input),
}).catch(console.warn);
if (!res?.ok) throw res;
return schema.parse(await res.json());
};

export const submitNNAssignForm = (input: NNAssignFormInput) =>
post(
`/api/v1/nn-assign/`,
NNAssignFormResponse,
NNAssignFormInput.parse(input),
);

export const submitQueryForm = (
export const submitQueryForm = async (
route: string,
input_type: string,
input: string,
password: string,
) =>
get(
`/api/v1/query/${route}/?${input_type}=${input}`,
QueryFormResponse,
password,
);
2 changes: 0 additions & 2 deletions src/lib/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { z } from "zod";

export const NNAssignFormInput = z.object({
install_number: z.number(),
password: z.string(), // TODO: Salt/hash/whatever this
});
export type NNAssignFormInput = z.infer<typeof NNAssignFormInput>;

Expand All @@ -20,7 +19,6 @@ export const QueryFormInput = z.object({
legacy: z.string().optional(),
query_type: z.string(),
data: z.string(),
password: z.string(), // TODO: Salt/hash/whatever this
});
export type QueryFormInput = z.infer<typeof QueryFormInput>;

Expand Down
Loading